QrcodeMiniForm.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models\third\ali;
  8. use app\models\AggregateQrcode;
  9. use app\models\StoreAliMini;
  10. use app\models\StoreAliMiniVersion;
  11. use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniQrcodeBindRequest;
  12. class QrcodeMiniForm extends BaseForm
  13. {
  14. /**
  15. * 聚合码form
  16. */
  17. public function inits()
  18. {
  19. try {
  20. $mini_id = $this->mini_id;
  21. if (!empty($mini_id)) {
  22. $store_mini = StoreAliMini::findOne($mini_id);
  23. if (empty($store_mini)) {
  24. return [
  25. 'code' => 1,
  26. 'msg' => "查找不到小程序信息"
  27. ];
  28. }
  29. $this->store_mini = $store_mini;
  30. $this->mini_version = StoreAliMiniVersion::find()->where(['mini_id' => $mini_id])->orderBy('id desc')->one();
  31. }
  32. //获取二维码信息
  33. $qrcode_id = $this->qrcode_id;
  34. if (!empty($qrcode_id)) {
  35. $qrcode = AggregateQrcode::findOne($qrcode_id);
  36. if (empty($qrcode)) {
  37. return [
  38. 'code' => 1,
  39. 'msg' => "二维码不存在"
  40. ];
  41. }
  42. if ($qrcode->ali_status == 1) {
  43. return [
  44. 'code' => 1,
  45. 'msg' => "二维码已经被使用"
  46. ];
  47. }
  48. $this->qrcode = $qrcode;
  49. }
  50. } catch (\Exception $e) {
  51. return [
  52. 'code' => 1,
  53. 'msg' => $e->getMessage()
  54. ];
  55. }
  56. }
  57. //关联普通二维码 alipay.open.mini.qrcode.bind(关联普通二维码)
  58. public function bindMiniQrcode()
  59. {
  60. try {
  61. $result = $this->inits();
  62. if ($result['code'] === 1) {
  63. return $result;
  64. }
  65. //获取小程序model
  66. $qrcode = $this->qrcode;
  67. $mini_id = $this->mini_id;
  68. //判断关键参数是否为空
  69. if (empty($qrcode->param_url) || empty($qrcode->ali_url)) {
  70. throw new \Exception("参数错误");
  71. }
  72. //配置请求参数
  73. $data = json_encode([
  74. "route_url" => $qrcode->param_url,
  75. 'mode' => "EXACT",
  76. "page_redirection" => $qrcode->ali_url
  77. ]);
  78. //获取接口信息
  79. $request = new AlipayOpenMiniQrcodeBindRequest();
  80. //请求接口
  81. $result = $this->miniCommon($request, $data);
  82. //请求接口错误
  83. if ($result->code != 10000) {
  84. throw new \Exception($result->sub_msg);
  85. }
  86. //将当前二维码支付宝状态改为已经被使用
  87. $qrcode->ali_mini_id = $mini_id;
  88. $qrcode->ali_status = 1;
  89. $qrcode->is_use = 2;
  90. if (!$qrcode->save()) {
  91. throw new \Exception('保存数据失败,绑定二维码成功');
  92. }
  93. return [
  94. 'code' => 0,
  95. 'msg' => "成功",
  96. 'data' => $result
  97. ];
  98. } catch (\Exception $e) {
  99. return [
  100. 'code' => 1,
  101. 'msg' => $e->getMessage()
  102. ];
  103. }
  104. }
  105. }