| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616 |
- <?php
- /**
- * @link:http://www.gdqijianshi.com/
- * @copyright: Copyright (c) 2020 广东七件事集团
- * 微信公众号管理
- * Author: lun
- * Date: 2021-09-17
- * Time: 16:12
- */
- namespace backend\modules\mall\controllers;
- use api\modules\v1\forms\user\WechatForm;
- use backend\controllers\MallController;
- use common\enums\StatusEnum;
- use common\helpers\ApiCode;
- use common\helpers\FileHelper;
- use common\logic\wechat\WechatMaterialLogic;
- use common\models\wechat\Material;
- use common\models\wechat\MaterialArticle;
- use common\models\wechat\ReplyRule;
- use common\models\wechat\RuleKeyword;
- use EasyWeChat\Factory;
- use EasyWeChat\Kernel\Exceptions\HttpException;
- use EasyWeChat\Kernel\Messages\Article;
- use Yii;
- use function Clue\StreamFilter\fun;
- class WechatController extends MallController
- {
- public function init()
- {
- parent::init(); // TODO: Change the autogenerated stub
- $wechat = \Yii::$app->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'],
- ]
- );
- }
- }
|