| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models\third\ali;
- use app\models\AggregateQrcode;
- use app\models\StoreAliMini;
- use app\models\StoreAliMiniVersion;
- use app\utils\Alipay\alipaySdk\aop\request\AlipayOpenMiniQrcodeBindRequest;
- class QrcodeMiniForm extends BaseForm
- {
- /**
- * 聚合码form
- */
- public function inits()
- {
- try {
- $mini_id = $this->mini_id;
- if (!empty($mini_id)) {
- $store_mini = StoreAliMini::findOne($mini_id);
- if (empty($store_mini)) {
- return [
- 'code' => 1,
- 'msg' => "查找不到小程序信息"
- ];
- }
- $this->store_mini = $store_mini;
- $this->mini_version = StoreAliMiniVersion::find()->where(['mini_id' => $mini_id])->orderBy('id desc')->one();
- }
- //获取二维码信息
- $qrcode_id = $this->qrcode_id;
- if (!empty($qrcode_id)) {
- $qrcode = AggregateQrcode::findOne($qrcode_id);
- if (empty($qrcode)) {
- return [
- 'code' => 1,
- 'msg' => "二维码不存在"
- ];
- }
- if ($qrcode->ali_status == 1) {
- return [
- 'code' => 1,
- 'msg' => "二维码已经被使用"
- ];
- }
- $this->qrcode = $qrcode;
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //关联普通二维码 alipay.open.mini.qrcode.bind(关联普通二维码)
- public function bindMiniQrcode()
- {
- try {
- $result = $this->inits();
- if ($result['code'] === 1) {
- return $result;
- }
- //获取小程序model
- $qrcode = $this->qrcode;
- $mini_id = $this->mini_id;
- //判断关键参数是否为空
- if (empty($qrcode->param_url) || empty($qrcode->ali_url)) {
- throw new \Exception("参数错误");
- }
- //配置请求参数
- $data = json_encode([
- "route_url" => $qrcode->param_url,
- 'mode' => "EXACT",
- "page_redirection" => $qrcode->ali_url
- ]);
- //获取接口信息
- $request = new AlipayOpenMiniQrcodeBindRequest();
- //请求接口
- $result = $this->miniCommon($request, $data);
- //请求接口错误
- if ($result->code != 10000) {
- throw new \Exception($result->sub_msg);
- }
- //将当前二维码支付宝状态改为已经被使用
- $qrcode->ali_mini_id = $mini_id;
- $qrcode->ali_status = 1;
- $qrcode->is_use = 2;
- if (!$qrcode->save()) {
- throw new \Exception('保存数据失败,绑定二维码成功');
- }
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $result
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|