| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models\third\wechat;
- use app\models\AggregateQrcode;
- use app\models\Salesman;
- use app\models\StoreMini;
- use EasyWeChat\Kernel\BaseClient;
- class QrcodeMiniForm extends BaseForm
- {
- /**
- * 绑定聚合码
- */
- //easyWechat配置
- public $openPlatform;
- //商城ID
- public $store_id;
- //小程序ID
- public $mini_id;
- //聚合码ID
- public $qrcode_id;
- //easyWechat接口配置
- public $miniProgram;
- //获取小程序
- public $store_mini;
- //聚合码信息
- public $qrcode_info;
- //小程序信息
- public $mini_info;
- //代理商ID
- public $admin_id;
- //业务员ID
- public $salesman_id;
- public function rules()
- {
- return [
- [['store_id', 'mini_id'], 'integer'],
- [['name_data', 'mini_info'], 'array']
- ];
- }
- public function inits()
- {
- try {
- //小程序ID
- $mini_id = $this->mini_id;
- //获取配置信息
- $form = new BaseForm();
- $this->store_mini = StoreMini::findOne($mini_id);
- $store_id = $this->store_mini->store_id;
- $form->store_id = $store_id;
- $form->mini_id = $mini_id;
- //获取easyWechat配置信息
- $this->openPlatform = $form->openPlatform;
- //判断商城ID 或 小程序ID是否不为空,不为空则获取请求微信的配置信息
- if (!empty($store_id) || !empty($mini_id)) {
- //获取请求微信的配置信息
- $result = $form->miniProgram();
- //获取小程序信息
- $this->store_mini = StoreMini::find()->where(['or', ['id' => $mini_id], ['store_id' => $store_id]])->andWhere(['is_cancle' => 0])->orderBy('id desc')->one();
- if ($result['code'] === 1) {
- return $result;
- }
- $this->miniProgram = $result;
- }
- //获取聚合码ID
- $qrcode_id = $this->qrcode_id;
- if (!empty($qrcode_id)) {
- $qrcode_info = AggregateQrcode::findOne($qrcode_id);
- //判断二维码是否存在
- if (empty($qrcode_info)) {
- return [
- 'code' => 1,
- 'msg' => "查找聚合码信息失败"
- ];
- }
- //判断状态是否被使用
- if ($qrcode_info->wx_status == 1) {
- return [
- 'code' => 1,
- 'msg' => "二维码已经被使用"
- ];
- }
- $this->qrcode_info = $qrcode_info;
- }
- //获取代理信息
- $saas_user_id = get_saas_user_id();
- $salesman = Salesman::find()->where(['saas_user_id' => $saas_user_id, 'is_delete' => 0])->select('id, admin_id')->one();
- if (!empty($salesman)) {
- $this->salesman_id = $salesman->id;
- $this->admin_id = $salesman->admin_id;
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage() . $e->getLine()
- ];
- }
- }
- //获取校验文件,并保存本地
- public function saveFile()
- {
- try {
- $result = $this->inits();
- if ($result['code'] === 1) {
- return $result;
- }
- //获取微信配置
- $miniProgram = $this->miniProgram;
- //使用自定义接口
- $client = new BaseClient($miniProgram);
- //请求自定义接口
- $result = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpdownload');
- if ($result['errcode'] === 0) {
- //判断目录是否存在
- if (!is_dir(\Yii::$app->basePath . '/web/face/pay')) {
- //不存在则创建目录
- mkdir(\Yii::$app->basePath . '/web/face/pay', 0777, true);
- }
- //文件写入
- $file_result = file_put_contents(\Yii::$app->basePath . '/web/face/pay/' . $result['file_name'], $result['file_content']);
- //写入成功
- if ($file_result) {
- return [
- 'code' => 0,
- 'msg' => "文件写入成功"
- ];
- }
- //写入失败
- \Yii::error(json_encode($result));
- throw new \Exception("文件写入失败,请重新请求或手动添加。");
- } elseif ($result['errcode'] != 0) {
- //获取中文错误信息(微信返回为英文信息)
- $msg = (new BaseForm())->getZnMsg($result);
- throw new \Exception($msg);
- } else {
- throw new \Exception("数据错误");
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //增加/修改二维码规则
- public function setQrcodeRules()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $result = $this->inits();
- if ($result['code'] === 1) {
- return $result;
- }
- //获取微信配置
- $miniProgram = $this->miniProgram;
- //获取小程序model
- $store_mini = $this->store_mini;
- //获取商城ID
- $store_id = $store_mini->store_id;
- //获取业务员ID
- $salesman_id = $this->salesman_id;
- //获取代理ID
- $admin_id = $this->admin_id;
- //获取聚合码信息
- $qrcode_info = $this->qrcode_info;
- //保存聚合码信息
- if (!empty($qrcode_info)) {
- //判断没有业务员以及代理商,则填充
- if (empty($qrcode_info->admin_id) || empty($qrcode_info->salesman_id)) {
- $qrcode_info->admin_id = $admin_id;
- $qrcode_info->salesman_id = $salesman_id;
- }
- //商城ID
- $qrcode_info->store_id = $store_id;
- //小程序ID
- $qrcode_info->wx_mini_id = $store_mini->id;
- if (!$qrcode_info->save()) {
- throw new \Exception(json_encode($qrcode_info->errors));
- }
- }
- $data = [
- 'prefix' => $qrcode_info->param_url,
- 'permit_sub_rule' => 1,
- 'path' => $qrcode_info->wx_url,
- 'open_version' => 2,
- 'debug_url' => [
- $qrcode_info->param_url
- ],
- 'is_edit' => 0
- ];
- //自定义请求接口
- $client = new BaseClient($miniProgram);
- //请求接口
- $result = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpadd', $data);
- if ($result['errcode'] === 0) {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "添加规则成功"
- ];
- } elseif ($result['errcode'] != 0) {
- //获取中文错误信息(微信返回为英文信息)
- $msg = (new BaseForm())->getZnMsg($result);
- throw new \Exception($msg);
- } else {
- throw new \Exception("数据错误");
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //发布二维码规则
- public function submitQrcodeRules()
- {
- //开启数据库事务
- $t = \Yii::$app->db->beginTransaction();
- try {
- $result = $this->inits();
- if ($result['code'] === 1) {
- return $result;
- }
- //获取聚合码信息
- $qrcode_info = $this->qrcode_info;
- //获取微信配置信息
- $miniProgram = $this->miniProgram;
- $mini_id = $this->mini_id;
- //判断二维码地址是否为空
- if (empty($qrcode_info->param_url)) {
- throw new \Exception('参数错误');
- }
- //将微信聚合码状态改为完成
- $qrcode_info->wx_mini_id = $mini_id;
- $qrcode_info->wx_status = 1;
- $qrcode_info->is_use = 2;
- //保存失败
- if (!$qrcode_info->save()) {
- throw new \Exception(json_encode($qrcode_info->errors));
- }
- //所需参数
- $data = [
- 'prefix' => $qrcode_info->param_url
- ];
- //自定义接口提交地址
- $client = new BaseClient($miniProgram);
- //发布二维码规则接口
- $result = $client->httpPostJson('cgi-bin/wxopen/qrcodejumppublish', $data);
- if ($result['errcode'] === 0) {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "发布成功"
- ];
- } elseif ($result['errcode'] != 0) {
- //获取中文错误信息(微信返回为英文信息)
- $msg = (new BaseForm())->getZnMsg($result);
- throw new \Exception($msg);
- } else {
- throw new \Exception("数据错误");
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|