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 (empty($this->store_mini)) { return [ 'code' => 1, 'msg' => "查找小程序失败" ]; } if ($result['code'] === 1) { return $result; } $this->miniProgram = $result; } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //更新代码模板 public function upgradeTemplate() { try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } //获取微信配置信息 $miniProgram = $this->miniProgram; //获取小程序model数据 $store_mini = $this->store_mini; //判断关键数据是否为空 if (empty($store_mini->appid) || empty($store_mini->store_id) || empty($store_mini->id)) { throw new \Exception("参数错误,数据不全"); } //生成ext文件 $ext = json_encode([ //是否使用ext 'extEnable' => true, //商家小程序Appid 'extAppid' => $store_mini->appid, //ext.json文件配置信息 'ext' => [ //访问链接 "apiurl" => "https://" . \Yii::$app->request->hostName, //商城ID "store_id" => $store_mini->store_id, //小程序ID "mini_id" => $store_mini->id ], "window" => [ "navigationBarTitleText" => "0000" ] ]); //选择后台模板 $template_id = Option::get("platform_template_id", 0, 'saas')['value']; if ($template_id === "-1") { throw new \Exception("未选择模板"); } //请求微信接口 $result = $miniProgram->code->commit($template_id, $ext, cyy_version(), "小程序新版本上传"); //如果请求成功则保存信息 if ($result['errcode'] === 0) { //是否上传模版 $store_mini->mini_up = -1; //上传的模板ID $store_mini->template_id = $template_id; //上传的小程序版本信息 $store_mini->user_version = cyy_version(); //版本描述 $store_mini->user_desc = "小程序新版本上传"; //ext文件信息 $store_mini->ext = $ext; //判断是否保存成功 if (!$store_mini->save()) { throw new \Exception(json_encode($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() ]; } } //提交审核 获取申请单ID public function submitAudit() { try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } //获取微信配置信息 $miniProgram = $this->miniProgram; //获取小程序model数据 $store_mini = $this->store_mini; //获取小程序信息 $mini_info = $this->mini_info; $is_fast_audit = empty($mini_info['is_fast_audit']) ? 0 : 1; //请求微信接口 $result = $miniProgram->code->submitAudit([], '', ''); //如果不为空则保存信息 if ($result['errcode'] === 0) { //状态进入审核状态 $store_mini->mini_up = 1; //保存申请单ID $store_mini->audit_id = (string)$result['auditid']; //保存状态信息 if (!$store_mini->save()) { throw new \Exception(json_encode($store_mini->errors)); } //如果为空则判断是否加急 if ($is_fast_audit === 1) { //加急请求 $result = $miniProgram->code->speedupAudit($result['auditid']); //判断加急请求是否完成 if ($result['errcode'] === 0) { return [ 'code' => 0, 'msg' => "成功" ]; } elseif ($result['errcode'] != 0) { //获取中文错误信息(微信返回为英文信息) $msg = (new BaseForm())->getZnMsg($result); return [ 'code' => 0, 'msg' => "加急审核处理失败," . $msg ]; } else { return [ 'code' => 0, '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() ]; } } //根据申请单ID查询审核状态 public function getSubmitAuditStatus() { try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } //小程序model信息 $store_mini = $this->store_mini; //微信请求配置信息 $miniProgram = $this->miniProgram; //判断audit_id是否存在 if (empty($store_mini->audit_id)) { throw new \Exception("申请单ID不存在"); } //申请单ID $audit_id = $store_mini->audit_id; //使用申请单ID查询小程序审核状态 $result = $miniProgram->code->getAuditStatus($audit_id); if ($result['errcode'] === 0) { switch ($result['status']) { //审核拒绝 case 1: $store_mini->mini_up = 3; break; case 2: //审核中 $store_mini->mini_up = 1; break; case 3: //已经撤回 $store_mini->mini_up = -1; break; case 4: //审核延期 $store_mini->mini_up = 4; break; default: //审核通过 $store_mini->mini_up = 2; break; } //如果status = 1(审核失败),则开始处理审核失败的错误信息截图 $media_arr = []; if ($result['status'] == 1 && !empty($result['screenshot'])) { //将字符串转数组 $media_arr = explode('|', $result['screenshot']); //遍历返回图片信息 foreach ($media_arr as &$item) { $item = $miniProgram->material->get($item); } } return [ 'code' => 0, 'msg' => "获取成功", 'data' => [ 'status' => $result['status'],//返回状态 'screenshot' => $media_arr, //失败截图 'reason' => $result['reason'] //返回错误原因 ] ]; //请求失败处理 } 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 release() { try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } //小程序model信息 $store_mini = $this->store_mini; //微信请求配置信息 $miniProgram = $this->miniProgram; $store_mini->mini_up = 5; //请求接口 $result = $miniProgram->code->release(); //如果状态为0 或者 提示应用已经上架 则完成发布 if ($result['errcode'] === 0 || $result['errcode'] === 85052) { //保存信息 if (!$store_mini->save()) { throw new \Exception(json_encode($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() ]; } } }