WechatMiniIcpForm.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. <?php
  2. namespace app\modules\admin\models;
  3. use app\models\StoreMedia;
  4. use app\models\StoreMiniIcp;
  5. use app\utils\CurlHelper;
  6. use app\utils\Image;
  7. use app\utils\Wechat\WechatMini;
  8. use EasyWeChat\Kernel\BaseClient;
  9. use yii\base\Model;
  10. class WechatMiniIcpForm extends Model
  11. {
  12. public $store_id;
  13. public $mini_id;
  14. public $app;
  15. public $params;
  16. public function rules()
  17. {
  18. return [
  19. [['params'], 'safe']
  20. ];
  21. }
  22. public function __construct($config = [])
  23. {
  24. //new BaseClient($app)->httpPostJson(); 访问链接
  25. //new Client($app); 上传文件
  26. parent::__construct($config);
  27. $this->store_id = $config['store_id'];
  28. $this->mini_id = $config['mini_id'];
  29. $this->app = (new WechatMini())::getWechatConfig($config['store_id'], $config['mini_id']);
  30. }
  31. public function storeIcpInfo() {
  32. try {
  33. $mini_id = $this->mini_id;
  34. //查询当前状态
  35. //查询人脸核身任务状态
  36. $this->queryIcpVerifyTask();
  37. //获取小程序备案状态及驳回原因
  38. $this->getIcpEntranceInfo();
  39. $store_mini_icp_info = StoreMiniIcp::find()->where(['mini_id' => $mini_id, 'is_delete' => 0])
  40. ->select('task_id, mini_id, face_status, icp_form, icp_audit_status, icp_audit_info')
  41. ->asArray()->one();
  42. if ($store_mini_icp_info) {
  43. $store_mini_icp_info['face_status'] = (int)$store_mini_icp_info['face_status'];
  44. if (intval($store_mini_icp_info['icp_audit_status']) >= 2) {
  45. $store_mini_icp_info['face_status'] = 3;
  46. }
  47. $store_mini_icp_info['icp_audit_status'] = (int)$store_mini_icp_info['icp_audit_status'];
  48. $store_mini_icp_info['icp_form'] = json_decode($store_mini_icp_info['icp_form'], true);
  49. $store_mini_icp_info['icp_audit_info'] = json_decode($store_mini_icp_info['icp_audit_info'], true) ?: '';
  50. } else {
  51. $store_mini_icp_info = (object)[];
  52. }
  53. $data = [
  54. 'icp_info' => $store_mini_icp_info
  55. ];
  56. //单位性质
  57. $icpSubjectTypesResult = $this->queryIcpSubjectTypes();
  58. if ($icpSubjectTypesResult['code'] === 0) {
  59. $data = array_merge($data, [
  60. 'subject_types' => $icpSubjectTypesResult['data']['data']
  61. ]);
  62. }
  63. //区域信息
  64. $icpDistrictCodeResult = $this->queryIcpDistrictCode();
  65. if ($icpDistrictCodeResult['code'] === 0) {
  66. $data = array_merge($data, [
  67. 'district_code' => $icpDistrictCodeResult['data']['data']
  68. ]);
  69. }
  70. //获取证件类型
  71. $icpCertificateTypesResult = $this->queryIcpCertificateTypes();
  72. if ($icpCertificateTypesResult['code'] === 0) {
  73. $data = array_merge($data, [
  74. 'certificate_types' => $icpCertificateTypesResult['data']['data']
  75. ]);
  76. }
  77. //获取前置审批项类型
  78. $queryIcpNrlxTypesResult = $this->queryIcpNrlxTypes();
  79. if ($queryIcpNrlxTypesResult['code'] === 0) {
  80. $data = array_merge($data, [
  81. 'nrlx_types' => $queryIcpNrlxTypesResult['data']['data']
  82. ]);
  83. }
  84. //获取小程序服务内容类型
  85. $icpServiceContentTypesResult = $this->queryIcpServiceContentTypes();
  86. if ($icpServiceContentTypesResult['code'] === 0) {
  87. $data = array_merge($data, [
  88. 'service_content_types' => $icpServiceContentTypesResult['data']['data']
  89. ]);
  90. }
  91. return [
  92. 'code' => 0,
  93. 'msg' => '获取成功',
  94. 'data' => $data
  95. ];
  96. } catch (\Exception $e) {
  97. return [
  98. 'code' => $e->getCode() ?: 1,
  99. 'msg' => $e->getMessage()
  100. ];
  101. }
  102. }
  103. /**
  104. * 申请小程序备案
  105. */
  106. public function applyIcpFiling() {
  107. try {
  108. //请求微信接口
  109. $app = $this->app;
  110. if (!$app) {
  111. throw new \Exception('获取小程序信息失败');
  112. }
  113. $params_ = $this->params;
  114. $mini_id = $this->mini_id;
  115. $store_mini_icp = StoreMiniIcp::findOne(['mini_id' => $mini_id, 'is_delete' => 0]);
  116. if (!$store_mini_icp) {
  117. throw new \Exception('未进行人脸识别核验');
  118. }
  119. $store_mini_icp->icp_form = json_encode($params_, JSON_UNESCAPED_UNICODE);
  120. if (!$store_mini_icp->save()) {
  121. throw new \Exception(json_encode($store_mini_icp->errors, JSON_UNESCAPED_UNICODE));
  122. }
  123. if (!$store_mini_icp->task_id) {
  124. throw new \Exception('未进行人脸识别核验');
  125. }
  126. // TODO 图片处理 ['media_id' => '', 'url' => ''] ['微信所需media_id' => '', '前端图片显示地址' => '']
  127. //备案主体信息-个人主体额外信息-临时居住证明照片
  128. $residence_permit = $params_['icp_subject']['personal_info']['residence_permit'];
  129. if ($residence_permit) {
  130. $params_['icp_subject']['personal_info']['residence_permit'] = $residence_permit['media_id'];
  131. }
  132. //备案主体信息-主体额外信息-主体证件照片
  133. $certificate_photo = $params_['icp_subject']['organize_info']['certificate_photo'];
  134. if ($certificate_photo) {
  135. $params_['icp_subject']['organize_info']['certificate_photo'] = $certificate_photo['media_id'];
  136. }
  137. //备案主体信息-主体负责人信息-负责人证件正面照片
  138. $certificate_photo_front = $params_['icp_subject']['principal_info']['certificate_photo_front'];
  139. if ($certificate_photo_front) {
  140. $params_['icp_subject']['principal_info']['certificate_photo_front'] = $certificate_photo_front['media_id'];
  141. }
  142. //备案主体信息-主体负责人信息-负责人证件背面照片
  143. $certificate_photo_back = $params_['icp_subject']['principal_info']['certificate_photo_back'];
  144. if ($certificate_photo_back) {
  145. $params_['icp_subject']['principal_info']['certificate_photo_back'] = $certificate_photo_back['media_id'];
  146. }
  147. //备案主体信息-主体负责人信息-授权书
  148. $authorization_letter = $params_['icp_subject']['principal_info']['authorization_letter'];
  149. if ($authorization_letter) {
  150. $params_['icp_subject']['principal_info']['authorization_letter'] = $authorization_letter['media_id'];
  151. }
  152. //备案主体信息-主体负责人信息-扫脸认证任务id
  153. $params_['icp_subject']['principal_info']['verify_task_id'] = $store_mini_icp->task_id;
  154. //微信小程序信息-微信小程序基本信息-前置审批项-前置审批媒体材料
  155. $nrlx_details_media = $params_['icp_applets']['base_info']['nrlx_details']['media'];
  156. if ($nrlx_details_media) {
  157. $params_['icp_applets']['base_info']['nrlx_details']['media'] = $nrlx_details_media['media_id'];
  158. }
  159. //微信小程序信息-小程序负责人信息-负责人证件正面照片
  160. $certificate_photo_front = $params_['icp_applets']['principal_info']['certificate_photo_front'];
  161. if ($certificate_photo_front) {
  162. $params_['icp_applets']['principal_info']['certificate_photo_front'] = $certificate_photo_front['media_id'];
  163. }
  164. //微信小程序信息-小程序负责人信息-负责人证件背面照片
  165. $certificate_photo_back = $params_['icp_applets']['principal_info']['certificate_photo_back'];
  166. if ($certificate_photo_back) {
  167. $params_['icp_applets']['principal_info']['certificate_photo_back'] = $certificate_photo_back['media_id'];
  168. }
  169. //微信小程序信息-小程序负责人信息-授权书
  170. $authorization_letter = $params_['icp_applets']['principal_info']['authorization_letter'];
  171. if ($authorization_letter) {
  172. $params_['icp_applets']['principal_info']['authorization_letter'] = $authorization_letter['media_id'];
  173. }
  174. //微信小程序信息-小程序负责人信息-扫脸认证任务id
  175. $params_['icp_applets']['principal_info']['verify_task_id'] = $store_mini_icp->task_id;
  176. //其他备案媒体材料-互联网信息服务承诺书 最大数量1
  177. $commitment_letter_ = $params_['icp_materials']['commitment_letter'];
  178. if ($commitment_letter_) {
  179. $params_['icp_materials']['commitment_letter'] = $this->arrayChange($commitment_letter_);
  180. }
  181. //其他备案媒体材料-主体更名函 media_id(非个人类型,且发生过更名时需要上传) 最大数量1
  182. $business_name_change_letter_ = $params_['icp_materials']['business_name_change_letter'];
  183. if ($business_name_change_letter_) {
  184. $params_['icp_materials']['business_name_change_letter'] = $this->arrayChange($business_name_change_letter_);
  185. }
  186. //其他备案媒体材料-党建确认函 最大数量1
  187. $party_building_confirmation_letter_ = $params_['icp_materials']['party_building_confirmation_letter'];
  188. if ($party_building_confirmation_letter_) {
  189. $params_['icp_materials']['party_building_confirmation_letter'] = $this->arrayChange($party_building_confirmation_letter_);
  190. }
  191. //其他备案媒体材料-承诺视频 最大数量1
  192. $promise_video_ = $params_['icp_materials']['promise_video'];
  193. if ($promise_video_) {
  194. $params_['icp_materials']['promise_video'] = $this->arrayChange($promise_video_);
  195. }
  196. //其他备案媒体材料-党建确认函 最大数量1
  197. $authenticity_responsibility_letter_ = $params_['icp_materials']['authenticity_responsibility_letter'];
  198. if ($authenticity_responsibility_letter_) {
  199. $params_['icp_materials']['authenticity_responsibility_letter'] = $this->arrayChange($authenticity_responsibility_letter_);
  200. }
  201. //其他备案媒体材料-小程序备案信息真实性承诺书 最大数量1
  202. $authenticity_commitment_letter_ = $params_['icp_materials']['authenticity_commitment_letter'];
  203. if ($authenticity_commitment_letter_) {
  204. $params_['icp_materials']['authenticity_commitment_letter'] = $this->arrayChange($authenticity_commitment_letter_);
  205. }
  206. //其他备案媒体材料-小程序建设方案书 最大数量1
  207. $website_construction_proposal_ = $params_['icp_materials']['website_construction_proposal'];
  208. if ($website_construction_proposal_) {
  209. $params_['icp_materials']['website_construction_proposal'] = $this->arrayChange($website_construction_proposal_);
  210. }
  211. //其他备案媒体材料-主体其它附件 最大数量10
  212. $subject_other_materials_ = $params_['icp_materials']['subject_other_materials'];
  213. if ($subject_other_materials_) {
  214. $params_['icp_materials']['subject_other_materials'] = $this->arrayChange($subject_other_materials_);
  215. }
  216. //其他备案媒体材料-小程序其它附件 最大数量10
  217. $applets_other_materials_ = $params_['icp_materials']['applets_other_materials'];
  218. if ($applets_other_materials_) {
  219. $params_['icp_materials']['applets_other_materials'] = $this->arrayChange($applets_other_materials_);
  220. }
  221. //其他备案媒体材料-手持证件照 最大数量1
  222. $holding_certificate_photo_ = $params_['icp_materials']['holding_certificate_photo'];
  223. if ($holding_certificate_photo_) {
  224. $params_['icp_materials']['holding_certificate_photo'] = $this->arrayChange($holding_certificate_photo_);
  225. }
  226. if ($params_['icp_applets']['base_info']['nrlx_details']) {
  227. foreach ($params_['icp_applets']['base_info']['nrlx_details'] as $nrlx_index => $nrlx_item) {
  228. if (intval($nrlx_item['type']) === 24) {//以上都不涉及
  229. $params_['icp_applets']['base_info']['nrlx_details'][$nrlx_index] = [
  230. 'type' => 24
  231. ];
  232. }
  233. }
  234. } else {
  235. $params_['icp_applets']['base_info']['nrlx_details'] = [
  236. [
  237. 'type' => 24
  238. ]
  239. ];
  240. }
  241. if (isset($params_['icp_subject']['base_info']['commnet'])) {
  242. unset($params_['icp_subject']['base_info']['commnet']);
  243. }
  244. $client = new BaseClient($app);
  245. $result = $client->httpPostJson('wxa/icp/apply_icp_filing', $params_);
  246. //返回微信信息
  247. if ($result['errcode'] !== 0) {
  248. $msg = $result['errmsg'];
  249. if ($result['hints']) {
  250. $msg = '';
  251. foreach ($result['hints'] as $hint) {
  252. if ($hint['errmsg']) {
  253. $msg .= $hint['errmsg'] . ";";
  254. }
  255. }
  256. }
  257. throw new \Exception($msg, $result['errcode']);
  258. }
  259. return [
  260. 'code' => 0,
  261. 'msg' => '申请已提交'
  262. ];
  263. } catch (\Exception $e) {
  264. if (strpos($e->getMessage(), 'cURL error 28') !== false) {
  265. return $this->applyIcpFiling();
  266. }
  267. return [
  268. 'code' => $e->getCode() ?: 1,
  269. 'msg' => $e->getMessage()
  270. ];
  271. }
  272. }
  273. /**
  274. * @param array $array
  275. * @return array
  276. * 数组转换
  277. */
  278. public function arrayChange($array = [])
  279. {
  280. $array_ = [];
  281. foreach ($array as $item) {
  282. if ($item['media_id']) {
  283. $array_[] = $item['media_id'];
  284. }
  285. }
  286. return $array_;
  287. }
  288. /**
  289. * 获取小程序备案状态及驳回原因
  290. */
  291. public function getIcpEntranceInfo() {
  292. try {
  293. //请求微信接口
  294. $app = $this->app;
  295. if (!$app) {
  296. throw new \Exception('获取小程序信息失败');
  297. }
  298. $mini_id = $this->mini_id;
  299. $client = new BaseClient($app);
  300. $result = $client->httpGet('wxa/icp/get_icp_entrance_info');
  301. //返回微信信息
  302. if ($result['errcode'] !== 0) {
  303. throw new \Exception($result['errmsg'], $result['errcode']);
  304. }
  305. $status = $result['info']['status'];
  306. $is_canceling = (int)$result['info']['is_canceling'];
  307. $audit_data = $result['info']['audit_data'];
  308. $store_mini_icp = StoreMiniIcp::findOne(['mini_id' => $mini_id, 'is_delete' => 0]);
  309. if ($store_mini_icp) {
  310. $store_mini_icp->icp_audit_status = (int)$status;
  311. $store_mini_icp->is_cancel_icp = $is_canceling;
  312. if ($is_canceling === 1) {
  313. $store_mini_icp->is_delete = 1;
  314. }
  315. $store_mini_icp->icp_audit_info = json_encode($audit_data, JSON_UNESCAPED_UNICODE);
  316. if (!$store_mini_icp->save()) {
  317. throw new \Exception(json_encode($store_mini_icp->errors, JSON_UNESCAPED_UNICODE));
  318. }
  319. }
  320. return [
  321. 'code' => 0,
  322. 'msg' => '状态已更新'
  323. ];
  324. } catch (\Exception $e) {
  325. return [
  326. 'code' => $e->getCode() ?: 1,
  327. 'msg' => $e->getMessage()
  328. ];
  329. }
  330. }
  331. /**
  332. * 撤销小程序备案申请
  333. */
  334. public function cancelApplyIcpFiling() {
  335. try {
  336. //请求微信接口
  337. $app = $this->app;
  338. if (!$app) {
  339. throw new \Exception('获取小程序信息失败');
  340. }
  341. $mini_id = $this->mini_id;
  342. $client = new BaseClient($app);
  343. $result = $client->httpPostJson('wxa/icp/cancel_apply_icp_filing');
  344. //返回微信信息
  345. if ($result['errcode'] !== 0) {
  346. throw new \Exception($result['errmsg'], $result['errcode']);
  347. }
  348. $store_mini_icp = StoreMiniIcp::findOne(['mini_id' => $mini_id, 'is_delete' => 0]);
  349. if ($store_mini_icp) {
  350. $store_mini_icp->is_cancel_icp = 1;
  351. $store_mini_icp->is_delete = 1;
  352. if (!$store_mini_icp->save()) {
  353. throw new \Exception(json_encode($store_mini_icp->errors, JSON_UNESCAPED_UNICODE));
  354. }
  355. }
  356. return [
  357. 'code' => 0,
  358. 'msg' => '申请已提交'
  359. ];
  360. } catch (\Exception $e) {
  361. return [
  362. 'code' => $e->getCode() ?: 1,
  363. 'msg' => $e->getMessage()
  364. ];
  365. }
  366. }
  367. /**
  368. * 注销小程序备案
  369. */
  370. public function cancelIcpfiling() {
  371. try {
  372. //请求微信接口
  373. $app = $this->app;
  374. if (!$app) {
  375. throw new \Exception('获取小程序信息失败');
  376. }
  377. $params_ = $this->params;
  378. $data = [
  379. 'cancel_type' => (int)$params_['cancel_type']
  380. ];
  381. $client = new BaseClient($app);
  382. $result = $client->httpPostJson('wxa/icp/cancel_icp_filing', $data);
  383. //返回微信信息
  384. if ($result['errcode'] !== 0) {
  385. throw new \Exception($result['errmsg'], $result['errcode']);
  386. }
  387. return [
  388. 'code' => 0,
  389. 'msg' => '提交成功,请注意短信通知,如果想终止注销流程,忽略该条短信即可'
  390. ];
  391. } catch (\Exception $e) {
  392. return [
  393. 'code' => $e->getCode() ?: 1,
  394. 'msg' => $e->getMessage()
  395. ];
  396. }
  397. }
  398. /**
  399. * 发起小程序管理员人脸核身
  400. */
  401. public function createIcpVerifyTask() {
  402. try {
  403. //请求微信接口
  404. $app = $this->app;
  405. if (!$app) {
  406. throw new \Exception('获取小程序信息失败');
  407. }
  408. $store_id = $this->store_id;
  409. $mini_id = $this->mini_id;
  410. $store_mini_icp = StoreMiniIcp::findOne(['mini_id' => $mini_id, 'is_delete' => 0]);
  411. // $end_time = intval($store_mini_icp->created_at) + (60 * 60 * 24);
  412. // if (($store_mini_icp && ($end_time < time())) || !$store_mini_icp) {
  413. $client = new BaseClient($app);
  414. $result = $client->httpPostJson('wxa/icp/create_icp_verifytask');
  415. //返回微信信息
  416. if ($result['errcode'] !== 0) {
  417. throw new \Exception($result['errmsg'], $result['errcode']);
  418. }
  419. // StoreMiniIcp::updateAll(['is_delete' => 1], ['mini_id' => $mini_id, 'is_delete' => 0]);
  420. // TODO 首次猜测可能每个小程序备案一次
  421. //保存人脸核验任务id task_id
  422. if (!$store_mini_icp) {
  423. $store_mini_icp = new StoreMiniIcp();
  424. }
  425. $store_mini_icp->task_id = $result['task_id'];
  426. // } else {
  427. // if (in_array($store_mini_icp->face_status, [0, 1]) && $end_time > time()) {
  428. // throw new \Exception('请在公众平台安全助手中点击小程序备案人脸核验提醒消息继续完成核验');
  429. // }
  430. // }
  431. $store_mini_icp->store_id = $store_id;
  432. $store_mini_icp->mini_id = $mini_id;
  433. $store_mini_icp->created_at = time();
  434. if (!$store_mini_icp->save()) {
  435. throw new \Exception(json_encode($store_mini_icp->errors, JSON_UNESCAPED_UNICODE));
  436. }
  437. return [
  438. 'code' => 0,
  439. 'msg' => '已发送人脸核身',
  440. 'data' => [
  441. 'task_id' => $result['task_id']
  442. ]
  443. ];
  444. } catch (\Exception $e) {
  445. return [
  446. 'code' => $e->getCode() ?: 1,
  447. 'msg' => $e->getMessage()
  448. ];
  449. }
  450. }
  451. /**
  452. * 查询人脸核身任务状态
  453. */
  454. public function queryIcpVerifyTask() {
  455. try {
  456. //请求微信接口
  457. $app = $this->app;
  458. if (!$app) {
  459. throw new \Exception('获取小程序信息失败');
  460. }
  461. $mini_id = $this->mini_id;
  462. $params_ = $this->params;
  463. $store_mini_icp = StoreMiniIcp::find()->where(['task_id' => $params_['task_id'], 'is_delete' => 0])
  464. ->select('id, task_id')->asArray()->one();
  465. if (!$store_mini_icp) {
  466. $store_mini_icp = StoreMiniIcp::find()->where(['mini_id' => $mini_id, 'is_delete' => 0])
  467. ->select('id, task_id')->asArray()->one();
  468. }
  469. if (empty($store_mini_icp)) {
  470. throw new \Exception('查询任务id失败');
  471. }
  472. $data = [
  473. 'task_id' => $store_mini_icp['task_id']
  474. ];
  475. $client = new BaseClient($app);
  476. $result = $client->httpPostJson('wxa/icp/query_icp_verifytask', $data);
  477. //返回微信信息
  478. if ($result['errcode'] !== 0) {
  479. throw new \Exception($result['errmsg'], $result['errcode']);
  480. }
  481. $store_mini_icp = StoreMiniIcp::findOne($store_mini_icp['id']);
  482. if ($result['face_status'] !== intval($store_mini_icp->face_status)) {
  483. $store_mini_icp->face_time = time();
  484. }
  485. $end_time = intval($store_mini_icp->created_at) + (60 * 60 * 24 * 3);
  486. if ($end_time > time() && intval($result['face_status']) === 0) {
  487. $result['face_status'] = -1;
  488. }
  489. $store_mini_icp->is_finish = (int)$result['is_finish'];
  490. $store_mini_icp->face_status = (int)$result['face_status'];
  491. if (!$store_mini_icp->save()) {
  492. throw new \Exception(json_encode($store_mini_icp->errors, JSON_UNESCAPED_UNICODE));
  493. }
  494. return [
  495. 'code' => 0,
  496. 'msg' => '已更新人脸核身状态'
  497. ];
  498. } catch (\Exception $e) {
  499. return [
  500. 'code' => $e->getCode() ?: 1,
  501. 'msg' => $e->getMessage()
  502. ];
  503. }
  504. }
  505. /**
  506. * 获取单位性质
  507. */
  508. public function queryIcpSubjectTypes() {
  509. try {
  510. //请求微信接口
  511. $app = $this->app;
  512. if (!$app) {
  513. throw new \Exception('获取小程序信息失败');
  514. }
  515. $client = new BaseClient($app);
  516. $result = $client->httpGet('wxa/icp/query_icp_subject_types');
  517. //返回微信信息
  518. if ($result['errcode'] !== 0) {
  519. throw new \Exception($result['errmsg'], $result['errcode']);
  520. }
  521. $icp_certificate_type_result = $this->queryIcpCertificateTypes();
  522. if ($icp_certificate_type_result['code'] === 0) {
  523. $icp_certificate_type = $icp_certificate_type_result['data']['data'];
  524. foreach ($result['items'] as &$item) {
  525. foreach ($icp_certificate_type as $type) {
  526. if ($type['subject_type'] === $item['type']) {
  527. $item['icp_type'][] = $type;
  528. }
  529. }
  530. }
  531. }
  532. return [
  533. 'code' => 0,
  534. 'msg' => '获取成功',
  535. 'data' => [
  536. 'data' => $result['items']
  537. ]
  538. ];
  539. } catch (\Exception $e) {
  540. return [
  541. 'code' => $e->getCode() ?: 1,
  542. 'msg' => $e->getMessage()
  543. ];
  544. }
  545. }
  546. /**
  547. * 获取区域信息
  548. */
  549. public function queryIcpDistrictCode() {
  550. try {
  551. //请求微信接口
  552. $app = $this->app;
  553. if (!$app) {
  554. throw new \Exception('获取小程序信息失败');
  555. }
  556. $client = new BaseClient($app);
  557. $result = $client->httpGet('wxa/icp/query_icp_district_code');
  558. //返回微信信息
  559. if ($result['errcode'] !== 0) {
  560. throw new \Exception($result['errmsg'], $result['errcode']);
  561. }
  562. return [
  563. 'code' => 0,
  564. 'msg' => '获取成功',
  565. 'data' => [
  566. 'data' => $result['items']
  567. ]
  568. ];
  569. } catch (\Exception $e) {
  570. return [
  571. 'code' => $e->getCode() ?: 1,
  572. 'msg' => $e->getMessage()
  573. ];
  574. }
  575. }
  576. /**
  577. * 获取证件类型
  578. */
  579. public function queryIcpCertificateTypes() {
  580. try {
  581. //请求微信接口
  582. $app = $this->app;
  583. if (!$app) {
  584. throw new \Exception('获取小程序信息失败');
  585. }
  586. $client = new BaseClient($app);
  587. $result = $client->httpGet('wxa/icp/query_icp_certificate_types');
  588. //返回微信信息
  589. if ($result['errcode'] !== 0) {
  590. throw new \Exception($result['errmsg'], $result['errcode']);
  591. }
  592. return [
  593. 'code' => 0,
  594. 'msg' => '获取成功',
  595. 'data' => [
  596. 'data' => $result['items']
  597. ]
  598. ];
  599. } catch (\Exception $e) {
  600. return [
  601. 'code' => $e->getCode() ?: 1,
  602. 'msg' => $e->getMessage()
  603. ];
  604. }
  605. }
  606. /**
  607. * 获取前置审批项类型
  608. */
  609. public function queryIcpNrlxTypes() {
  610. try {
  611. //请求微信接口
  612. $app = $this->app;
  613. if (!$app) {
  614. throw new \Exception('获取小程序信息失败');
  615. }
  616. $client = new BaseClient($app);
  617. $result = $client->httpGet('wxa/icp/query_icp_nrlx_types');
  618. //返回微信信息
  619. if ($result['errcode'] !== 0) {
  620. throw new \Exception($result['errmsg'], $result['errcode']);
  621. }
  622. return [
  623. 'code' => 0,
  624. 'msg' => '获取成功',
  625. 'data' => [
  626. 'data' => $result['items']
  627. ]
  628. ];
  629. } catch (\Exception $e) {
  630. return [
  631. 'code' => $e->getCode() ?: 1,
  632. 'msg' => $e->getMessage()
  633. ];
  634. }
  635. }
  636. /**
  637. * 获取小程序服务内容类型
  638. */
  639. public function queryIcpServiceContentTypes() {
  640. try {
  641. //请求微信接口
  642. $app = $this->app;
  643. if (!$app) {
  644. throw new \Exception('获取小程序信息失败');
  645. }
  646. $client = new BaseClient($app);
  647. $result = $client->httpGet('wxa/icp/query_icp_service_content_types');
  648. //返回微信信息
  649. if ($result['errcode'] !== 0) {
  650. throw new \Exception($result['errmsg'], $result['errcode']);
  651. }
  652. $result['items'] = $this->getChild($result['items']);
  653. return [
  654. 'code' => 0,
  655. 'msg' => '获取成功',
  656. 'data' => [
  657. 'data' => $result['items']
  658. ]
  659. ];
  660. } catch (\Exception $e) {
  661. return [
  662. 'code' => $e->getCode() ?: 1,
  663. 'msg' => $e->getMessage()
  664. ];
  665. }
  666. }
  667. public function getChild($content, $type = 0){
  668. $arr = [];
  669. foreach ($content as $k => $v) {
  670. if($v['parent_type'] == $type){
  671. $v['child'] = $this->getChild($content, $v['type']);
  672. $arr[] = $v;
  673. }
  674. }
  675. return $arr;
  676. }
  677. /**
  678. * 上传media图片
  679. */
  680. public function uploadIcpMedia() {
  681. try {
  682. //请求微信接口
  683. $app = $this->app;
  684. if (!$app) {
  685. throw new \Exception('获取小程序信息失败');
  686. }
  687. $params_ = $this->params;
  688. $client = new BaseClient($app);
  689. $params = [
  690. 'media' => $params_['media']
  691. ];
  692. $image_path = $this->saveTempImage($params['media']);
  693. $imageUrl = Image::ImageSize($image_path, 2);
  694. $result = $client->httpUpload('wxa/icp/upload_icp_media', [
  695. 'media' => $imageUrl
  696. ], [
  697. 'type' => $params_['type'],
  698. 'certificate_type' => $params_['certificate_type'],
  699. 'icp_order_field' => $params_['icp_order_field'],
  700. ]);
  701. //返回微信信息
  702. if ($result['errcode'] !== 0) {
  703. throw new \Exception($result['errmsg'], $result['errcode']);
  704. }
  705. $store_media = new StoreMedia();
  706. $store_media->store_id = $this->store_id;
  707. $store_media->mini_id = $this->mini_id;
  708. $store_media->media_id = $result['media_id'];
  709. $store_media->event = $params_['icp_order_field'];
  710. $store_media->local_url = $params['media'];
  711. $store_media->save();
  712. return [
  713. 'code' => 0,
  714. 'msg' => '获取成功',
  715. 'data' => [
  716. 'media_id' => $result['media_id']
  717. ]
  718. ];
  719. } catch (\Exception $e) {
  720. return [
  721. 'code' => $e->getCode() ?: 1,
  722. 'msg' => $e->getMessage()
  723. ];
  724. }
  725. }
  726. //获取网络图片到临时目录
  727. private function saveTempImage($url)
  728. {
  729. if (strpos($url, 'http') === false) {
  730. $url = 'http:' . trim($url);
  731. }
  732. if (!is_dir(\Yii::$app->runtimePath . '/image')) {
  733. mkdir(\Yii::$app->runtimePath . '/image');
  734. }
  735. $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg';
  736. CurlHelper::download($url, $save_path);
  737. return $save_path;
  738. }
  739. }