123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- /**
- * 微信公众号关键词回复设置
- * @file:WechatKeywordsController.php
- * @Date: 2023/7/5
- * @Time: 15:00
- */
- namespace Modules\WechatPlatform\Http\Controllers;
- use Catch\Base\CatchController;
- use Catch\Exceptions\FailedException;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use Modules\Manage\Enmus\MiniprogramType;
- use Modules\User\Http\Controllers\UserTrait;
- use Modules\WechatPlatform\Http\Requests\WechatKeywordsRequest;
- use Modules\WechatPlatform\Services\WechatKeywordsService;
- class WechatKeywordsController extends CatchController
- {
- use UserTrait;
- /**
- * 关键词列表
- * name: List
- * @param Request $request
- * date 2023/07/05 15:10
- */
- public function List(Request $request)
- {
- $param = $request->all();
- $userContext = $this->getUserContext(null);
- $param['user_id'] = $userContext['loginUser']->id;
- $param['puser_id'] = $userContext['loginUser']->pid;
- return WechatKeywordsService::getKeywordsList($param);
- }
- /**
- * 添加
- * name: add
- * @param WechatKeywordsRequest $request
- * date 2023/07/05 15:36
- */
- public function add(WechatKeywordsRequest $request)
- {
- $param = $request->validated();
- $param = $this->wechatKeywordsParam($param);
- $userContext = $this->getUserContext(null);
- $param['user_id'] = $userContext['loginUser']->id;
- $param['puser_id'] = $userContext['loginUser']->pid;
- $param['wechat_accounts'] = [];
- return WechatKeywordsService::addKeyword($param);
- }
- private function wechatKeywordsParam(mixed $param)
- {
- $info = DB::table('miniprogram')->where('id',$param['miniprogram_id'])->first();
- if(empty($info)){
- throw new FailedException("小程序不正确");
- }
- if($info->status != 1){
- throw new FailedException("此小程序暂不提供使用");
- }
- if ($info->type != MiniprogramType::WEIXIN->value()){
- throw new FailedException("关键词回复设置仅支持微信小程序");
- }
- $param['miniprogram_appid'] = $info->appid;
- $info = DB::table('user_has_miniprograms')->where('uid',$this->getLoginUserId())->where('miniprogram_id',$param['miniprogram_id'])->where('is_enabled',1)->value('id');
- if(empty($info)){
- throw new FailedException("没有此小程序的使用权限");
- }
- $param['send_content'] = "";
- foreach ($param['content'] as & $val){
- if (!is_array($val)){
- throw new FailedException("回复内容格式不正确");
- }
- if (getProp($val,'url')){
- // 跳转小程序
- $val['content'] = "<a href=\"\" data-miniprogram-appid=\"{$param['miniprogram_appid']}\" data-miniprogram-path=\"{$val['url']}\">{$val['title']}</a>";
- }else{
- $val['content'] = $val['title'];
- $val['url'] = "";
- }
- $param['send_content'] .= $val['content']."\n";
- }
- rtrim($param['send_content'],"\n");
- return $param;
- }
- /**
- * 编辑
- * name: edit
- * @param $id
- * @param WechatKeywordsRequest $request
- * date 2023/07/05 15:36
- */
- public function edit($id, WechatKeywordsRequest $request)
- {
- $param = $request->validated();
- $param = $this->wechatKeywordsParam($param);
- return WechatKeywordsService::updateKeyWords($id,$param);
- }
- /**
- * 详情
- * name: detail
- * @param $id
- * date 2023/07/05 15:36
- */
- public function detail($id)
- {
- return WechatKeywordsService::detail($id);
- }
- /**
- * 分配
- * name: allocation
- * @param $id
- * @param Request $request
- * date 2023/07/05 16:03
- */
- public function allocation($id,Request $request)
- {
- if (!$request->has('wx_auth_ids')){
- throw new FailedException("参数错误");
- }
- if (!empty($request->input('wx_auth_ids'))) {
- $wxAuthIds = explode(',', $request->input('wx_auth_ids'));
- }else{
- $wxAuthIds = [];
- }
- return WechatKeywordsService::allocation($id,$wxAuthIds);
- }
- /**
- * 删除
- * name: del
- * @param $id
- * date 2023/07/05 15:47
- */
- public function del(Request $request)
- {
- $ids = $request->input('ids');
- if (empty($ids)){
- throw new FailedException('要删除的数据参数错误');
- }
- $ids = explode(',',$ids);
- return WechatKeywordsService::delKeywords($ids);
- }
- /**
- * 公众号授权列表
- * name: authList
- * @param $id
- * date 2023/07/06 15:17
- */
- public function authList($id)
- {
- $userId = $this->getLoginUserId();
- return WechatKeywordsService::WechaAccountAuthListInfo($id,$userId);
- }
- /**
- * 获取设置
- * name: getConfig
- * @param $miniprogramId
- * @return int
- * date 2023/07/10 09:33
- */
- public function getConfig($miniprogramId)
- {
- $userContext = $this->getUserContext(null);
- $userId = $userContext['loginUser']->id;
- $puserId = $userContext['loginUser']->pid;
- return WechatKeywordsService:: getConfig($userId,$puserId,$miniprogramId);
- }
- /**
- * 更新设置
- * name: setConfig
- * @param $miniprogramId
- * @param Request $request
- * @return string
- * date 2023/07/10 09:33
- */
- public function setConfig($miniprogramId,Request $request)
- {
- if (!$request->has('value') ){
- throw new FailedException("参数错误");
- }
- $param = $request->all(['value']);
- if (!in_array($param['value'],[0,1]) ){
- throw new FailedException("参数错误");
- }
- $userContext = $this->getUserContext(null);
- $param['miniprogram_id'] = $miniprogramId;
- $param['user_id'] = $userContext['loginUser']->id;
- $param['puser_id'] = $userContext['loginUser']->pid;
- return WechatKeywordsService::setConfig($param);
- }
- }
|