Explorar o código

被关注回复功能更新

zqwang hai 1 ano
pai
achega
cef8870978

+ 17 - 2
modules/WechatPlatform/Http/Controllers/WechatKeywordsController.php

@@ -167,6 +167,13 @@ class WechatKeywordsController extends CatchController
         return WechatKeywordsService::WechaAccountAuthListInfo($id,$userId);
     }
 
+    /**
+     * 获取设置
+     * name: getConfig
+     * @param $miniprogramId
+     * @return int
+     * date 2023/07/10 09:33
+     */
     public function getConfig($miniprogramId)
     {
 
@@ -176,14 +183,22 @@ class WechatKeywordsController extends CatchController
         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 FailedExceptio("参数错误");
+            throw  new FailedException("参数错误");
         }
         $param = $request->all(['value']);
         if (!in_array($param['value'],[0,1]) ){
-            throw  new FailedExceptio("参数错误");
+            throw  new FailedException("参数错误");
         }
         $userContext = $this->getUserContext(null);
         $param['miniprogram_id'] = $miniprogramId;

+ 21 - 0
modules/WechatPlatform/Http/Controllers/WechatMenuController.php

@@ -10,8 +10,29 @@
 namespace Modules\WechatPlatform\Http\Controllers;
 
 use Catch\Base\CatchController;
+use Illuminate\Http\Request;
+use Modules\User\Http\Controllers\UserTrait;
+
 
 class WechatMenuController 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;
+    }
+
+
+    public function add(){
 
+    }
 }

+ 192 - 0
modules/WechatPlatform/Http/Controllers/WechatSubscribeController.php

@@ -10,8 +10,200 @@
 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\WechatSubscribeRequest;
+use Modules\WechatPlatform\Services\WechatKeywordsService;
+use Modules\WechatPlatform\Services\WechatSubscribeService;
 
 class WechatSubscribeController extends CatchController
 {
 
+    use UserTrait;
+
+    /**
+     *  关注回复列表
+     * name: list
+     * @param Request $request
+     * date 2023/07/10 09:29
+     */
+    public function list(Request $request)
+    {
+
+    }
+
+    /**
+     *  添加关注回复
+     * name: add
+     * @param WechatSubscribeRequest $request
+     * @return string|void
+     * date 2023/07/10 10:07
+     */
+    public function add(WechatSubscribeRequest $request)
+    {
+        $param = $request->validated();
+        $param = $this->handelParam($param);
+        $userContext = $this->getUserContext(null);
+        $param['user_id'] = $userContext['loginUser']->id;
+        $param['puser_id'] = $userContext['loginUser']->pid;
+        $param['wechat_accounts'] = [];
+        return WechatSubscribeService::addSubscribe($param);
+    }
+
+    /**
+     *  编辑
+     * name: edit
+     * @param $id
+     * @param WechatSubscribeRequest $request
+     * date 2023/07/10 10:51
+     */
+    public function edit($id, WechatSubscribeRequest $request)
+    {
+        $param = $this->handelParam($request->validated());
+        return WechatSubscribeService::updateSubscribe($id, $param);
+    }
+
+    /**
+     *  详情
+     * name: detail
+     * @param $id
+     * date 2023/07/10 10:07
+     */
+    public function detail($id)
+    {
+        return WechatSubscribeService::getDetail($id);
+    }
+
+    /**
+     *  分配公众号
+     * name: allocation
+     * @param $id
+     * @param Request $request
+     * @return string
+     * date 2023/07/10 10:22
+     */
+    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 WechatSubscribeService::allocation($id, $wxAuthIds);
+    }
+
+    /**
+     *  公众号分配列表
+     * name: authList
+     * @param $id
+     * date 2023/07/10 11:08
+     */
+    public function authList($id)
+    {
+        $userId  = $this->getLoginUserId();
+        return WechatSubscribeService::WechaAccountAuthListInfo($id,$userId);
+    }
+
+    /**
+     *  删除
+     * name: del
+     * @param Request $request
+     * @return string
+     * date 2023/07/10 11:08
+     */
+    public function del(Request $request)
+    {
+        $ids = $request->input('ids');
+        if (empty($ids)) {
+            throw  new  FailedException('要删除的数据参数错误');
+        }
+
+        $ids = explode(',', $ids);
+        return WechatSubscribeService::del($ids);
+    }
+
+    /**
+     * 获取设置
+     * 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 WechatSubscribeService:: 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 WechatSubscribeService::setConfig($param);
+    }
+
+    private function handelParam(mixed $param)
+    {
+        if (getProp($param, 'type') != 'miniprogram') {
+            return $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;
+    }
 }

+ 0 - 1
modules/WechatPlatform/Http/Requests/WechatKeywordsRequest.php

@@ -10,7 +10,6 @@
 namespace Modules\WechatPlatform\Http\Requests;
 
 use Illuminate\Foundation\Http\FormRequest;
-use Illuminate\Support\Facades\DB;
 use Modules\User\Http\Controllers\UserTrait;
 use Modules\WechatPlatform\Services\WechatKeywordsService;
 

+ 62 - 0
modules/WechatPlatform/Http/Requests/WechatSubscribeRequest.php

@@ -0,0 +1,62 @@
+<?php
+/**
+ *
+ * @file:WechatKeywordsRequest.php
+ * @Date: 2023/7/5
+ * @Time: 15:11
+ */
+
+
+namespace Modules\WechatPlatform\Http\Requests;
+
+use Illuminate\Foundation\Http\FormRequest;
+use Modules\User\Http\Controllers\UserTrait;
+use Modules\WechatPlatform\Services\WechatKeywordsService;
+
+class WechatSubscribeRequest extends FormRequest
+{
+
+    use  UserTrait;
+    /**
+     * rules
+     *
+     * @return array
+     */
+    public function rules(): array
+    {
+        return [
+            'type' => [
+                "required",
+                function ($attribute, $value, $fail) {
+                    $types = array_column(WechatKeywordsService::getWechatKeywordType(), 'value');
+                    if (!in_array($value, $types)) {
+                        $fail("消息类型不正确!");
+                        exit();
+                    }
+                }
+            ],
+            'miniprogram_id' => [
+                'required',
+                'Integer',
+                "gt:0",
+            ],
+            "content" =>   "required|array",
+        ];
+    }
+
+
+    /**
+     * messages
+     *
+     * @return string[]
+     */
+    public function messages(): array
+    {
+        return [
+            'type.required' => '消息类类型必须填写',
+            "miniprogram_id.required" => "小程序必须填写",
+            "content.required" => "内容必须填写",
+            "content.array" => "内容格式不正确",
+        ];
+    }
+}

+ 15 - 0
modules/WechatPlatform/Models/WechatAccountSubscribeDetail.php

@@ -0,0 +1,15 @@
+<?php
+
+namespace Modules\WechatPlatform\Models;
+
+use Modules\Common\Models\BaseModel;
+
+class WechatAccountSubscribeDetail extends BaseModel
+{
+    protected $table = 'wechat_account_subscribe_detail';
+
+    protected $fillable = [
+        'id', 'user_id', 'puser_id', 'subscribe_id', 'appid', 'wechat_authorization_info_id', 'miniprogram_id', 'nick_name', 'content', 'status', 'send_total', 'updated_at', 'created_at',
+    ];
+
+}

+ 17 - 0
modules/WechatPlatform/Models/WechatSubscribeMsg.php

@@ -0,0 +1,17 @@
+<?php
+
+namespace Modules\WechatPlatform\Models;
+
+use Modules\Common\Models\BaseModel;
+
+
+class WechatSubscribeMsg extends BaseModel
+{
+    protected $table = 'wechat_subscribe_msg';
+
+    protected $fillable = [
+        'id', 'user_id', 'puser_id', 'miniprogram_id', 'type', 'miniprogram_appid', 'content', 'send_content', 'wechat_accounts', 'send_total', 'is_del', 'del_at', 'updated_at', 'created_at',
+    ];
+    protected $casts = ['content' => 'array','wechat_accounts' => 'array'];
+
+}

+ 1 - 1
modules/WechatPlatform/Services/WechatKeywordsService.php

@@ -222,8 +222,8 @@ class WechatKeywordsService extends BaseService
                     ];
                 }
             }
+            unset($wechatAccountInfos,$allSet);
         }
-        unset($wechatAccountInfos,$allSet);
         DB::beginTransaction();
         try {
             WechatAccountKeywordLog::where('weacht_keyworld_id',$id)->update(['status' => 0]);

+ 228 - 0
modules/WechatPlatform/Services/WechatSubscribeService.php

@@ -0,0 +1,228 @@
+<?php
+/**
+ *
+ * @file:WechatSubscribeService.php
+ * @Date: 2023/7/10
+ * @Time: 09:35
+ */
+
+
+namespace Modules\WechatPlatform\Services;
+
+use Illuminate\Support\Facades\DB;
+use Modules\Common\Services\BaseService;
+use Modules\WechatPlatform\Models\MiniprogramWechatGlobalConfig;
+use Modules\WechatPlatform\Models\WechatAccountSubscribeDetail;
+use Modules\WechatPlatform\Models\WechatSubscribeMsg;
+
+class WechatSubscribeService extends BaseService
+{
+
+    const KEYWORD_SET_KEY = "miniprogram_wechat_subscribe";
+    protected static array $hideField = ['updated_at', 'is_del', 'del_at', 'user_id', 'puser_id', 'send_content']; // 公用的影藏字段
+
+
+    /**
+     *  获取配置
+     * name: getConfig
+     * @param $userId
+     * @param $puserId
+     * @param $miniprogramId
+     * @return int
+     * date 2023/07/10 09:40
+     */
+    public static function getConfig($userId, $puserId, $miniprogramId)
+    {
+        $set = MiniprogramWechatGlobalConfig::where(['user_id' =>  $userId,'puser_id' =>  $puserId,'miniprogram_id' =>  $miniprogramId])
+            ->where('type',self::KEYWORD_SET_KEY)->value('value');
+        return $set == 1 ? 1 :0;
+    }
+
+    /**
+     *  保存配置
+     * name: setConfig
+     * @param $param
+     * @return string
+     * date 2023/07/10 09:39
+     */
+    public static function setConfig($param)
+    {
+        $param['type'] = self::KEYWORD_SET_KEY;
+        $res = MiniprogramWechatGlobalConfig::updateOrCreate(
+            ['user_id' =>  $param['user_id'],'puser_id' => $param['puser_id'],'miniprogram_id' =>  $param['miniprogram_id'],'type' => self::KEYWORD_SET_KEY],
+            $param
+        );
+        if($res){
+            return "操作成功";
+        }
+        self::throwErrMsg("操作失败");
+    }
+
+    public static function addSubscribe(mixed $param)
+    {
+        $res = WechatSubscribeMsg::create($param);
+        if ($res) {
+            return "操作成功";
+        }
+        self::throwErrMsg("操作失败");
+    }
+
+
+    /**
+     *  获取详情
+     * name: getDetail
+     * @param $id
+     * date 2023/07/10 10:08
+     */
+    public static function getDetail($id)
+    {
+        return WechatSubscribeMsg::query()->where('id', $id)->where('is_del', 0)->first()->makeHidden(array_merge(self::$hideField,['wechat_accounts']));
+    }
+
+    public static function allocation($id, $wxAuthIds)
+    {
+        $info = WechatSubscribeMsg::query()->where('id', $id)->where('is_del', 0)->first();
+        if (is_empty($info)) {
+            self::throwErrMsg('该关注回复不存在或已删除');
+        }
+        $data = [];
+        $list = [];
+        $appIds =[];
+        if (empty($wxAuthIds)){
+            $data['wechat_accounts'] = [];
+        }else {
+            $wechatAccountInfos = DB::table('wechat_authorization_infos')
+                ->whereIn('id', $wxAuthIds)
+                ->where('is_enabled', 1)
+                ->where('user_id', $info->user_id)
+                ->get();
+            if ($wechatAccountInfos->isEmpty()) {
+                self::throwErrMsg("优化师对提交的公众号没有使用权限");
+            }
+
+            $canNotUsed = $wechatAccountInfos->pluck('id')->toArray();
+            $canNotUsed = array_diff($wxAuthIds, $canNotUsed);
+            if (count($canNotUsed) > 0) {
+                self::throwErrMsg("优化师对id:为:" . implode(',', $canNotUsed) . "的公众号没有使用权限");
+            }
+            foreach ($wechatAccountInfos as $val){
+                $data['wechat_accounts'][] = [
+                    'id' =>  $val->id,
+                    'appid' =>$val->authorizer_appid,
+                    'nick_name' => $val->nick_name,
+                    'component_appid' => $val->component_appid
+                ];
+                $appIds[] = $val->authorizer_appid;
+                $list[] = [
+                    'subscribe_id' => $info->id,
+                    'user_id' => $info->user_id,
+                    'puser_id' => $info->puser_id,
+                    'miniprogram_id' =>  $info->miniprogram_id,
+                    'appid' => $val->authorizer_appid,
+                    'wechat_authorization_info_id'=> $val->id,
+                    'nick_name' => $val->nick_name,
+                    'content' => $info->send_content,
+                    'status' => 1,
+                ];
+            }
+            unset($wechatAccountInfos);
+
+        }
+        DB::beginTransaction();
+        try {
+            if (!empty($appIds)){
+                // 关闭所有要配置的公众号关注回复
+                WechatAccountSubscribeDetail::whereIn('appid',$appIds)->update(['status' => 0]);
+                foreach ($list as $val){
+                    WechatAccountSubscribeDetail::updateOrCreate(
+                        [
+                            'subscribe_id' => $val['subscribe_id'],
+                            'miniprogram_id' => $val['miniprogram_id'],
+                            'wechat_authorization_info_id' => $val['wechat_authorization_info_id']
+                        ],$val);
+                }
+            }else{
+                // 关闭当前回复所涉及的公众号关注回复
+                WechatAccountSubscribeDetail::where('subscribe_id',$id)->update(['status' => 0]);
+            }
+
+            WechatSubscribeMsg::where('id',$id)->update($data);
+            DB::commit();
+        }catch (\Exception $exception){
+            self::throwErrMsg('操作失败');
+        }
+        return "操作成功";
+    }
+
+    /**
+     *  编辑
+     * name: updateSubscribe
+     * @param $id
+     * @param mixed $param
+     * @return string
+     * date 2023/07/10 11:03
+     */
+    public static function updateSubscribe($id, mixed $param)
+    {
+        $info = WechatSubscribeMsg::query()->where('id', $id)->where('is_del', 0)->first();
+        if (is_empty($info)) {
+            self::throwErrMsg('该关注回复不存在或已删除');
+        }
+
+        DB::beginTransaction();
+        try {
+            $info->save($param);
+            WechatAccountSubscribeDetail::where('subscribe_id',$info->id)->update(['content' => $param['send_content']]);
+            DB::commit();
+        }catch (\Exception $exception){
+            self::throwErrMsg('操作失败');
+        }
+        return "操作成功";
+    }
+
+    /**
+     *  删除
+     * name: del
+     * @param array $ids
+     * date 2023/07/10 11:03
+     */
+    public static function del(array $ids)
+    {
+        if (empty($ids)) {
+            self::throwErrMsg('要删除的数据不能为空');
+        }
+
+        DB::beginTransaction();
+        try {
+            WechatSubscribeMsg::query()->whereIn('id',$ids)->update(['is_del' => 1, 'del_at' => get_date()]);
+            WechatAccountSubscribeDetail::query()->whereIn('subscribe_id',$ids)->update(['status' => 0]);
+            DB::commit();
+        }catch (\Exception $exception){
+            self::throwErrMsg('操作失败');
+        }
+        return "操作成功";
+    }
+
+    public static function WechaAccountAuthListInfo($id, mixed $userId)
+    {
+        $list = DB::table('wechat_authorization_infos')
+            ->where('user_id', $userId)
+            ->select('id', 'nick_name', 'is_enabled')->get();
+        $authList = WechatAccountSubscribeDetail::where('subscribe_id', $id)->get();
+        if (!$list->isEmpty()) {
+            foreach ($list as $val) {
+                $val->is_auth = 0;
+                $info = $authList->where('wechat_authorization_info_id', $val->id)->first();
+                if (getProp($info, 'status') == 1) {
+                    $val->is_auth = 1;
+                }
+                if ($val->is_enabled != 1) {
+                    $val->nick_name .= "(取消授权)";
+                }
+                unset($val->is_enabled);
+            }
+        }
+        return $list;
+    }
+
+}