store_id; //小程序ID $mini_id = $this->mini_id; //获取配置信息 $form = new BaseForm(); $form->store_id = $store_id; $form->mini_id = $mini_id; //获取easyWechat配置信息 $this->openPlatform = $form->openPlatform; //判断商城ID 或 小程序ID是否不为空,不为空则获取请求微信的配置信息 if (!empty($store_id) || !empty($mini_id)) { //获取请求微信的配置信息 $result = $form->miniProgram(); //获取小程序信息 $this->store_mini = StoreMini::find()->where(['or', ['id' => $mini_id], ['store_id' => $store_id]])->andWhere(['is_cancle' => 0])->orderBy('id desc')->one(); if ($result['code'] === 1) { return $result; } $this->miniProgram = $result; } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //修改小程序名称 public function setMiniName() { try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } //判断小程序是否存在 $store_mini = $this->store_mini; if (empty($store_mini)) { throw new \Exception("查找不到小程序信息"); } //获取接口配置信息 $miniProgram = $this->miniProgram; $media = new Client($miniProgram); //判断是get方式请求 // if (\Yii::$app->request->isGet) { // //营业执照信息 // $license_pic = $store_mini->license_pic ?: ""; // $name_data['license'] = $license_pic; // // //名称审核资料 // $name_data = $store_mini->nickname_audit_data; // $name_data = json_decode($name_data, true); // // //小程序名称 // $nickname = $store_mini->mini_nickname; // // return [ // 'code' => 0, // 'msg' => "获取成功", // 'data' => [ // 'mini_info' => [ // 'nickname' => $nickname // ], // 'name_data' => $name_data // ] // ]; // // } //获取请求信息 //post方式请求 //获取小程序名称 $mini_info = $this->mini_info; //获取名称审核资料 $name_data = $this->name_data; //保存名称审核资料 $store_mini->nickname_audit_data = json_encode($name_data); //保存小程序名称 $store_mini->mini_nickname = $mini_info['nickname'] ?: ''; //重新填充营业执照信息 $name_data['license'] = $store_mini->license_pic; if (!empty($name_data)) { //循环 ,将数据保存到本地 foreach ($name_data as &$item) { if (!empty($item)) { //保存数据到本地 $local_item = (new BaseForm())->saveTempImage($item); //从微信获取media_id $result = $media->uploadImage($local_item); //判断media信息是否正确 if (empty($result['media_id'])) { throw new \Exception($this->getZnMsg($result)); } $item = $result['media_id']; } } } $license_media_id = $name_data['license']; //剔除营业执照 个人身份证信息 unset($name_data['license']); unset($name_data['id_card']); //请求微信审核名称接口 $result = $miniProgram->setting->setNickname($mini_info['nickname'], '', $license_media_id, $name_data); if ($result['errcode'] == 0) { self::addMiniCategory(); } //判断审核名称是否存在audit_id;如果存在则进入审核阶段 if ($result['errcode'] == 0 && !empty($result['audit_id'])) { $store_mini->apply_name_status = 1; $store_mini->nickname_audit_id = $result['audit_id']; $msg = "开始审核!"; //判断审核名称是否存在audit_id;如果不存在则不需要审核 } elseif ($result['errcode'] == 0 && empty($result['audit_id'])) { $store_mini->apply_name_status = 3; $msg = "审核通过!"; //判断是否存在错误信息 } elseif ($result['errcode'] !== 0) { $msg = (new BaseForm())->getZnMsg($result); throw new \Exception($msg); } else { throw new \Exception("参数错误"); } //判断是否保存成功 if (!$store_mini->save()) { throw new \Exception(json_encode($store_mini->errors)); } return [ 'code' => 0, 'msg' => $msg ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //微信名称检测 检测名称是否可用 需要mini_id public function checkNickname() { try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } //获取小程序名称 $mini_info = $this->mini_info; $nickname = $mini_info['nickname']; //判断名称是否为空 if (empty($nickname)) { throw new \Exception("名称不能为空"); } //请求微信接口获取检测结果 $miniProgram = $this->miniProgram; $result = $miniProgram->setting->isAvailableNickname($nickname); //请求成功,判断是否存在敏感字段 如果存在则返回说明描述 if ($result['errcode'] == 0) { return [ 'code' => 0, 'hit_condition' => $result['hit_condition'], 'msg' => $result['wording'] ]; //返回信息错误,则返回错误信息 } elseif ($result['errcode'] !== 0) { $msg = (new BaseForm())->getZnMsg($result); throw new \Exception($msg); } else { throw new \Exception("参数错误"); } } catch (\Exception $e) { return [ 'code' => 0, 'msg' => $e->getMessage() ]; } } //查询.小程序名称审核状态 public function getMiniStatus() { try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } //获取小程序信息 $store_mini = $this->store_mini; //判断小程序是否已经审核通过(不请求接口) if (isset($store_mini->apply_name_status) && ($store_mini->apply_name_status == 0) && !empty($store_mini->mini_nickname)) { return [ 'code' => 0, 'msg' => "审核已经通过" ]; } //获取审核单ID $audit_id = $store_mini->nickname_audit_id; if (empty($audit_id)) { throw new \Exception("参数获取失败"); } //通过审核单ID请求微信接口 $miniProgram = $this->miniProgram; $result = $miniProgram->setting->getNicknameAuditStatus($audit_id); if ($result['errcode'] === 0) { //审核中 if ($result['audit_stat'] == 1) { $status = "名称审核中"; $store_mini->apply_name_status = 1; //审核失败 } elseif ($result['audit_stat'] == 2) { $status = "名称审核失败:" . $result['fail_reason']; $store_mini->apply_name_error = $result['fail_reason']; $store_mini->apply_name_status = 2; //审核成功 } else { $status = "名称审核成功"; $store_mini->apply_name_status = 3; } //保存小程序名称审核信息 if (!$store_mini->save()) { throw new \Exception(json_encode($store_mini->errors)); } return [ 'code' => 0, 'msg' => $status, 'status' => $result['audit_stat'] ]; } elseif($result['errcode'] !== 0) { $msg = (new BaseForm())->getZnMsg($result); throw new \Exception($msg); } else { throw new \Exception("参数错误"); } } catch (\Exception $e) { return [ 'code' => 0, 'msg' => $e->getMessage() ]; } } //修改头像 public function setHeadImg() { try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } $store_mini = $this->store_mini; //获取小程序头像 $mini_info = $this->mini_info; $head_image = $mini_info['mini_url']; $store_mini->mini_url = $head_image; //将头像地址保存本地 $img_url = (new BaseForm())->saveTempImage($head_image); //获取微信配置信息 $miniProgram = $this->miniProgram; //获取微信上传图片配置 $media = new Client($miniProgram); //上传缩略图,获取media_id $head_image = $media->uploadThumb($img_url); //判断上传缩略图时是否报错 if (empty($head_image['thumb_media_id'])) { $msg = (new BaseForm())->getZnMsg($head_image); throw new \Exception($msg); } //使用生成的media_id 上传头像 $result = $miniProgram->account->updateAvatar($head_image['thumb_media_id'], 0, 0, 1, 1); if ($result['errcode'] === 0) { //保存头像信息 if (!$store_mini->save()) { throw new \Exception($store_mini->errors); } return [ 'code' => 0, 'msg' => "保存成功" ]; } elseif ($result['errcode'] != 0) { //获取错误信息 $msg = (new BaseForm())->getZnMsg($result); throw new \Exception($msg); } else { throw new \Exception("参数错误"); } } catch (\Exception $e) { //返回错误信息 return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //查询所有可设置的小程序类目 //获取mini_id public function getAllCategory() { try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } //获取微信配置信息 $miniProgram = $this->miniProgram; //获取所有可用的类目 $result = $miniProgram->setting->getAllCategories(); if ($result['errcode'] === 0) { $old_data = $result['categories_list']['categories']; //使用递归处理数据 返回父子数据 $data = (new BaseForm())->getdata($old_data); return [ 'code' => 0, 'msg' => "获取成功", 'data' => $data, 'old_data' => $old_data ]; //微信错误信息 } elseif ($result['errcode'] != 0) { $msg = (new BaseForm())->getZnMsg($result); throw new \Exception($msg); } else { throw new \Exception("数据错误"); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //添加小程序类目 (带资质的类目需要审核,申请只能被动推送) 需要mini_id 不设置类目到数据库 public function addMiniCategory() { try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } //获取微信请求配置 $miniProgram = $this->miniProgram; //获取添加的分类信息 // $mini_info = $this->mini_info; // $data = $mini_info['category']; // // //获取微信media处理配置 // $media = new Client($miniProgram); // // foreach ($data as &$item) { // //判断分类是否为空 // if (!empty($item['certicates'])) { // // //遍历类目 // foreach ($item['certicates'] as &$cate) { // // //保存图片到本地 // $url = (new BaseForm())->saveTempImage($cate['value']); // // //获取media_id // $cate['value'] = $media->uploadImage($url)['media_id']; // } // } // } $data = [ [ "first"=>304, "second"=> 1309 ] ]; $result = $miniProgram->setting->addCategories($data); if ($result['errcode'] === 0) { $result = $this->getMiniCategory(); if ($result['code'] != 0) { throw new \Exception($result['msg']); } return [ 'code' => 0, 'msg' => "操作成功" ]; } elseif ($result['errcode'] != 0) { $msg = (new BaseForm())->getZnMsg($result); throw new \Exception($msg); } else { throw new \Exception("数据错误"); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //查询已经设置的小程序类目 public function getMiniCategory() { try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } //获取微信请求配置 $miniProgram = $this->miniProgram; //获取小程序信息 $store_mini = $this->store_mini; //请求微信接口 $result = $miniProgram->setting->getCategories(); if ($result['errcode'] === 0) { //保存分类信息 $store_mini->categories = json_encode($result['categories']) ?: ""; //保存失败 if (!$store_mini->save()) { throw new \Exception(json_encode($store_mini->errors)); } //保存成功 return [ 'code' => 0, 'msg' => "获取成功", 'data' => $result['categories'] ]; } elseif (empty($result)) { throw new \Exception("数据错误"); } else { $msg = (new BaseForm())->getZnMsg($result); throw new \Exception($msg); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //删除类目 public function delMiniCategory() { try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } //获取微信请求配置 $miniProgram = $this->miniProgram; //获取分类信息 $mini_info = $this->mini_info; $category = $mini_info['category']; //请求微信删除类目接口 $result = $miniProgram->setting->deleteCategories($category['cate_first'], $category['cate_second']); //请求成功 if ($result['errcode'] === 0) { $result = $this->getMiniCategory(); if ($result['code'] != 0) { throw new \Exception($result['msg']); } return [ 'code' => 0, 'msg' => "删除成功" ]; //请求微信返回错误信息 } elseif ($result['errcode'] != 0) { $msg = (new BaseForm())->getZnMsg($result); throw new \Exception($msg); //请求失败 } else { throw new \Exception("数据错误"); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //修改小程序简介 public function setSignature() { $t = \Yii::$app->db->beginTransaction(); try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } //获取微信请求配置 $miniProgram = $this->miniProgram; //获取简介 $mini_info = $this->mini_info; $signature = $mini_info['signature']; //获取小程序model $store_mini = $this->store_mini; //设置简介 $store_mini->signature = $signature; if (!$store_mini->save()) { throw new \Exception($store_mini->errors); } //请求微信接口 $result = $miniProgram->account->updateSignature($signature); if ($result['errcode'] === 0) { $t->commit(); return [ 'code' => 0, 'msg' => "保存成功" ]; } elseif ($result['errcode'] != 0) { $msg = (new BaseForm())->getZnMsg($result); throw new \Exception($msg); } else { throw new \Exception("数据错误"); } } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //修改设置小程序隐私设置 public function setPrivacyForm() { try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } $data = [ "privacy_ver" => 2, "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" => "审核资质" ] ], ]; $miniProgram = $this->miniProgram; //创建请求方式 $client = new BaseClient($miniProgram); //请求设置隐私保护指引接口 $result = $client->httpPostJson('cgi-bin/component/setprivacysetting', $data); if ($result['errcode'] === 0) { return [ 'code' => 0, 'msg' => "设置成功" ]; } elseif ($result['errcode'] != 0) { $msg = (new BaseForm())->getZnMsg($result); throw new \Exception($msg); } else { throw new \Exception("数据错误"); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //修改小程序服务器域名 public function setDomainName() { try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } //获取微信请求配置 $miniProgram = $this->miniProgram; //设置配置服务器域名资料信息 $data = [ 'action' => 'set',//操作类型 add添加 delete删除 set覆盖 get获取 '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] ]; //请求微信 $result = $miniProgram->domain->modify($data); //返回成功 if ($result['errcode'] === 0) { return [ 'code' => 0, 'msg' => "设置成功" ]; //返回失败输出错误信息 } elseif ($result['errcode'] != 0) { $msg = (new BaseForm())->getZnMsg($result); throw new \Exception($msg); } else { throw new \Exception("数据错误"); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //修改小程序业务域名 public function setWebviewDomain() { try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } $store_mini = $this->store_mini; //获取微信请求配置 $miniProgram = $this->miniProgram; $result = $miniProgram->domain->setWebviewDomain([ 'https://' . \Yii::$app->request->hostName, 'https://' . \Yii::$app->params['ws_url'], ], 'set'); //返回成功 if ($result['errcode'] === 0) { return [ 'code' => 0, 'msg' => "设置成功" ]; //返回失败输出错误信息 } elseif ($result['errcode'] != 0) { $msg = (new BaseForm())->getZnMsg($result); throw new \Exception($msg); } else { throw new \Exception("数据错误"); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //查询最后一次审核状态 public function getLastAuditStatus() { try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } $miniProgram = $this->miniProgram; $result = $miniProgram->code->getLatestAuditStatus(); if ($result['errcode'] === 0) { switch ($result['status']) { case 1: $msg = "审核被拒绝"; break; case 2: $msg = "审核中"; break; case 3: $msg = "已撤回"; break; default: $msg = "审核成功"; break; } return [ 'code' => 0, 'msg' => $msg, 'data' => [ 'reason' => $result ] ]; } elseif ($result['errcode'] != 0) { $msg = (new BaseForm())->getZnMsg($result); throw new \Exception($msg); } else { throw new \Exception("数据错误"); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }