WechatThirdAuthForm.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\Option;
  9. use app\models\Store;
  10. use app\models\StoreMini;
  11. use app\models\StoreMiniAuth;
  12. use app\utils\CurlHelper;
  13. use app\utils\QrCode;
  14. use app\utils\Wechat\WechatMini;
  15. use EasyWeChat\BasicService\Media\Client;
  16. use EasyWeChat\Kernel\BaseClient;
  17. use yii\base\Model;
  18. use yii\helpers\ArrayHelper;
  19. class WechatThirdAuthForm extends Model
  20. {
  21. public $auth_data;
  22. public $baseClient;
  23. // public $customer_type; //身份类型企业为1,个体工商户 为12,个人是15
  24. //
  25. // public $taskid; //认证任务id,打回重审调用reauth时为必填
  26. //
  27. // public $name; //认证联系人姓名
  28. //
  29. // public $email; //认证联系人邮箱
  30. //
  31. // public $invoice_type; //发票类型 1: 不开发票 2: 电子发票 3: 增值税专票
  32. //
  33. // public $invoice_id; //纳税识别号(15位、17、18或20位)
  34. //
  35. // public $desc; //发票备注(选填)
  36. //
  37. // public $enterprise_phone; //企业电话
  38. //
  39. // public $enterprise_address; //企业注册地址
  40. //
  41. // public $bank_name; //企业开户银行
  42. //
  43. // public $bank_account; //企业银行账号
  44. //
  45. // public $mailing_address; //发票邮寄地址邮编
  46. //
  47. // public $address; //街道地址
  48. //
  49. // public $phone; //联系电话
  50. //
  51. // public $province; //省份
  52. //
  53. // public $city; //城市
  54. //
  55. // public $district; //县区
  56. //
  57. // public $invoice_title; //发票抬头,需要和认证主体名称一样
  58. //
  59. // public $qualification; //非个人类型必填。主体资质材料 media_id 支持jpg,jpeg .bmp.gif .png格式,仅支持一张图片
  60. //
  61. // public $qualification_other; //主体资质其他证明材料 media_id 支持jpg,jpeg .bmp.gif .png格式,最多上传10张图片
  62. //
  63. // public $account_name; //小程序账号名称
  64. //
  65. // public $account_name_type; //小程序账号名称命名类型 1:基于自选词汇命名 2:基于商标命名
  66. //
  67. // public $account_supplemental; //名称命中关键词-补充材料 media_id 支持jpg,jpeg .bmp.gif .png格式,支持上传多张图片
  68. //
  69. // public $pay_type; //支付方式 1:消耗服务商预购包 2:小程序开发者自行支付
  70. //
  71. // public $third_party_phone; //第三方联系电话
  72. //
  73. // public $service_appid; //选择服务商代缴模式时必填。服务市场appid,该服务市场账号主体必须与服务商账号主体一致
  74. public $mini_id; //小程序id
  75. public $store_id;
  76. public function rules()
  77. {
  78. return [
  79. [['customer_type', 'invoice_type', 'account_name_type', 'pay_type', 'mini_id', 'store_id'], 'integer'],
  80. [['taskid', 'name', 'email', 'invoice_id', 'desc', 'enterprise_phone', 'enterprise_address', 'bank_name',
  81. 'bank_account', 'mailing_address', 'address', 'phone', 'province', 'city', 'district', 'invoice_title',
  82. 'qualification', 'qualification_other', 'account_name', 'account_supplemental', 'third_party_phone', 'service_appid'
  83. ], 'string'],
  84. [['auth_data'], 'safe']
  85. ];
  86. }
  87. // 提交申请
  88. public function applyWxaauth() {
  89. // $t = \Yii::$app->db->beginTransaction();
  90. try {
  91. $app = WechatMini::getWechatConfig($this->store_id, $this->mini_id);
  92. if (!$app) {
  93. throw new \Exception('小程序信息未找到');
  94. }
  95. $this->baseClient = $baseClient = new BaseClient($app);
  96. $mini = StoreMini::findOne($this->mini_id);
  97. if (!$mini) {
  98. throw new \Exception('小程序信息查询失败');
  99. }
  100. $store = Store::findOne($this->store_id);
  101. if (!$store) {
  102. throw new \Exception('商城信息查询失败');
  103. }
  104. $data = $this->auth_data ?: [
  105. 'customer_type' => 1,
  106. 'taskid' => '',
  107. 'contact_info' => [
  108. 'name' => '',
  109. 'email' =>''
  110. ],
  111. 'invoice_info' => [
  112. 'invoice_type' => 2,
  113. 'electronic' => [
  114. 'id' => '',
  115. 'desc' => ''
  116. ],
  117. 'vat' => [
  118. 'enterprise_phone' => '',
  119. 'id' => '',
  120. 'enterprise_address' => '',
  121. 'bank_name' => '',
  122. 'bank_account' => '',
  123. 'mailing_address' => '',
  124. 'address' => '',
  125. 'name' => '',
  126. 'phone' => '',
  127. 'province' => '',
  128. 'city' => '',
  129. 'district' => '',
  130. 'desc' => '',
  131. ],
  132. 'invoice_title' => '',
  133. ],
  134. 'qualification' => [
  135. 'media_id' => '',
  136. 'url' => ''
  137. ],
  138. 'qualification_other' => [
  139. [
  140. 'media_id' => '',
  141. 'url' => ''
  142. ]
  143. ],
  144. 'account_name' => $mini->mini_nickname,
  145. 'account_name_type' => 1,
  146. 'account_supplemental' => [
  147. [
  148. 'media_id' => '',
  149. 'url' => ''
  150. ]
  151. ],
  152. 'pay_type' => StoreMiniAuth::PAY_TYPE_SELF,
  153. 'third_party_phone' => Option::get("platform_phone", 0, 'saas')['value'],
  154. 'service_appid' => Option::get("platform_wechat_fuwu_appid", 0, 'saas')['value']
  155. ];
  156. if (isset($data['third_party_phone']) && empty($data['third_party_phone'])) {
  157. $data['third_party_phone'] = Option::get("platform_phone", 0, 'saas')['value'];
  158. } else {
  159. $data = array_merge($data, ['third_party_phone' => Option::get("platform_phone", 0, 'saas')['value']]);
  160. }
  161. if (intval($data['pay_type']) === StoreMiniAuth::PAY_TYPE_SERVICE) {
  162. if ($store->mini_auth_num <= 0) {
  163. throw new \Exception('当前商城认证代缴次数不足,请联系平台管理员');
  164. }
  165. if (isset($data['service_appid']) && empty($data['service_appid'])) {
  166. $data['service_appid'] = Option::get('platform_wechat_fuwu_appid', 0, 'saas', '')['value'];
  167. } else {
  168. $data = array_merge($data, ['service_appid' => Option::get('platform_wechat_fuwu_appid', 0, 'saas', '')['value']]);
  169. }
  170. }
  171. $mini_auth = StoreMiniAuth::findOne(['store_id' => $this->store_id, 'mini_id' => $this->mini_id]) ?: new StoreMiniAuth();
  172. $mini_auth->store_id = $this->store_id;
  173. $mini_auth->mini_id = $this->mini_id;
  174. if ($mini_auth->task_id) {
  175. $apply_status = intval($mini_auth->apply_status);
  176. if ($apply_status === 5) {//猜测如果已经认证最终失败,则不能重新调用重新提审接口,只能重新开始认证
  177. if (isset($data['taskid'])) {
  178. unset($data['taskid']);
  179. }
  180. } else {
  181. $data['taskid'] = $mini_auth->task_id;
  182. }
  183. } else {
  184. unset($data['taskid']);
  185. }
  186. if (isset($data['task_id'])) {
  187. unset($data['task_id']);
  188. }
  189. $mini_auth->auth_form = json_encode($data, JSON_UNESCAPED_UNICODE);
  190. if (!$mini_auth->save()) {
  191. throw new \Exception(json_encode($mini_auth->errors, JSON_UNESCAPED_UNICODE));
  192. }
  193. if (!in_array($data['customer_type'], [1, 12])) {
  194. throw new \Exception('身份类型枚举值错误');
  195. }
  196. // 支付方式
  197. if (!in_array($data['pay_type'], [StoreMiniAuth::PAY_TYPE_SERVICE, StoreMiniAuth::PAY_TYPE_SELF])) {
  198. throw new \Exception('支付方式枚举值错误');
  199. }
  200. if (intval($data['pay_type']) === StoreMiniAuth::PAY_TYPE_SERVICE) {//支付方式 1:消耗服务商预购包
  201. //发票信息,如果是服务商代缴模式,不需要改参数
  202. // unset($data['invoice_info']);
  203. $data['invoice_info']['invoice_type'] = 1;
  204. if (empty($data['service_appid'])) {
  205. throw new \Exception('请选择服务市场appid');
  206. }
  207. } else {
  208. // 发票类型
  209. if (!in_array($data['invoice_info']['invoice_type'], [1, 2, 3])) {
  210. throw new \Exception('发票类型枚举值错误');
  211. }
  212. }
  213. //不开发票
  214. if (intval($data['invoice_info']['invoice_type']) === 1) {
  215. unset($data['invoice_info']['vat'], $data['invoice_info']['electronic']);
  216. }
  217. // 电子发票
  218. if (intval($data['invoice_info']['invoice_type']) === 2) {
  219. // 未填写电子发票开票信息
  220. if (empty($data['invoice_info']['electronic'])) {
  221. throw new \Exception('未填写电子发票开票信息');
  222. }
  223. // 未填写电子发票纳税识别号
  224. if (empty($data['invoice_info']['electronic']['id'])) {
  225. throw new \Exception('未填写电子发票纳税识别号');
  226. }
  227. unset($data['invoice_info']['vat']);
  228. }
  229. // 增值税专票
  230. if (intval($data['invoice_info']['invoice_type']) === 3) {
  231. $data['invoice_info']['invoice_type'] = 4;
  232. // 未填写电子发票开票信息
  233. if (empty($data['invoice_info']['vat'])) {
  234. throw new \Exception('未填写增值税专票开票信息');
  235. }
  236. // 未填写企业电话
  237. if (empty($data['invoice_info']['vat']['enterprise_phone'])) {
  238. throw new \Exception('未填写企业电话');
  239. }
  240. // 未填写电子发票纳税识别号
  241. if (empty($data['invoice_info']['vat']['id'])) {
  242. throw new \Exception('未填写增值税专票纳税识别号');
  243. }
  244. // 未填写企业注册地址
  245. if (empty($data['invoice_info']['vat']['enterprise_address'])) {
  246. throw new \Exception('未填写企业注册地址');
  247. }
  248. // 未填写企业开户银行
  249. if (empty($data['invoice_info']['vat']['bank_name'])) {
  250. throw new \Exception('未填写企业开户银行');
  251. }
  252. // 未填写企业银行账号
  253. if (empty($data['invoice_info']['vat']['bank_account'])) {
  254. throw new \Exception('未填写企业银行账号');
  255. }
  256. // 未填写发票邮寄地址邮编
  257. if (empty($data['invoice_info']['vat']['mailing_address'])) {
  258. throw new \Exception('未填写发票邮寄地址邮编');
  259. }
  260. // 未填写街道地址
  261. if (empty($data['invoice_info']['vat']['address'])) {
  262. throw new \Exception('未填写街道地址');
  263. }
  264. // 未填写联系人
  265. if (empty($data['invoice_info']['vat']['name'])) {
  266. throw new \Exception('未填写联系人');
  267. }
  268. // 未填写联系电话
  269. if (empty($data['invoice_info']['vat']['phone'])) {
  270. throw new \Exception('未填写联系电话');
  271. }
  272. // 未填写省份
  273. if (empty($data['invoice_info']['vat']['province'])) {
  274. throw new \Exception('未填写省份');
  275. }
  276. // 未填写城市
  277. if (empty($data['invoice_info']['vat']['city'])) {
  278. throw new \Exception('未填写城市');
  279. }
  280. // 未填写县区
  281. if (empty($data['invoice_info']['vat']['district'])) {
  282. throw new \Exception('未填写县区市');
  283. }
  284. unset($data['invoice_info']['electronic']);
  285. }
  286. // 主体资质材料
  287. if (!empty($data['qualification'])) {
  288. if (!empty($data['qualification']['url'])) {
  289. $media_result = $this->updateMedia($data['qualification']['url']);
  290. if (empty($media_result['media_id'])) {
  291. throw new \Exception($media_result['msg']);
  292. }
  293. $data['qualification'] = $media_result['media_id'];
  294. } else {
  295. throw new \Exception('未填写主体资质材料');
  296. }
  297. } else {
  298. throw new \Exception('未填写主体资质材料');
  299. }
  300. // 主体资质其他证明材料
  301. if (!empty($data['qualification_other'])) {
  302. $qualification_other = [];
  303. foreach ($data['qualification_other'] as $media_item) {
  304. if (!empty($media_item['url'])) {
  305. $media_result = $this->updateMedia($media_item['url']);
  306. if (empty($media_result['media_id'])) {
  307. throw new \Exception($media_result['msg']);
  308. }
  309. array_push($qualification_other, $media_result['media_id']);
  310. } else {
  311. array_push($qualification_other, '');
  312. }
  313. }
  314. if (!empty($qualification_other)) {
  315. $data['qualification_other'] = $qualification_other;
  316. }
  317. }
  318. // 小程序账号名称
  319. if (empty($data['account_name'])) {
  320. throw new \Exception('需要填写小程序账号名称');
  321. }
  322. // 小程序账号名称命名类型
  323. if (!in_array($data['account_name_type'], [1, 2])) {
  324. throw new \Exception('小程序账号名称命名类型枚举值错误');
  325. }
  326. // 名称命中关键词-补充材料
  327. if (!empty($data['account_supplemental'])) {
  328. $account_supplemental = [];
  329. foreach ($data['account_supplemental'] as $media_item) {
  330. if (!empty($media_item['url'])) {
  331. $media_result = $this->updateMedia($media_item['url']);
  332. if (empty($media_result['media_id'])) {
  333. throw new \Exception($media_result['msg']);
  334. }
  335. array_push($account_supplemental, $media_result['media_id']);
  336. } else {
  337. array_push($account_supplemental, '');
  338. }
  339. }
  340. if (!empty($account_supplemental)) {
  341. $data['account_supplemental'] = $account_supplemental;
  342. }
  343. }
  344. // 第三方联系电话
  345. if (empty($data['third_party_phone'])) {
  346. throw new \Exception('需要填写第三方联系电话');
  347. }
  348. $url = 'wxa/sec/wxaauth';
  349. if (isset($data['taskid'])) {
  350. $url = 'wxa/sec/reauth';
  351. }
  352. $result = $baseClient->httpPostJson($url, ['auth_data' => $data]);
  353. if (empty($result['errcode']) && !empty($result)) {
  354. debug_log(['old_task_id' => $mini_auth->task_id, 'task_id' => $result['taskid'], 'result' => $result], 'wxaaut.log');
  355. if ($result['taskid']) {
  356. $mini_auth->task_id = $result['taskid'];
  357. }
  358. $mini_auth->auth_url = $result['auth_url'];
  359. $url = $result['auth_url'];
  360. if ($url) {
  361. $pic_url = $this->setPicUrl($url, $mini_auth->id);
  362. debug_log(['pic_url' => $pic_url], 'wxaaut.log');
  363. $mini_auth->pic_url = $pic_url;
  364. }
  365. if (intval($data['pay_type']) === StoreMiniAuth::PAY_TYPE_SERVICE) {
  366. $store->mini_auth_num -= 1;
  367. if (!$store->save()) {
  368. debug_log(['errors' => $store->errors], 'wechatAuth.log');
  369. }
  370. $mini_auth->is_use_service = 1;
  371. }
  372. if (!$mini_auth->save()) {
  373. throw new \Exception(json_encode($mini_auth->errors, JSON_UNESCAPED_UNICODE));
  374. }
  375. // $t->commit();
  376. return [
  377. 'code' => 0,
  378. 'msg' => "成功",
  379. 'data' => [
  380. 'auth_url' => $result['auth_url']
  381. ]
  382. ];
  383. } else {
  384. throw new \Exception($result['errmsg'], $result['errcode']);
  385. }
  386. } catch (\Exception $e) {
  387. // $t->rollBack();
  388. return [
  389. 'code' => $e->getCode() ?: 1,
  390. 'msg' => $e->getMessage()
  391. ];
  392. }
  393. }
  394. public function setPicUrl($auth_url, $id) {
  395. $filename = md5(date('Ym') . 'mini_auth' . $id);
  396. $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  397. $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
  398. QrCode::image($auth_url, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  399. return $pic_url;
  400. }
  401. // 获取信息
  402. public function getApplyData() {
  403. try {
  404. $mini = StoreMini::findOne($this->mini_id);
  405. if (!$mini) {
  406. throw new \Exception('小程序信息查询失败');
  407. }
  408. $data = [
  409. 'customer_type' => 1,
  410. 'taskid' => '',
  411. 'contact_info' => [
  412. 'name' => '',
  413. 'email' =>''
  414. ],
  415. 'pay_type' => 2,
  416. 'invoice_info' => [
  417. 'invoice_type' => 2,
  418. 'electronic' => [
  419. 'id' => '',
  420. 'desc' => ''
  421. ],
  422. 'vat' => [
  423. 'enterprise_phone' => '',
  424. 'id' => '',
  425. 'enterprise_address' => '',
  426. 'bank_name' => '',
  427. 'bank_account' => '',
  428. 'mailing_address' => '',
  429. 'address' => '',
  430. 'name' => '',
  431. 'phone' => '',
  432. 'province' => '',
  433. 'city' => '',
  434. 'district' => '',
  435. 'desc' => '',
  436. ],
  437. 'invoice_title' => '',
  438. ],
  439. 'qualification' => [
  440. 'media_id' => '',
  441. 'url' => ''
  442. ],
  443. 'qualification_other' => [
  444. [
  445. 'media_id' => '',
  446. 'url' => ''
  447. ]
  448. ],
  449. 'account_name' => $mini->mini_nickname,
  450. 'account_name_type' => 1,
  451. 'account_supplemental' => [
  452. [
  453. 'media_id' => '',
  454. 'url' => ''
  455. ]
  456. ],
  457. 'third_party_phone' => Option::get("platform_phone", 0, 'saas')['value'],
  458. 'service_appid' => Option::get("platform_third_appid", 0, 'saas')['value']
  459. ];
  460. $mini_auth = StoreMiniAuth::findOne(['store_id' => $this->store_id, 'mini_id' => $this->mini_id]);
  461. $is_edit = 1;
  462. $pic_url = '';
  463. if ($mini_auth) {
  464. $is_edit = 0;
  465. $this->getTaskStatus($mini_auth);
  466. $auth_form = json_decode($mini_auth->auth_form, JSON_UNESCAPED_UNICODE);
  467. if ($auth_form) {
  468. $data = json_decode($mini_auth->auth_form, true);
  469. }
  470. $task_status = $mini_auth->task_status;
  471. if (in_array($task_status, [-1, 1, 2, 5, 8, 11, 14])) {
  472. $is_edit = 1;
  473. }
  474. $apply_status = intval($mini_auth->apply_status);
  475. if (in_array($apply_status, [3, 5])) {
  476. $is_edit = 1;
  477. }
  478. if (!$mini_auth->pic_url && !empty($mini_auth->auth_url)) {
  479. $pic_url = $this->setPicUrl($mini_auth->auth_url, $mini_auth->id);
  480. $mini_auth->pic_url = $pic_url;
  481. $mini_auth->save();
  482. } else {
  483. $pic_url = $mini_auth->pic_url;
  484. // if (!file_exists($pic_url)) {
  485. // QrCode::image($pic_url, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  486. // }
  487. }
  488. }
  489. return [
  490. 'code' => 0,
  491. 'msg' => '获取成功',
  492. 'data' => [
  493. 'auth_data' => $data,
  494. 'message' => $mini_auth->task_message ?: '',
  495. 'auth_url' => $pic_url ?: '',
  496. 'apply_status' => intval($mini_auth->apply_status) ?: 0,
  497. 'task_status' => intval($mini_auth->task_status) ?: -1,
  498. 'is_edit' => $is_edit
  499. ]
  500. ];
  501. } catch (\Exception $e) {
  502. return [
  503. 'code' => 1,
  504. 'msg' => $e->getMessage()
  505. ];
  506. }
  507. }
  508. // 认证进度查询
  509. private function getTaskStatus(&$mini_auth) {
  510. try {
  511. $app = WechatMini::getWechatConfig($this->store_id, $this->mini_id);
  512. $baseClient = new BaseClient($app);
  513. if (!$mini_auth || empty($mini_auth->task_id)) {
  514. $mini_auth->auth_url = '';
  515. $mini_auth->pic_url = '';
  516. $mini_auth->save();
  517. throw new \Exception('未找到申请认证信息');
  518. }
  519. $result = $baseClient->httpPostJson('wxa/sec/queryauth', ['taskid' => $mini_auth->task_id]);
  520. if (empty($result['errcode']) && !empty($result)) {
  521. $mini_auth->task_status = (int)$result['task_status'];
  522. $mini_auth->apply_status = (int)$result['apply_status'];
  523. $task_message = '';
  524. if ($result['apply_status'] < 2) {
  525. switch ($result['task_status']) {
  526. case 1:
  527. $task_message = "任务超时,24小时内有效";
  528. break;
  529. case 2:
  530. $task_message = "用户授权拒绝";
  531. break;
  532. case 3:
  533. $task_message = "用户授权同意";
  534. break;
  535. case 4:
  536. $task_message = "发起人脸流程";
  537. break;
  538. case 5:
  539. $task_message = "人脸认证失败";
  540. break;
  541. case 6:
  542. $task_message = "人脸认证成功";
  543. break;
  544. case 7:
  545. $task_message = "人脸认证后,已经提交手机号码下发验证码";
  546. break;
  547. case 8:
  548. $task_message = "手机验证失败";
  549. break;
  550. case 9:
  551. $task_message = "手机验证成功";
  552. break;
  553. case 11:
  554. $task_message = "创建认证审核单失败";
  555. break;
  556. case 12:
  557. $task_message = "创建认证审核单成功";
  558. break;
  559. case 14:
  560. $task_message = "验证失败";
  561. break;
  562. case 15:
  563. $task_message = "等待支付";
  564. break;
  565. }
  566. } else {
  567. switch ($result['apply_status']) {
  568. case 2:
  569. $task_message = "审核中";
  570. break;
  571. case 3:
  572. $task_message = "打回重填:" . $result['refill_reason'];
  573. break;
  574. case 4:
  575. $task_message = "认证通过";
  576. break;
  577. case 5:
  578. $task_message = "认证最终失败:" . $result['fail_reason'];
  579. break;
  580. }
  581. }
  582. if (in_array($result['task_status'], [1, 2, 5, 8, 11, 14]) || in_array($result['apply_status'], [3, 5])) {
  583. if ($mini_auth->is_use_service) {
  584. $mini_auth->is_use_service = 0;
  585. $store = Store::findOne($this->store_id);
  586. if (!$store) {
  587. throw new \Exception('店铺不存在');
  588. }
  589. $store->mini_auth_num += 1;
  590. if (!$store->save()) {
  591. throw new \Exception(json_encode($store->errors, JSON_UNESCAPED_UNICODE));
  592. }
  593. }
  594. }
  595. $mini_auth->task_message = $task_message;
  596. if (!$mini_auth->save()) {
  597. throw new \Exception(json_encode($mini_auth->errors, JSON_UNESCAPED_UNICODE));
  598. }
  599. return [
  600. 'code' => 0,
  601. 'msg' => "更新成功"
  602. ];
  603. } else {
  604. return [
  605. 'code' => $result['errcode'],
  606. 'msg' => $result['errmsg']
  607. ];
  608. }
  609. } catch (\Exception $e) {
  610. return [
  611. 'code' => 0,
  612. 'msg' => $e->getMessage()
  613. ];
  614. }
  615. }
  616. /**
  617. * @param $local_media_url
  618. * @return array
  619. * 获取media_id
  620. */
  621. public function updateMedia($local_media_url) {
  622. try {
  623. $baseClient = $this->baseClient;
  624. $result = $baseClient->httpUpload('wxa/sec/uploadauthmaterial', [
  625. 'media' => $this->saveTempImage($local_media_url)
  626. ]);
  627. if (empty($result['errcode']) && !empty($result)) {
  628. return [
  629. 'code' => 0,
  630. 'msg' => "成功",
  631. 'media_id' => $result['mediaid']
  632. ];
  633. } else {
  634. return [
  635. 'code' => $result['errcode'],
  636. 'msg' => $result['errmsg']
  637. ];
  638. }
  639. } catch (\Exception $e) {
  640. return [
  641. 'code' => 1,
  642. 'msg' => $e->getMessage()
  643. ];
  644. }
  645. }
  646. //获取网络图片到临时目录
  647. private function saveTempImage($url)
  648. {
  649. if (strpos($url, 'http') === false) {
  650. $url = 'http:' . trim($url);
  651. }
  652. if (!is_dir(\Yii::$app->runtimePath . '/image')) {
  653. mkdir(\Yii::$app->runtimePath . '/image');
  654. }
  655. $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg';
  656. CurlHelper::download($url, $save_path);
  657. return $save_path;
  658. }
  659. }