| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\District;
- use app\models\MerchantInfo;
- use app\models\Option;
- use app\models\StoreMini;
- use app\models\StoreSchedule;
- use app\models\WechatConfig;
- use app\utils\Wechat\WechatMini;
- use app\utils\WechatMerchant\Merchant;
- use yii\base\Model;
- class WechatMiniMerchantForm extends Model
- {
- public $merchant_info;
- public $status;
- public $id;
- public $is_use;
- public $appid;
- public $mch_id;
- public $subject_type;
- public $merchant_name;
- public $store_id;
- public function rules()
- {
- return [
- [['merchant_info'], 'safe'],
- [['status', 'id','is_use', 'store_id'], 'integer'],
- [['appid', 'mch_id', 'subject_type', 'merchant_name'], 'string']
- ];
- }
- //获取进件参数
- public function merchantInfo(){
- $store_id = get_store_id();
- $id = $this->id;
- try {
- $MerchantInfo = MerchantInfo::find()->where(['bind_store_id' => $store_id, 'id' => $id, 'is_delete' => 0])->asArray()->one();
- // $StoreSchedule = StoreSchedule::find()->where(['store_id' => $store_id])->one();
- $merchant_info = [];
- // if (!empty($StoreSchedule->merchant_info)) {
- // $merchant_info = json_decode($StoreSchedule->merchant_info,true);
- // // $business_license_info = $merchant_info['subject_info']['business_license_info'];
- // }
- if (!empty($MerchantInfo)) {
- $merchant_info = $MerchantInfo;
- $merchant_info['subject_info'] = json_decode($merchant_info['subject_info'],true) ?: [
- 'subject_type' => 'SUBJECT_TYPE_ENTERPRISE',
- 'business_license_info' => [
- 'license_copy' => '',
- 'license_number' => '',
- 'merchant_name' => '',
- 'legal_person' => ''
- ],
- 'certificate_info' => [
- 'is_long' => '',
- 'cert_copy' => '',
- 'cert_type' => '',
- 'cert_number' => '',
- 'merchant_name' => '',
- 'company_address' => '',
- 'legal_person' => '',
- 'period_begin' => '',
- 'period_end' => '',
- ],
- 'organization_info' => [
- 'is_composite' => 1,
- 'organization_copy' => '',
- 'organization_code' => '',
- 'org_period_begin' => '',
- 'org_period_end' => '',
- 'is_long' => 0
- ],
- 'identity_info' => [
- 'is_long' => 0,
- 'id_doc_type' => 'IDENTIFICATION_TYPE_IDCARD',
- 'id_card_info' => [
- 'id_card_copy' => '',
- 'id_card_national' => '',
- 'id_card_name' => '',
- 'id_card_number' => '',
- 'card_period_begin' => '',
- 'card_period_end' => ''
- ],
- 'id_doc_info' => [
- 'id_doc_copy' => '',
- 'id_doc_name' => '',
- 'id_doc_number' => '',
- 'doc_period_begin' => '',
- 'doc_period_end' => ''
- ],
- 'owner' => true
- ]
- ];
- if (intval($merchant_info['subject_info']['business_license_info']['is_long']) === 1) {
- $merchant_info['subject_info']['business_license_info']['period_end'] = '2999-12-31';
- }
- // $business_license_info = $merchant_info['subject_info']['business_license_info'];
- $merchant_info['business_info'] = json_decode($merchant_info['business_info'],true) ?: [
- 'merchant_shortname' => '',
- 'service_phone' => '',
- 'sales_info' => [
- 'sales_scenes_type' => [
- ''
- ],
- 'mini_program_info' => [
- 'mini_program_appid' => ''
- ]
- ]
- ];
- $merchant_info['settlement_info'] = json_decode($merchant_info['settlement_info'],true) ?: [];
- $merchant_info['bank_account_info'] = json_decode($merchant_info['bank_account_info'],true) ?: [
- 'bank_account_type' => '',
- 'account_name' => '',
- 'account_bank' => '',
- 'bank_address_code' => '',
- 'account_number' => '',
- 'code_list' => [
- ],
- 'address_list' => [
- ]
- ];
- $merchant_info['contact_info'] = json_decode($merchant_info['contact_info'],true) ?: [
- 'contact_name' => '',
- 'contact_id_number' => '',
- 'mobile_phone' => '',
- 'contact_email' => ''
- ];
- }
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $merchant_info
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //保存进件参数
- public function save(){
- $store_id = get_store_id();
- //商户进件信息ID
- $id = $this->id;
- //post商户ID
- $merchant_info = $this->merchant_info;
- //判断是否是三方的小程序
- $wechat = WechatConfig::find()->where(['store_id'=>$store_id])->select('app_id')->one();
- if(!empty($wechat->app_id)){
- $appid = $wechat->app_id;
- $mini = StoreMini::find()->where(['store_id' => $store_id, 'appid' => $appid])->orderBy('id desc')->select('appid')->one();
- }
-
- //如果不是三方的小程序则使用系统默认的小程序
- if (empty($mini)) {
- $appid = "";
- }
- $self_mini = Option::get('self_mini', $store_id, 'store', 0)['value'];
- if (\Yii::$app->prod_is_dandianpu() && !$self_mini) {
- $appid = Option::get('one_store_wechat_appid', 0, 'saas', '')['value'];
- }
- $Merchant = new Merchant();
- 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']);
- }
- //获取进件列表
- public function list()
- {
- $store_id = get_store_id();
- $status = $this->status;
- \Yii::error($status,"这还是status");
- //保存商户信息
- $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');
- if ($status != -1) {
- $query->andWhere(['status' => $status]);
- }
- $query->andWhere(['is_yunst' => 0]);
- $query->orderBy(['id' => SORT_DESC]);
- \Yii::error($query->createCommand()->getRawSql());
- $pagination = pagination_make($query);
- $merchant_info = $pagination['list'];
- array_walk($merchant_info, function (&$item) {
- $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
- $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']);
- $subject_info = json_decode($item['subject_info'], true);
- $item['type'] = $subject_info['subject_type'];
- switch ($item['type']) {
- case "SUBJECT_TYPE_INDIVIDUAL":
- $item['type'] = "个体户";
- break;
- case "SUBJECT_TYPE_ENTERPRISE":
- $item['type'] = "企业";
- break;
- case "SUBJECT_TYPE_GOVERNMENT":
- $item['type'] = "政府机关";
- break;
- case "SUBJECT_TYPE_INSTITUTIONS":
- $item['type'] = "事业单位";
- break;
- case "SUBJECT_TYPE_OTHERS":
- $item['type'] = "社会组织";
- break;
- }
- if($item['sign_url']){
- $sign_url = json_decode($item['sign_url'], true);
- $item['sign_url'] = $sign_url['applyment_state_msg'] . '<br/> <img width="300" height="300" src="' .$sign_url['sign_url']. '"/>';
- }
- if (in_array($item['state'], [Merchant::APPLYMENT_STATE_TO_BE_SIGNED, Merchant::APPLYMENT_STATE_TO_BE_CONFIRMED]) && (int)$item['status'] === 1) {
- $item['status'] = '4';
- };
- // if ((int)$item['status'] == 2 && !empty($item['refuse_desc'])) {
- // $refuse_desc = json_decode($item['refuse_desc'], true);
- // $item['refuse_desc'] = '';
- // foreach ($refuse_desc as $val) {
- // $item['refuse_desc'] .= $val['field'] . ':' . $val['reject_reason'] . ';';
- // }
- // }
- $item['state_str'] = $item['state'] ? Merchant::$valid_state_desc[$item['state']] : '审核中';
- unset($item['subject_info']);
- });
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $merchant_info,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount']
- ];
- }
- //获取进件列表
- public function list2()
- {
- $store_id = get_store_id();
- $status = $this->status;
- \Yii::error($status,"这还是status");
- //保存商户信息
- $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');
- if ($status != -1) {
- $query->andWhere(['status' => $status]);
- }
- $query->andWhere(['is_yunst'=>1]);
- $query->orderBy(['id' => SORT_DESC]);
- \Yii::error($query->createCommand()->getRawSql());
- $pagination = pagination_make($query);
- $merchant_info = $pagination['list'];
- array_walk($merchant_info, function (&$item) {
- $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
- $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']);
- $subject_info = json_decode($item['subject_info'], true);
- $item['type'] = $subject_info['subject_type'];
- switch ($item['type']) {
- case "SUBJECT_TYPE_INDIVIDUAL":
- $item['type'] = "个体户";
- break;
- case "SUBJECT_TYPE_ENTERPRISE":
- $item['type'] = "企业";
- break;
- case "SUBJECT_TYPE_GOVERNMENT":
- $item['type'] = "政府机关";
- break;
- case "SUBJECT_TYPE_INSTITUTIONS":
- $item['type'] = "事业单位";
- break;
- case "SUBJECT_TYPE_OTHERS":
- $item['type'] = "社会组织";
- break;
- }
- if($item['sign_url']){
- $sign_url = json_decode($item['sign_url'], true);
- $item['sign_url'] = $sign_url['applyment_state_msg'] . '<br/> <img width="300" height="300" src="' .$sign_url['sign_url']. '"/>';
- }
- if ($item['state'] === 'APPLYMENT_STATE_TO_BE_SIGNED' && (int)$item['status'] === 1) {
- $item['status'] = '4';
- };
- // if ((int)$item['status'] == 2 && !empty($item['refuse_desc'])) {
- // $refuse_desc = json_decode($item['refuse_desc'], true);
- // $item['refuse_desc'] = '';
- // foreach ($refuse_desc as $val) {
- // $item['refuse_desc'] .= $val['field'] . ':' . $val['reject_reason'] . ';';
- // }
- // }
- if ($item['state'] == '00'){
- $item['state_str'] = '待审核';
- }elseif ($item['state'] == '01'){
- $item['state_str'] = '审核中';
- }elseif ($item['state'] == '02'){
- $item['state_str'] = '审核通过';
- }elseif ($item['state'] == '03'){
- $item['state_str'] = '审核失败';
- }elseif ($item['state'] == '04'){
- $item['state_str'] = '提交审核失败';
- }
- if ($item['status'] == 1){
- $item['state_str'] = '审核中';
- }
- // $item['state_str'] = $item['state'] ? Merchant::$valid_state_desc[$item['state']] : '审核中';
- unset($item['subject_info']);
- });
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $merchant_info,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount']
- ];
- }
- public function getCountry(){
- $District = District::find()->where(['level' => ['country', 'province', 'city', 'district']])->asArray()->all();
- $data = $this->getdata($District);
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $data
- ];
- }
- //递归
- public function getdata($data, $id = 0)
- {
- $arr = [];
- foreach ($data as $k => $v) {
- if ($v['parent_id'] == $id) {
- $children = $this->getdata($data, $v['id']);
- $v['children'] = $children ? $children : null;
- $arr[] = $v;
- }
- }
- return $arr;
- }
- /**
- * 暂停/开始使用
- */
- public function unbind()
- {
- try {
- $merchant = MerchantInfo::findOne(['id' => $this->id, 'is_delete' => 0]);
- if (empty($merchant)) {
- throw new \Exception("数据错误,获取小程序信息失败");
- }
- $store_id = get_store_id();
- if ($this->is_use == 1 || empty($this->is_use)) {
- MerchantInfo::updateAll(['is_use' => 2], ['bind_store_id' => $store_id, 'is_delete' => 0]);
- $merchant->is_use = 1;
- } elseif ($this->is_use == 2) {
- $merchant->is_use = 2;
- }
- $res = $merchant->save();
- if ($res) {
- //设置店铺的appid
- if($merchant->is_use == 1){
- $merchant_name = '';
- if(isset($merchant['subject_info']) && !empty($merchant['subject_info'])){
- $subject_info = json_decode($merchant['subject_info'],true);
- if(isset($subject_info['business_license_info']['merchant_name']) && !empty($subject_info['business_license_info']['merchant_name'])){
- $merchant_name = $subject_info['business_license_info']['merchant_name'];
- }
- }
- $wechat_type_arr = [
- WechatConfig::TYPE_CONFIG_MINI,
- WechatConfig::TYPE_CONFIG_MP,
- WechatConfig::TYPE_CONFIG_APP,
- WechatConfig::TYPE_CONFIG_H5
- ];
- foreach ($wechat_type_arr as $wechat_type_value) {
- $wechat = WechatConfig::findOne(['store_id' => $store_id, 'type' => $wechat_type_value, 'is_delete' => 0]);
- if (!$wechat) {
- $wechat = new WechatConfig();
- $wechat->store_id = $store_id;
- $wechat->type = $wechat_type_value;
- $wechat->app_id = 'app_id';
- $wechat->app_secret = 'app_secret';
- }
- $wechat->mch_id = $merchant->sub_mch_id;
- $wechat->name = $merchant_name;
- $wechat->updated_at = time();
- $wechat->save();
- }
- }
- return [
- 'code' => 0,
- 'msg' => "设置成功",
- 'data' => $res
- ];
- } else {
- throw new \Exception("数据库添加失败");
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function deleteMerchantInfo() {
- $id = $this->id;
- $merchant_info = MerchantInfo::findOne(['id' => $id, 'is_delete' => 0]);
- if (!$merchant_info) {
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- }
- $merchant_info->is_delete = 1;
- if (!$merchant_info->save()) {
- return [
- 'code' => 1,
- 'msg' => '操作失败:' . json_encode($merchant_info->errors, JSON_UNESCAPED_UNICODE)
- ];
- };
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- }
- //绑定商户号 非进件逻辑需要添加进件数据
- public function setTemporaryInfo() {
- $appid = $this->appid;
- $mch_id = $this->mch_id;
- $subject_type = $this->subject_type;
- $merchant_info = $this->merchant_info;
- $store_id = $this->store_id;
- if (\Yii::$app->request->isPost) {
- if (!in_array($subject_type, [
- "SUBJECT_TYPE_INDIVIDUAL",
- "SUBJECT_TYPE_ENTERPRISE",
- "SUBJECT_TYPE_GOVERNMENT",
- "SUBJECT_TYPE_INSTITUTIONS",
- "SUBJECT_TYPE_OTHERS"
- ])) {
- return [
- 'code' => 1,
- 'msg' => '企业类型所选值错误'
- ];
- }
- $merchant = new MerchantInfo();
- $merchant->store_id = $store_id;
- $merchant->saas_user_id = 0;
- $merchant->business_code = "";
- $merchant->contact_info = json_encode([
- 'contact_name' => '',
- 'contact_id_number' => '',
- 'mobile_phone' => '',
- 'contact_email' => ''
- ], JSON_UNESCAPED_UNICODE);
- $merchant->subject_info = json_encode([
- 'subject_type' => $subject_type,
- 'business_license_info' => [
- 'license_copy' => '',
- 'license_number' => '',
- 'merchant_name' => $merchant_info,
- 'legal_person' => ''
- ],
- 'certificate_info' => [
- 'is_long' => '',
- 'cert_copy' => '',
- 'cert_type' => '',
- 'cert_number' => '',
- 'merchant_name' => '',
- 'company_address' => '',
- 'legal_person' => '',
- 'period_begin' => '',
- 'period_end' => '',
- ],
- 'organization_info' => [
- 'is_composite' => 1,
- 'organization_copy' => '',
- 'organization_code' => '',
- 'org_period_begin' => '',
- 'org_period_end' => '',
- 'is_long' => 0
- ],
- 'identity_info' => [
- 'is_long' => 0,
- 'id_doc_type' => 'IDENTIFICATION_TYPE_IDCARD',
- 'id_card_info' => [
- 'id_card_copy' => '',
- 'id_card_national' => '',
- 'id_card_name' => '',
- 'id_card_number' => '',
- 'card_period_begin' => '',
- 'card_period_end' => ''
- ],
- 'id_doc_info' => [
- 'id_doc_copy' => '',
- 'id_doc_name' => '',
- 'id_doc_number' => '',
- 'doc_period_begin' => '',
- 'doc_period_end' => ''
- ],
- 'owner' => true
- ]
- ], JSON_UNESCAPED_UNICODE);
- $merchant->settlement_info = json_encode([]);;
- $merchant->business_info = json_encode([
- 'merchant_shortname' => '',
- 'service_phone' => '',
- 'sales_info' => [
- 'sales_scenes_type' => [
- ''
- ],
- 'mini_program_info' => [
- 'mini_program_appid' => $appid
- ]
- ]
- ], JSON_UNESCAPED_UNICODE);
- $merchant->bank_account_info = json_encode([]);
- $merchant->applyment_id = "";
- $merchant->status = 3;
- $merchant->state = "APPLYMENT_STATE_FINISHED";
- $merchant->bind_store_id = $store_id;
- $merchant->sub_mch_id = $mch_id;
- if (!$merchant->save()) {
- return [
- 'code' => 1,
- 'msg' => implode(';', array_values($merchant->firstErrors))
- ];
- }
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- } else {
- $app_config = [];
- $app = WechatMini::getWechatConfig($store_id);
- if (!empty($app)) {
- $app_config = $app->getConfig();
- }
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => [
- 'appid' => $app_config['app_id'] ?? ''
- ]
- ];
- }
- }
-
- }
|