RegisterMiniForm.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1\third\wechat;
  8. use app\models\MerchantInfo;
  9. use app\models\Option;
  10. use app\models\Store;
  11. use app\models\StoreMini;
  12. use app\models\StoreSchedule;
  13. use app\utils\WechatMerchant\Merchant;
  14. use yii\base\Model;
  15. class RegisterMiniForm extends Model
  16. {
  17. //商城ID
  18. public $store_id;
  19. //小程序ID
  20. public $mini_id;
  21. //快速注册信息
  22. public $merchant_info;
  23. //注册时的资料信息
  24. public $license_info;
  25. //easyWechat配置
  26. public $openPlatform;
  27. //设置规则
  28. public function rules()
  29. {
  30. return [
  31. [['store_id', 'mini_id'], 'integer'],
  32. [['merchant_info'], 'array']
  33. ];
  34. }
  35. public function inits()
  36. {
  37. try {
  38. $store_id = $this->store_id;
  39. //获取easyWechat配置
  40. $form = new BaseForm();
  41. $this->openPlatform = $form->openPlatform;
  42. //拿取注册时的资料信息
  43. if (!empty($store_id)) {
  44. // $schedule = StoreSchedule::find()->where(['store_id' => $store_id])->asArray()->one();
  45. // $merchant_info = json_decode($schedule['merchant_info'], true);
  46. // $subject_info = $merchant_info['subject_info'];
  47. $this->license_info = [];
  48. }
  49. } catch (\Exception $e) {
  50. return [
  51. 'code' => 1,
  52. 'msg' => $e->getMessage()
  53. ];
  54. }
  55. }
  56. //快速注册小程序
  57. public function fastRegisterMini() {
  58. try {
  59. $result = $this->inits();
  60. if ($result['code'] === 1) {
  61. return $result;
  62. }
  63. //easyWechat配置
  64. $openPlatform = $this->openPlatform;
  65. //店铺ID
  66. $store_id = $this->store_id;
  67. //快速注册时信息
  68. $merchant_info = $this->merchant_info;
  69. //获取三方平台手机号
  70. $phone = Option::get("platform_phone", 0, 'saas')['value'];
  71. //企业名称
  72. $merchant_name = $merchant_info['name'];
  73. //企业代码
  74. $license_number = $merchant_info['code'];
  75. //企业类型 1:统一社会信用代码(18 位) 2:组织机构代码(9 位 xxxxxxxx-x) 3:营业执照注册号(15 位)
  76. $code_type = $merchant_info['code_type'] * 1 ?: 1;
  77. //法人微信号
  78. $legal_persona_wechat = $merchant_info['legal_persona_wechat'];
  79. //法人姓名
  80. $legal_person = $merchant_info['legal_persona_name'];
  81. //营业执照
  82. $license_pic = $merchant_info['license_pic'];
  83. //快速注册资料信息
  84. $data = [
  85. 'name' => $merchant_name,//企业名称
  86. 'code' => $license_number,//企业代码
  87. 'code_type' => $code_type,//企业类型
  88. 'legal_persona_wechat' => $legal_persona_wechat, //法人微信号
  89. 'legal_persona_name' => $legal_person,//法人姓名
  90. 'component_phone' => $phone
  91. ];
  92. $store_mini = StoreMini::findOne(['store_id' => $store_id]) ?: new StoreMini();
  93. //商城ID
  94. $store_mini->store_id = $store_id;
  95. //企业名称
  96. $store_mini->name = $merchant_name;
  97. //企业代码
  98. $store_mini->code = $license_number;
  99. //企业代码类型
  100. $store_mini->code_type = $code_type * 1;
  101. //企业法人微信
  102. $store_mini->legal_persona_wechat = $legal_persona_wechat;
  103. //企业法人姓名
  104. $store_mini->legal_persona_name = $legal_person;
  105. //营业执照
  106. $store_mini->license_pic = $license_pic;
  107. //调用快速注册接口
  108. $res = $openPlatform->component->registerMiniProgram($data);
  109. if ($res['errcode'] == 0) {
  110. //状态改为 申请中-1
  111. $store_mini->status = -1;
  112. $store_mini->save();
  113. return [
  114. 'code' => 0,
  115. 'msg' => "请注意查收人脸识别验证并等待审核",
  116. 'mini_id' => $store_mini->id
  117. ];
  118. } elseif (empty($res)) {
  119. $msg = "数据错误";
  120. throw new \Exception($msg);
  121. } else {
  122. $msg = (new BaseForm())->getZnMsg($res);
  123. throw new \Exception($msg);
  124. }
  125. } catch (\Exception $e) {
  126. return [
  127. 'code' => 1,
  128. 'msg' => $e->getMessage() . $e->getFile() . $e->getLine()
  129. ];
  130. }
  131. }
  132. //查询.快速注册状态
  133. public function getFastRegisterStatus ()
  134. {
  135. try {
  136. $result = $this->inits();
  137. if ($result['code'] === 1) {
  138. return $result;
  139. }
  140. //easyWechat配置
  141. $openPlatform = $this->openPlatform;
  142. //店铺ID
  143. $store_id = $this->store_id;
  144. //小程序ID
  145. $mini_id = $this->mini_id;
  146. //获取小程序信息
  147. $store_mini = StoreMini::find()->where(['or', ['id' => $mini_id], ['store_id' => $store_id]])->orderBy('id desc')->one();
  148. //判断所需参数是否为空
  149. if (empty($store_mini->name) || empty($store_mini->legal_persona_wechat) || empty($store_mini->legal_persona_name)) {
  150. return [
  151. 'code' => 1,
  152. 'msg' => "缺少参数"
  153. ];
  154. }
  155. //请求微信接口
  156. $res = $openPlatform->component->getRegistrationStatus($store_mini->name, $store_mini->legal_persona_wechat, $store_mini->legal_persona_name);
  157. if ($res['errcode'] != 0) {
  158. //获取中文注释
  159. $msg = (new BaseForm())->getZnMsg($res);
  160. throw new \Exception($msg);
  161. }
  162. return [
  163. 'code' => 0,
  164. 'msg' => "审核通过"
  165. ];
  166. } catch (\Exception $e) {
  167. return [
  168. 'code' => 1,
  169. 'msg' => $e->getMessage()
  170. ];
  171. }
  172. }
  173. //使用授权码获取授权信息 拿取appid token refresh_token
  174. public function getToken()
  175. {
  176. try {
  177. $result = $this->inits();
  178. if ($result['code'] === 1) {
  179. return $result;
  180. }
  181. //easyWechat配置
  182. $openPlatform = $this->openPlatform;
  183. //店铺ID
  184. $store_id = $this->store_id;
  185. //小程序ID
  186. $mini_id = $this->mini_id;
  187. //获取小程序信息
  188. $store_mini = StoreMini::find()->where(['or', ['id' => $mini_id], ['store_id' => $store_id]])->orderBy('id desc')->one();
  189. if (empty($store_mini->authorization_code)) {
  190. throw new \Exception("获取不到code参数");
  191. }
  192. //从数据库中拿取code的json数据
  193. $authorization_code = $store_mini->authorization_code;
  194. $authorization_code = json_decode($authorization_code, true);
  195. $code = $authorization_code['authorization_code'];
  196. $res = $openPlatform->handleAuthorize($code);
  197. if ($res['errcode'] !== 0) {
  198. $store_mini->appid = $res['authorization_info']['authorizer_appid'];
  199. $store_mini->authorizer_access_token = json_encode([
  200. 'authorizer_access_token' => $res['authorization_info']['authorizer_access_token'],
  201. 'end_time' => $res['authorization_info']['expires_in'] * 1 + time()
  202. ]);
  203. $store_mini->authorizer_refresh_token = $res['authorization_info']['authorizer_refresh_token'];
  204. if (!$store_mini->save()) {
  205. throw new \Exception(json_encode($store_mini->errors));
  206. }
  207. return [
  208. 'code' => 0,
  209. 'msg' => "保存成功"
  210. ];
  211. } else {
  212. $msg = (new BaseForm())->getZnMsg($res);
  213. throw new \Exception($msg);
  214. }
  215. } catch (\Exception $e) {
  216. return [
  217. 'code' => 1,
  218. 'msg' => $e->getMessage()
  219. ];
  220. }
  221. }
  222. //微信支付进件
  223. public function mchRegister()
  224. {
  225. $result = $this->inits();
  226. if ($result['code'] === 1) {
  227. return $result;
  228. }
  229. //小程序ID
  230. $mini_id = $this->mini_id;
  231. //店铺ID
  232. $store_id = $this->store_id;
  233. $store = Store::findOne($store_id);
  234. $merchant = MerchantInfo::find()->where(['bind_store_id' => $store_id, 'is_delete' => 0])->one();
  235. if ((int)$store->business_model !== 1) {
  236. $store_id = "";
  237. }
  238. $merchant_info = $this->merchant_info;
  239. $merchant->contact_info = json_encode($merchant_info['contact_info']);
  240. $merchant->subject_info = json_encode($merchant_info['subject_info']);
  241. $merchant->business_info = json_encode($merchant_info['business_info']);
  242. $merchant->bank_account_info = json_encode($merchant_info['bank_account_info']);
  243. if (!$merchant->save()) {
  244. return [
  245. 'code' => 1,
  246. 'msg' => $merchant->errors
  247. ];
  248. };
  249. //获取小程序信息
  250. $store_mini = StoreMini::find()->where(['or', ['id' => $mini_id], ['store_id' => $store_id]])->orderBy('id desc')->one();
  251. if (!empty($store_mini->appid)) {
  252. $appid = $store_mini->appid;
  253. } else {
  254. $appid = "";
  255. }
  256. $Merchant = new Merchant();
  257. return $Merchant->submit($merchant_info['contact_info'], $merchant_info['subject_info'], $merchant_info['business_info'], $merchant_info['bank_account_info'], !empty($merchant->id) ? $merchant->id : 0, $appid, $store_id);
  258. }
  259. }