services->setting->mall->get($this->mall_id, 'wechat'); // if ($wechat) { \Yii::$app->params['wechatConfig'] = [ 'app_id' => $wechat['app_id'] ?? '', 'secret' => $wechat['secret'] ?? '', 'token' => $wechat['token'] ?? '', 'aes_key' => $wechat['aes_key'] ?? '', ]; // } } /** * * 公众号设置 * @return mixed|string|void */ public function actionEdit() { $request = Yii::$app->request; if ($request->isAjax) { if ($request->isGet) { $wechat = Yii::$app->services->setting->mall->get($this->mall_id, 'wechat'); // 服务器地址(URL) $api_url = Yii::$app->services->setting->platform->get('system.api_url'); $web_url = $api_url . '/wechat/index?mall-sign=' . $this->mall['mall_sign']; $this->success('获取成功', compact('wechat', 'web_url')); } else { $params = Yii::$app->request->post('form'); $res = Yii::$app->services->validate->validate($params, [ [['app_id', 'secret', 'token'], 'required'], [['app_id', 'name'], 'string', 'max' => 45], [['secret', 'token', 'aes_key','wechat_js'], 'string', 'max' => 64], [['qrcode'], 'string', 'max' => 255], [['aes_key', 'qrcode','original_id'], 'safe'] ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); // 保存数据 $res = Yii::$app->services->setting->mall->set($this->mall_id, ['wechat' => $params]); if ($res === false) $this->error('保存失败'); $this->success('保存成功'); } } return $this->render($this->action->id); } /** * 自定义菜单 * @return string * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException * @throws \GuzzleHttp\Exception\GuzzleException */ public function actionMenus() { /** @var \jianyan\easywechat\Wechat $wechat */ $wechat = Yii::$app->wechat; $app = $wechat->app; $request = Yii::$app->request; if ($request->isAjax) { if ($request->isPost) { $buttons = $request->post('form'); $web_url = Yii::$app->urlManager->createAbsoluteUrl('/h5/#/pages/index/index?mall_id=' . $this->mall_id); $web_url = str_replace("web/index.php?r=","",urldecode($web_url)); foreach ($buttons["button"] as &$item){ if(empty($item["sub_button"]) && empty($item['type'])) return $this->error('一级菜单如果没有子菜单,需要设置菜单内容'); if(isset($item["type"]) && $item["type"] == "miniprogram"){ $item["url"] = isset($item["url"]) ? $item["url"] : $web_url; } if(isset($item["sub_button"])){ foreach ($item["sub_button"] as &$itemChild){ if($itemChild["type"] == "miniprogram"){ $itemChild["url"] = isset($item["url"]) ? $item["url"] : $web_url; } } } } $res = $app->menu->create($buttons['button']); if ($res['errcode'] == 0) { return $this->success('发布成功'); } return $this->error('发布失败,错误码:' . $res['errmsg']); } else { try { $list = $app->menu->list(); } catch (HttpException $e) { return $this->error("获取菜单失败,errcode:{$e->formattedResponse['errcode']},errmsg:{$e->formattedResponse['errmsg']}"); } return $this->success('获取成功', $list); } } return $this->render($this->action->id); } /** * 自定义回复 * @return mixed|string|void */ public function actionReplyRule() { $request = Yii::$app->request; if ($request->isAjax) { if ($request->isGet) { $params = Yii::$app->request->get(); $res = Yii::$app->services->validate->validate($params, [ [['page', 'limit'], 'integer'], [['page', 'limit'], 'safe'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $where = []; $where[] = ['=', 'mall_id', $this->mall_id]; $where[] = ['<>', 'status', StatusEnum::DELETE]; $where[] = ['=', 'is_follow_reply', 0]; $with = ['ruleKeyword' => function($query){ $query->select('rule_id,type,keyword')->andWhere(['<>', 'status', StatusEnum::DELETE]); }]; $order = 'id desc'; $select = ['id', 'status', 'name', 'reply_type', 'type', 'content']; $params = ['select' => $select, 'where' => $where, 'with' => $with, 'order' => $order]; $data = ReplyRule::listPage($params, false, true); // 组合匹配关键词 if (!empty($data['list'])) { foreach ($data['list'] as &$item) { $match_keywords = []; $include_keywords = []; foreach ($item['ruleKeyword'] as $keyword) { if ($keyword['type'] == RuleKeyword::TYPE_MATCH) { $match_keywords[] = $keyword['keyword']; } elseif ($keyword['type'] == RuleKeyword::TYPE_INCLUDE) { $include_keywords[] = $keyword['keyword']; } $item['match_keywords'] = $match_keywords; $item['include_keywords'] = $include_keywords; } } } $this->success('获取成功', $data); } else { $params = Yii::$app->request->post('form'); $res = Yii::$app->services->validate->validate($params, [ [['name', 'reply_type', 'content', 'status'], 'required'], [['name', 'reply_type', 'content'], 'string'], [['status', 'id'], 'integer'], [['id', 'match_keywords', 'include_keywords'], 'safe'] ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); // 保存数据 $res = ReplyRule::setData($this->mall_id, $params); if ($res == false) return $this->error('保存失败:'.ReplyRule::getStaticError()); $this->success('保存成功'); } } return $this->render($this->action->id); } /** * 自定义回复 * @return mixed|string|void */ public function actionFollowReplyRule() { $request = Yii::$app->request; if ($request->isAjax) { if ($request->isGet) { $params = Yii::$app->request->get(); $res = Yii::$app->services->validate->validate($params, [ [['page', 'limit'], 'integer'], [['page', 'limit'], 'safe'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $where = []; $where[] = ['=', 'mall_id', $this->mall_id]; $where[] = ['<>', 'status', StatusEnum::DELETE]; $where[] = ['=', 'is_follow_reply', 1]; $order = 'id desc'; $select = ['id', 'status', 'name', 'reply_type', 'type', 'content','is_follow_reply']; $params = ['select' => $select, 'where' => $where, 'order' => $order]; $data = ReplyRule::listPage($params, false, true); $this->success('获取成功', $data); } else { $params = Yii::$app->request->post('form'); $res = Yii::$app->services->validate->validate($params, [ [['name', 'reply_type', 'content', 'status'], 'required'], [['name', 'reply_type', 'content'], 'string'], [['status', 'id','is_follow_reply'], 'integer'], [['id', 'match_keywords', 'include_keywords'], 'safe'] ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $exist = ReplyRule::getOne([['mall_id'=>$this->mall_id,'status'=>StatusEnum::ENABLED,'is_follow_reply'=>1]]); if($exist && $params['id'] != $exist->id){ return $this->error('当前已有记录,请修改已存在记录'); } // 保存数据 $res = ReplyRule::setData($this->mall_id, $params); if ($res == false) return $this->error('保存失败'); $this->success('保存成功'); } } return $this->render($this->action->id); } /** * 删除自动回复 * @return mixed|void */ public function actionReplyRuleDelete() { $request = Yii::$app->request; if ($request->isAjax && $request->isPost) { $params = $request->post('form'); $res = Yii::$app->services->validate->validate($params, [[['id'], 'required'], [['id'], 'integer']]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); try { // 删除数据 ReplyRule::updateAll(['status' => StatusEnum::DELETE], ['id' => $params['id'], 'mall_id' => $this->mall_id]); RuleKeyword::updateAll(['status' => StatusEnum::DELETE], ['rule_id' => $params['id'], 'mall_id' => $this->mall_id]); $this->success('删除成功'); } catch (\Exception $e) { return $this->error('删除失败' . $e->getMessage()); } } } /** * 素材管理 * @return mixed|string|void */ public function actionMaterial() { $request = Yii::$app->request; if ($request->isAjax) { if ($request->isGet) { $params = Yii::$app->request->Post(); $res = Yii::$app->services->validate->validate($params, [ [['page', 'limit'], 'integer'], [['page', 'limit'], 'safe'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $where = []; $where[] = ['=', 'mall_id', $this->mall_id]; $where[] = ['<>', 'status', StatusEnum::DELETE]; $order = 'id desc'; $select = ['id', 'status', 'wechat_app_id', 'media_id', 'url', 'name', 'media_type', 'material_desc', 'updated_at']; $params = ['select' => $select, 'where' => $where, 'order' => $order]; $data = Material::listPage($params, false, true); if (!empty($data['list'])) { foreach ($data['list'] as &$item) { $item['updated_at'] = date('Y-m-d H:i:s', $item['updated_at']); } } $this->success('获取成功', $data); } else { $params = $request->post('form'); $res = Yii::$app->services->validate->validate($params, [ [['name', 'media_type', 'file_path'], 'required'], [['name', 'media_type', 'file_path', 'material_desc'], 'string'], [['material_desc'], 'safe'] ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $wechat = Yii::$app->wechat; $app = $wechat->app; $file = FileHelper::downFile(Yii::getAlias('@backend').'/web/uploads',md5(time()),$params['file_path']); if(empty($file)) return $this->error('获取图片失败'); switch ($params['media_type']) { case 'images': $result = $app->material->uploadImage($file); if ($result) { //存入数据库 $params['url'] = $result['url']; $params['media_id'] = $result['media_id']; } break; case 'videos': $result = $app->material->uploadVideo($file, $params['name'], $params['material_desc']); if ($result) { //存入数据库 $params['media_id'] = $result['media_id']; $params['url'] = $result['url']; } break; case 'thumb': $result = $app->material->uploadThumb($file); if ($result) { //存入数据库 $params['media_id'] = $result['media_id']; $params['url'] = $result['url']; } break; case 'voices': $result = $app->material->uploadVoice($file); if ($result) { //存入数据库 $params['media_id'] = $result['media_id']; $params['url'] = $result['url']; } break; } $params['wechat_app_id'] = \Yii::$app->params['wechatConfig']['app_id']; // 保存数据 $res = Material::setData($this->mall_id, $params); if ($res == false) return $this->error('保存失败,msg:' . Material::getStaticError()); unlink($file); $this->success('保存成功'); } } return $this->render($this->action->id); } /** * 删除素材管理 * @return mixed|void */ public function actionMaterialDelete() { $request = Yii::$app->request; if ($request->isAjax && $request->isPost) { $params = $request->post('form'); $res = Yii::$app->services->validate->validate($params, [[['id'], 'required'], [['id'], 'integer']]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); try { // 删除数据 Material::updateAll(['status' => StatusEnum::DELETE], ['id' => $params['id'], 'mall_id' => $this->mall_id]); $this->success('删除成功'); } catch (\Exception $e) { return $this->error('删除失败' . $e->getMessage()); } } } /** * 图文素材 * @return mixed|string|void */ public function actionArticle() { $request = Yii::$app->request; if ($request->isAjax) { if ($request->isGet) { $params = Yii::$app->request->Post(); $res = Yii::$app->services->validate->validate($params, [ [['page', 'limit'], 'integer'], [['page', 'limit'], 'safe'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $where = []; $where[] = ['=', 'mall_id', $this->mall_id]; $where[] = ['<>', 'status', StatusEnum::DELETE]; $order = 'id desc'; $select = ['id', 'status', 'title', 'cover_pic', 'show_cover', 'article_id', 'updated_at']; $params = ['select' => $select, 'where' => $where, 'order' => $order]; $data = MaterialArticle::listPage($params, false, true); if (!empty($data['list'])) { foreach ($data['list'] as &$item) { $item['updated_at'] = date('Y-m-d H:i:s', $item['updated_at']); } } $this->success('获取成功', $data); } else { $params = $request->post('form'); $res = Yii::$app->services->validate->validate($params, [ [['title', 'cover_pic', 'url', 'source_url'], 'required'], [['title', 'cover_pic', 'url', 'source_url','article_desc'], 'string'], [['show_cover'], 'integer'], [['content','author'], 'safe'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $file = FileHelper::downFile(Yii::getAlias('@backend').'/web/uploads',md5(time()),$params['cover_pic']); if(empty($file)) return $this->error('获取图片失败'); /** @var \jianyan\easywechat\Wechat $wechat */ $wechat = \Yii::$app->wechat; $app = $wechat->getApp(); $result = $app->material->uploadImage($file); if ($result) { //存入数据库 $params['thumb_media_id'] = $result['media_id']; $params['thumb_url'] = $params['cover_pic']; unlink($file);// 删除临时保存图片 $content = (new WechatMaterialLogic())->handleMaterialImgs($params['content']); $article['articles'][] =[ 'title' => $params['title'], 'author' => $params['author'], // 作者 'digest' => $params['article_desc'], 'content' => $content, 'content_source_url'=>'', 'thumb_media_id' => $params['thumb_media_id'], ]; // 获取media_id $res = $app->material->httpPostJson('cgi-bin/draft/add',$article); if(isset($res['errcode'])){ return $this->error('保存失败,微信接口报错:'.$res['errmsg']); } $media_id = $res['media_id']; $res = $app->material->httpPostJson('cgi-bin/freepublish/submit',['media_id'=>$media_id]); if(isset($res['errcode']) && $res["errcode"] != 0){ return $this->error('保存失败,微信接口报错:'.$res['errmsg']); } \Yii::error('=====+++==:'.json_encode($res)); $params['publish_id'] = $res['publish_id']; $params['media_id'] = $media_id; } else { return $this->error('封面图上传失败'); } // 保存数据 $res = MaterialArticle::setData($this->mall_id, $params); if ($res == false) return $this->error('保存失败,msg:' . MaterialArticle::getStaticError()); $this->success('保存成功'); } } return $this->render($this->action->id); } public function actionArticleMenuList() { $request = Yii::$app->request; if ($request->isGet) { $params = Yii::$app->request->get(); $res = Yii::$app->services->validate->validate($params, [ [['page', 'limit'], 'integer'], [['page', 'limit'], 'safe'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $where = []; $where[] = ['=', 'mall_id', $this->mall_id]; $where[] = ['<>', 'article_id', '']; $where[] = ['<>', 'status', StatusEnum::DELETE]; $order = 'id desc'; $select = ['id', 'status', 'title', 'cover_pic', 'show_cover', 'article_id', 'updated_at']; $params = ['select' => $select, 'where' => $where, 'order' => $order]; $data = MaterialArticle::listPage($params, false, true); if (!empty($data['list'])) { foreach ($data['list'] as &$item) { $item['updated_at'] = date('Y-m-d H:i:s', $item['updated_at']); } } $this->success('获取成功', $data); } } /** * 删除图文素材 * @return mixed|void */ public function actionArticleDelete() { $request = Yii::$app->request; if ($request->isAjax && $request->isPost) { $params = $request->post('form'); $res = Yii::$app->services->validate->validate($params, [[['id'], 'required'], [['id'], 'integer']]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); try { // 删除数据 // MaterialArticle::updateAll(['status' => StatusEnum::DELETE], ['id' => $params['id'], 'mall_id' => $this->mall_id]); $article = MaterialArticle::findOne(['id' => $params['id'],'mall_id' => $this->mall_id]); if (!$article) { return $this->asJson(['code' => ApiCode::CODE_FAIL, 'msg' => '所选内容不存在或已被删除!']); } $article->status = StatusEnum::DELETE; if ($article->save()) { $app = \Yii::$app->wechat->getApp(); $res = $app->material->httpPostJson('cgi-bin/freepublish/get',['publish_id'=>$article->publish_id]); if($res['publish_status'] == 0 && isset($res['article_id'])){ $app->material->httpPostJson('cgi-bin/freepublish/delete',['publish_id'=>$res['publish_id'],'index'=>1]); // $app->material->freepublishDelete($res['article_id']); } $this->success('删除成功'); } $this->success('删除失败'); } catch (\Exception $e) { return $this->error('删除失败' . $e->getMessage()); } } } public function actionArticleQuery() { $request = Yii::$app->request; if ($request->isAjax && $request->isPost) { $params = $request->post('form'); $res = Yii::$app->services->validate->validate($params, [[['id'], 'required'], [['id'], 'integer']]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); try { $article = MaterialArticle::findOne(['id' => $params['id'],'mall_id' => $this->mall_id]); if (empty($article)) return $this->error('所选内容不存在或已被删除'); $app = \Yii::$app->wechat->getApp(); $res = $app->material->httpPostJson('cgi-bin/freepublish/get',['publish_id'=>$article->publish_id]); //0:成功, 1:发布中,2:原创失败, 3: 常规失败, 4:平台审核不通过, 5:成功后用户删除所有文章, 6: 成功后系统封禁所有文章 \Yii::error('----:'.json_encode($res)); $msg = ''; if(isset($res['publish_status'])){ switch ($res['publish_status']){ case 0: $msg = '成功'; break; case 1: $msg ='发布中'; break; case 2: $msg = '原创失败'; break; case 3: $msg =''; break; case 4: $msg='平台审核不通过'; break; case 5: $msg='成功后用户删除所有文章'; break; case 6: $msg='成功后系统封禁所有文章'; break; } if ($res['publish_status'] == 0) { $article->article_id = $res['article_id']; $article->save(); $this->success($msg); } else { $this->error($msg); } }else{ $this->error('查询异常'); } } catch (\Exception $e) { return $this->error('查询异常' . $e->getMessage()); } } } /** * 获取商城H5链接 * * @return void */ public function actionGetH5Url() { return $this->success( 'ok', [ 'url' => Yii::$app->services->setting->platform->get('system.h5_url'), 'sign' => $this->mall['mall_sign'], ] ); } }