mini_id; //获取配置信息 $form = new BaseForm(); $this->store_mini = StoreMini::findOne($mini_id); $store_id = $this->store_mini->store_id; $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; } //获取聚合码ID $qrcode_id = $this->qrcode_id; if (!empty($qrcode_id)) { $qrcode_info = AggregateQrcode::findOne($qrcode_id); //判断二维码是否存在 if (empty($qrcode_info)) { return [ 'code' => 1, 'msg' => "查找聚合码信息失败" ]; } //判断状态是否被使用 if ($qrcode_info->wx_status == 1) { return [ 'code' => 1, 'msg' => "二维码已经被使用" ]; } $this->qrcode_info = $qrcode_info; } //获取代理信息 $saas_user_id = get_saas_user_id(); $salesman = Salesman::find()->where(['saas_user_id' => $saas_user_id, 'is_delete' => 0])->select('id, admin_id')->one(); if (!empty($salesman)) { $this->salesman_id = $salesman->id; $this->admin_id = $salesman->admin_id; } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() . $e->getLine() ]; } } //获取校验文件,并保存本地 public function saveFile() { try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } //获取微信配置 $miniProgram = $this->miniProgram; //使用自定义接口 $client = new BaseClient($miniProgram); //请求自定义接口 $result = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpdownload'); if ($result['errcode'] === 0) { //判断目录是否存在 if (!is_dir(\Yii::$app->basePath . '/web/face/pay')) { //不存在则创建目录 mkdir(\Yii::$app->basePath . '/web/face/pay', 0777, true); } //文件写入 $file_result = file_put_contents(\Yii::$app->basePath . '/web/face/pay/' . $result['file_name'], $result['file_content']); //写入成功 if ($file_result) { return [ 'code' => 0, 'msg' => "文件写入成功" ]; } //写入失败 \Yii::error(json_encode($result)); throw new \Exception("文件写入失败,请重新请求或手动添加。"); } 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 setQrcodeRules() { $t = \Yii::$app->db->beginTransaction(); try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } //获取微信配置 $miniProgram = $this->miniProgram; //获取小程序model $store_mini = $this->store_mini; //获取商城ID $store_id = $store_mini->store_id; //获取业务员ID $salesman_id = $this->salesman_id; //获取代理ID $admin_id = $this->admin_id; //获取聚合码信息 $qrcode_info = $this->qrcode_info; //保存聚合码信息 if (!empty($qrcode_info)) { //判断没有业务员以及代理商,则填充 if (empty($qrcode_info->admin_id) || empty($qrcode_info->salesman_id)) { $qrcode_info->admin_id = $admin_id; $qrcode_info->salesman_id = $salesman_id; } //商城ID $qrcode_info->store_id = $store_id; //小程序ID $qrcode_info->wx_mini_id = $store_mini->id; if (!$qrcode_info->save()) { throw new \Exception(json_encode($qrcode_info->errors)); } } $data = [ 'prefix' => $qrcode_info->param_url, 'permit_sub_rule' => 1, 'path' => $qrcode_info->wx_url, 'open_version' => 2, 'debug_url' => [ $qrcode_info->param_url ], 'is_edit' => 0 ]; //自定义请求接口 $client = new BaseClient($miniProgram); //请求接口 $result = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpadd', $data); 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 submitQrcodeRules() { //开启数据库事务 $t = \Yii::$app->db->beginTransaction(); try { $result = $this->inits(); if ($result['code'] === 1) { return $result; } //获取聚合码信息 $qrcode_info = $this->qrcode_info; //获取微信配置信息 $miniProgram = $this->miniProgram; $mini_id = $this->mini_id; //判断二维码地址是否为空 if (empty($qrcode_info->param_url)) { throw new \Exception('参数错误'); } //将微信聚合码状态改为完成 $qrcode_info->wx_mini_id = $mini_id; $qrcode_info->wx_status = 1; $qrcode_info->is_use = 2; //保存失败 if (!$qrcode_info->save()) { throw new \Exception(json_encode($qrcode_info->errors)); } //所需参数 $data = [ 'prefix' => $qrcode_info->param_url ]; //自定义接口提交地址 $client = new BaseClient($miniProgram); //发布二维码规则接口 $result = $client->httpPostJson('cgi-bin/wxopen/qrcodejumppublish', $data); 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() ]; } } }