WechatMiniMerchantForm.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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\District;
  9. use app\models\MerchantInfo;
  10. use app\models\Option;
  11. use app\models\StoreMini;
  12. use app\models\StoreSchedule;
  13. use app\models\WechatConfig;
  14. use app\utils\Wechat\WechatMini;
  15. use app\utils\WechatMerchant\Merchant;
  16. use yii\base\Model;
  17. class WechatMiniMerchantForm extends Model
  18. {
  19. public $merchant_info;
  20. public $status;
  21. public $id;
  22. public $is_use;
  23. public $appid;
  24. public $mch_id;
  25. public $subject_type;
  26. public $merchant_name;
  27. public $store_id;
  28. public function rules()
  29. {
  30. return [
  31. [['merchant_info'], 'safe'],
  32. [['status', 'id','is_use', 'store_id'], 'integer'],
  33. [['appid', 'mch_id', 'subject_type', 'merchant_name'], 'string']
  34. ];
  35. }
  36. //获取进件参数
  37. public function merchantInfo(){
  38. $store_id = get_store_id();
  39. $id = $this->id;
  40. try {
  41. $MerchantInfo = MerchantInfo::find()->where(['bind_store_id' => $store_id, 'id' => $id, 'is_delete' => 0])->asArray()->one();
  42. // $StoreSchedule = StoreSchedule::find()->where(['store_id' => $store_id])->one();
  43. $merchant_info = [];
  44. // if (!empty($StoreSchedule->merchant_info)) {
  45. // $merchant_info = json_decode($StoreSchedule->merchant_info,true);
  46. // // $business_license_info = $merchant_info['subject_info']['business_license_info'];
  47. // }
  48. if (!empty($MerchantInfo)) {
  49. $merchant_info = $MerchantInfo;
  50. $merchant_info['subject_info'] = json_decode($merchant_info['subject_info'],true) ?: [
  51. 'subject_type' => 'SUBJECT_TYPE_ENTERPRISE',
  52. 'business_license_info' => [
  53. 'license_copy' => '',
  54. 'license_number' => '',
  55. 'merchant_name' => '',
  56. 'legal_person' => ''
  57. ],
  58. 'certificate_info' => [
  59. 'is_long' => '',
  60. 'cert_copy' => '',
  61. 'cert_type' => '',
  62. 'cert_number' => '',
  63. 'merchant_name' => '',
  64. 'company_address' => '',
  65. 'legal_person' => '',
  66. 'period_begin' => '',
  67. 'period_end' => '',
  68. ],
  69. 'organization_info' => [
  70. 'is_composite' => 1,
  71. 'organization_copy' => '',
  72. 'organization_code' => '',
  73. 'org_period_begin' => '',
  74. 'org_period_end' => '',
  75. 'is_long' => 0
  76. ],
  77. 'identity_info' => [
  78. 'is_long' => 0,
  79. 'id_doc_type' => 'IDENTIFICATION_TYPE_IDCARD',
  80. 'id_card_info' => [
  81. 'id_card_copy' => '',
  82. 'id_card_national' => '',
  83. 'id_card_name' => '',
  84. 'id_card_number' => '',
  85. 'card_period_begin' => '',
  86. 'card_period_end' => ''
  87. ],
  88. 'id_doc_info' => [
  89. 'id_doc_copy' => '',
  90. 'id_doc_name' => '',
  91. 'id_doc_number' => '',
  92. 'doc_period_begin' => '',
  93. 'doc_period_end' => ''
  94. ],
  95. 'owner' => true
  96. ]
  97. ];
  98. if (intval($merchant_info['subject_info']['business_license_info']['is_long']) === 1) {
  99. $merchant_info['subject_info']['business_license_info']['period_end'] = '2999-12-31';
  100. }
  101. // $business_license_info = $merchant_info['subject_info']['business_license_info'];
  102. $merchant_info['business_info'] = json_decode($merchant_info['business_info'],true) ?: [
  103. 'merchant_shortname' => '',
  104. 'service_phone' => '',
  105. 'sales_info' => [
  106. 'sales_scenes_type' => [
  107. ''
  108. ],
  109. 'mini_program_info' => [
  110. 'mini_program_appid' => ''
  111. ]
  112. ]
  113. ];
  114. $merchant_info['settlement_info'] = json_decode($merchant_info['settlement_info'],true) ?: [];
  115. $merchant_info['bank_account_info'] = json_decode($merchant_info['bank_account_info'],true) ?: [
  116. 'bank_account_type' => '',
  117. 'account_name' => '',
  118. 'account_bank' => '',
  119. 'bank_address_code' => '',
  120. 'account_number' => '',
  121. 'code_list' => [
  122. ],
  123. 'address_list' => [
  124. ]
  125. ];
  126. $merchant_info['contact_info'] = json_decode($merchant_info['contact_info'],true) ?: [
  127. 'contact_name' => '',
  128. 'contact_id_number' => '',
  129. 'mobile_phone' => '',
  130. 'contact_email' => ''
  131. ];
  132. }
  133. return [
  134. 'code' => 0,
  135. 'msg' => "获取成功",
  136. 'data' => $merchant_info
  137. ];
  138. } catch (\Exception $e) {
  139. return [
  140. 'code' => 1,
  141. 'msg' => $e->getMessage()
  142. ];
  143. }
  144. }
  145. //保存进件参数
  146. public function save(){
  147. $store_id = get_store_id();
  148. //商户进件信息ID
  149. $id = $this->id;
  150. //post商户ID
  151. $merchant_info = $this->merchant_info;
  152. //判断是否是三方的小程序
  153. $wechat = WechatConfig::find()->where(['store_id'=>$store_id])->select('app_id')->one();
  154. if(!empty($wechat->app_id)){
  155. $appid = $wechat->app_id;
  156. $mini = StoreMini::find()->where(['store_id' => $store_id, 'appid' => $appid])->orderBy('id desc')->select('appid')->one();
  157. }
  158. //如果不是三方的小程序则使用系统默认的小程序
  159. if (empty($mini)) {
  160. $appid = "";
  161. }
  162. $self_mini = Option::get('self_mini', $store_id, 'store', 0)['value'];
  163. if (\Yii::$app->prod_is_dandianpu() && !$self_mini) {
  164. $appid = Option::get('one_store_wechat_appid', 0, 'saas', '')['value'];
  165. }
  166. $Merchant = new Merchant();
  167. return $Merchant->submit($merchant_info['contact_info'], $merchant_info['subject_info'], $merchant_info['business_info'], $merchant_info['bank_account_info'], $id, $appid, $store_id, $merchant_info['settlement_info']);
  168. }
  169. //获取进件列表
  170. public function list()
  171. {
  172. $store_id = get_store_id();
  173. $status = $this->status;
  174. \Yii::error($status,"这还是status");
  175. //保存商户信息
  176. $query = MerchantInfo::find()->where(['bind_store_id'=>$store_id, 'is_delete' => 0])->select('id, status, refuse_desc, sub_mch_id, business_code, created_at, updated_at, subject_info, sign_url, state, is_use');
  177. if ($status != -1) {
  178. $query->andWhere(['status' => $status]);
  179. }
  180. $query->andWhere(['is_yunst' => 0]);
  181. $query->orderBy(['id' => SORT_DESC]);
  182. \Yii::error($query->createCommand()->getRawSql());
  183. $pagination = pagination_make($query);
  184. $merchant_info = $pagination['list'];
  185. array_walk($merchant_info, function (&$item) {
  186. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  187. $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']);
  188. $subject_info = json_decode($item['subject_info'], true);
  189. $item['type'] = $subject_info['subject_type'];
  190. switch ($item['type']) {
  191. case "SUBJECT_TYPE_INDIVIDUAL":
  192. $item['type'] = "个体户";
  193. break;
  194. case "SUBJECT_TYPE_ENTERPRISE":
  195. $item['type'] = "企业";
  196. break;
  197. case "SUBJECT_TYPE_GOVERNMENT":
  198. $item['type'] = "政府机关";
  199. break;
  200. case "SUBJECT_TYPE_INSTITUTIONS":
  201. $item['type'] = "事业单位";
  202. break;
  203. case "SUBJECT_TYPE_OTHERS":
  204. $item['type'] = "社会组织";
  205. break;
  206. }
  207. if($item['sign_url']){
  208. $sign_url = json_decode($item['sign_url'], true);
  209. $item['sign_url'] = $sign_url['applyment_state_msg'] . '<br/> <img width="300" height="300" src="' .$sign_url['sign_url']. '"/>';
  210. }
  211. if (in_array($item['state'], [Merchant::APPLYMENT_STATE_TO_BE_SIGNED, Merchant::APPLYMENT_STATE_TO_BE_CONFIRMED]) && (int)$item['status'] === 1) {
  212. $item['status'] = '4';
  213. };
  214. // if ((int)$item['status'] == 2 && !empty($item['refuse_desc'])) {
  215. // $refuse_desc = json_decode($item['refuse_desc'], true);
  216. // $item['refuse_desc'] = '';
  217. // foreach ($refuse_desc as $val) {
  218. // $item['refuse_desc'] .= $val['field'] . ':' . $val['reject_reason'] . ';';
  219. // }
  220. // }
  221. $item['state_str'] = $item['state'] ? Merchant::$valid_state_desc[$item['state']] : '审核中';
  222. unset($item['subject_info']);
  223. });
  224. return [
  225. 'code' => 0,
  226. 'msg' => "获取成功",
  227. 'data' => $merchant_info,
  228. 'pageNo' => $pagination['pageNo'],
  229. 'totalCount' => $pagination['totalCount']
  230. ];
  231. }
  232. //获取进件列表
  233. public function list2()
  234. {
  235. $store_id = get_store_id();
  236. $status = $this->status;
  237. \Yii::error($status,"这还是status");
  238. //保存商户信息
  239. $query = MerchantInfo::find()->where(['bind_store_id'=>$store_id, 'is_delete' => 0])->select('id, status, refuse_desc, sub_mch_id, business_code, created_at, updated_at, subject_info, sign_url, state, is_use');
  240. if ($status != -1) {
  241. $query->andWhere(['status' => $status]);
  242. }
  243. $query->andWhere(['is_yunst'=>1]);
  244. $query->orderBy(['id' => SORT_DESC]);
  245. \Yii::error($query->createCommand()->getRawSql());
  246. $pagination = pagination_make($query);
  247. $merchant_info = $pagination['list'];
  248. array_walk($merchant_info, function (&$item) {
  249. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  250. $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']);
  251. $subject_info = json_decode($item['subject_info'], true);
  252. $item['type'] = $subject_info['subject_type'];
  253. switch ($item['type']) {
  254. case "SUBJECT_TYPE_INDIVIDUAL":
  255. $item['type'] = "个体户";
  256. break;
  257. case "SUBJECT_TYPE_ENTERPRISE":
  258. $item['type'] = "企业";
  259. break;
  260. case "SUBJECT_TYPE_GOVERNMENT":
  261. $item['type'] = "政府机关";
  262. break;
  263. case "SUBJECT_TYPE_INSTITUTIONS":
  264. $item['type'] = "事业单位";
  265. break;
  266. case "SUBJECT_TYPE_OTHERS":
  267. $item['type'] = "社会组织";
  268. break;
  269. }
  270. if($item['sign_url']){
  271. $sign_url = json_decode($item['sign_url'], true);
  272. $item['sign_url'] = $sign_url['applyment_state_msg'] . '<br/> <img width="300" height="300" src="' .$sign_url['sign_url']. '"/>';
  273. }
  274. if ($item['state'] === 'APPLYMENT_STATE_TO_BE_SIGNED' && (int)$item['status'] === 1) {
  275. $item['status'] = '4';
  276. };
  277. // if ((int)$item['status'] == 2 && !empty($item['refuse_desc'])) {
  278. // $refuse_desc = json_decode($item['refuse_desc'], true);
  279. // $item['refuse_desc'] = '';
  280. // foreach ($refuse_desc as $val) {
  281. // $item['refuse_desc'] .= $val['field'] . ':' . $val['reject_reason'] . ';';
  282. // }
  283. // }
  284. if ($item['state'] == '00'){
  285. $item['state_str'] = '待审核';
  286. }elseif ($item['state'] == '01'){
  287. $item['state_str'] = '审核中';
  288. }elseif ($item['state'] == '02'){
  289. $item['state_str'] = '审核通过';
  290. }elseif ($item['state'] == '03'){
  291. $item['state_str'] = '审核失败';
  292. }elseif ($item['state'] == '04'){
  293. $item['state_str'] = '提交审核失败';
  294. }
  295. if ($item['status'] == 1){
  296. $item['state_str'] = '审核中';
  297. }
  298. // $item['state_str'] = $item['state'] ? Merchant::$valid_state_desc[$item['state']] : '审核中';
  299. unset($item['subject_info']);
  300. });
  301. return [
  302. 'code' => 0,
  303. 'msg' => "获取成功",
  304. 'data' => $merchant_info,
  305. 'pageNo' => $pagination['pageNo'],
  306. 'totalCount' => $pagination['totalCount']
  307. ];
  308. }
  309. public function getCountry(){
  310. $District = District::find()->where(['level' => ['country', 'province', 'city', 'district']])->asArray()->all();
  311. $data = $this->getdata($District);
  312. return [
  313. 'code' => 0,
  314. 'msg' => "获取成功",
  315. 'data' => $data
  316. ];
  317. }
  318. //递归
  319. public function getdata($data, $id = 0)
  320. {
  321. $arr = [];
  322. foreach ($data as $k => $v) {
  323. if ($v['parent_id'] == $id) {
  324. $children = $this->getdata($data, $v['id']);
  325. $v['children'] = $children ? $children : null;
  326. $arr[] = $v;
  327. }
  328. }
  329. return $arr;
  330. }
  331. /**
  332. * 暂停/开始使用
  333. */
  334. public function unbind()
  335. {
  336. try {
  337. $merchant = MerchantInfo::findOne(['id' => $this->id, 'is_delete' => 0]);
  338. if (empty($merchant)) {
  339. throw new \Exception("数据错误,获取小程序信息失败");
  340. }
  341. $store_id = get_store_id();
  342. if ($this->is_use == 1 || empty($this->is_use)) {
  343. MerchantInfo::updateAll(['is_use' => 2], ['bind_store_id' => $store_id, 'is_delete' => 0]);
  344. $merchant->is_use = 1;
  345. } elseif ($this->is_use == 2) {
  346. $merchant->is_use = 2;
  347. }
  348. $res = $merchant->save();
  349. if ($res) {
  350. //设置店铺的appid
  351. if($merchant->is_use == 1){
  352. $merchant_name = '';
  353. if(isset($merchant['subject_info']) && !empty($merchant['subject_info'])){
  354. $subject_info = json_decode($merchant['subject_info'],true);
  355. if(isset($subject_info['business_license_info']['merchant_name']) && !empty($subject_info['business_license_info']['merchant_name'])){
  356. $merchant_name = $subject_info['business_license_info']['merchant_name'];
  357. }
  358. }
  359. $wechat_type_arr = [
  360. WechatConfig::TYPE_CONFIG_MINI,
  361. WechatConfig::TYPE_CONFIG_MP,
  362. WechatConfig::TYPE_CONFIG_APP,
  363. WechatConfig::TYPE_CONFIG_H5
  364. ];
  365. foreach ($wechat_type_arr as $wechat_type_value) {
  366. $wechat = WechatConfig::findOne(['store_id' => $store_id, 'type' => $wechat_type_value, 'is_delete' => 0]);
  367. if (!$wechat) {
  368. $wechat = new WechatConfig();
  369. $wechat->store_id = $store_id;
  370. $wechat->type = $wechat_type_value;
  371. $wechat->app_id = 'app_id';
  372. $wechat->app_secret = 'app_secret';
  373. }
  374. $wechat->mch_id = $merchant->sub_mch_id;
  375. $wechat->name = $merchant_name;
  376. $wechat->updated_at = time();
  377. $wechat->save();
  378. }
  379. }
  380. return [
  381. 'code' => 0,
  382. 'msg' => "设置成功",
  383. 'data' => $res
  384. ];
  385. } else {
  386. throw new \Exception("数据库添加失败");
  387. }
  388. } catch (\Exception $e) {
  389. return [
  390. 'code' => 1,
  391. 'msg' => $e->getMessage()
  392. ];
  393. }
  394. }
  395. public function deleteMerchantInfo() {
  396. $id = $this->id;
  397. $merchant_info = MerchantInfo::findOne(['id' => $id, 'is_delete' => 0]);
  398. if (!$merchant_info) {
  399. return [
  400. 'code' => 0,
  401. 'msg' => '操作成功'
  402. ];
  403. }
  404. $merchant_info->is_delete = 1;
  405. if (!$merchant_info->save()) {
  406. return [
  407. 'code' => 1,
  408. 'msg' => '操作失败:' . json_encode($merchant_info->errors, JSON_UNESCAPED_UNICODE)
  409. ];
  410. };
  411. return [
  412. 'code' => 0,
  413. 'msg' => '操作成功'
  414. ];
  415. }
  416. //绑定商户号 非进件逻辑需要添加进件数据
  417. public function setTemporaryInfo() {
  418. $appid = $this->appid;
  419. $mch_id = $this->mch_id;
  420. $subject_type = $this->subject_type;
  421. $merchant_info = $this->merchant_info;
  422. $store_id = $this->store_id;
  423. if (\Yii::$app->request->isPost) {
  424. if (!in_array($subject_type, [
  425. "SUBJECT_TYPE_INDIVIDUAL",
  426. "SUBJECT_TYPE_ENTERPRISE",
  427. "SUBJECT_TYPE_GOVERNMENT",
  428. "SUBJECT_TYPE_INSTITUTIONS",
  429. "SUBJECT_TYPE_OTHERS"
  430. ])) {
  431. return [
  432. 'code' => 1,
  433. 'msg' => '企业类型所选值错误'
  434. ];
  435. }
  436. $merchant = new MerchantInfo();
  437. $merchant->store_id = $store_id;
  438. $merchant->saas_user_id = 0;
  439. $merchant->business_code = "";
  440. $merchant->contact_info = json_encode([
  441. 'contact_name' => '',
  442. 'contact_id_number' => '',
  443. 'mobile_phone' => '',
  444. 'contact_email' => ''
  445. ], JSON_UNESCAPED_UNICODE);
  446. $merchant->subject_info = json_encode([
  447. 'subject_type' => $subject_type,
  448. 'business_license_info' => [
  449. 'license_copy' => '',
  450. 'license_number' => '',
  451. 'merchant_name' => $merchant_info,
  452. 'legal_person' => ''
  453. ],
  454. 'certificate_info' => [
  455. 'is_long' => '',
  456. 'cert_copy' => '',
  457. 'cert_type' => '',
  458. 'cert_number' => '',
  459. 'merchant_name' => '',
  460. 'company_address' => '',
  461. 'legal_person' => '',
  462. 'period_begin' => '',
  463. 'period_end' => '',
  464. ],
  465. 'organization_info' => [
  466. 'is_composite' => 1,
  467. 'organization_copy' => '',
  468. 'organization_code' => '',
  469. 'org_period_begin' => '',
  470. 'org_period_end' => '',
  471. 'is_long' => 0
  472. ],
  473. 'identity_info' => [
  474. 'is_long' => 0,
  475. 'id_doc_type' => 'IDENTIFICATION_TYPE_IDCARD',
  476. 'id_card_info' => [
  477. 'id_card_copy' => '',
  478. 'id_card_national' => '',
  479. 'id_card_name' => '',
  480. 'id_card_number' => '',
  481. 'card_period_begin' => '',
  482. 'card_period_end' => ''
  483. ],
  484. 'id_doc_info' => [
  485. 'id_doc_copy' => '',
  486. 'id_doc_name' => '',
  487. 'id_doc_number' => '',
  488. 'doc_period_begin' => '',
  489. 'doc_period_end' => ''
  490. ],
  491. 'owner' => true
  492. ]
  493. ], JSON_UNESCAPED_UNICODE);
  494. $merchant->settlement_info = json_encode([]);;
  495. $merchant->business_info = json_encode([
  496. 'merchant_shortname' => '',
  497. 'service_phone' => '',
  498. 'sales_info' => [
  499. 'sales_scenes_type' => [
  500. ''
  501. ],
  502. 'mini_program_info' => [
  503. 'mini_program_appid' => $appid
  504. ]
  505. ]
  506. ], JSON_UNESCAPED_UNICODE);
  507. $merchant->bank_account_info = json_encode([]);
  508. $merchant->applyment_id = "";
  509. $merchant->status = 3;
  510. $merchant->state = "APPLYMENT_STATE_FINISHED";
  511. $merchant->bind_store_id = $store_id;
  512. $merchant->sub_mch_id = $mch_id;
  513. if (!$merchant->save()) {
  514. return [
  515. 'code' => 1,
  516. 'msg' => implode(';', array_values($merchant->firstErrors))
  517. ];
  518. }
  519. return [
  520. 'code' => 0,
  521. 'msg' => '保存成功'
  522. ];
  523. } else {
  524. $app_config = [];
  525. $app = WechatMini::getWechatConfig($store_id);
  526. if (!empty($app)) {
  527. $app_config = $app->getConfig();
  528. }
  529. return [
  530. 'code' => 0,
  531. 'msg' => '获取成功',
  532. 'data' => [
  533. 'appid' => $app_config['app_id'] ?? ''
  534. ]
  535. ];
  536. }
  537. }
  538. }