AlipayVoucherController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace api\modules\v1\controllers;
  3. use api\controllers\OnAuthController;
  4. use common\enums\AddonsEnum;
  5. use common\enums\RedisKeyEnum;
  6. use common\expand\alipay\auth\src\OauthTokenService;
  7. use common\expand\alipay\transfer\enums\AlipayVoucherEnum;
  8. use common\expand\alipay\voucher\src\ConsultService;
  9. use common\models\mall\MallAddons;
  10. use Yii;
  11. class AlipayVoucherController extends OnAuthController
  12. {
  13. public $modelClass = '';
  14. /**
  15. *
  16. * 不用进行登录验证的方法
  17. *
  18. * 例如: ['index', 'update', 'create', 'view', 'delete']
  19. * 默认全部需要验证
  20. *
  21. * @var array
  22. */
  23. protected $authOptional = [];
  24. public function actionList()
  25. {
  26. try {
  27. $rules = [
  28. [['code'], 'string'],
  29. [['order_amount'], 'number'],
  30. [['code'], 'required', 'message' => '请输入code'],
  31. [['order_amount'], 'required', 'message' => '请输入金额'],
  32. [['goods'], 'safe']
  33. ];
  34. $params = Yii::$app->request->post();
  35. $res = Yii::$app->services->validate->validate($params, $rules);
  36. if ($res === false) throw new \Exception(Yii::$app->services->validate->getErrorMessage());
  37. // 校验当前系统是否安装了支付宝商家券插件
  38. if (!MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_ALIPAY_MCH_COUPON)) {
  39. throw new \Exception('您还未安装当前插件');
  40. }
  41. // 先获取token
  42. $auth_token = Yii::$app->cache->get(RedisKeyEnum::suffix(RedisKeyEnum::ALIPAY_ACCESS_TOKEN, "voucher:{$this->user_id}"));
  43. if (empty($auth_token)) {
  44. // 获取token
  45. $auth_token = $this->getAccessToken($params['code']);
  46. }
  47. $res = (new ConsultService([
  48. 'mall_id' => $this->mall_id,
  49. 'auth_token' => $auth_token,
  50. 'order_amount' => $params['order_amount'],
  51. 'goods' => $params['goods']
  52. ]))->exec();
  53. if (empty($res['code']) || $res['code'] != 10000) throw new \Exception($res['msg'] ?? '获取代金券失败');
  54. $ret = $this->formatVoucherList($res['voucher_consult_list'] ?? []);
  55. return $this->success('获取成功', $ret);
  56. } catch (\Exception $e) {
  57. return $this->error($e->getMessage());
  58. }
  59. }
  60. /**
  61. * @param string $code
  62. * @return mixed
  63. * @throws \Exception
  64. */
  65. protected function getAccessToken(string $code)
  66. {
  67. $res = (new OauthTokenService([
  68. 'mall_id' => $this->mall_id,
  69. 'code' => $code
  70. ]))->exec();
  71. if (empty($res['access_token'])) throw new \Exception($res['msg'] ?? '获取token失败');
  72. Yii::$app->cache->set(RedisKeyEnum::suffix(RedisKeyEnum::ALIPAY_ACCESS_TOKEN, "voucher:{$this->user_id}"),
  73. $res['access_token'], $res['expires_in'] - 10);
  74. return $res['access_token'];
  75. }
  76. /**
  77. * 格式化代金券数据
  78. * @param array $ret
  79. * @return array
  80. */
  81. protected function formatVoucherList(array $ret): array
  82. {
  83. // voucher_type 1)全场券(ALL) 2)单品券(ITEM)
  84. // promo_type 优惠类型: 代金(FIX),折扣(DISCOUNT),减至(SPECIAL,即特价券)
  85. $data = [];
  86. if (empty($ret)) return $data;
  87. foreach ($ret as $key => $value) {
  88. // $data[$key] = [
  89. // 'list' => [],
  90. // 'voucher_name' => $value['voucher_name'],
  91. // 'promo_type' => $value['promo_type'],
  92. // 'voucher_type' => $value['voucher_type'],
  93. // 'promo_type_txt' => AlipayVoucherEnum::PROMO_TYPE_REMARKS[$value['promo_type']] ?? '未知类型',
  94. // 'voucher_type_txt' => AlipayVoucherEnum::VOUCHER_TYPE_REMARKS[$value['voucher_type']] ?? '未知类型',
  95. // ];
  96. $list = $value['item_promo_info']['item_consult_list'];
  97. if (!empty($list)) {
  98. foreach ($list as $item) {
  99. $data[$item['item_id']] = [
  100. 'id' => $item['item_id'],
  101. 'promo_amount' => $item['promo_amount'], // 商品优惠总金额
  102. 'promo_count' => $item['promo_count'], // 商品优惠总数量
  103. 'voucher_name' => $value['voucher_name'],
  104. 'promo_type' => $value['promo_type'],
  105. 'voucher_type' => $value['voucher_type'],
  106. 'promo_type_txt' => AlipayVoucherEnum::PROMO_TYPE_REMARKS[$value['promo_type']] ?? '未知类型',
  107. 'voucher_type_txt' => AlipayVoucherEnum::VOUCHER_TYPE_REMARKS[$value['voucher_type']] ?? '未知类型',
  108. ];
  109. }
  110. }
  111. }
  112. return $data;
  113. }
  114. }