store_id = $this->store_id ?: get_store_id(); $verify_ticket = Option::get("component_verify_ticket",0,'saas')['value']; if (empty($verify_ticket)) { $this->openPlatform = null; }else { $this->config = [ 'app_id' => Option::get("platform_third_appid", 0, 'saas')['value'], 'secret' => Option::get("platform_third_secret", 0, 'saas')['value'], 'token' => Option::get("platform_token", 0, 'saas')['value'], 'aes_key' => Option::get("platform_encodingAesKey", 0, 'saas')['value'] ]; $this->config['http'] = [ 'max_retries' => 0, 'retry_delay' => 500, 'timeout' => 50.0, 'read_timeout' => 50, // 'base_uri' => 'https://api.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri ]; $this->openPlatform = Factory::openPlatform($this->config); } } public function is_trade_managed($app) { if (!$app) { return; } $appid = $app->getConfig()['app_id']; $key = 'is_trade_managed' . (string)$appid; $cache = cache()->get($key); if($cache > 0){ return true; } if($cache < 0){ return false; } $postData = [ 'appid' => (string)$appid, ]; $client = new BaseClient($app); $result = $client->httpPostJson('wxa/sec/order/is_trade_managed', $postData); if (!empty($result)) { if ($result['errcode'] == '0') { if($result['is_trade_managed']){ cache()->set($key, 1, 86400); return true; }else{ cache()->set($key, -1, 86400); } }else{ cache()->set($key, -1, 86400); } \Yii::error(['--------is_trade_managed-error---- ', $result]); } return false; } public function storeWxOrderShipping($store_id) { try { Wechat::init($store_id); //$app = Wechat::$wechat_mini; $app = WechatMini::getWechatConfig($store_id); if (!$app) { return; } $is_trade_managed = $this->is_trade_managed($app); if(!$is_trade_managed){ // \Yii::error(['--------$is_trade_managed=false---- ', $store_id]); return; } //收货 $query = Order::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'pay_type' => Order::PAY_TYPE_WECHAT, 'is_pay' => 1]); $query->andWhere(['trade_status' => Order::ORDER_FLOW_SEND]); $orders = $query->limit(50)->all(); // debug_log([$store_id, count($orders)], __CLASS__); foreach ($orders as $order) { if($this->wxOrderStateIsConfirm($app, $order)){ $order->trade_status = Order::ORDER_FLOW_CONFIRM; $order->confirm_time = time(); $order->save(); } } //发货 $wxOrderGetNoShippingList = $this->wxOrderGetNoShippingList($app); // debug_log([$store_id, $wxOrderGetNoShippingList], __CLASS__); if($wxOrderGetNoShippingList['code'] != 0){ return; } if(empty($wxOrderGetNoShippingList['data']['order_list'])){ return; } $max = 300; foreach($wxOrderGetNoShippingList['data']['order_list'] as $item){ $order_no = (string)$item['merchant_trade_no']; $order = Order::findOne(['order_no' => $order_no]); //商城订单需要看发货状态,其他订单直接发货 if($order && !$order['send_time']){ continue; } //有些订单会延时写order表,所以支付后3分钟再处理发货逻辑 if(!$order && ($item['pay_time'] > (time() - 60 * 3))){ continue; } //拼团订单,没写order表时,跳出 if((!$order) && \app\utils\OrderNo::checkOrderNo($order_no, \app\utils\OrderNo::ORDER_PT)){ continue; } $client = new BaseClient($app); $postData = [ 'order_key' => [ 'order_number_type' => 2, 'transaction_id' => (string)$item['transaction_id'], ], 'logistics_type' => Order::getWxOrderLocalType($store_id, (string)$item['merchant_trade_no'], $order), 'delivery_mode' => 1, 'shipping_list' => [ [ 'item_desc' => '微信支付', ], ], 'upload_time' => date(DATE_RFC3339), 'payer' => [ 'openid' => (string)$item['openid'], ], ]; //快递发货 if($postData['logistics_type'] == 1){ $tracking_no = $order['express_no'] ?: $order['order_no']; $express_company = \app\models\Express::getExpressCode($order['express']); $receiver_contact = substr_replace($order['mobile'], '****', 3, 4); $postData['shipping_list'] = [ [ 'item_desc' => '微信支付', 'tracking_no' => $tracking_no, 'express_company' => $express_company, 'contact' => [ 'receiver_contact' => $receiver_contact, ], ] ]; } $result = $client->httpPostJson('wxa/sec/order/upload_shipping_info', $postData); \Yii::error([$result, json_encode($postData)]); if($max-- <= 0){ break; } } } catch (\Exception $e) { } } //订单是否已收货状态 public function wxOrderStateIsConfirm($app, $order) { if(!$order){ return false; } if ($order['pay_time'] > 0 && $order['pay_time'] < strtotime('-30 DAY')) { return true; } $orderRes = $this->wxOrderGet($app, $order['transaction_id']); if(!empty($orderRes['data']) && !empty($orderRes['data']['order'])){ return in_array($orderRes['data']['order']['order_state'], [3, 4]); } return false; } //查询订单 public function wxOrderGet($app, $transaction_id) { try { $postData = [ 'transaction_id' => (string)$transaction_id, ]; $client = new BaseClient($app); $result = $client->httpPostJson('wxa/sec/order/get_order', $postData); if (!empty($result)) { if ($result['errcode'] == '0') { return [ 'code' => 0, 'msg' => "获取成功", 'data' => $result, ]; } if ($result['errcode'] == "10060001") {//支付单不存在 return [ 'code' => 0, 'msg' => "获取成功", 'data' => [ 'order' => [ 'order_state' => 4 ] ], ]; } } \Yii::warning(['--------wxOrderGet-error---- ', $result]); return [ 'code' => 1, 'msg' => "获取失败" . $result['errmsg'], 'data' => $result, ]; } catch(\Exception $e) { \Yii::warning(['--------wxOrderGet-error---- ', $e]); } return [ 'code' => 1, 'msg' => "获取失败", ]; } //查询订单 public function wxOrderGetNoShippingList($app, $last_index = null) { try { $postData = [ 'order_state' => 1, ]; $last_index && $postData['last_index'] = $last_index; $client = new BaseClient($app); $result = $client->httpPostJson('wxa/sec/order/get_order_list', $postData); if (!empty($result)) { if ($result['errcode'] == '0') { if($result['has_more']){ $more = $this->wxOrderGetNoShippingList($app, $result['last_index']); if(!$more['code'] && $more['data']['order_list']){ $result['order_list'] = array_merge($result['order_list'], $more['data']['order_list']); } } return [ 'code' => 0, 'msg' => "获取成功", 'data' => $result, ]; } } \Yii::warning(['--------wxOrderGet-error---- ', $result]); return [ 'code' => 1, 'msg' => "获取失败" . $result['errmsg'], 'data' => $result, ]; } catch(\Exception $e) { \Yii::warning(['--------wxOrderGet-error---- ', $e]); } return [ 'code' => 1, 'msg' => "获取失败", ]; } /** * mini业务 */ public function miniProgram($appid = "") { $store_id = !empty($this->bind_store_id) ? $this->bind_store_id : $this->store_id; $store = StoreMini::find()->where(['appid' => $appid, 'store_id' => $store_id ?: get_store_id()])->one(); if (empty($store)) { $store = StoreMini::find()->where(['or', ['id' => $this->mini_id], ['store_id' => $store_id ?: get_store_id()]])->orderBy("id desc")->one(); } return $this->openPlatform->miniProgram($store->appid, $store->authorizer_refresh_token); } /** * 获取授权方的帐号基本信息 */ public function api_get_authorizer_info() { $t = \Yii::$app->db->beginTransaction(); try { $store = StoreMini::findOne($this->mini_id); if (empty($store->appid)) { throw new \Exception("当前没有绑定小程序信息"); } if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $res = $this->openPlatform->getAuthorizer($store->appid); if (empty($res->errcode) && !empty($res)) { $store = StoreMini::find()->where(['store_id' => $this->store_id])->one(); if (!empty($res->authorizer_info->qrcode_url)) { $store->mini_url = $res->authorizer_info->qrcode_url; $store->save(); } $t->commit(); return [ 'code' => 0, 'msg' => "获取成功", 'data' => $res ]; } elseif (empty($res)) { throw new \Exception("数据错误"); } else { throw new \Exception($res->errmsg); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 快速注册小程序 */ public function fastRegisterMini($refresh = 0) { $t = \Yii::$app->db->beginTransaction(); try { $data = [ 'name' => $this->name, 'code' => $this->code, 'code_type' => $this->code_type, 'legal_persona_wechat' => $this->legal_persona_wechat, 'legal_persona_name' => $this->legal_persona_name, 'component_phone' => Option::get("platform_phone", 0, 'saas')['value'] ]; if (!empty($this->bind_store_id)) { $store_id = $this->bind_store_id; } else { $store_id = $this->store_id; } if(!$refresh){ $store = StoreMini::find()->where(['AND', ['code' => $this->code, 'status' => -1, 'store_id' => $store_id], ['IS', 'appid', NULL] ])->one(); } if ($refresh || empty($store)) { $store = new StoreMini(); $store->store_id = $store_id; } $store->name = $this->name; $store->code = $this->code; $store->code_type = $this->code_type * 1; $store->legal_persona_wechat = $this->legal_persona_wechat; $store->legal_persona_name = $this->legal_persona_name; $store->license_pic = $this->license_pic; $store->merchant_id = $this->merchant_id; if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $res = $this->openPlatform->component->registerMiniProgram($data); // if ($res['errcode'] == '89249') { // debug_log([__METHOD__, $res], __CLASS__ . '.log'); // $res = $this->openPlatform->component->getRegistrationStatus($this->name, $this->legal_persona_wechat, $this->legal_persona_name); // if ($res['errcode'] == 89251) { // debug_log([__METHOD__, $res], __CLASS__ . '.log'); // $store->status = -1; // $store->save(); // } // return [ // 'code' => $res['errcode'], // 'msg' => $this->getZnMsg($res), // ]; // } if (empty($res['errcode']) && !empty($res)) { $store->status = -1; $store->save(); $t->commit(); return [ 'code' => 0, 'msg' => "申请成功,请等待审核", 'data' => $store->id ]; } elseif (empty($res)) { $store->save(); return [ 'code' => 1, 'msg' => "数据错误" ]; } else { $store->save(); throw new \Exception($this->getZnMsg($res)); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } // $weChatUrl = $this->base_url."component/fastregisterweapp?action=create&component_access_token=".$this->data['component_access_token']; } /** * 手动查询快速创建任务状态 */ public function getRegisterStatus($id = 0) { try { if (!empty($this->bind_store_id)) { $store_id = $this->bind_store_id; } else { $store_id = $this->store_id; } if($id){ $store = StoreMini::findOne($id); }else{ $store = StoreMini::findOne(['store_id' => $store_id]); } if (!empty($store->name) && !empty($store->legal_persona_wechat) && !empty($store->legal_persona_name)) { if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $res = $this->openPlatform->component->getRegistrationStatus($store->name, $store->legal_persona_wechat, $store->legal_persona_name); \Yii::error($res); return $res; } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } } /** * 返回授权需要的参数 */ public function backAuthInfo() { try { if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $res = $this->openPlatform->getPreAuthorizationUrl(\Yii::$app->request->hostInfo . '/admin/#/saasChannel/applet', ['auth_type' => 2]); return [ 'code' => 0, 'msg' => "请访问链接依次来授权", 'url' => $res ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 存储授权码 */ // public function setAuthorization_code(){ // try { // $store = new StoreMini(); // $store->store_id = get_store_id(); // $store->auth_code = json_encode([ // 'authorization_code'=>$this->authorization_code, // 'end_time'=>time()*1 + 3600*1 // ]); // $store->save(); // return $this->getAuthorization_info($this->authorization_code); // }catch (\Exception $e){ // return [ // 'code'=>1, // 'msg'=>$e->getMessage() // ]; // } // // // } public function getAuthorizers() { $res = $this->openPlatform->getAuthorizers(); return [ 'code' => 0, 'data' => $res, ]; } /** * 使用授权码获取授权信息 */ public function getAuthorization_info($authorization_code) { try { if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $res = $this->openPlatform->handleAuthorize($authorization_code); \Yii::error($res); if (empty($res['errcode']) && !empty($res)) { $store_id = $this->store_id; if ($store_id * 1 > 0 || !empty($this->mini_id)) { $store = StoreMini::find()->where(['appid' => $res['authorization_info']['authorizer_appid'], 'store_id' => $store_id])->one(); if (empty($store)) { $store = new StoreMini(); $store->store_id = $store_id; } $store->appid = $res['authorization_info']['authorizer_appid']; $store->is_cancle = 0; $store->status = 1; $store->auth_code = json_encode([ 'authorization_code' => $authorization_code, 'end_time' => time() * 1 + 3600 * 1 ]); $store->authorizer_access_token = json_encode([ 'authorizer_access_token' => $res['authorization_info']['authorizer_access_token'], 'end_time' => $res['authorization_info']['expires_in'] * 1 + time() ]); $store->authorizer_refresh_token = $res['authorization_info']['authorizer_refresh_token']; $result = $store->save(); \Yii::error($result); if ($result) { return $this->getMiniInfo($res['authorization_info']['authorizer_access_token'], $res['authorization_info']['authorizer_appid']); } } } else { throw new \Exception($this->getZnMsg($res)); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } } /** * 获取小程序基础信息 */ public function getMiniInfo($authorizer_access_token = "", $appid = "") { $t = \Yii::$app->db->beginTransaction(); try { $store_id = $this->store_id; $store = StoreMini::findOne(['appid' => $appid, 'store_id' => $store_id]); if (empty($authorizer_access_token) && empty($store)) { throw new \Exception("暂未授权"); } if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $miniProgram = $this->miniProgram($appid); $res = $miniProgram->account->getBasicInfo(); if (empty($res['errcode']) && !empty($res)) { $store->mini_info = json_encode($res, JSON_UNESCAPED_UNICODE); if ($res['nickname']) { $store->mini_nickname = $res['nickname']; } if ($res['head_image_info']['head_image_url']) { $store->mini_url = $res['head_image_info']['head_image_url']; } $store->signature = $res['signature_info']['signature']; //$store->mini_url = $res['head_image_info']['head_image_url']; $saveRes = $store->save(); if ($saveRes) { switch ($res['account_type']) { case 1: $res['account_type'] = "订阅号"; break; case 2: $res['account_type'] = "服务号"; break; case 3: $res['account_type'] = "小程序"; break; } $t->commit(); //将Appid填充系统设置 if ((int)$store->is_use === 1) { $WechatConfig = WechatConfig::find()->where(['store_id' => $store_id, 'type' => 1])->one(); if (empty($WechatConfig)) { $WechatConfig = new WechatConfig(); $WechatConfig->store_id = $store_id; $WechatConfig->type = 1; } $WechatConfig->app_id = $store->appid; $WechatConfig->app_secret = "0"; // $WechatConfig->name = ($res['principal_name'] ?: $store->name) ?: ''; $WechatConfig->updated_at = time(); if (!$WechatConfig->save()) { throw new \Exception(json_encode($WechatConfig->errors)); } } return [ 'code' => 0, 'msg' => "获取成功", 'data' => $res, 'appid' => $appid, 'store_mini' => $store, ]; } throw new \Exception(json_encode($store->errors)); } else { $t->rollBack(); return [ 'code' => $res['errcode'], 'msg' => $this->getZnMsg($res), ]; } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } } /** * 设置获取修改服务器域名 */ public function setDomainName() { $t = \Yii::$app->db->beginTransaction(); try { if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $store = StoreMini::findOne($this->mini_id); } if (empty($store)) { throw new \Exception("数据错误,获取小程序信息失败"); } $res = $this->setWebviewDomainName(); if ($res['code'] == 1) { throw new \Exception($res['msg']); } $DomainData = $res; if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $webview_domain_name = $store->webview_domain_name; $requestdomain = [\Yii::$app->request->hostName]; if (!empty($webview_domain_name)) { $webview_domain_name = json_decode($webview_domain_name, true); if (!empty($webview_domain_name)) { foreach ($webview_domain_name as $url_item) { if (!empty($url_item['url'])) { $index = strstr($url_item['url'], 'https'); if ($index) { $url_item['url'] = \str_replace('https://', '', $url_item['url']); } else { $index = strstr($url_item['url'], 'http'); if ($index) { $url_item['url'] = \str_replace('http://', '', $url_item['url']); } } array_push($requestdomain, $url_item['url']); } } } } $uploaddomain = $downloaddomain = $udpdomain = $tcpdomain = $wsrequestdomain = $requestdomain; array_walk($requestdomain, function(&$item) { $item = "https://" . $item; }); array_walk($wsrequestdomain, function(&$item) { $item = "wss://" . $item; }); array_walk($uploaddomain, function(&$item) { $item = "https://" . $item; }); array_walk($downloaddomain, function(&$item) { $item = "https://" . $item; }); array_walk($udpdomain, function(&$item) { $item = "udp://" . $item; }); array_walk($tcpdomain, function(&$item) { $item = "tcp://" . $item; }); $miniProgram = $this->miniProgram($store->appid); $data = [ 'action' => 'set', 'requestdomain' => $requestdomain, "wsrequestdomain" => $wsrequestdomain, "uploaddomain" => $uploaddomain, "downloaddomain" => $downloaddomain, "udpdomain" => $udpdomain, "tcpdomain" => $tcpdomain ]; $res = $miniProgram->domain->modify($data); if (empty($res['errcode']) && !empty($res)) { $t->commit(); return [ 'code' => 0, 'msg' => "设置成功", 'data' => $res, 'DomainData' => $DomainData ]; } elseif (empty($res)) { throw new \Exception("数据错误"); } else { throw new \Exception($this->getZnMsg($res)); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => '服务器域名错误' . $e->getMessage(), ]; } } /** * 下载校验文件并提交至客服系统 * @param mixed $app * @return array * @throws \Exception * @date 2023-02-20 */ public function addConfirmFile($app) { try { $access_token = $app->access_token->getToken()['authorizer_access_token']; $res = http_post('https://api.weixin.qq.com/wxa/get_webviewdomain_confirmfile?access_token=' . $access_token, [ 'body' => '{}' ]); if ($res->getStatusCode() != 200) { throw new \Exception('下载校验文件出错'); } $result = \json_decode((string)$res->getBody(), true); if ($result['errcode'] != 0) { throw new \Exception($result['errmsg']); } return [ 'code' => 0, 'msg' => '', 'data' => $result ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function addFile2Ws($result) { try { $res = http_post('https://ws2.cyyvip.com/web/index.php?r=common/default/add-confirm-file', [ 'form_params' => [ 'token' => 'tianxin100', 'file_name' => $result['file_name'], 'file_content' => $result['file_content'], ] ]); if ($res->getStatusCode() != 200) { throw new \Exception('添加校验文件出错'); } return [ 'code' => 0, 'msg' => '完成' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function addFile2Other($result) { try { if (!is_dir(\Yii::$app->runtimePath . '/store/mini/')) { mkdir(\Yii::$app->runtimePath . '/store/mini/', 0777, true); } $file_name = \Yii::$app->runtimePath . '/store/mini/' . $result['file_name']; file_put_contents($file_name, $result['file_content']); return [ 'code' => 0, 'msg' => '完成', 'data' => [ 'file' => 'https://' . \Yii::$app->request->hostName . '/runtime/store/mini/' . $result['file_name'] ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * @return array * 设置业务域名 * @throws \GuzzleHttp\Exception\GuzzleException */ public function setWebviewDomainName() { $t = \Yii::$app->db->beginTransaction(); try { if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $store = StoreMini::findOne($this->mini_id); } if (empty($store)) { throw new \Exception("数据错误,获取小程序信息失败"); } if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $miniProgram = $this->miniProgram($store->appid); $result = $this->addConfirmFile($miniProgram); if ($result['code'] === 1) { return $result; } $result_ws = $this->addFile2Ws($result['data']); if ($result_ws['code'] === 1) { return $result_ws; } $webview_domain_name = $store->webview_domain_name; $url_arr = []; if (!empty($webview_domain_name)) { $webview_domain_name = json_decode($webview_domain_name, true); if (!empty($webview_domain_name)) { foreach ($webview_domain_name as $url_item) { if (!empty($url_item['url'])) { array_push($url_arr, $url_item['url']); } } } } $res = $miniProgram->domain->setWebviewDomain($url_arr, 'set'); if (empty($res['errcode']) && !empty($res)) { $store->is_set_web = 1; $store->save(); $t->commit(); return [ 'code' => 0, 'msg' => "设置成功", 'data' => $res ]; } elseif (empty($res)) { throw new \Exception("数据错误"); } else { throw new \Exception($this->getZnMsg($res)); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => '业务域名错误' . $e->getMessage() ]; } } /** * 暂停/开始使用 */ public function unbind() { try { if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $store = StoreMini::findOne($this->mini_id); } if (empty($store)) { throw new \Exception("数据错误,获取小程序信息失败"); } if ($this->is_use == 1 || empty($this->is_use)) { StoreMini::updateAll(['is_use' => 2], ['store_id' => $this->store_id]); $store->is_use = 1; } elseif ($this->is_use == 2) { $store->is_use = 2; } $res = $store->save(); if ($res) { //设置店铺的appid if($store->is_use == 1){ $wechat = WechatConfig::find()->where(['store_id'=>$this->store_id,'type'=>1,'is_delete'=>0])->one(); if (!$wechat) { $wechat = new WechatConfig(); $wechat->store_id = $this->store_id; $wechat->type = 1; } $wechat->app_id = $store->appid; $wechat->save(); } //$store->save(); return [ 'code' => 0, 'msg' => "设置成功", 'data' => $res ]; } else { throw new \Exception("数据库添加失败"); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 注:前端提交微信临时素材接口,获取素材ID * 设置小程序名称 */ public function setMiniNianname() { $t = \Yii::$app->db->beginTransaction(); try { if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $store = StoreMini::findOne($this->mini_id); } if (empty($store)) { throw new \Exception("数据错误,获取小程序信息失败"); } if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $miniProgram = $this->miniProgram($store->appid); $media = new Client($miniProgram); $data = [ 'id_card' => $this->id_card ? $this->id_card : '', 'license' => $this->license ? $this->license : $store->license_pic, 'naming_other_stuff_1' => $this->naming_other_stuff_1, 'naming_other_stuff_2' => $this->naming_other_stuff_2, 'naming_other_stuff_3' => $this->naming_other_stuff_3, 'naming_other_stuff_4' => $this->naming_other_stuff_4, 'naming_other_stuff_5' => $this->naming_other_stuff_5, ]; $store->apply_mini_info = json_encode($data); // $this->addMedia($data); foreach ($data as $index => $item) { if (!empty($item)) { $head_img_media_id = $media->uploadImage($this->saveTempImage($item)); if (empty($head_img_media_id['media_id'])) { throw new \Exception($this->getZnMsg($head_img_media_id)); } $data[$index] = $head_img_media_id['media_id']; } } $id_card = $data['id_card'] ?? ""; $license = $data['license'] ?? ""; $res = $miniProgram->setting->setNickname($this->mini_nickname, $id_card, $license, [ $data['naming_other_stuff_1'], $data['naming_other_stuff_2'], $data['naming_other_stuff_3'], $data['naming_other_stuff_4'], $data['naming_other_stuff_5'] ]); //self::addCategory(); $store->mini_nickname = $this->mini_nickname; if (empty($res['errcode']) && !empty($res)) { $store->nickname_audit_id = $res['audit_id']; $store->apply_name_status = 1; if (empty($res['audit_id'])) { $store->apply_name_status = 0; } $store->save(); $t->commit(); return [ 'code' => 0, 'msg' => "开始审核", 'data' => $res ]; } elseif (empty($res)) { $store->save(); throw new \Exception("数据错误"); } else { if ($res['errcode'] == 91019) { $store->apply_name_status = 1; $store->save(); $t->commit(); return [ 'code' => 0, 'msg' => "小程序审核中" ]; } throw new \Exception($this->getZnMsg($res).'|'.$res['errcode']); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 查询名称审核状态 */ public function getNicknameAuditStatus() { $t = \Yii::$app->db->beginTransaction(); try { if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $store = StoreMini::findOne($this->mini_id); } if (empty($store) || empty($store->nickname_audit_id)) { $t->commit(); return [ 'code' => 0, 'msg' => '数据错误或审核已经通过', 'status' => 3 ]; } if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $miniProgram = $this->miniProgram($store->appid); $result = $miniProgram->setting->getNicknameAuditStatus($store->nickname_audit_id); if (empty($result['errcode']) && !empty($result)) { $msg = "获取成功"; switch ($result['audit_stat']) { case 0: $msg = "名称审核中"; $store->apply_name_status = 1; break; case 1: $msg = "名称审核完成"; $store->apply_name_status = 3; break; case 2: $msg = "名称审核失败"; $store->apply_name_status = 2; $store->apply_name_error = $result['fail_reason']; break; } if (!$store->save()) { throw new \Exception("保存失败"); } $t->commit(); return [ 'code' => 0, 'msg' => $msg, 'status' => $store->apply_name_status ]; } throw new \Exception("查询失败"); } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 增加Media事件 */ public function addMedia($data) { foreach ($data as $index => $item) { $mediaForm = StoreMedia::find()->where(['media_id' => $item])->one(); if (empty($mediaForm)) { $mediaForm = new StoreMedia(); $mediaForm->store_id = $this->store_id; $mediaForm->media_id = $item; $mediaForm->end_time = time() * 1 + 60 * 60 * 24 * 3; } $mediaForm->event = $index; $mediaForm->end_time = time() * 1 + 60 * 60 * 24 * 3; switch ($index) { case 'id_card': $mediaForm->desc = "身份证照片"; break; case 'license': $mediaForm->desc = "组织机构代码证或营业执照"; break; case 'naming_other_stuff_1': $mediaForm->desc = "其他证明材料1"; break; case 'naming_other_stuff_2': $mediaForm->desc = "其他证明材料2"; break; case 'naming_other_stuff_3': $mediaForm->desc = "其他证明材料3"; break; case 'naming_other_stuff_4': $mediaForm->desc = "其他证明材料4"; break; case 'naming_other_stuff_5': $mediaForm->desc = "其他证明材料5"; break; case 'head_img_media_id': $mediaForm->desc = "头像素材"; break; case "ext_file_media_id": $mediaForm->desc = "用户隐私指引"; } $result = $mediaForm->save(); } } /** * 微信认证名称检测 */ public function checkWxVerifyNickname() { try { if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $store = StoreMini::findOne($this->mini_id); } if (empty($store)) { return [ 'code' => 1, 'msg' => "数据错误" ]; } if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $miniProgram = $this->miniProgram($store->appid); $res = $miniProgram->setting->isAvailableNickname($this->mini_nickname); if (empty($res['errcode']) && !empty($res)) { return [ 'code' => 0, 'msg' => "成功", 'data' => $res ]; } elseif (empty($res)) { return [ 'code' => 1, 'msg' => "数据错误" ]; } else { return [ 'code' => $res['errcode'], 'msg' => $this->getZnMsg($res), 'data' => $res ]; } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } } /** * 修改头像 */ public function setHeadImage() { $t = \Yii::$app->db->beginTransaction(); try { if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $store = StoreMini::findOne($this->mini_id); } if (empty($store)) { throw new \Exception("数据错误,获取小程序信息失败"); } if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $miniProgram = $this->miniProgram($store->appid); $media = new Client($miniProgram); $head_img_media_id = $media->uploadImage($this->saveTempImage($this->head_img_media_id)); if (empty($head_img_media_id['media_id'])) { throw new \Exception($this->getZnMsg($head_img_media_id)); } \Yii::error($head_img_media_id['thumb_media_id']); $res = $miniProgram->account->updateAvatar($head_img_media_id['media_id'], $this->x1, $this->y1, $this->x2, $this->y2); if ($res['errcode'] == '53202') { $store->mini_url = $this->head_img_media_id; $store->save(); $t->commit(); return [ 'code' => 0, 'msg' => "设置成功", ]; } if (empty($res['errcode']) && !empty($res)) { $store->mini_url = $this->head_img_media_id; $store->save(); $t->commit(); return [ 'code' => 0, 'msg' => "设置成功", 'data' => $res ]; } elseif (empty($res)) { throw new \Exception("数据错误"); } else { throw new \Exception($this->getZnMsg($res)); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 修改简介 */ public function setSignature() { $t = \Yii::$app->db->beginTransaction(); try { if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $store = StoreMini::findOne($this->mini_id); } if (empty($store)) { throw new \Exception("数据错误,获取小程序信息错误"); } if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $miniProgram = $this->miniProgram($store->appid); $res = $miniProgram->account->updateSignature($this->signature); if ($res['errcode'] == 53200) { $store->signature = $this->signature; $store->save(); $t->commit(); return [ 'code' => 0, 'msg' => "设置成功", 'data' => $res ]; } if (empty($res['errcode']) && !empty($res)) { $store->signature = $this->signature; $store->save(); $t->commit(); return [ 'code' => 0, 'msg' => "设置成功", 'data' => $res ]; } elseif (empty($res)) { throw new \Exception("数据错误"); } else { throw new \Exception($this->getZnMsg($res)); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function getdata($data, $id = [0]) { foreach ($data as $k => $v) { if (in_array($v['id'], $id)) { $v['children'] = $this->getdata($data, $v['children']); $arr[] = $v; } } return $arr; } /** * @return array * 获取全部可选分类 */ public function getAllCategories() { try { if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $store = StoreMini::findOne($this->mini_id); } if (empty($store->authorizer_access_token)) { return [ 'code' => 1, 'msg' => "没有授权数据", 'data' => [] ]; } if (empty($store)) { return [ 'code' => 1, 'msg' => "数据错误" ]; } if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $miniProgram = $this->miniProgram($store->appid); $res = $miniProgram->setting->getAllCategories(); if (empty($res['errcode']) && !empty($res)) { $data = $res['categories_list']['categories']; $data = $this->getdata($data); return [ 'code' => 0, 'msg' => "获取成功", 'data' => $data, 'old_data' => $res['categories_list']['categories'] ]; } elseif (empty($res)) { return [ 'code' => 1, 'msg' => "数据错误" ]; } else { return [ 'code' => $res['errcode'], 'msg' => $this->getZnMsg($res), 'data' => $res ]; } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * @return array * 获取已有分类 */ public function getCategories() { try { if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $store = StoreMini::findOne($this->mini_id); } if (empty($store)) { throw new \Exception("数据错误,查询小程序信息失败"); } if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } if (!$store->appid) { throw new \Exception("参数配置错误"); } $miniProgram = $this->miniProgram($store->appid); $res = $miniProgram->setting->getCategories(); if (empty($res['errcode']) && !empty($res)) { $store->categories = !empty($res['categories']) ? json_encode($res['categories']) : ""; $store->save(); return [ 'code' => 0, 'msg' => "获取成功", 'data' => $res['categories'] ]; } elseif (empty($res)) { throw new \Exception("数据错误"); } else { throw new \Exception($this->getZnMsg($res)); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 添加类目 */ public function addCategory() { try { if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $store = StoreMini::findOne($this->mini_id); } if (empty($store)) { throw new \Exception("数据错误,查找不到小程序信息"); } if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $miniProgram = $this->miniProgram($store->appid); $data = $this->categories; $media = new Client($miniProgram); \Yii::error($data); foreach ($data as $index => &$item) { if(!empty($item['certicates'])){ if(isset($item['certicates']['key'])){ $item['certicates'] = [$item['certicates']]; } foreach ($item['certicates'] as $cateIndex => &$cate) { $url = $this->saveTempImage($cate['value']); $cate['value'] = $media->uploadImage($url)['media_id']; } } } \Yii::error($data); // $data = [ // [ // "first" => 304, // "second"=> 1309 // ] // ]; $res = $miniProgram->setting->addCategories($data); if (empty($res['errcode']) && !empty($res)) { $cat_res = $this->getCategories(); return [ 'code' => 0, 'msg' => "设置成功", 'data' => $res, 'cat_data' => $cat_res['data'] ]; } elseif (empty($res)) { throw new \Exception("数据错误"); } else { throw new \Exception($this->getZnMsg($res)); } } catch (\Exception $e) { return [ 'code' => 0, 'msg' => $e->getMessage(), 'e' => $e, ]; } } /** * 删除类目 */ public function delCategory() { $t = \Yii::$app->db->beginTransaction(); try { if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $store = StoreMini::findOne($this->mini_id); } if (empty($store)) { throw new \Exception("数据错误"); } if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $miniProgram = $this->miniProgram($store->appid); $res = $miniProgram->setting->deleteCategories($this->cate_first, $this->cate_second); if (empty($res['errcode']) && !empty($res)) { $t->commit(); return [ 'code' => 0, 'msg' => "删除成功", 'data' => $res ]; } elseif (empty($res)) { throw new \Exception("数据错误"); } else { throw new \Exception($this->getZnMsg($res)); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 获取代码模版列表 */ public function getTemplateList() { try { if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $res = $this->openPlatform->code_template->list(0); if (empty($res['errcode']) && !empty($res)) { $templateList = $res['template_list']; foreach ($templateList as &$item) { $item['c_time'] = date('Y-m-d H:i:s', $item['create_time']); $item['template_id'] = (string)$item['template_id']; } $last_names = array_column($templateList, 'create_time'); array_multisort($last_names, SORT_DESC, $templateList); if (!empty($templateList) && count($templateList) > 15) { $last_template = end($templateList); if (isset($last_template['template_id'])) { $result = $this->delLastTemplate($last_template['template_id']); if ($result['code'] === 0) { array_pop($templateList); } } } return [ 'code' => 0, 'msg' => "获取成功", 'data' => [ 'template_list' => $templateList ] ]; } elseif (empty($res)) { return [ 'code' => 1, 'msg' => "数据错误" ]; } else { return [ 'code' => $res['errcode'], 'msg' => $this->getZnMsg($res), 'data' => $res ]; } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } } public function delLastTemplate($templateId = 0) { try { if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } if (empty(trim($templateId))) { throw new \Exception("缺失关键参数"); } $res = $this->openPlatform->code_template->delete($templateId); if (empty($res['errcode']) && !empty($res)) { return [ 'code' => 0, 'msg' => "操作成功", ]; } elseif (empty($res)) { return [ 'code' => 1, 'msg' => "数据错误" ]; } else { return [ 'code' => $res['errcode'], 'msg' => $this->getZnMsg($res), 'data' => $res ]; } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } } public function pluginList($miniProgram) { return $miniProgram->plugin->list(); } //具备某个插件权限 public function hasPlugin($miniProgram, $appid = 'wx2b03c6e691cd7370') { $res = $this->pluginList($miniProgram); if (empty($res['errcode']) && !empty($res)) { if (!empty($res['plugin_list'])) { foreach($res['plugin_list'] as $item){ //直播 if($item['appid'] == $appid && $item['status'] == 2){ return true; } } } else { throw new \Exception("权限数据失败"); } } elseif (empty($res)) { throw new \Exception("数据错误"); } else { throw new \Exception($this->getZnMsg($res)); } return false; } //具备某个三方授权权限 public function hasAuthorizationId($id = 52) { $res = $this->api_get_authorizer_info(); if($res['code'] == 0){ $items = $res['data']['authorization_info']['func_info']; foreach($items as $item){ if($item['funcscope_category']['id'] == $id){ return true; } } } return false; } public function canUsePlugin($appid = 'wx2b03c6e691cd7370', $aid = 52) { if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $store = StoreMini::findOne($this->mini_id); } if (empty($store)) { throw new \Exception("数据错误,获取不到小程序信息"); } if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $miniProgram = $this->miniProgram($store->appid); $hasPlugin = $this->hasPlugin($miniProgram, $appid); $hasAuthorizationId = $this->hasAuthorizationId($aid); $canUse = $hasPlugin && $hasAuthorizationId; return [ 'code' => 0, 'msg' => "成功", 'canUse' => intval($canUse), 'hasPlugin' => intval($hasPlugin), 'hasAuthorizationId' => intval($hasAuthorizationId), ]; } /** * 上传小程序 */ public function upMini($useLive = 0, $useStudent = 0, $useB2b = 0) { $t = \Yii::$app->db->beginTransaction(); try { if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $store = StoreMini::findOne($this->mini_id); } if (empty($store)) { throw new \Exception("数据错误,获取不到小程序信息"); } if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $miniProgram = $this->miniProgram($store->appid); $self_mini = Option::get('self_mini', $store->store_id, 'store', 0)['value']; $ext = [ 'extEnable' => true, 'extAppid' => $store->appid, 'ext' => [ 'self_mini' => $self_mini ? 1 : 0, "apiurl" => "https://" . \Yii::$app->request->hostName, "store_id" => $store->store_id, "mini_id" => $store->id ], "window" => [ "navigationBarTitleText" => "0000" ] ]; // 此方法是把插件放主包,暂时不用 // $plugins = []; // if($useLive){ // $plugins['live-player-plugin'] = [ // 'version' => '1.3.2', // 'provider' => 'wx2b03c6e691cd7370', // ]; // } // if($useB2b){ // $plugins['B2bPlugin'] = [ // 'version' => 'latest', // 'provider' => 'wx69b7451feb427f0e', // ]; // } // if ($useLive || $useB2b) { // $ext['plugins'] = $plugins; // } if($useLive){ $ext['plugins'] = [ 'live-player-plugin' => [ 'version' => '1.3.2', 'provider' => 'wx2b03c6e691cd7370', ], ]; } // 此方法是把插件放分包,每次更新需要更新config/ext.php if ($useB2b) { // 获取config/ext.php内容 $extSub = require \Yii::getAlias('@app/config/ext.php'); $extSub = \json_decode($extSub, true); $ext['subPackages'] = $extSub; } if ($useStudent) { $ext['usingComponents'] = [ 'student' => '/wxcomponents/student/index', 'single-header' => '/components/diySystemPage/components/singleHeader/index', 'foot-nav' => '/components/diyNew/footNav/index', 'transition-page' => '/components/transitionPage/index', 'get-success' => '/components/coupon/getSuccessPopup', 'shenhe' => '/components/shenhe/shenhe', ]; } $template = Option::get("platform_template_id", 0, 'saas');// $template_id = $template['value']; if (empty($template) || $template_id === "-1") { throw new \Exception("未选择模板"); } $res = $this->setDomainName(); if ($res['code'] == 1) { throw new \Exception($res['msg']); } $DomainData = $res; $res = $miniProgram->code->commit($template_id, json_encode($ext), cyy_version(), $this->user_desc); if (empty($res['errcode']) && !empty($res)) { $store->mini_up = -1; $store->template_id = $template_id; $store->user_version = cyy_version(); $store->user_desc = $this->user_desc; $store->ext = json_encode($ext); $res = $store->save(); if ($res) { $res = $this->getMiniQrcode($store->id); if ($res['code'] === 0) { $t->commit(); return [ 'code' => 0, 'msg' => "成功", 'data' => $res['data'], 'datas' => $DomainData ]; } else { throw new \Exception("获取体验二维码失败"); } } else { throw new \Exception("存储数据失败"); } } elseif (empty($res)) { throw new \Exception("数据错误"); } else { throw new \Exception($this->getZnMsg($res)); } } catch (\Exception $e) { $t->rollBack(); if (strpos($e->getMessage(), 'cURL error 28') !== false) { return $this->upMini(); } return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } } /** * 添加体验者 * @return array * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException * @throws \GuzzleHttp\Exception\GuzzleException */ public function addTester() { try { $store = StoreMini::findOne($this->mini_id); if (empty($store)) { return [ 'code' => 1, 'msg' => "数据错误" ]; } if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $miniProgram = $this->miniProgram($store->appid); $res = $miniProgram->tester->bind($this->wechatId); if (empty($res['errcode']) && !empty($res)) { return [ 'code' => 0, 'msg' => "成功", 'data' => $res ]; } elseif (empty($res)) { return [ 'code' => 1, 'msg' => "数据错误" ]; } else { return [ 'code' => $res['errcode'], 'msg' => $this->getZnMsg($res), 'data' => $res ]; } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 获取体验版二维码 */ public function getMiniQrcode($id = 0) { try { if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $id = $id !== 0 ? $id : $this->mini_id; $store = StoreMini::findOne($id); } if (empty($store)) { return [ 'code' => 1, 'msg' => "数据错误" ]; } if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $miniProgram = $this->miniProgram($store->appid); $qrcode = \Yii::$app->basePath . "/web/temp/" . $this->store_id . "_" . date('YmdHis') . ".jpg"; $res = $miniProgram->code->getQrCode($this->mini_path); if (empty(json_decode($res, true)['errcode'])) { file_put_contents($qrcode, $res); $url = str_replace(\Yii::$app->basePath, \Yii::$app->request->hostInfo, $qrcode); return [ 'code' => 0, 'msg' => "成功", 'data' => $url ]; } elseif (empty($res)) { return [ 'code' => 1, 'msg' => "数据错误" ]; } else { $res = json_decode($res, true); return [ 'code' => $res->errcode, 'msg' => $this->getZnMsg($res), 'data' => $res ]; } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 提交审核 */ public function submitAudit() { $t = \Yii::$app->db->beginTransaction(); try { $result = $this->setPrivacySetting(); if ($result['code'] === 0) { if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $store = StoreMini::findOne($this->mini_id); } if (empty($store)) { throw new \Exception("数据错误,未找到对应的小程序信息"); } if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $miniProgram = $this->miniProgram($store->appid); $data = [ 'item_list' => $this->item_list ?? [], 'order_path' => 'order/order/order', ]; $res = $miniProgram->code->submitAudit($data, '', ''); if (empty($res['errcode']) && !empty($res)) { //是否加急审核 if ($this->is_fast_audit === 1) { $res = $miniProgram->code->speedupAudit($res['auditid']); } $store->mini_up = 1; $store->audit_id = (string)$res['auditid']; $store->save(); $t->commit(); return [ 'code' => 0, 'msg' => "成功", 'data' => $res ]; } elseif (empty($res)) { throw new \Exception("数据错误"); } else { throw new \Exception($this->getZnMsg($res)); } } else { throw new \Exception($result['msg']); } } catch (\Exception $e) { $t->rollBack(); if (strpos($e->getMessage(), 'cURL error 28') !== false) { return $this->submitAudit(); } return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 查询最后一次提交审核的状态 */ public function lastAuditStatus() { $t = \Yii::$app->db->beginTransaction(); try { if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $store = StoreMini::findOne($this->mini_id); } if (empty($store)) { throw new \Exception("数据错误"); } if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $miniProgram = $this->miniProgram($store->appid); $result = $miniProgram->code->getLatestAuditStatus(); $msg = "返回成功"; if (empty($result['errcode']) && !empty($result)) { switch ($result['status']) { case 0: $msg = "审核通过"; $store->mini_up = 2; break; case 1: $msg = "审核已拒绝:" . $result['reason']; $store->mini_up = 3; $store->mini_up_error = $result['reason']; break; case 2: $msg = "审核中"; $store->mini_up = 1; break; case 3: $msg = "未提交审核或已撤回"; $store->mini_up = 0; break; } if (!$store->save()) { throw new \Exception("保存失败"); } $t->commit(); return [ 'code' => 0, 'msg' => $msg ]; } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 提交审核的状态 */ public function getAuditStatus($mini_id) { //20240816微信回调老是处理失败 增加主动查询的逻辑 // $t = \Yii::$app->db->beginTransaction(); try { if ($mini_id) { $store = StoreMini::findOne($mini_id); } if (empty($store)) { throw new \Exception("数据错误"); } if (empty($store->audit_id)) { throw new \Exception("未查询到审核信息"); } if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $miniProgram = $this->miniProgram($store->appid); $result = $miniProgram->code->getAuditStatus($store->audit_id); $msg = "返回成功"; if (empty($result['errcode']) && !empty($result)) { switch ($result['status']) { case 0: $msg = "审核通过"; if (intval($store->mini_up) === 1) { $store->mini_up = 2; } break; case 1: $msg = "审核已拒绝:" . $result['reason']; $store->mini_up = 3; $store->mini_up_error = $result['reason']; break; case 2: $msg = "审核中"; $store->mini_up = 1; break; case 3: $msg = "未提交审核或已撤回"; $store->mini_up = 0; break; } if (!$store->save()) { throw new \Exception("保存失败"); } // $t->commit(); return [ 'code' => 0, 'msg' => $msg, 'mini_up' => $store->mini_up ]; } throw new \Exception(json_encode($result, JSON_UNESCAPED_UNICODE)); } catch (\Exception $e) { // $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 审核撤回 */ public function unDoCodeAudit() { $t = \Yii::$app->db->beginTransaction(); try { if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $store = StoreMini::findOne($this->mini_id); } if (empty($store)) { throw new \Exception("数据错误,未查询到小程序信息"); } if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $miniProgram = $this->miniProgram($store->appid); $res = $miniProgram->code->withdrawAudit(); if (empty($res['errcode']) && !empty($res)) { $store->mini_up = -1; $store->save(); $t->commit(); return [ 'code' => 0, 'msg' => "成功", 'data' => $res ]; } elseif (empty($res)) { throw new \Exception("数据错误"); } else { throw new \Exception($this->getZnMsg($res)); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 发布已通过审核的小程序 */ public function release() { $t = \Yii::$app->db->beginTransaction(); try { if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $store = StoreMini::findOne($this->mini_id); } $store_id = !empty($this->bind_store_id) ? $this->bind_store_id : get_store_id(); if (empty($store)) { throw new \Exception("数据错误,获取不到小程序信息"); } if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $miniProgram = $this->miniProgram($store->appid); $this->is_use = 1; $this->unbind(); $res = $miniProgram->code->release(); if ((empty($res['errcode']) && !empty($res)) || $res['errcode'] == "85052") { $store->mini_up = 5; if ($store->save()) { $t->commit(); $res = $this->getMiniQr($store->id); if ($res['code'] === 0) { return [ 'code' => 0, 'msg' => "成功", 'data' => $res['data'] ]; } else { throw new \Exception("获取二维码数据错误"); } } else { throw new \Exception("保存数据错误"); } } elseif (empty($res)) { throw new \Exception("数据错误"); } else { throw new \Exception($this->getZnMsg($res)); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 设置小程序用户隐私保护指引 */ public function setPrivacySetting() { $data = [ "privacy_ver" => $this->privacy_ver, "owner_setting" => [ "contact_phone" => "17090402350", "notice_method" => "短信" ], "setting_list" => [ [ "privacy_key" => "UserInfo", "privacy_text" => "统计订单" ], [ "privacy_key" => "Location", "privacy_text" => "显示附近自提点" ], [ "privacy_key" => "Address", "privacy_text" => "显示附近自提点" ], [ "privacy_key" => "RunData", "privacy_text" => "消费步数兑换商品" ], [ "privacy_key" => "Record", "privacy_text" => "售后咨询" ], [ "privacy_key" => "Camera", "privacy_text" => "提交订单备注资料" ], [ "privacy_key" => "AlbumWriteOnly", "privacy_text" => "保存海报" ], [ "privacy_key" => "PhoneNumber", "privacy_text" => "多端口数据统一" ], [ "privacy_key" => "Album", "privacy_text" => "审核资质" ], [ "privacy_key" =>"MessageFile", "privacy_text" => "订单评价拍照" ], [ "privacy_key" =>"Clipboard", "privacy_text" => "支持用户信息复制信息" ], [ "privacy_key" =>"ChooseLocation", "privacy_text" => "计算距离" ], [ "privacy_key" =>"DeviceInfo", "privacy_text" => "生成适应页面" ] ], ]; try { $this->apply_privacy_interface(); if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $store = StoreMini::findOne($this->mini_id); } $contact_tel = Option::get(OptionSetting::STORE_CONTACT_TEL, $store->store_id, '')['value']; if (!$contact_tel) { throw new \Exception("更新隐私政策失败,请前往'系统-功能设置-基础设置'填写保存手机号信息。"); } $data['owner_setting']['contact_phone'] = $contact_tel; if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $client = new BaseClient($this->miniProgram($store->appid)); $res = $client->httpPostJson('cgi-bin/component/setprivacysetting', $data); if (empty($res['errcode']) && !empty($res)) { return [ 'code' => 0, 'msg' => "成功", 'data' => $res ]; } elseif (empty($res)) { return [ 'code' => 1, 'msg' => "数据错误" ]; } else { return [ 'code' => $res['errcode'], 'msg' => $this->getZnMsg($res) ]; } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function apply_privacy_interface() { try { if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $store = StoreMini::findOne($this->mini_id); } $client = new BaseClient($this->miniProgram($store->appid)); $arr = [ [ 'api_name' => 'wx.chooseLocation', 'content' => '获取用户选择的省市区位置信息,帮助快速生成收货地址' ], [ 'api_name' => 'wx.getLocation', 'content' => '获取用户位置信息,用户能查看到附近的自提地点' ] ]; foreach ($arr as $item) { $res = $client->httpPostJson('wxa/security/apply_privacy_interface', $item); debug_log('申请——', 'privacy_interface.log'); debug_log($res, 'privacy_interface.log'); if ($res['errcode']) { return [ 'code' => $res['errcode'], 'msg' => $this->getZnMsg($res) ]; } } return [ 'code' => 0, 'msg' => '开始申请...' ]; } catch (\Exception $e) { debug_log('申请——', 'privacy_interface.log'); debug_log(['code' => $e->getCode(), 'msg' => $e->getMessage()], 'privacy_interface.log'); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function get_privacy_interface() { try { if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $store = StoreMini::findOne($this->mini_id); } if (!$store->appid) { throw new \Exception("参数配置错误"); } $client = new BaseClient($this->miniProgram($store->appid)); $res = $client->httpGet('wxa/security/get_privacy_interface'); debug_log('获取——', 'privacy_interface.log'); debug_log($res, 'privacy_interface.log'); } catch (\Exception $e) { debug_log('获取——', 'privacy_interface.log'); debug_log(['code' => $e->getCode(), 'msg' => $e->getMessage()], 'privacy_interface.log'); } } /** * 查询小程序用户隐私保护指引 */ public function getPrivacySetting() { try { $data = [ "privacy_ver" => $this->privacy_ver, ]; if (!empty($this->bind_store_id)) { $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one(); } else { $store = StoreMini::findOne($this->mini_id); } if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $client = new BaseClient($this->miniProgram($store->appid)); $res = $client->httpPostJson('cgi-bin/component/getprivacysetting', $data); if (empty($res['errcode']) && !empty($res)) { return [ 'code' => 0, 'msg' => "成功", 'data' => $res ]; } elseif (empty($res)) { return [ 'code' => 1, 'msg' => "数据错误" ]; } else { return [ 'code' => $res['errcode'], 'msg' => $this->getZnMsg($res), 'data' => $res ]; } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 查询小程序码 */ public function getMiniQr($id = 0, $sence = '', $page = '') { try { if (empty($id)) { $id = $this->mini_id; } $store = StoreMini::findOne($id); if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $miniProgram = $this->miniProgram($store->appid); $response = $miniProgram->app_code->getUnlimit($sence ?: "store=" . $this->store_id, [ 'page' => $page ]); $filename = md5(time()) . '.jpg'; $filePath = \Yii::$app->basePath . '/web/uploads/images/store_' . $this->store_id . '/' . date('Y-m-d'); if (!is_dir($filePath)) { mkdir($filePath, 0777, true); chmod($filePath , 0777); } $filePath = \Yii::$app->basePath . '/web/uploads/images/store_' . $this->store_id . '/' . date('Y-m-d'); if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) { $resfilename = $response->saveAs($filePath, $filename); if (!empty($resfilename)) { $filePath = str_replace(\Yii::$app->basePath, \Yii::$app->request->hostInfo, $filePath . '/' . $filename); $store->qrcode = $filePath; $res = $store->save(); if ($res) { return [ 'code' => 0, 'msg' => "获取成功", 'data' => $filePath ]; } else { throw new \Exception("添加小程序码失败"); } } else { throw new \Exception("数据错误"); } } else { throw new \Exception("数据错误"); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 查询小程序 */ public function getMiniStatus() { $mini_info = StoreMini::find()->where(['appid' => $this->appid])->select("mini_url,mini_up,is_cancle,appid,status,msg")->asArray()->one(); return [ 'code' => 0, 'msg' => "获取成功", 'data' => $mini_info ]; } /** * 获取列表 */ public function getMiniList() { $store_id = $this->store_id; $query = StoreMini::find()->where(['store_id' => $store_id, 'is_cancle' => 0]) ->select("*"); if (!empty($this->mini_nickname)) { $query->andWhere(['LIKE', 'mini_nickname', $this->mini_nickname]); } if (isset($this->fuwu_type)) { $query->andWhere(['fuwu_type' => $this->fuwu_type]); } if (!empty($this->status)) { switch ($this->status) { case 1: $query->andWhere(['OR', ['mini_nickname' => ""], ['signature' => ""], ['mini_url' => ""]]); break; case 2: $query->andWhere(['AND', ['<>', 'mini_nickname', ""], ['<>', 'signature', ""], ['<>', 'mini_url', ""]]); break; case 3: $query->andWhere(['status' => 2]); break; case 4: $query->andWhere(['status' => -1]); break; } } if ($this->appid) { $query->andWhere(['LIKE', 'appid', $this->appid]); } if ($this->start_time) { $start_time = strtotime($this->start_time); $query->andWhere(['>=', 'created_at', $start_time]); } if ($this->end_time) { $end_time = strtotime($this->end_time); $query->andWhere(['<=', 'created_at', $end_time]); } if (!empty($this->is_use)) { $query->andWhere(['is_use' => $this->is_use]); } if (!empty($this->mini_nickname)) { $query->andWhere(['LIKE', 'mini_info', $this->mini_nickname]); } $query->orderBy('id desc'); $pagination = pagination_make($query); $admins = $pagination['list']; foreach ($admins as $index => &$item) { //判断是否认证 $item['is_wxauth'] = 1; if ($item['created_at'] > strtotime("2023-12-25") || empty($item['created_at'])) { $item['is_wxauth'] = 0; $store_mini_auth = StoreMiniAuth::findOne(['mini_id' => $item['id'], 'is_delete' => 0, 'apply_status' => 4]);//已通过 if ($store_mini_auth) { $item['is_wxauth'] = 1; } } $mini_info = json_decode($item['mini_info'], true); $item['principal_name'] = $mini_info['principal_name']; $item['nickname'] = $mini_info['nickname']; // $item['app_secret'] = ''; // if ($item['audit_id'] && (int)$item['mini_up'] !== 5) { // $res = $this->getAuditStatus($item['id']); // if ($res['code'] === 0) { // $item['mini_up'] = $res['mini_up']; // } // } $item['is_finish'] = 0; if (!empty($item['categories'])) { $item['categories'] = json_decode($item['categories'], true); } elseif (empty($item['categories']) && !empty($item['appid']) && empty($this->fuwu_type)) { $this->mini_id = $item['id']; $item['categories'] = $this->getCategories()['data']; } if (!empty($item['mini_info'])) { $item['mini_info'] = json_decode($item['mini_info'], true); } $item['qr'] = false; if (!empty($item['qrcode'])) { $item['qr'] = $item['qrcode']; } $item['user_version'] = cyy_version(); $item['qrcode'] = ''; $item['created_at'] = $item['created_at'] ? date("Y-m-d H:i:s", $item['created_at']) : ''; if (!empty($item['appid']) && empty($this->fuwu_type)) { // $res = $this->getQrcodeRules($item['appid']); // $item['qrcode_rules'] = $res; // \Yii::error($res); $miniInfo = $this->getMiniInfo("", $item['appid']); if ($miniInfo["code"] === 0) { \Yii::error($miniInfo); if (!empty($item['signature']) && !empty($item['mini_url']) && !empty($item['mini_nickname'] && !empty($item['categories']))) { $item['is_finish'] = 1; } } $res = $this->getMiniQrcode($item['id']); if ($res['code'] == 0) { $item['qrcode'] = $res['data']; } //20240816微信回调老是处理失败 增加主动查询的逻辑 $item['audit_result'] = $this->getAuditStatus($item['id']); } if (!empty($this->fuwu_type)) { $item['call_back_url'] = 'https://' . \Yii::$app->request->hostName . '/index.php/wechat/video-shop-order/callback/' . $item['id']; $item['token'] = Option::get('token', $store_id, 'video_shop_config_' . $item['id'], '')['value']; $item['encodingAesKey'] = Option::get('encodingAesKey', $store_id, 'video_shop_config_' . $item['id'], '')['value']; $item['finderUserName'] = Option::get('finderUserName', $store_id, 'video_shop_config_' . $item['id'], '')['value']; $WechatMini = WechatMini::getWechatConfig($this->store_id, $item['id'], 1); try { $client = new BaseClient($WechatMini); $res = $client->httpGet('channels/ec/basics/info/get'); if (!$res['errcode'] && !empty($res)) { $store_mini = StoreMini::findOne($item['id']); $item['mini_nickname'] = $store_mini->mini_nickname = $res['info']['nickname']; $item['mini_url'] = $store_mini->mini_url = $res['info']['headimg_url']; $store_mini->save(); } } catch (\Exception $e) { } } } return [ 'code' => 0, 'msg' => "获取成功", 'data' => $admins, 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'], 'isSaas' => \Yii::$app->isSaas() ]; } /** * 获取单个 */ public function getMini() { $query = StoreMini::find()->where(['id' => $this->mini_id]) ->select("id, name, code, code_type, legal_persona_wechat, legal_persona_name, status, msg, appid, mini_up, mini_url, is_cancle, mini_up_error, mini_info, apply_name_status, apply_name_error, is_use, signature, categories, license_pic, apply_mini_info, mini_nickname"); $data = $query->asArray()->one(); // try { // $this->get_privacy_interface(); // } catch (\Exception $e) { // // } if (!empty(json_decode($data['mini_info']))) { $data['principal_name'] = json_decode($data['mini_info'], true)['principal_name']; $data['nickname'] = json_decode($data['mini_info'], true)['nickname'] ?: $data['mini_nickname']; $data['mini_url'] = $data['mini_url'] ?? json_decode($data['mini_info'])->head_image_info->head_image_url; $data['apply_mini_info'] = $data['apply_mini_info'] ? json_decode($data['apply_mini_info'], true): []; $data['categories'] = []; if ($this->getCategories()['code'] === 0) { $data['categories'] = $this->getCategories()['data']; } //获取认证资料 $storeMiniAuth = StoreMiniAuth::findOne(['mini_id' => $this->mini_id]); // if ($result['apply_status'] < 2) { $data['auth_info'] = [ 'is_finish' => intval($storeMiniAuth->apply_status) === 4 ? 1 : 0, 'message' => intval($storeMiniAuth->task_status) !== -1 ? $storeMiniAuth->task_message : '未提交认证信息' ]; //获取备案资料 $storeMiniIcp = StoreMiniIcp::findOne(['mini_id' => $this->mini_id, 'is_cancel_icp' => 0]); // if ($result['apply_status'] < 2) { $is_finish = 0; if ($storeMiniIcp) {//人身核验是否通过 $is_finish = intval($storeMiniIcp->icp_audit_status) == 6 ? 1 : 0; if (intval($storeMiniIcp->icp_audit_status) >= 2) { $icp_message = StoreMiniIcp::$audit_status_text[$storeMiniIcp->icp_audit_status]; } else { $icp_message = StoreMiniAuthIcp::FACE_STATUS_NAME[intval($storeMiniIcp->face_status)]; } } else { $icp_message = '未开始人身核验'; } $data['icp_info'] = [ 'is_finish' => $is_finish, 'message' => $icp_message ]; //获取认证以及备案资料 $storeMiniAuthIcp = StoreMiniAuthIcp::findOne(['mini_id' => $this->mini_id]); $wechatThirdAuthAndIcpForm = new \app\modules\admin\models\WechatThirdAuthAndIcpForm(['store_id' => get_store_id(), 'mini_id' => $this->mini_id]); if ($storeMiniAuthIcp->task_id) { $wechatThirdAuthAndIcpForm->queryIcpVerifyTask(); } if ($storeMiniAuthIcp->procedure_id) { $wechatThirdAuthAndIcpForm->queryAuthAndIcp(); } //重新加载数据 $storeMiniAuthIcp = StoreMiniAuthIcp::findOne(['mini_id' => $this->mini_id]); $data['icp_auth_info'] = [ 'is_finish' => intval($storeMiniAuthIcp->procedure_status) === StoreMiniAuthIcp::PROCEDURE_STATUS_SUCCESS ? 1 : 0, 'message' => intval($storeMiniAuthIcp->face_is_finish) === StoreMiniAuthIcp::FACE_FINISH ? StoreMiniAuthIcp::PROCEDURE_STATUS_NAME[intval($storeMiniAuthIcp->procedure_status)] : StoreMiniAuthIcp::FACE_STATUS_NAME[intval($storeMiniAuthIcp->face_status)] ]; } return [ 'code' => 0, 'msg' => "获取成功", 'data' => $data ]; } public static function getUsedStoreMini($store_id) { $mini = StoreMini::find()->where(['store_id' => $store_id, 'is_cancle' => 0, 'is_use' => 1, 'fuwu_type' => 0])->one(); return $mini; } public static function getStoreMiniSelectList($store_id) { $list = StoreMini::find()->where(['store_id' => $store_id, 'is_cancle' => 0, 'fuwu_type' => 0])->select('id, mini_nickname, mini_url, is_use')->asArray()->all(); foreach($list as &$item){ empty($item['mini_nickname']) && $item['mini_nickname'] = '--'; } return [ 'code' => 0, 'msg' => "获取成功", 'data' => $list, ]; } public static function checkOrBindOpen($store_id) { $mini = self::getUsedStoreMini($store_id); if(!$mini){ return [ 'code' => 1, 'msg' => '没有使用中的小程序,请到小程序管理列表查看', ]; } $form = new self(); $form->mini_id = $mini->id; $openGet = $form->openGet(); if($openGet['code'] == 0){ return $openGet; } $openCreate = $form->openCreate(); return $openCreate; } public function openGet() { try { $mini = StoreMini::findOne($this->mini_id); $client = new BaseClient($this->miniProgram($mini->appid)); $data = [ 'appid' => $mini->appid, ]; $res = $client->httpPostJson('cgi-bin/open/get', $data); \Yii::error($res); if ((empty($res['errcode']) && !empty($res))) { return [ 'code' => 0, 'msg' => "成功", 'data' => $res, ]; } elseif (empty($res)) { throw new \Exception("数据错误"); } else { throw new \Exception($this->getZnMsg($res)); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function openCreate() { try { $mini = StoreMini::findOne($this->mini_id); $client = new BaseClient($this->miniProgram($mini->appid)); $data = [ 'appid' => $mini->appid, ]; $res = $client->httpPostJson('cgi-bin/open/create', $data); \Yii::error($res); if ((empty($res['errcode']) && !empty($res))) { return [ 'code' => 0, 'msg' => "成功", 'data' => $res, ]; } elseif (empty($res)) { throw new \Exception("数据错误"); } else { throw new \Exception($this->getZnMsg($res)); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function openBind($open_appid) { try { $mini = StoreMini::findOne($this->mini_id); $client = new BaseClient($this->miniProgram($mini->appid)); $data = [ 'appid' => $mini->appid, 'open_appid' => $open_appid, ]; $res = $client->httpPostJson('cgi-bin/open/bind', $data); \Yii::error($res); if ((empty($res['errcode']) && !empty($res))) { return [ 'code' => 0, 'msg' => "成功", 'data' => $res, 'open_appid' => $open_appid, ]; } elseif (empty($res)) { throw new \Exception("数据错误"); } else { throw new \Exception($this->getZnMsg($res)); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function openUnbind() { try { $get = $this->openGet(); if($get['code'] == 0 && $get['data']['open_appid']){ $open_appid = $get['data']['open_appid']; }else{ return $get; } $mini = StoreMini::findOne($this->mini_id); $client = new BaseClient($this->miniProgram($mini->appid)); $data = [ 'appid' => $mini->appid, 'open_appid' => $open_appid, ]; $res = $client->httpPostJson('cgi-bin/open/unbind', $data); \Yii::error($res); if ((empty($res['errcode']) && !empty($res))) { return [ 'code' => 0, 'msg' => "成功", 'data' => $res, 'open_appid' => $open_appid, ]; } elseif (empty($res)) { throw new \Exception("数据错误"); } else { throw new \Exception($this->getZnMsg($res)); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //添加二维码规则 public function setQrcodeRules($id, $appid) { try { $qrcode = AggregateQrcode::find()->where(['wx_mini_id' => $id])->asArray()->one(); $data = [ 'prefix' => $qrcode['param_url'], 'permit_sub_rule' => 1, 'path' => $qrcode['wx_url'], 'open_version' => 2, 'debug_url' => [ $qrcode['param_url'] ], 'is_edit' => 0 ]; \Yii::error($data); if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $client = new BaseClient($this->miniProgram($appid)); $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpadd', $data); \Yii::error($res); if ((empty($res['errcode']) && !empty($res)) || ($res['errcode'] == "85071")) { return [ 'code' => 0, 'msg' => "成功", 'data' => $res ]; } elseif (empty($res)) { throw new \Exception("数据错误"); } else { throw new \Exception($this->getZnMsg($res)); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //发布二维码规则 public function submitQrcodeRules($id, $appid) { try { $qrcode = AggregateQrcode::find()->where(['wx_mini_id' => $id])->one(); if (empty($qrcode->param_url)) { return [ 'code' => 1, 'msg' => '参数错误' ]; } $data = [ 'prefix' => $qrcode->param_url ]; if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $client = new BaseClient($this->miniProgram($appid)); $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumppublish', $data); if (empty($res->errcode) && !empty($res)) { $qrcode->wx_status = 1; $res = $qrcode->save(); if (!$res) { throw new \Exception('保存数据失败,二维码规则发布生成'); } return [ 'code' => 0, 'msg' => "成功", 'data' => $res ]; } elseif (empty($res)) { return [ 'code' => 1, 'msg' => "数据错误" ]; } else { $res = json_decode($res, true); return [ 'code' => $res['errcode'], 'msg' => $this->getZnMsg($res), 'data' => $res ]; } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //获取二维码规则 public function getQrcodeRules($appid) { try { if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $client = new BaseClient($this->miniProgram($appid)); $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpget'); if (empty($res->errcode) && !empty($res)) { return [ 'code' => 0, 'msg' => "成功", 'data' => $res ]; } elseif (empty($res)) { return [ 'code' => 1, 'msg' => "数据错误" ]; } else { $res = json_decode($res, true); return [ 'code' => $res['errcode'], 'msg' => $this->getZnMsg($res), 'data' => $res ]; } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //下载文件 public function downQrcodeRules($id, $appid) { try { if (!$this->openPlatform) { throw new \Exception("参数配置错误"); } $client = new BaseClient($this->miniProgram($appid)); $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpdownload'); if (empty($res['errcode']) && !empty($res)) { if (!is_dir(\Yii::$app->basePath . '/web/face/pay')) { // dir doesn't exist, make it mkdir(\Yii::$app->basePath . '/web/face/pay', 0777, true); } $file_result = file_put_contents(\Yii::$app->basePath . '/web/face/pay/' . $res['file_name'], $res['file_content']); if ($file_result) { $resRules = $this->setQrcodeRules($id, $appid); if ($resRules['code'] === 0) { $ress = $this->submitQrcodeRules($id, $appid); \Yii::error($ress); if ($ress['code'] === 0) { $resGet = $this->getQrcodeRules($appid); \Yii::error($resGet); } else { throw new \Exception($ress['msg']); } } else { throw new \Exception($resRules['msg']); } } else { throw new \Exception("创建文件失败"); } return [ 'code' => 0, 'msg' => "成功", 'data' => $res, 'file' => $file_result ]; } elseif (empty($res)) { throw new \Exception("数据错误"); } else { $res = json_decode($res, true); throw new \Exception($res['errmsg']); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //删除二维码规则 public function delQrcodeRules() { try { $store = StoreMini::findOne($this->mini_id); if ($store->appid) { //查询所有普通二维码 $res = $this->getQrcodeRules($store->appid); if ($res['code'] !== 0) { return $res; } $rules_list = $res['data']['rule_list']; if (!empty($rules_list)) { //全部删除 foreach ($rules_list as $item) { $data = [ 'prefix' => $item['prefix'] ]; $client = new BaseClient($this->miniProgram($store->appid)); $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpdelete', $data); if (empty($res)) { return [ 'code' => 1, 'msg' => "数据错误" ]; } elseif (!empty($res->errcode)) { $res = json_decode($res, true); return [ 'code' => $res['errcode'], 'msg' => $this->getZnMsg($res), 'data' => $res ]; } } } //将商城显示解除绑定 $store->bind_food_qr = 0; if (!$store->save()) { throw new \Exception(json_encode($store->errors)); } Option::set('store_home_is_bind_wx', 0, get_store_id(), 'store'); $store_qrcode = AggregateQrcode::findOne(['store_id' => get_store_id()]); if ($store_qrcode) { $store_qrcode->wx_status = 0; $store_qrcode->wx_mini_id = 0; if (!$store_qrcode->save()) { throw new \Exception(json_encode($store_qrcode->errors)); } } return [ 'code' => 0, 'msg' => '解除成功' ]; } throw new \Exception('没有绑定小程序'); } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //微信支付进件 public function mchRegister() { $mini = StoreMini::find()->where(['id' => $this->mini_id])->select('id,appid')->one(); if (empty($mini->appid)) { return [ 'code' => 0, 'msg' => "未找到对应的小程序信息" ]; } $merchant_info = MerchantInfo::find()->where(['bind_store_id' => get_store_id(), 'is_delete' => 0])->one(); if (empty($merchant_info)) { // $StoreSchedule = StoreSchedule::find()->where(['store_id' => get_store_id()])->one(); // if (!empty($StoreSchedule->merchant_info)) { // $merchant_info = json_decode($StoreSchedule->merchant_info, true); // $contact_info = $merchant_info['contact_info']; // $subject_info = $merchant_info['subject_info']; // $business_info = $merchant_info['business_info']; // $bank_account_info = $merchant_info['bank_account_info']; // } else { // return [ // 'code' => 1, // 'msg' => "未找到对应的进件数据" // ]; // } return [ 'code' => 1, 'msg' => "未找到对应的进件数据" ]; } else { $contact_info = !empty($merchant_info->contact_info) ? json_decode($merchant_info->contact_info, true) : ""; $subject_info = !empty($merchant_info->subject_info) ? json_decode($merchant_info->subject_info, true) : ""; $business_info = !empty($merchant_info->business_info) ? json_decode($merchant_info->business_info, true) : ""; $bank_account_info = !empty($merchant_info->bank_account_info) ? json_decode($merchant_info->bank_account_info, true) : ""; } $Merchant = new Merchant(); return $Merchant->submit($contact_info, $subject_info, $business_info, $bank_account_info, !empty($merchant_info->id) ? $merchant_info->id : 0, $mini->appid); } public function servicemarket_service_login_auth($code) { try { $client = new BaseClient($this->openPlatform); $postData = [ 'code' => $code, ]; $res = $client->httpPostJson('wxa/servicemarket/service/login_auth', $postData); \Yii::error([$res, json_encode($postData)]); // var_dump($res);die; // $res = [ // 'order_id' => 3118683324757311491, // 'openid' => '', // 'appid' => 'wxedd15801cce16326', // 'service_id' => 3118677029492637699, // 'sku_id' => '4711132', // 'spec_id' => 'free', // 'errcode' => 0, // 'errmsg' => 'ok', // ]; \Yii::error([$res, json_encode($postData)]); if (empty($res['errcode']) && !empty($res)) { return [ 'code' => 0, 'msg' => "成功", 'data' => $res, ]; } else { throw new \Exception($res['errmsg'] ?? ''); } } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //服务市场appid实例化 public function fuwu_app() { try { $appid = Option::get('platform_wechat_fuwu_appid', 0, 'saas', '')['value']; $secret = Option::get('platform_wechat_fuwu_secret', 0, 'saas', '')['value']; if(empty($appid) || empty($secret)){ throw new \Exception("平台后台未配置服务市场信息"); } $config =[]; $config['app_id'] = $appid; $config['secret'] = $secret; $config['response_type'] = 'array'; $app = Factory::officialAccount($config); return [ 'code' => 0, 'data' => $app, ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //服务市场token获取 public function fuwu_token() { try { $fuwu_app = $this->fuwu_app(); if($fuwu_app['code'] != 0){ throw new \Exception($fuwu_app['msg']); } $app = $fuwu_app['data']; $accessToken = $app->access_token; $token = $accessToken->getToken(); if(empty($token['access_token'])){ throw new \Exception("服务市场token信息获取失败"); } return [ 'code' => 0, 'data' => $token['access_token'], ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function servicemarket_getwxaqrcode($service_id = '', $custom_params = ['a' => 1]) { try { $fuwu_app = $this->fuwu_app(); if($fuwu_app['code'] != 0){ throw new \Exception($fuwu_app['msg']); } $app = $fuwu_app['data']; $client = new BaseClient($app); $postData = [ 'service_id' => (string)$service_id, 'custom_params' => http_build_query($custom_params), ]; $res = $client->httpPostJson('wxa/servicemarket/getwxaqrcode', $postData); // var_dump($res);die; \Yii::error([$res, json_encode($postData)]); if (empty($res['errcode']) && !empty($res)) { return [ 'code' => 0, 'msg' => "成功", 'service_id' => $service_id, 'data' => $res, ]; } else { throw new \Exception($res['errmsg'] ?? ''); } } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage(), 'service_id' => $service_id, ]; } } public function servicemarket_get_paid_order_list($appid = '', $service_id = '') { try { $fuwu_app = $this->fuwu_app(); if($fuwu_app['code'] != 0){ throw new \Exception($fuwu_app['msg']); } $app = $fuwu_app['data']; $client = new BaseClient($app); $postData = [ 'appid' => $appid, 'service_id' => $service_id, 'offset' => 0, 'limit' => 10, 'buyer_type' => 1, ]; $res = $client->httpPostJson('wxa/servicemarket/get_paid_order_list', $postData); // $res = [ // 'errcode' => 0, // 'errmsg' => 'ok', // 'order_list' => [ // [ // 'order_id' => 3138343822027653126, // 'effective_time' => 1696652147, // 'expire_time' => 1728188147, // 'specification_id' => 'new', // 'service_id' => 3118677029492637699, // 'total_price' => 100, // 'phone' => '', // 'sku_id' => 3144177, // ], // [ // 'order_id' => 3118683324757311491, // 'effective_time' => 1695480283, // 'expire_time' => 1696085083, // 'specification_id' => 'free', // 'service_id' => 3118677029492637699, // 'total_price' => 0, // 'phone' => '', // 'sku_id' => 4711132, // ], // ], // 'total' => 2, // ]; \Yii::error([$res, json_encode($postData)]); // var_dump($res, $postData);die; if (empty($res['errcode']) && !empty($res)) { return [ 'code' => 0, 'msg' => "成功", 'data' => $res, ]; } else { throw new \Exception($res['errmsg'] ?? ''); } } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function servicemarket_service_get_service_buyer_list($service_id = '') { try { $fuwu_app = $this->fuwu_app(); if($fuwu_app['code'] != 0){ throw new \Exception($fuwu_app['msg']); } $app = $fuwu_app['data']; $client = new BaseClient($app); $postData = [ 'service_id' => $service_id, 'offset' => 0, 'limit' => 10, 'buyer_type' => 1, ]; $res = $client->httpPostJson('wxa/servicemarket/service/get_service_buyer_list', $postData); // $res = [ // 'errcode' => 0, // 'errmsg' => 'ok', // 'buyer_list' => [ // [ // 'appid' => 'wxedd15801cce16326', // 'service_id' => 3118677029492637699, // 'spec_list' => [ // [ // 'specification_id' => 'free', // 'expire_time' => 1696085083, // ], // [ // 'specification_id' => 'new', // 'expire_time' => 1728188147, // ], // ], // ], // ], // 'count' => 1, // ]; \Yii::error([$res, json_encode($postData)]); // var_dump($res, $postData);die; if (empty($res['errcode']) && !empty($res)) { return [ 'code' => 0, 'msg' => "成功", 'data' => $res, ]; } else { throw new \Exception($res['errmsg'] ?? ''); } } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function servicemarket_service_get_service_buyer($appid = '', $service_id = '') { try { $fuwu_app = $this->fuwu_app(); if($fuwu_app['code'] != 0){ throw new \Exception($fuwu_app['msg']); } $app = $fuwu_app['data']; $client = new BaseClient($app); $postData = [ 'appid' => $appid, 'service_id' => $service_id, ]; $res = $client->httpPostJson('wxa/servicemarket/service/get_service_buyer', $postData); // $res = [ // 'errcode' => 0, // 'errmsg' => 'ok', // 'buyer' => [ // 'appid' => 'wxedd15801cce16326', // 'service_id' => 3118677029492637699, // 'spec_list' => [ // [ // 'specification_id' => 'free', // 'expire_time' => 1696085083, // ], // [ // 'specification_id' => 'new', // 'expire_time' => 1728188147, // ], // ], // ], // ]; \Yii::error([$res, json_encode($postData)]); // var_dump($res, $postData);die; if (empty($res['errcode']) && !empty($res)) { return [ 'code' => 0, 'msg' => "成功", 'data' => $res, ]; } else { throw new \Exception($res['errmsg'] ?? ''); } } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //从服务市场登陆 public function servicemarketAuthCode($code = '', $fuwu_type = 1) { try { $auth = $this->servicemarket_service_login_auth($code); if($auth['code'] != 0){ throw new \Exception($auth['msg']); } // return $this->servicemarket_getwxaqrcode($auth['data']['service_id']); // return $this->servicemarket_get_paid_order_list($auth['data']['appid'], $auth['data']['service_id']); // return $this->servicemarket_service_get_service_buyer_list($auth['data']['service_id']); // return $this->servicemarket_service_get_service_buyer($auth['data']['appid'], $auth['data']['service_id']); return $this->servicemarketAuth($auth['data']['appid'], $auth['data']['service_id'], $fuwu_type); } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //从服务市场登陆时(认证、初始化小程序) public function servicemarketAuth($appid, $service_id = '', $fuwu_type = 1) { try { $service_ids = explode(',', Option::get('platform_wechat_fuwu_serviceId', 0, 'saas', '')['value']); if(!in_array($service_id, $service_ids)){ throw new \Exception("服务信息未配置"); } $mini = StoreMini::findOne(['appid' => $appid]); if(!$mini){ $mini = new StoreMini(); } //初始化小程序 $mini->fuwu_type = $fuwu_type; $mini->appid = $appid; $mini->is_cancle = 0; $hasAuth = 0; $res = $this->openPlatform->getAuthorizer($appid); if (empty($res['errcode']) && !empty($res)) { $hasAuth = 1; $mini->authorizer_refresh_token = $res['authorization_info']['authorizer_refresh_token']; }else{ debug_log([__METHOD__, __LINE__, $res], __CLASS__ . '.log'); throw new \Exception($res['errmsg']); } $mini->save(); if($hasAuth){ //如果授权过三方,填充小程序信息 $miniProgram = $this->openPlatform->miniProgram($mini->appid, $mini->authorizer_refresh_token); $client = new BaseClient($miniProgram); $res = $client->httpGet('channels/ec/basics/info/get'); // var_dump($res);die; if (empty($res['errcode']) && !empty($res)) { $mini->mini_nickname = $res['info']['nickname']; $mini->mini_url = $res['info']['headimg_url']; if(!$mini->save()){ debug_log([__METHOD__, __LINE__, $mini->getFirstErrors()], __CLASS__ . '.log'); } } } if($mini->fuwu_type == StoreMini::FUWU_TYPE_VIDEO_SHOP && $hasAuth && $mini->mini_nickname){ $buyer = $this->servicemarket_service_get_service_buyer($appid, $service_id); if($buyer['code'] != 0){ debug_log([__METHOD__, __LINE__, $buyer], __CLASS__ . '.log'); throw new \Exception($buyer['msg']); } $end_time = 0; if(isset($buyer['data']['buyer']['spec_list'])){ foreach($buyer['data']['buyer']['spec_list'] as $item){ if($item['expire_time'] > $end_time){ $end_time = $item['expire_time']; } } } // var_dump($end_time);die; if(!$mini->store_id){ //初始化店铺 if($end_time < time()){ $end_time = time() + 86400 * 7; } $storeForm = new StoreForm(); $username = $mini->mini_nickname . time(); $add = $storeForm->add($username, '', $username, [], 0, $mini->mini_url, $end_time); if($add['code'] != 0){ debug_log([__METHOD__, __LINE__, $add['msg']], __CLASS__ . '.log'); }else{ $store = $add['store']; $mini->store_id = $store['id']; if(!$mini->save()){ debug_log([__METHOD__, __LINE__, $mini->getFirstErrors()], __CLASS__ . '.log'); } } }else{ //更新店铺到期时间 $store = Store::findOne($mini->store_id); if($end_time > time() && $store->end_time < $end_time){ $store->end_time = $end_time; $store->save(); } } } // var_dump($store->attributes, $mini->attributes);die; return [ 'code' => 0, 'msg' => "成功", 'data' => [ 'mini' => $mini, 'store' => $store ?? null, ], ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //下载文件 public function getWebviewFile() { $mini_id = $this->mini_id; $store_mini = StoreMini::findOne($mini_id); if (empty($store_mini)) { return [ 'code' => 1, 'msg' => '查询数据失败' ]; } if (!$this->openPlatform) { return [ 'code' => 1, 'msg' => '参数配置错误' ]; } $miniProgram = $this->miniProgram($store_mini->appid); $result = $this->addConfirmFile($miniProgram); if ($result['code'] === 1) { return $result; } return $this->addFile2Other($result['data']); } //设置业务域名 (三方平台修改小程序业务域名) public function setWebviewDomain() { $mini_id = $this->mini_id; $store_mini = StoreMini::findOne($mini_id); if (empty($store_mini)) { return [ 'code' => 1, 'msg' => '查询数据失败' ]; } if (\Yii::$app->request->isPost) { $webview_domain_name_ = post_params('webview_domain_name'); $webview_domain_name_ = json_decode($webview_domain_name_, true); $webview_domain_name = []; foreach ($webview_domain_name_ as $item) { if (!empty($item['url'])) { $index = strstr($item['url'], 'http'); if (!$index) { $item['url'] = 'https://' . $item['url']; } $item['url'] = \str_replace('http://', 'https://', $item['url']); $webview_domain_name = array_merge($webview_domain_name, [[ 'url' => $item['url'] ]]); } } $store_mini->webview_domain_name = json_encode($webview_domain_name); if (!$store_mini->save()) { return [ 'code' => 1, 'msg' => implode(';', array_values($store_mini->firstErrors)) ]; }; return $this->setDomainName(); } else { $data = [ 'init_webview_domain_name' => [ // [ // 'url' => 'https://' . \Yii::$app->params['ws_url'], // ] ], 'webview_domain_name' => [ [ 'url' => '' ] ] ]; if (!empty($store_mini->webview_domain_name)) { $webview_domain_name = json_decode($store_mini->webview_domain_name, true); if (!empty($webview_domain_name)) { $data['webview_domain_name'] = $webview_domain_name; } } return [ 'code' => 0, 'msg' => '获取成功', 'data' => $data ]; } } //获取小程序短链$app->short_link->getShortLink public function getShortLink($path = '', $mini_id = 0, $is_platform = 0, $is_serve = 0) { try { $app = WechatMini::getWechatConfig($this->store_id, $mini_id, 0, $is_platform, $is_serve); if (empty($app)) { throw new \Exception(''); } if (empty($path)) { throw new \Exception("页面路径不能为空"); } //获取接口缓存 // $app_id = $app->getConfig()['app_id']; // $cache_key = md5($this->store_id . '_'. $app_id . '_' . $path . '_short_link_'); // if ($cache_key) { // return [ // 'code' => 0, // 'msg' => "设置成功", // 'data' => $cache_key // ]; // } $result = $app->short_link->getShortLink($path, ''); if (empty($result['errcode']) && !empty($result)) { if (empty($result['link'])) { throw new \Exception("参数获取失败"); } //增加接口缓存 // cache()->set($cache_key, $result, 60); return [ 'code' => 0, 'msg' => "设置成功", 'data' => $result ]; } elseif (empty($result)) { throw new \Exception("数据错误"); } else { throw new \Exception($this->getZnMsg($result) ); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //服务市场服务二维码批量获取 public function serviceQrcode($service_ids = '') { if($service_ids && !is_array($service_ids)){ $service_ids = explode(',', $service_ids); } $ret = []; foreach((array)$service_ids as $id){ $qr = $this->servicemarket_getwxaqrcode($id); $ret[] = $qr; } return $ret; } //获取网络图片到临时目录 private function saveTempImage($url) { if (strpos($url, 'http') === false) { $url = 'http:' . trim($url); } if (!is_dir(\Yii::$app->runtimePath . '/image')) { mkdir(\Yii::$app->runtimePath . '/image'); } $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg'; CurlHelper::download($url, $save_path); return $save_path; } //返回中文错误信息 public function getZnMsg($result) { \Yii::error([__METHOD__, $result]); $ErrorMsg = new ErrorMsg(); $arr = $ErrorMsg->getArray(); $msg = !empty($arr[$result['errcode']]) ? $arr[$result['errcode']] : $result['errmsg']; return $msg; } //返回中文错误信息 public static function staticGetZnMsg($result) { \Yii::error([__METHOD__, $result]); $ErrorMsg = new ErrorMsg(); $arr = $ErrorMsg->getArray(); $msg = !empty($arr[$result['errcode']]) ? $arr[$result['errcode']] : $result['errmsg']; return $msg; } }