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->store_id = !empty($this->store_id) ? $this->store_id : get_store_id(); $this->openPlatform = Factory::openPlatform($this->config); } /** * 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("当前没有绑定小程序信息"); } $res = $this->openPlatform->getAuthorizer($store->appid); if (empty($res->errcode) && !empty($res)) { $store = StoreMini::find()->where(['store_id' => $this->store_id])->one(); $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() { $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; } $store = StoreMini::findOne(['store_id' => $store_id]); if (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; $res = $this->openPlatform->component->registerMiniProgram($data); if ($res['errcode'] == '89249') { $res = $this->openPlatform->component->getRegistrationStatus($this->name, $this->legal_persona_wechat, $this->legal_persona_name); if ($res['errcode'] == 89251) { $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() { if (!empty($this->bind_store_id)) { $store_id = $this->bind_store_id; } else { $store_id = $this->store_id; } $store = StoreMini::findOne(['store_id' => $store_id]); if (!empty($store->name) && !empty($store->legal_persona_wechat) && !empty($store->legal_persona_name)) { $res = $this->openPlatform->component->getRegistrationStatus($store->name, $store->legal_persona_wechat, $store->legal_persona_name); \Yii::error($res); } } /** * 返回授权需要的参数 */ public function backAuthInfo() { $res = $this->openPlatform->getPreAuthorizationUrl(\Yii::$app->request->hostInfo . '/admin/#/saasChannel/applet', ['auth_type' => 2]); return [ 'code' => 0, 'msg' => "请访问链接依次来授权", 'url' => $res ]; } /** * 存储授权码 */ // 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 getAuthorization_info($authorization_code) { try { $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->appid = $res['authorization_info']['authorizer_appid']; $store->store_id = $store_id; } $store->is_cancle = 0; $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)) { $authorizer_access_token = $store->authorizer_access_token; if (empty($authorizer_access_token)) { throw new \Exception("暂未授权"); } } $miniProgram = $this->miniProgram($appid); $res = $miniProgram->account->getBasicInfo(); if (empty($res['errcode']) && !empty($res)) { $store->mini_info = json_encode($res); $store->mini_nickname = $res['nickname']; $store->signature = $res['signature_info']['signature']; $store->mini_url = $res['head_image_info']['head_image_url']; $res = $store->save(); if ($res) { 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填充系统设置 $WechatConfig = WechatConfig::find()->where(['store_id' => $store_id])->one(); if (empty($WechatConfig)) { $WechatConfig = new WechatConfig(); $WechatConfig->store_id = $store_id; } $WechatConfig->app_id = $store->appid; $WechatConfig->app_secret = "0"; $WechatConfig->name = $store->name ?: ""; $WechatConfig->updated_at = time(); if ($WechatConfig->save()) { return [ 'code' => 0, 'msg' => "获取成功", 'data' => $res, 'appid' => $appid ]; } else { throw new \Exception(json_encode($WechatConfig->errors)); } } 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("数据错误,获取小程序信息失败"); } $DomainData = ""; $res = $this->setWebviewDomainName(); if ($res['code'] == 1) { throw new \Exception($res['msg']); } $DomainData = $res; $miniProgram = $this->miniProgram($store->appid); $data = [ 'action' => 'set', 'requestdomain' => ["https://" . \Yii::$app->request->hostName], "wsrequestdomain" => ["wss://" . \Yii::$app->request->hostName], "uploaddomain" => ["https://" . \Yii::$app->request->hostName], "downloaddomain" => ["https://" . \Yii::$app->request->hostName], "udpdomain" => ["udp://" . \Yii::$app->request->hostName], "tcpdomain" => ["tcp://" . \Yii::$app->request->hostName] ]; $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(), ]; } } /** * @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("数据错误,获取小程序信息失败"); } $miniProgram = $this->miniProgram($store->appid); $res = $miniProgram->domain->setWebviewDomain([ 'https://' . \Yii::$app->request->hostName, 'https://' . \Yii::$app->params['ws_url'], ], '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) { $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("数据错误,获取小程序信息失败"); } $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, ]; // $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'] ]); $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 ]; } $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() { 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' => "数据错误" ]; } $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 ]; } } /** * 修改头像 */ 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("数据错误,获取小程序信息失败"); } $miniProgram = $this->miniProgram($store->appid); $media = new Client($miniProgram); $head_img_media_id = $media->uploadThumb($this->saveTempImage($this->head_img_media_id)); if (empty($head_img_media_id['thumb_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['thumb_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("数据错误,获取小程序信息错误"); } $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() { 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' => "数据错误" ]; } $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 ]; } } /** * @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("数据错误,查询小程序信息失败"); } $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("数据错误,查找不到小程序信息"); } $miniProgram = $this->miniProgram($store->appid); $data = $this->categories; $media = new Client($miniProgram); \Yii::error($data); foreach ($data as $index => &$item) { if(!empty($item['categories'])){ foreach ($item['categories'] as $cateIndex => &$cate) { $url = $this->saveTempImage($cate['value']); $cate['value'] = $media->uploadImage($url)['media_id']; } } } $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() ]; } } /** * 删除类目 */ 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("数据错误"); } $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() { $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_ASC, $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 ]; } } /** * 上传小程序 */ public function upMini() { $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("数据错误,获取不到小程序信息"); } $miniProgram = $this->miniProgram($store->appid); $ext = [ 'extEnable' => true, 'extAppid' => $store->appid, 'ext' => [ "apiurl" => "https://" . \Yii::$app->request->hostName, "store_id" => $store->store_id, "mini_id" => $store->id ], "window" => [ "navigationBarTitleText" => "0000" ] ]; $template_id = Option::get("platform_template_id", 0, 'saas')['value']; if ($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(); return [ 'code' => 1, 'msg' => $e->getMessage(), ]; } } /** * 获取已上传的代码的页面列表 */ public function getPageList() { $store = StoreMini::findOne($this->mini_id); if (empty($store)) { return [ 'code' => 1, 'msg' => "数据错误" ]; } $miniProgram = $this->miniProgram($store->appid); $res = $miniProgram->code->getPage(); 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 ]; } } /** * 添加体验者 * @return array * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException * @throws \GuzzleHttp\Exception\GuzzleException */ public function addTester() { $store = StoreMini::findOne($this->mini_id); if (empty($store)) { return [ 'code' => 1, 'msg' => "数据错误" ]; } $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 ]; } } /** * 获取体验版二维码 */ public function getMiniQrcode($id = 0) { 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' => "数据错误" ]; } $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)->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); return [ 'code' => $res->errcode, 'msg' => $this->getZnMsg($res), 'data' => $res ]; } } /** * 提交审核 */ 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("数据错误,未找到对应的小程序信息"); } $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->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(); 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("数据错误"); } $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 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("数据错误,未查询到小程序信息"); } $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("数据错误,获取不到小程序信息"); } $miniProgram = $this->miniProgram($store->appid); $this->unbind(); $res = $miniProgram->code->release(); if ((empty($res['errcode']) && !empty($res)) || $res['errcode'] == "85052") { $store->mini_up = 5; $res = $this->getMiniQr($store->id); if ($res['code'] === 0) { if ($store->save()) { $t->commit(); 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" => "订单评价拍照" ] ], ]; 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)); $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) ]; } } /** * 查询小程序用户隐私保护指引 */ public function getPrivacySetting() { $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); } $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 ]; } } /** * 查询小程序码 */ public function getMiniQr($id = 0) { try { if (empty($id)) { $id = $this->mini_id; } $store = StoreMini::findOne($id); $miniProgram = $this->miniProgram($store->appid); $response = $miniProgram->app_code->getUnlimit("store=" . $this->store_id); $filename = md5(time()) . '.jpg'; $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("id,mini_url,mini_up,is_cancle,appid,status,msg,mini_info,is_use,mini_up_error,categories,mini_nickname,signature,qrcode"); if (!empty($this->mini_nickname)) { $query->andWhere(['LIKE', 'mini_nickname', $this->mini_nickname]); } 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 (!empty($this->is_use)) { $query->andWhere(['is_use' => $this->is_use]); } if (!empty($this->mini_nickname)) { $query->andWhere(['LIKE', 'mini_info', $this->mini_nickname]); } $pagination = pagination_make($query); $admins = $pagination['list']; foreach ($admins as $index => &$item) { \Yii::error($item); $item['principal_name'] = json_decode($item['mini_info'])->principal_name; $item['nickname'] = json_decode($item['mini_info'])->nickname; $item['is_finish'] = 0; if (!empty($item['categories'])) { $item['categories'] = json_decode($item['categories']); } elseif (empty($item['categories']) && !empty($item['appid'])) { $this->mini_id = $item['id']; $item['categories'] = $this->getCategories()['data']; } if (!empty($item['mini_info'])) { $item['mini_info'] = json_decode($item['mini_info']); } $item['qr'] = false; if (!empty($item['qrcode'])) { $item['qr'] = $item['qrcode']; } $item['user_version'] = cyy_version(); $item['qrcode'] = ''; if (!empty($item['appid'])) { $res = $this->getQrcodeRules($item['appid']); \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'])) { $item['is_finish'] = 1; } } $res = $this->getMiniQrcode($item['id']); if ($res['code'] == 0) { $item['qrcode'] = $res['data']; } } } return [ 'code' => 0, 'msg' => "获取成功", 'data' => $admins, 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'], ]; } /** * 获取单个 */ 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,"); $data = $query->asArray()->one(); if (!empty(json_decode($data['mini_info']))) { $data['principal_name'] = json_decode($data['mini_info'])->principal_name; $data['nickname'] = json_decode($data['mini_info'])->nickname; $data['mini_url'] = $data['mini_url'] ?? json_decode($data['mini_info'])->head_image_info->head_image_url; $data['categories'] = []; if ($this->getCategories()['code'] === 0) { $data['categories'] = $this->getCategories()['data']; } } return [ 'code' => 0, 'msg' => "获取成功", 'data' => $data ]; } //添加二维码规则 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); $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) { $qrcode = AggregateQrcode::find()->where(['wx_mini_id' => $id])->one(); if (empty($qrcode->param_url)) { return [ 'code' => 1, 'msg' => '参数错误' ]; } $data = [ 'prefix' => $qrcode->param_url ]; $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); return [ 'code' => $res['errcode'], 'msg' => $this->getZnMsg($res), 'data' => $res ]; } } //获取二维码规则 public function getQrcodeRules($appid) { $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); return [ 'code' => $res['errcode'], 'msg' => $this->getZnMsg($res), 'data' => $res ]; } } //下载文件 public function downQrcodeRules($id, $appid) { try { $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); throw new \Exception($res['errmsg']); } } 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' => "未找到对应的进件数据" ]; } } 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); } //获取网络图片到临时目录 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) { $ErrorMsg = new ErrorMsg(); $arr = $ErrorMsg->getArray(); $msg = !empty($arr[$result['errcode']]) ? $arr[$result['errcode']] : $result['errmsg']; return $msg; } }