QrcodeMiniForm.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1\third\wechat;
  8. use app\models\AggregateQrcode;
  9. use app\models\Salesman;
  10. use app\models\StoreMini;
  11. use EasyWeChat\Kernel\BaseClient;
  12. class QrcodeMiniForm extends BaseForm
  13. {
  14. /**
  15. * 绑定聚合码
  16. */
  17. //easyWechat配置
  18. public $openPlatform;
  19. //商城ID
  20. public $store_id;
  21. //小程序ID
  22. public $mini_id;
  23. //聚合码ID
  24. public $qrcode_id;
  25. //easyWechat接口配置
  26. public $miniProgram;
  27. //获取小程序
  28. public $store_mini;
  29. //聚合码信息
  30. public $qrcode_info;
  31. //小程序信息
  32. public $mini_info;
  33. //代理商ID
  34. public $admin_id;
  35. //业务员ID
  36. public $salesman_id;
  37. public function rules()
  38. {
  39. return [
  40. [['store_id', 'mini_id'], 'integer'],
  41. [['name_data', 'mini_info'], 'array']
  42. ];
  43. }
  44. public function inits()
  45. {
  46. try {
  47. //小程序ID
  48. $mini_id = $this->mini_id;
  49. //获取配置信息
  50. $form = new BaseForm();
  51. $this->store_mini = StoreMini::findOne($mini_id);
  52. $store_id = $this->store_mini->store_id;
  53. $form->store_id = $store_id;
  54. $form->mini_id = $mini_id;
  55. //获取easyWechat配置信息
  56. $this->openPlatform = $form->openPlatform;
  57. //判断商城ID 或 小程序ID是否不为空,不为空则获取请求微信的配置信息
  58. if (!empty($store_id) || !empty($mini_id)) {
  59. //获取请求微信的配置信息
  60. $result = $form->miniProgram();
  61. //获取小程序信息
  62. $this->store_mini = StoreMini::find()->where(['or', ['id' => $mini_id], ['store_id' => $store_id]])->andWhere(['is_cancle' => 0])->orderBy('id desc')->one();
  63. if ($result['code'] === 1) {
  64. return $result;
  65. }
  66. $this->miniProgram = $result;
  67. }
  68. //获取聚合码ID
  69. $qrcode_id = $this->qrcode_id;
  70. if (!empty($qrcode_id)) {
  71. $qrcode_info = AggregateQrcode::findOne($qrcode_id);
  72. //判断二维码是否存在
  73. if (empty($qrcode_info)) {
  74. return [
  75. 'code' => 1,
  76. 'msg' => "查找聚合码信息失败"
  77. ];
  78. }
  79. //判断状态是否被使用
  80. if ($qrcode_info->wx_status == 1) {
  81. return [
  82. 'code' => 1,
  83. 'msg' => "二维码已经被使用"
  84. ];
  85. }
  86. $this->qrcode_info = $qrcode_info;
  87. }
  88. //获取代理信息
  89. $saas_user_id = get_saas_user_id();
  90. $salesman = Salesman::find()->where(['saas_user_id' => $saas_user_id])->select('id, admin_id')->one();
  91. if (!empty($salesman)) {
  92. $this->salesman_id = $salesman->id;
  93. $this->admin_id = $salesman->admin_id;
  94. }
  95. } catch (\Exception $e) {
  96. return [
  97. 'code' => 1,
  98. 'msg' => $e->getMessage() . $e->getLine()
  99. ];
  100. }
  101. }
  102. //获取校验文件,并保存本地
  103. public function saveFile()
  104. {
  105. try {
  106. $result = $this->inits();
  107. if ($result['code'] === 1) {
  108. return $result;
  109. }
  110. //获取微信配置
  111. $miniProgram = $this->miniProgram;
  112. //使用自定义接口
  113. $client = new BaseClient($miniProgram);
  114. //请求自定义接口
  115. $result = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpdownload');
  116. if ($result['errcode'] === 0) {
  117. //判断目录是否存在
  118. if (!is_dir(\Yii::$app->basePath . '/web/face/pay')) {
  119. //不存在则创建目录
  120. mkdir(\Yii::$app->basePath . '/web/face/pay', 0777, true);
  121. }
  122. //文件写入
  123. $file_result = file_put_contents(\Yii::$app->basePath . '/web/face/pay/' . $result['file_name'], $result['file_content']);
  124. //写入成功
  125. if ($file_result) {
  126. return [
  127. 'code' => 0,
  128. 'msg' => "文件写入成功"
  129. ];
  130. }
  131. //写入失败
  132. \Yii::error(json_encode($result));
  133. throw new \Exception("文件写入失败,请重新请求或手动添加。");
  134. } elseif ($result['errcode'] != 0) {
  135. //获取中文错误信息(微信返回为英文信息)
  136. $msg = (new BaseForm())->getZnMsg($result);
  137. throw new \Exception($msg);
  138. } else {
  139. throw new \Exception("数据错误");
  140. }
  141. } catch (\Exception $e) {
  142. return [
  143. 'code' => 1,
  144. 'msg' => $e->getMessage()
  145. ];
  146. }
  147. }
  148. //增加/修改二维码规则
  149. public function setQrcodeRules()
  150. {
  151. $t = \Yii::$app->db->beginTransaction();
  152. try {
  153. $result = $this->inits();
  154. if ($result['code'] === 1) {
  155. return $result;
  156. }
  157. //获取微信配置
  158. $miniProgram = $this->miniProgram;
  159. //获取小程序model
  160. $store_mini = $this->store_mini;
  161. //获取商城ID
  162. $store_id = $store_mini->store_id;
  163. //获取业务员ID
  164. $salesman_id = $this->salesman_id;
  165. //获取代理ID
  166. $admin_id = $this->admin_id;
  167. //获取聚合码信息
  168. $qrcode_info = $this->qrcode_info;
  169. //保存聚合码信息
  170. if (!empty($qrcode_info)) {
  171. //判断没有业务员以及代理商,则填充
  172. if (empty($qrcode_info->admin_id) || empty($qrcode_info->salesman_id)) {
  173. $qrcode_info->admin_id = $admin_id;
  174. $qrcode_info->salesman_id = $salesman_id;
  175. }
  176. //商城ID
  177. $qrcode_info->store_id = $store_id;
  178. //小程序ID
  179. $qrcode_info->wx_mini_id = $store_mini->id;
  180. if (!$qrcode_info->save()) {
  181. throw new \Exception(json_encode($qrcode_info->errors));
  182. }
  183. }
  184. $data = [
  185. 'prefix' => $qrcode_info->param_url,
  186. 'permit_sub_rule' => 1,
  187. 'path' => $qrcode_info->wx_url,
  188. 'open_version' => 2,
  189. 'debug_url' => [
  190. $qrcode_info->param_url
  191. ],
  192. 'is_edit' => 0
  193. ];
  194. //自定义请求接口
  195. $client = new BaseClient($miniProgram);
  196. //请求接口
  197. $result = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpadd', $data);
  198. if ($result['errcode'] === 0) {
  199. $t->commit();
  200. return [
  201. 'code' => 0,
  202. 'msg' => "添加规则成功"
  203. ];
  204. } elseif ($result['errcode'] != 0) {
  205. //获取中文错误信息(微信返回为英文信息)
  206. $msg = (new BaseForm())->getZnMsg($result);
  207. throw new \Exception($msg);
  208. } else {
  209. throw new \Exception("数据错误");
  210. }
  211. } catch (\Exception $e) {
  212. $t->rollBack();
  213. return [
  214. 'code' => 1,
  215. 'msg' => $e->getMessage()
  216. ];
  217. }
  218. }
  219. //发布二维码规则
  220. public function submitQrcodeRules()
  221. {
  222. //开启数据库事务
  223. $t = \Yii::$app->db->beginTransaction();
  224. try {
  225. $result = $this->inits();
  226. if ($result['code'] === 1) {
  227. return $result;
  228. }
  229. //获取聚合码信息
  230. $qrcode_info = $this->qrcode_info;
  231. //获取微信配置信息
  232. $miniProgram = $this->miniProgram;
  233. $mini_id = $this->mini_id;
  234. //判断二维码地址是否为空
  235. if (empty($qrcode_info->param_url)) {
  236. throw new \Exception('参数错误');
  237. }
  238. //将微信聚合码状态改为完成
  239. $qrcode_info->wx_mini_id = $mini_id;
  240. $qrcode_info->wx_status = 1;
  241. //保存失败
  242. if (!$qrcode_info->save()) {
  243. throw new \Exception(json_encode($qrcode_info->errors));
  244. }
  245. //所需参数
  246. $data = [
  247. 'prefix' => $qrcode_info->param_url
  248. ];
  249. //自定义接口提交地址
  250. $client = new BaseClient($miniProgram);
  251. //发布二维码规则接口
  252. $result = $client->httpPostJson('cgi-bin/wxopen/qrcodejumppublish', $data);
  253. if ($result['errcode'] === 0) {
  254. $t->commit();
  255. return [
  256. 'code' => 0,
  257. 'msg' => "发布成功"
  258. ];
  259. } elseif ($result['errcode'] != 0) {
  260. //获取中文错误信息(微信返回为英文信息)
  261. $msg = (new BaseForm())->getZnMsg($result);
  262. throw new \Exception($msg);
  263. } else {
  264. throw new \Exception("数据错误");
  265. }
  266. } catch (\Exception $e) {
  267. $t->rollBack();
  268. return [
  269. 'code' => 1,
  270. 'msg' => $e->getMessage()
  271. ];
  272. }
  273. }
  274. }