| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1\third\wechat;
- use app\models\StoreMini;
- use EasyWeChat\BasicService\Media\Client;
- use EasyWeChat\Kernel\BaseClient;
- use yii\base\Model;
- class PerfectMiniForm extends Model
- {
- /**
- * 完善小程序
- */
- //easyWechat配置
- public $openPlatform;
- //商城ID
- public $store_id;
- //小程序ID
- public $mini_id;
- //easyWechat接口配置
- public $miniProgram;
- //获取小程序
- public $store_mini;
- //名称审核资料
- public $name_data;
- //小程序信息
- public $mini_info;
- public function rules()
- {
- return [
- [['store_id', 'mini_id'], 'integer'],
- [['name_data', 'mini_info'], 'array']
- ];
- }
- public function inits()
- {
- try {
- //商城ID
- $store_id = $this->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()
- ];
- }
- }
- }
|