| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1\third\wechat;
- use app\models\MerchantInfo;
- use app\models\Option;
- use app\models\Store;
- use app\models\StoreMini;
- use app\models\StoreSchedule;
- use app\utils\WechatMerchant\Merchant;
- use yii\base\Model;
- class RegisterMiniForm extends Model
- {
- //商城ID
- public $store_id;
- //小程序ID
- public $mini_id;
- //快速注册信息
- public $merchant_info;
- //注册时的资料信息
- public $license_info;
- //easyWechat配置
- public $openPlatform;
- //设置规则
- public function rules()
- {
- return [
- [['store_id', 'mini_id'], 'integer'],
- [['merchant_info'], 'array']
- ];
- }
- public function inits()
- {
- try {
- $store_id = $this->store_id;
- //获取easyWechat配置
- $form = new BaseForm();
- $this->openPlatform = $form->openPlatform;
- //拿取注册时的资料信息
- if (!empty($store_id)) {
- // $schedule = StoreSchedule::find()->where(['store_id' => $store_id])->asArray()->one();
- // $merchant_info = json_decode($schedule['merchant_info'], true);
- // $subject_info = $merchant_info['subject_info'];
- $this->license_info = [];
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //快速注册小程序
- public function fastRegisterMini() {
- try {
- $result = $this->inits();
- if ($result['code'] === 1) {
- return $result;
- }
- //easyWechat配置
- $openPlatform = $this->openPlatform;
- //店铺ID
- $store_id = $this->store_id;
- //快速注册时信息
- $merchant_info = $this->merchant_info;
- //获取三方平台手机号
- $phone = Option::get("platform_phone", 0, 'saas')['value'];
- //企业名称
- $merchant_name = $merchant_info['name'];
- //企业代码
- $license_number = $merchant_info['code'];
- //企业类型 1:统一社会信用代码(18 位) 2:组织机构代码(9 位 xxxxxxxx-x) 3:营业执照注册号(15 位)
- $code_type = $merchant_info['code_type'] * 1 ?: 1;
- //法人微信号
- $legal_persona_wechat = $merchant_info['legal_persona_wechat'];
- //法人姓名
- $legal_person = $merchant_info['legal_persona_name'];
- //营业执照
- $license_pic = $merchant_info['license_pic'];
- //快速注册资料信息
- $data = [
- 'name' => $merchant_name,//企业名称
- 'code' => $license_number,//企业代码
- 'code_type' => $code_type,//企业类型
- 'legal_persona_wechat' => $legal_persona_wechat, //法人微信号
- 'legal_persona_name' => $legal_person,//法人姓名
- 'component_phone' => $phone
- ];
- $store_mini = StoreMini::findOne(['store_id' => $store_id]) ?: new StoreMini();
- //商城ID
- $store_mini->store_id = $store_id;
- //企业名称
- $store_mini->name = $merchant_name;
- //企业代码
- $store_mini->code = $license_number;
- //企业代码类型
- $store_mini->code_type = $code_type * 1;
- //企业法人微信
- $store_mini->legal_persona_wechat = $legal_persona_wechat;
- //企业法人姓名
- $store_mini->legal_persona_name = $legal_person;
- //营业执照
- $store_mini->license_pic = $license_pic;
- //调用快速注册接口
- $res = $openPlatform->component->registerMiniProgram($data);
- if ($res['errcode'] == 0) {
- //状态改为 申请中-1
- $store_mini->status = -1;
- $store_mini->save();
- return [
- 'code' => 0,
- 'msg' => "请注意查收人脸识别验证并等待审核",
- 'mini_id' => $store_mini->id
- ];
- } elseif (empty($res)) {
- $msg = "数据错误";
- throw new \Exception($msg);
- } else {
- $msg = (new BaseForm())->getZnMsg($res);
- throw new \Exception($msg);
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage() . $e->getFile() . $e->getLine()
- ];
- }
- }
- //查询.快速注册状态
- public function getFastRegisterStatus ()
- {
- try {
- $result = $this->inits();
- if ($result['code'] === 1) {
- return $result;
- }
- //easyWechat配置
- $openPlatform = $this->openPlatform;
- //店铺ID
- $store_id = $this->store_id;
- //小程序ID
- $mini_id = $this->mini_id;
- //获取小程序信息
- $store_mini = StoreMini::find()->where(['or', ['id' => $mini_id], ['store_id' => $store_id]])->orderBy('id desc')->one();
- //判断所需参数是否为空
- if (empty($store_mini->name) || empty($store_mini->legal_persona_wechat) || empty($store_mini->legal_persona_name)) {
- return [
- 'code' => 1,
- 'msg' => "缺少参数"
- ];
- }
- //请求微信接口
- $res = $openPlatform->component->getRegistrationStatus($store_mini->name, $store_mini->legal_persona_wechat, $store_mini->legal_persona_name);
- if ($res['errcode'] != 0) {
- //获取中文注释
- $msg = (new BaseForm())->getZnMsg($res);
- throw new \Exception($msg);
- }
- return [
- 'code' => 0,
- 'msg' => "审核通过"
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //使用授权码获取授权信息 拿取appid token refresh_token
- public function getToken()
- {
- try {
- $result = $this->inits();
- if ($result['code'] === 1) {
- return $result;
- }
- //easyWechat配置
- $openPlatform = $this->openPlatform;
- //店铺ID
- $store_id = $this->store_id;
- //小程序ID
- $mini_id = $this->mini_id;
- //获取小程序信息
- $store_mini = StoreMini::find()->where(['or', ['id' => $mini_id], ['store_id' => $store_id]])->orderBy('id desc')->one();
- if (empty($store_mini->authorization_code)) {
- throw new \Exception("获取不到code参数");
- }
- //从数据库中拿取code的json数据
- $authorization_code = $store_mini->authorization_code;
- $authorization_code = json_decode($authorization_code, true);
- $code = $authorization_code['authorization_code'];
- $res = $openPlatform->handleAuthorize($code);
- if ($res['errcode'] !== 0) {
- $store_mini->appid = $res['authorization_info']['authorizer_appid'];
- $store_mini->authorizer_access_token = json_encode([
- 'authorizer_access_token' => $res['authorization_info']['authorizer_access_token'],
- 'end_time' => $res['authorization_info']['expires_in'] * 1 + time()
- ]);
- $store_mini->authorizer_refresh_token = $res['authorization_info']['authorizer_refresh_token'];
- if (!$store_mini->save()) {
- throw new \Exception(json_encode($store_mini->errors));
- }
- return [
- 'code' => 0,
- 'msg' => "保存成功"
- ];
- } else {
- $msg = (new BaseForm())->getZnMsg($res);
- throw new \Exception($msg);
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //微信支付进件
- public function mchRegister()
- {
- $result = $this->inits();
- if ($result['code'] === 1) {
- return $result;
- }
- //小程序ID
- $mini_id = $this->mini_id;
- //店铺ID
- $store_id = $this->store_id;
- $store = Store::findOne($store_id);
- $merchant = MerchantInfo::find()->where(['bind_store_id' => $store_id, 'is_delete' => 0])->one();
- if ((int)$store->business_model !== 1) {
- $store_id = "";
- }
- $merchant_info = $this->merchant_info;
- $merchant->contact_info = json_encode($merchant_info['contact_info']);
- $merchant->subject_info = json_encode($merchant_info['subject_info']);
- $merchant->business_info = json_encode($merchant_info['business_info']);
- $merchant->bank_account_info = json_encode($merchant_info['bank_account_info']);
- if (!$merchant->save()) {
- return [
- 'code' => 1,
- 'msg' => $merchant->errors
- ];
- };
- //获取小程序信息
- $store_mini = StoreMini::find()->where(['or', ['id' => $mini_id], ['store_id' => $store_id]])->orderBy('id desc')->one();
- if (!empty($store_mini->appid)) {
- $appid = $store_mini->appid;
- } else {
- $appid = "";
- }
- $Merchant = new Merchant();
- 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);
- }
- }
|