PerfectMiniForm.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models\third\wechat;
  8. use app\models\StoreMini;
  9. use EasyWeChat\BasicService\Media\Client;
  10. use EasyWeChat\Kernel\BaseClient;
  11. use yii\base\Model;
  12. class PerfectMiniForm extends Model
  13. {
  14. /**
  15. * 完善小程序
  16. */
  17. //easyWechat配置
  18. public $openPlatform;
  19. //商城ID
  20. public $store_id;
  21. //小程序ID
  22. public $mini_id;
  23. //easyWechat接口配置
  24. public $miniProgram;
  25. //获取小程序
  26. public $store_mini;
  27. //名称审核资料
  28. public $name_data;
  29. //小程序信息
  30. public $mini_info;
  31. public function rules()
  32. {
  33. return [
  34. [['store_id', 'mini_id'], 'integer'],
  35. [['name_data', 'mini_info'], 'array']
  36. ];
  37. }
  38. public function inits()
  39. {
  40. try {
  41. //商城ID
  42. $store_id = $this->store_id;
  43. //小程序ID
  44. $mini_id = $this->mini_id;
  45. //获取配置信息
  46. $form = new BaseForm();
  47. $form->store_id = $store_id;
  48. $form->mini_id = $mini_id;
  49. //获取easyWechat配置信息
  50. $this->openPlatform = $form->openPlatform;
  51. //判断商城ID 或 小程序ID是否不为空,不为空则获取请求微信的配置信息
  52. if (!empty($store_id) || !empty($mini_id)) {
  53. //获取请求微信的配置信息
  54. $result = $form->miniProgram();
  55. //获取小程序信息
  56. $this->store_mini = StoreMini::find()->where(['or', ['id' => $mini_id], ['store_id' => $store_id]])->andWhere(['is_cancle' => 0])->orderBy('id desc')->one();
  57. if ($result['code'] === 1) {
  58. return $result;
  59. }
  60. $this->miniProgram = $result;
  61. }
  62. } catch (\Exception $e) {
  63. return [
  64. 'code' => 1,
  65. 'msg' => $e->getMessage()
  66. ];
  67. }
  68. }
  69. //修改小程序名称
  70. public function setMiniName()
  71. {
  72. try {
  73. $result = $this->inits();
  74. if ($result['code'] === 1) {
  75. return $result;
  76. }
  77. //判断小程序是否存在
  78. $store_mini = $this->store_mini;
  79. if (empty($store_mini)) {
  80. throw new \Exception("查找不到小程序信息");
  81. }
  82. //获取接口配置信息
  83. $miniProgram = $this->miniProgram;
  84. $media = new Client($miniProgram);
  85. //判断是get方式请求
  86. // if (\Yii::$app->request->isGet) {
  87. // //营业执照信息
  88. // $license_pic = $store_mini->license_pic ?: "";
  89. // $name_data['license'] = $license_pic;
  90. //
  91. // //名称审核资料
  92. // $name_data = $store_mini->nickname_audit_data;
  93. // $name_data = json_decode($name_data, true);
  94. //
  95. // //小程序名称
  96. // $nickname = $store_mini->mini_nickname;
  97. //
  98. // return [
  99. // 'code' => 0,
  100. // 'msg' => "获取成功",
  101. // 'data' => [
  102. // 'mini_info' => [
  103. // 'nickname' => $nickname
  104. // ],
  105. // 'name_data' => $name_data
  106. // ]
  107. // ];
  108. //
  109. // }
  110. //获取请求信息
  111. //post方式请求
  112. //获取小程序名称
  113. $mini_info = $this->mini_info;
  114. //获取名称审核资料
  115. $name_data = $this->name_data;
  116. //保存名称审核资料
  117. $store_mini->nickname_audit_data = json_encode($name_data);
  118. //保存小程序名称
  119. $store_mini->mini_nickname = $mini_info['nickname'] ?: '';
  120. //重新填充营业执照信息
  121. $name_data['license'] = $store_mini->license_pic;
  122. if (!empty($name_data)) {
  123. //循环 ,将数据保存到本地
  124. foreach ($name_data as &$item) {
  125. if (!empty($item)) {
  126. //保存数据到本地
  127. $local_item = (new BaseForm())->saveTempImage($item);
  128. //从微信获取media_id
  129. $result = $media->uploadImage($local_item);
  130. //判断media信息是否正确
  131. if (empty($result['media_id'])) {
  132. throw new \Exception($this->getZnMsg($result));
  133. }
  134. $item = $result['media_id'];
  135. }
  136. }
  137. }
  138. $license_media_id = $name_data['license'];
  139. //剔除营业执照 个人身份证信息
  140. unset($name_data['license']);
  141. unset($name_data['id_card']);
  142. if (empty($mini_info['nickname'])) {
  143. throw new \Exception("信息不全,请填写信息");
  144. }
  145. //请求微信审核名称接口
  146. $result = $miniProgram->setting->setNickname($mini_info['nickname'], '', $license_media_id, $name_data);
  147. if ($result['errcode'] == 0) {
  148. self::addMiniCategory();
  149. }
  150. //判断审核名称是否存在audit_id;如果存在则进入审核阶段
  151. if ($result['errcode'] == 0 && !empty($result['audit_id'])) {
  152. $store_mini->apply_name_status = 1;
  153. $store_mini->nickname_audit_id = $result['audit_id'];
  154. $msg = "开始审核!";
  155. //判断审核名称是否存在audit_id;如果不存在则不需要审核
  156. } elseif ($result['errcode'] == 0 && empty($result['audit_id'])) {
  157. $store_mini->apply_name_status = 3;
  158. $msg = "审核通过!";
  159. //判断是否存在错误信息
  160. } elseif ($result['errcode'] !== 0) {
  161. $msg = (new BaseForm())->getZnMsg($result);
  162. throw new \Exception($msg);
  163. } else {
  164. throw new \Exception("参数错误");
  165. }
  166. //判断是否保存成功
  167. if (!$store_mini->save()) {
  168. throw new \Exception(json_encode($store_mini->errors));
  169. }
  170. return [
  171. 'code' => 0,
  172. 'msg' => $msg
  173. ];
  174. } catch (\Exception $e) {
  175. return [
  176. 'code' => 1,
  177. 'msg' => $e->getMessage()
  178. ];
  179. }
  180. }
  181. //微信名称检测 检测名称是否可用 需要mini_id
  182. public function checkNickname()
  183. {
  184. try {
  185. $result = $this->inits();
  186. if ($result['code'] === 1) {
  187. return $result;
  188. }
  189. //获取小程序名称
  190. $mini_info = $this->mini_info;
  191. $nickname = $mini_info['nickname'];
  192. //判断名称是否为空
  193. if (empty($nickname)) {
  194. throw new \Exception("名称不能为空");
  195. }
  196. //请求微信接口获取检测结果
  197. $miniProgram = $this->miniProgram;
  198. $result = $miniProgram->setting->isAvailableNickname($nickname);
  199. //请求成功,判断是否存在敏感字段 如果存在则返回说明描述
  200. if ($result['errcode'] == 0) {
  201. return [
  202. 'code' => 0,
  203. 'hit_condition' => $result['hit_condition'],
  204. 'msg' => $result['wording']
  205. ];
  206. //返回信息错误,则返回错误信息
  207. } elseif ($result['errcode'] !== 0) {
  208. $msg = (new BaseForm())->getZnMsg($result);
  209. throw new \Exception($msg);
  210. } else {
  211. throw new \Exception("参数错误");
  212. }
  213. } catch (\Exception $e) {
  214. return [
  215. 'code' => 0,
  216. 'msg' => $e->getMessage()
  217. ];
  218. }
  219. }
  220. //查询.小程序名称审核状态
  221. public function getMiniStatus()
  222. {
  223. try {
  224. $result = $this->inits();
  225. if ($result['code'] === 1) {
  226. return $result;
  227. }
  228. //获取小程序信息
  229. $store_mini = $this->store_mini;
  230. //判断小程序是否已经审核通过(不请求接口)
  231. if (isset($store_mini->apply_name_status) && ($store_mini->apply_name_status == 0) && !empty($store_mini->mini_nickname)) {
  232. return [
  233. 'code' => 0,
  234. 'msg' => "审核已经通过"
  235. ];
  236. }
  237. //获取审核单ID
  238. $audit_id = $store_mini->nickname_audit_id;
  239. if (empty($audit_id)) {
  240. throw new \Exception("参数获取失败");
  241. }
  242. //通过审核单ID请求微信接口
  243. $miniProgram = $this->miniProgram;
  244. $result = $miniProgram->setting->getNicknameAuditStatus($audit_id);
  245. if ($result['errcode'] === 0) {
  246. //审核中
  247. if ($result['audit_stat'] == 1) {
  248. $status = "名称审核中";
  249. $store_mini->apply_name_status = 1;
  250. //审核失败
  251. } elseif ($result['audit_stat'] == 2) {
  252. $status = "名称审核失败:" . $result['fail_reason'];
  253. $store_mini->apply_name_error = $result['fail_reason'];
  254. $store_mini->apply_name_status = 2;
  255. //审核成功
  256. } else {
  257. $status = "名称审核成功";
  258. $store_mini->apply_name_status = 3;
  259. }
  260. //保存小程序名称审核信息
  261. if (!$store_mini->save()) {
  262. throw new \Exception(json_encode($store_mini->errors));
  263. }
  264. return [
  265. 'code' => 0,
  266. 'msg' => $status,
  267. 'status' => $result['audit_stat']
  268. ];
  269. } elseif($result['errcode'] !== 0) {
  270. $msg = (new BaseForm())->getZnMsg($result);
  271. throw new \Exception($msg);
  272. } else {
  273. throw new \Exception("参数错误");
  274. }
  275. } catch (\Exception $e) {
  276. return [
  277. 'code' => 0,
  278. 'msg' => $e->getMessage()
  279. ];
  280. }
  281. }
  282. //修改头像
  283. public function setHeadImg()
  284. {
  285. try {
  286. $result = $this->inits();
  287. if ($result['code'] === 1) {
  288. return $result;
  289. }
  290. $store_mini = $this->store_mini;
  291. //获取小程序头像
  292. $mini_info = $this->mini_info;
  293. $head_image = $mini_info['mini_url'];
  294. $store_mini->mini_url = $head_image;
  295. //将头像地址保存本地
  296. $img_url = (new BaseForm())->saveTempImage($head_image);
  297. //获取微信配置信息
  298. $miniProgram = $this->miniProgram;
  299. //获取微信上传图片配置
  300. $media = new Client($miniProgram);
  301. //上传缩略图,获取media_id
  302. $head_image = $media->uploadImage($img_url);
  303. //判断上传缩略图时是否报错
  304. if (empty($head_image['media_id'])) {
  305. $msg = (new BaseForm())->getZnMsg($head_image);
  306. throw new \Exception($msg);
  307. }
  308. //使用生成的media_id 上传头像
  309. $result = $miniProgram->account->updateAvatar($head_image['media_id'], 0, 0, 1, 1);
  310. if ($result['errcode'] === 0) {
  311. //保存头像信息
  312. if (!$store_mini->save()) {
  313. throw new \Exception($store_mini->errors);
  314. }
  315. return [
  316. 'code' => 0,
  317. 'msg' => "保存成功"
  318. ];
  319. } elseif ($result['errcode'] != 0) {
  320. //获取错误信息
  321. $msg = (new BaseForm())->getZnMsg($result);
  322. throw new \Exception($msg);
  323. } else {
  324. throw new \Exception("参数错误");
  325. }
  326. } catch (\Exception $e) {
  327. //返回错误信息
  328. return [
  329. 'code' => 1,
  330. 'msg' => $e->getMessage()
  331. ];
  332. }
  333. }
  334. //查询所有可设置的小程序类目 //获取mini_id
  335. public function getAllCategory()
  336. {
  337. try {
  338. $result = $this->inits();
  339. if ($result['code'] === 1) {
  340. return $result;
  341. }
  342. //获取微信配置信息
  343. $miniProgram = $this->miniProgram;
  344. //获取所有可用的类目
  345. $result = $miniProgram->setting->getAllCategories();
  346. if ($result['errcode'] === 0) {
  347. $old_data = $result['categories_list']['categories'];
  348. //使用递归处理数据 返回父子数据
  349. $data = (new BaseForm())->getdata($old_data);
  350. return [
  351. 'code' => 0,
  352. 'msg' => "获取成功",
  353. 'data' => $data,
  354. 'old_data' => $old_data
  355. ];
  356. //微信错误信息
  357. } elseif ($result['errcode'] != 0) {
  358. $msg = (new BaseForm())->getZnMsg($result);
  359. throw new \Exception($msg);
  360. } else {
  361. throw new \Exception("数据错误");
  362. }
  363. } catch (\Exception $e) {
  364. return [
  365. 'code' => 1,
  366. 'msg' => $e->getMessage()
  367. ];
  368. }
  369. }
  370. //添加小程序类目 (带资质的类目需要审核,申请只能被动推送) 需要mini_id 不设置类目到数据库
  371. public function addMiniCategory()
  372. {
  373. try {
  374. $result = $this->inits();
  375. if ($result['code'] === 1) {
  376. return $result;
  377. }
  378. //获取微信请求配置
  379. $miniProgram = $this->miniProgram;
  380. //获取添加的分类信息
  381. // $mini_info = $this->mini_info;
  382. // $data = $mini_info['category'];
  383. //
  384. // //获取微信media处理配置
  385. // $media = new Client($miniProgram);
  386. //
  387. // foreach ($data as &$item) {
  388. // //判断分类是否为空
  389. // if (!empty($item['certicates'])) {
  390. //
  391. // //遍历类目
  392. // foreach ($item['certicates'] as &$cate) {
  393. //
  394. // //保存图片到本地
  395. // $url = (new BaseForm())->saveTempImage($cate['value']);
  396. //
  397. // //获取media_id
  398. // $cate['value'] = $media->uploadImage($url)['media_id'];
  399. // }
  400. // }
  401. // }
  402. $data = [
  403. [
  404. "first"=>304,
  405. "second"=> 1309
  406. ]
  407. ];
  408. $result = $miniProgram->setting->addCategories($data);
  409. if ($result['errcode'] === 0) {
  410. $result = $this->getMiniCategory();
  411. if ($result['code'] != 0) {
  412. throw new \Exception($result['msg']);
  413. }
  414. return [
  415. 'code' => 0,
  416. 'msg' => "操作成功"
  417. ];
  418. } elseif ($result['errcode'] != 0) {
  419. $msg = (new BaseForm())->getZnMsg($result);
  420. throw new \Exception($msg);
  421. } else {
  422. throw new \Exception("数据错误");
  423. }
  424. } catch (\Exception $e) {
  425. return [
  426. 'code' => 1,
  427. 'msg' => $e->getMessage()
  428. ];
  429. }
  430. }
  431. //查询已经设置的小程序类目
  432. public function getMiniCategory()
  433. {
  434. try {
  435. $result = $this->inits();
  436. if ($result['code'] === 1) {
  437. return $result;
  438. }
  439. //获取微信请求配置
  440. $miniProgram = $this->miniProgram;
  441. //获取小程序信息
  442. $store_mini = $this->store_mini;
  443. //请求微信接口
  444. $result = $miniProgram->setting->getCategories();
  445. if ($result['errcode'] === 0) {
  446. //保存分类信息
  447. $store_mini->categories = json_encode($result['categories']) ?: "";
  448. //保存失败
  449. if (!$store_mini->save()) {
  450. throw new \Exception(json_encode($store_mini->errors));
  451. }
  452. //保存成功
  453. return [
  454. 'code' => 0,
  455. 'msg' => "获取成功",
  456. 'data' => $result['categories']
  457. ];
  458. } elseif (empty($result)) {
  459. throw new \Exception("数据错误");
  460. } else {
  461. $msg = (new BaseForm())->getZnMsg($result);
  462. throw new \Exception($msg);
  463. }
  464. } catch (\Exception $e) {
  465. return [
  466. 'code' => 1,
  467. 'msg' => $e->getMessage()
  468. ];
  469. }
  470. }
  471. //删除类目
  472. public function delMiniCategory()
  473. {
  474. try {
  475. $result = $this->inits();
  476. if ($result['code'] === 1) {
  477. return $result;
  478. }
  479. //获取微信请求配置
  480. $miniProgram = $this->miniProgram;
  481. //获取分类信息
  482. $mini_info = $this->mini_info;
  483. $category = $mini_info['category'];
  484. //请求微信删除类目接口
  485. $result = $miniProgram->setting->deleteCategories($category['cate_first'], $category['cate_second']);
  486. //请求成功
  487. if ($result['errcode'] === 0) {
  488. $result = $this->getMiniCategory();
  489. if ($result['code'] != 0) {
  490. throw new \Exception($result['msg']);
  491. }
  492. return [
  493. 'code' => 0,
  494. 'msg' => "删除成功"
  495. ];
  496. //请求微信返回错误信息
  497. } elseif ($result['errcode'] != 0) {
  498. $msg = (new BaseForm())->getZnMsg($result);
  499. throw new \Exception($msg);
  500. //请求失败
  501. } else {
  502. throw new \Exception("数据错误");
  503. }
  504. } catch (\Exception $e) {
  505. return [
  506. 'code' => 1,
  507. 'msg' => $e->getMessage()
  508. ];
  509. }
  510. }
  511. //修改小程序简介
  512. public function setSignature()
  513. {
  514. $t = \Yii::$app->db->beginTransaction();
  515. try {
  516. $result = $this->inits();
  517. if ($result['code'] === 1) {
  518. return $result;
  519. }
  520. //获取微信请求配置
  521. $miniProgram = $this->miniProgram;
  522. //获取简介
  523. $mini_info = $this->mini_info;
  524. $signature = $mini_info['signature'];
  525. //获取小程序model
  526. $store_mini = $this->store_mini;
  527. //设置简介
  528. $store_mini->signature = $signature;
  529. if (!$store_mini->save()) {
  530. throw new \Exception($store_mini->errors);
  531. }
  532. //请求微信接口
  533. $result = $miniProgram->account->updateSignature($signature);
  534. if ($result['errcode'] === 0) {
  535. $t->commit();
  536. return [
  537. 'code' => 0,
  538. 'msg' => "保存成功"
  539. ];
  540. } elseif ($result['errcode'] != 0) {
  541. $msg = (new BaseForm())->getZnMsg($result);
  542. throw new \Exception($msg);
  543. } else {
  544. throw new \Exception("数据错误");
  545. }
  546. } catch (\Exception $e) {
  547. $t->rollBack();
  548. return [
  549. 'code' => 1,
  550. 'msg' => $e->getMessage()
  551. ];
  552. }
  553. }
  554. //修改设置小程序隐私设置
  555. public function setPrivacyForm()
  556. {
  557. try {
  558. $result = $this->inits();
  559. if ($result['code'] === 1) {
  560. return $result;
  561. }
  562. $data = [
  563. "privacy_ver" => 2,
  564. "owner_setting" => [
  565. "contact_phone" => "17090402350",
  566. "notice_method" => "短信"
  567. ],
  568. "setting_list" => [
  569. [
  570. "privacy_key" => "UserInfo",
  571. "privacy_text" => "统计订单"
  572. ],
  573. [
  574. "privacy_key" => "Location",
  575. "privacy_text" => "显示附近自提点"
  576. ],
  577. [
  578. "privacy_key" => "Address",
  579. "privacy_text" => "显示附近自提点"
  580. ],
  581. [
  582. "privacy_key" => "RunData",
  583. "privacy_text" => "消费步数兑换商品"
  584. ],
  585. [
  586. "privacy_key" => "Record",
  587. "privacy_text" => "售后咨询"
  588. ],
  589. [
  590. "privacy_key" => "Camera",
  591. "privacy_text" => "提交订单备注资料"
  592. ],
  593. [
  594. "privacy_key" => "AlbumWriteOnly",
  595. "privacy_text" => "保存海报"
  596. ],
  597. [
  598. "privacy_key" => "PhoneNumber",
  599. "privacy_text" => "多端口数据统一"
  600. ],
  601. [
  602. "privacy_key" => "Album",
  603. "privacy_text" => "审核资质"
  604. ]
  605. ],
  606. ];
  607. $miniProgram = $this->miniProgram;
  608. //创建请求方式
  609. $client = new BaseClient($miniProgram);
  610. //请求设置隐私保护指引接口
  611. $result = $client->httpPostJson('cgi-bin/component/setprivacysetting', $data);
  612. if ($result['errcode'] === 0) {
  613. return [
  614. 'code' => 0,
  615. 'msg' => "设置成功"
  616. ];
  617. } elseif ($result['errcode'] != 0) {
  618. $msg = (new BaseForm())->getZnMsg($result);
  619. throw new \Exception($msg);
  620. } else {
  621. throw new \Exception("数据错误");
  622. }
  623. } catch (\Exception $e) {
  624. return [
  625. 'code' => 1,
  626. 'msg' => $e->getMessage()
  627. ];
  628. }
  629. }
  630. //修改小程序服务器域名
  631. public function setDomainName()
  632. {
  633. try {
  634. $result = $this->inits();
  635. if ($result['code'] === 1) {
  636. return $result;
  637. }
  638. //获取微信请求配置
  639. $miniProgram = $this->miniProgram;
  640. //设置配置服务器域名资料信息
  641. $data = [
  642. 'action' => 'set',//操作类型 add添加 delete删除 set覆盖 get获取
  643. 'requestdomain' => ["https://" . \Yii::$app->request->hostName],
  644. "wsrequestdomain" => ["wss://" . \Yii::$app->request->hostName],
  645. "uploaddomain" => ["https://" . \Yii::$app->request->hostName],
  646. "downloaddomain" => ["https://" . \Yii::$app->request->hostName],
  647. "udpdomain" => ["udp://" . \Yii::$app->request->hostName],
  648. "tcpdomain" => ["tcp://" . \Yii::$app->request->hostName]
  649. ];
  650. //请求微信
  651. $result = $miniProgram->domain->modify($data);
  652. //返回成功
  653. if ($result['errcode'] === 0) {
  654. return [
  655. 'code' => 0,
  656. 'msg' => "设置成功"
  657. ];
  658. //返回失败输出错误信息
  659. } elseif ($result['errcode'] != 0) {
  660. $msg = (new BaseForm())->getZnMsg($result);
  661. throw new \Exception($msg);
  662. } else {
  663. throw new \Exception("数据错误");
  664. }
  665. } catch (\Exception $e) {
  666. return [
  667. 'code' => 1,
  668. 'msg' => $e->getMessage()
  669. ];
  670. }
  671. }
  672. /**
  673. * 下载校验文件并提交至客服系统
  674. * @param mixed $app
  675. * @return void
  676. * @throws \Exception
  677. * @date 2023-02-20
  678. */
  679. public function addConfirmFile($app)
  680. {
  681. $access_token = $app->access_token->getToken()['authorizer_access_token'];
  682. $res = http_post('https://api.weixin.qq.com/wxa/get_webviewdomain_confirmfile?access_token=' . $access_token, [
  683. 'body' => '{}'
  684. ]);
  685. if ($res->getStatusCode() != 200) {
  686. throw new \Exception('下载校验文件出错');
  687. }
  688. $result = \json_decode((string)$res->getBody(), true);
  689. if ($result['errcode'] != 0) {
  690. throw new \Exception($result['errmsg']);
  691. }
  692. $res = http_post('https://ws2.cyyvip.com/web/index.php?r=common/default/add-confirm-file', [
  693. 'form_params' => [
  694. 'token' => 'tianxin100',
  695. 'file_name' => $result['file_name'],
  696. 'file_content' => $result['file_content'],
  697. ]
  698. ]);
  699. if ($res->getStatusCode() != 200) {
  700. throw new \Exception('添加校验文件出错');
  701. }
  702. if ($result['code'] != 0) {
  703. throw new \Exception($result['msg']);
  704. }
  705. }
  706. //修改小程序业务域名
  707. public function setWebviewDomain()
  708. {
  709. try {
  710. $result = $this->inits();
  711. if ($result['code'] === 1) {
  712. return $result;
  713. }
  714. $store_mini = $this->store_mini;
  715. //获取微信请求配置
  716. $miniProgram = $this->miniProgram;
  717. $this->addConfirmFile($miniProgram);
  718. $result = $miniProgram->domain->setWebviewDomain([
  719. 'https://' . \Yii::$app->params['ws_url'],
  720. ], 'set');
  721. //返回成功
  722. if ($result['errcode'] === 0) {
  723. return [
  724. 'code' => 0,
  725. 'msg' => "设置成功"
  726. ];
  727. //返回失败输出错误信息
  728. } elseif ($result['errcode'] != 0) {
  729. $msg = (new BaseForm())->getZnMsg($result);
  730. throw new \Exception($msg);
  731. } else {
  732. throw new \Exception("数据错误");
  733. }
  734. } catch (\Exception $e) {
  735. return [
  736. 'code' => 1,
  737. 'msg' => $e->getMessage()
  738. ];
  739. }
  740. }
  741. //查询最后一次审核状态
  742. public function getLastAuditStatus()
  743. {
  744. try {
  745. $result = $this->inits();
  746. if ($result['code'] === 1) {
  747. return $result;
  748. }
  749. $miniProgram = $this->miniProgram;
  750. $result = $miniProgram->code->getLatestAuditStatus();
  751. if ($result['errcode'] === 0) {
  752. switch ($result['status']) {
  753. case 1:
  754. $msg = "审核被拒绝";
  755. break;
  756. case 2:
  757. $msg = "审核中";
  758. break;
  759. case 3:
  760. $msg = "已撤回";
  761. break;
  762. default:
  763. $msg = "审核成功";
  764. break;
  765. }
  766. return [
  767. 'code' => 0,
  768. 'msg' => $msg,
  769. 'data' => [
  770. 'reason' => $result
  771. ]
  772. ];
  773. } elseif ($result['errcode'] != 0) {
  774. $msg = (new BaseForm())->getZnMsg($result);
  775. throw new \Exception($msg);
  776. } else {
  777. throw new \Exception("数据错误");
  778. }
  779. } catch (\Exception $e) {
  780. return [
  781. 'code' => 1,
  782. 'msg' => $e->getMessage()
  783. ];
  784. }
  785. }
  786. }