Browse Source

Merge branch 'liuzj-1001016-dev' into test

# Conflicts:
#	modules/WechatPlatform/routes/route.php
liuzejian 1 year ago
parent
commit
c6c8663e0a

+ 19 - 0
modules/Common/Repository/Options/CommonParams.php

@@ -3,6 +3,7 @@
 namespace Modules\Common\Repository\Options;
 
 use Catch\Enums\Status as StatusEnum;
+use Modules\WechatPlatform\Services\WechatPlatformConstService;
 
 class CommonParams implements OptionInterface
 {
@@ -25,6 +26,24 @@ class CommonParams implements OptionInterface
              * 首页列表类型
              */
             'firstPageListType' => $commonConfig['firstPageListType'],
+            /**
+             * 微信公众号运营
+             */
+            'wechatPlatform' => [
+                'kfMessageType' => $this->_turn(WechatPlatformConstService::KF_MESSAGE_TYPE_MAPPER),
+                'kfMessageStatus' => $this->_turn(WechatPlatformConstService::KF_MESSAGE_STATUS_MAPPER),
+            ]
         ];
     }
+
+    private function _turn($arr) {
+        $result = [];
+        foreach ($arr as $key => $val) {
+            $result[] = [
+                'label' => $val,
+                'value' => $key
+            ];
+        }
+        return $result;
+    }
 }

+ 1 - 1
modules/Common/config/common.php

@@ -14,7 +14,7 @@ return [
      * 模块和小程序type值的映射
      */
     'moduleMap' => [
-        '1' => 'weixin',
+        '1' => 'wechatPlatform',
         '2' => 'douyin',
     ],
     /**

+ 0 - 1
modules/Permissions/Middlewares/PermissionGate.php

@@ -16,7 +16,6 @@ class PermissionGate
 
         /* @var User $user */
         $user = $request->user(getGuardName());
-
         if (! $user->can()) {
             throw new PermissionForbidden();
         }

+ 211 - 0
modules/WechatPlatform/Http/Controllers/KFMessageController.php

@@ -0,0 +1,211 @@
+<?php
+
+namespace Modules\WechatPlatform\Http\Controllers;
+
+use Catch\Base\CatchController;
+use Illuminate\Foundation\Validation\ValidatesRequests;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\DB;
+use Modules\WechatPlatform\Services\WechatPlatformConstService;
+
+class KFMessageController extends CatchController
+{
+    use ValidatesRequests;
+    /**
+     * 客服消息列表
+     * @param Request $request
+     */
+    public function list(Request $request) {
+        $name = $request->input('name');
+        $messageType = $request->input('messageType');
+        $status = $request->input('status');
+        $sendAtStart = $request->input('send_at_start');
+        $sendAtEnd = $request->input('send_at_end');
+        $gzhid = $request->input('gzh_id');
+        $result = DB::table('wechat_kf_messages')
+            ->where([
+                ['is_enabled' , '=', 1],
+                ['user_id', '=', $this->getLoginUserId()]
+            ])->when($name, function ($query, $name){
+                return $query->where('name', 'like', '%'. $name. '%');
+            })->when($messageType, function ($query, $messageType){
+                return $query->where('message_type', $messageType);
+            })->when($status, function ($query, $status){
+                return $query->where('status', $status);
+            })->when($sendAtStart, function ($query, $sendAtStart){
+                return $query->where('start_at', '>=', $sendAtStart);
+            })->when($sendAtEnd, function ($query, $sendAtEnd){
+                return $query->where('start_at', '<=', $sendAtEnd . ' 23:59:59');
+            })->when($gzhid, function ($query, $gzhid){
+                return $query->where('gzh_ids', 'like', '%#'. $gzhid . '#%');
+            })->orderBy('id', 'desc')
+            ->paginate($request->input('limit', 20));
+        $gzhIds = $gzhs = collect();
+        $result->pluck('gzh_ids')->every(function ($item) use ($gzhIds){
+            $gzhIds->merge(explode('#', trim($item, '#')));
+        });
+
+        if($gzhIds->isNotEmpty()) {
+            $gzhs = DB::table('wechat_authorization_infos')
+                ->whereIn('id', $gzhIds)
+                ->select('id', 'nick_name')
+                ->get()->keyBy('id');
+        }
+
+        foreach ($result as $item) {
+            $item->gzh_names = $this->strGzhId($item->gzh_ids, $gzhs);
+            $item->status_str = WechatPlatformConstService::KF_MESSAGE_STATUS_MAPPER[$item->status] ?? '';
+            $item->message_content_arr = \json_decode($item->message_content, true);
+            $item->message_type = WechatPlatformConstService::KF_MESSAGE_TYPE_MAPPER[$item->message_type] ?? '';
+            // todo:完善
+            $item->uc_str = '';
+        }
+
+        return $result;
+    }
+
+    private function strGzhId($gzh_ids, $gzhs) {
+        if(!$gzh_ids){
+            return '';
+        }
+        $gzh_names = collect();
+        foreach (explode('#', trim($gzh_ids, '#')) as $gzh_id) {
+            $gzh_names->push($gzhs->get($gzh_id)->nick_name ?? 'ID:'. $gzh_id);
+        }
+
+        return $gzh_names->unique()->join(', ');
+    }
+
+    /**
+     * 新增
+     * @param Request $request
+     */
+    public function add(Request $request) {
+        $this->validate($request, [
+            'name' => 'required', 'message_type' => 'required|in:1', 'message_content' => 'required|array',
+            'u_type' => 'required|in:1,2', 'uc_unique_key' => 'required_if:u_type,2', 'send_at' => 'required|date_format:Y-m-d H:i:s',
+        ]);
+        $now = date('Y-m-d H:i:s');
+        DB::table('wechat_kf_messages')
+            ->insert([
+                'name' => $request->input('name'),
+                'message_type' => $request->input('message_type'),
+                'message_content' => \json_encode($request->input('message_content'), JSON_UNESCAPED_UNICODE),
+                'u_type' => $request->input('u_type'),
+                'uc_unique_key' => $request->input('uc_unique_key', ''),
+                'send_at' => $request->input('send_at'),
+                'status' => WechatPlatformConstService::KF_MESSAGE_STATUS_PRE_SEND,
+                'user_id' => $this->getLoginUserId(),
+                'created_at' => $now,
+                'updated_at' => $now,
+            ]);
+
+        return 'ok';
+    }
+
+    /**
+     * 修改客服消息内容
+     * @param Request $request
+     */
+    public function updateContent(Request $request) {
+        $this->validate($request, [
+            'id' => 'required',
+        ]);
+        $now = date('Y-m-d H:i:s');
+        DB::table('wechat_kf_messages')
+            ->where([
+                ['id', '=', $request->input('id')],
+                ['user_id', '=', $this->getLoginUserId()],
+                ['status', '=', WechatPlatformConstService::KF_MESSAGE_STATUS_PRE_SEND],
+                ['is_enabled', '=', 1],
+            ])->update([
+                'name' => $request->input('name'),
+                'message_type' => $request->input('message_type'),
+                'message_content' =>  \json_encode($request->input('message_content'), JSON_UNESCAPED_UNICODE),
+                'u_type' => $request->input('u_type'),
+                'uc_unique_key' => $request->input('uc_unique_key', ''),
+                'send_at' => $request->input('send_at'),
+                'updated_at' => $now,
+            ]);
+
+        return 'ok';
+    }
+
+    /**
+     * 修改配置的公众号
+     * @param Request $request
+     */
+    public function updateGZH(Request $request){
+        $this->validate($request, [
+            'id' => 'required',
+            'gzh_ids' => 'required|array'
+        ]);
+        $now = date('Y-m-d H:i:s');
+        DB::table('wechat_kf_messages')
+            ->where([
+                ['id', '=', $request->input('id')],
+                ['user_id', '=', $this->getLoginUserId()],
+                ['status', '=', WechatPlatformConstService::KF_MESSAGE_STATUS_PRE_SEND],
+                ['is_enabled', '=', 1],
+            ])->update([
+                'gzh_ids' => sprintf('#%s#', join('#', array_unique($request->input('gzh_ids')))),
+                'updated_at' => $now,
+            ]);
+
+        return 'ok';
+    }
+
+    /**
+     * 停止发送
+     * @param Request $request
+     */
+    public function stop(Request $request) {
+        $this->validate($request, [
+            'id' => 'required'
+        ]);
+        $now = date('Y-m-d H:i:s');
+        DB::table('wechat_kf_messages')
+            ->where([
+                ['id', '=', $request->input('id')],
+                ['user_id', '=', $this->getLoginUserId()],
+                ['status', '=', WechatPlatformConstService::KF_MESSAGE_STATUS_PRE_SEND],
+                ['is_enabled', '=', 1],
+            ])->update([
+                'status' => WechatPlatformConstService::KF_MESSAGE_STATUS_STOP,
+                'updated_at' => $now,
+            ]);
+
+        return 'ok';
+    }
+
+    /**
+     * 删除
+     * @param Request $request
+     */
+    public function delete(Request $request){
+        $this->validate($request, [
+            'id' => 'required'
+        ]);
+        $now = date('Y-m-d H:i:s');
+        DB::table('wechat_kf_messages')
+            ->where([
+                ['id', '=', $request->input('id')],
+                ['user_id', '=', $this->getLoginUserId()],
+                ['status', '<>', WechatPlatformConstService::KF_MESSAGE_STATUS_SENDING],
+                ['is_enabled', '=', 1],
+            ])->update([
+                'is_enabled' => 0,
+                'updated_at' => $now,
+            ]);
+        DB::table('wechat_kf_message_send_records')
+            ->where([
+                ['message_id', '=', $request->input('id')],
+                ['is_enabled', '=', 1]
+            ])->update([
+                'is_enabled' => 0,
+                'updated_at' => $now,
+            ]);
+        return 'ok';
+    }
+
+}

+ 36 - 0
modules/WechatPlatform/Services/WechatPlatformConstService.php

@@ -0,0 +1,36 @@
+<?php
+
+namespace Modules\WechatPlatform\Services;
+
+class WechatPlatformConstService
+{
+    /**
+     * 客服消息状态:待发送
+     */
+    public const KF_MESSAGE_STATUS_PRE_SEND=1;
+    /**
+     * 客服消息状态:发送中
+     */
+    public const KF_MESSAGE_STATUS_SENDING=2;
+    /**
+     * 客服消息状态:发送完成
+     */
+    public const KF_MESSAGE_STATUS_FINISH=3;
+    /**
+     * 客服消息状态:已停止
+     */
+    public const KF_MESSAGE_STATUS_STOP=4;
+    /**
+     * 客服消息状态-映射表
+     */
+    public const KF_MESSAGE_STATUS_MAPPER = [
+        '1' => '待发送', '2'  => '发送中', '3' => '发送完毕', '4' => '已停止'
+    ];
+
+    /**
+     * 客服消息类型:纯文本
+     */
+    public const KF_MESSAGE_TYPE_MAPPER = [
+        '1' => '纯文本'
+    ];
+}

+ 10 - 1
modules/WechatPlatform/routes/route.php

@@ -2,8 +2,9 @@
 
 use Illuminate\Support\Facades\Route;
 use Modules\WechatPlatform\Http\Controllers\WechatKeywordsController;
+use Modules\WechatPlatform\Http\Controllers\KFMessageController;
 
-Route::prefix('wechat')->group(function () {
+Route::prefix('wechatPlatform')->group(function () {
     // 微信公众号设置
     Route::prefix('officialAccount')->group(function () {
         // 关键字列表
@@ -22,4 +23,12 @@ Route::prefix('wechat')->group(function () {
             Route::post('allocation/id',[WechatKeywordsController::class,'allocation']);
         });
     });
+    Route::prefix('kfMessage')->middleware('roleCheck:optimizer')->group(function(){
+        Route::get('list', [KFMessageController::class, 'list']);
+        Route::post('add', [KFMessageController::class, 'add']);
+        Route::post('stop', [KFMessageController::class, 'stop']);
+        Route::post('delete', [KFMessageController::class, 'delete']);
+        Route::post('updateContent', [KFMessageController::class, 'updateContent']);
+        Route::post('updateGZH', [KFMessageController::class, 'updateGZH']);
+    });
 });

+ 70 - 0
tests/WechatPlatform/Http/KFMessageControllerTest.php

@@ -0,0 +1,70 @@
+<?php
+
+namespace Tests\WechatPlatform\Http;
+
+use Modules\WechatPlatform\Http\Controllers\KFMessageController;
+use PHPUnit\Framework\TestCase;
+use Tests\UsedTestCase;
+
+class KFMessageControllerTest extends UsedTestCase
+{
+
+    public function testAdd()
+    {
+        $STR =  <<<AAA
+文本内容<a href="http://www.qq.com" data-miniprogram-appid="appid" data-miniprogram-path="pages/index/index">点击跳小程序</a>
+AAA;
+
+        $res = $this->withHeaders([
+            'Authorization' => 'Bearer '. $this->token,
+        ])->json('post','http://localhost/api/wechatPlatform/kfMessage/add', [
+            'name' => 'liuzj-test-'. uniqid(),
+            'send_at' => date('Y-m-d H:i:s', strtotime('tomorrow')),
+            'message_type' => 1,
+            'u_type' => 1,
+            'message_content' => [
+                ['text' => '普通文本👌🏻'],
+                ['text' => $STR]
+            ]
+        ]);
+        $this->dumpJson($res);
+    }
+
+    public function testList()
+    {
+        $res = $this->withHeaders([
+            'Authorization' => 'Bearer '. $this->token,
+        ])->json('get','http://localhost/api/wechatPlatform/kfMessage/list', [
+        ]);
+        $this->dumpJson($res);
+    }
+
+    public function testupdateContent()
+    {
+        $res = $this->withHeaders([
+            'Authorization' => 'Bearer '. $this->token,
+        ])->json('get','http://localhost/api/wechatPlatform/kfMessage/list', [
+        ]);
+        $this->dumpJson($res);
+    }
+
+    public function teststop()
+    {
+        $res = $this->withHeaders([
+            'Authorization' => 'Bearer '. $this->token,
+        ])->json('post','http://localhost/api/wechatPlatform/kfMessage/stop', [
+            'id' => 3
+        ]);
+        $this->dumpJson($res);
+    }
+
+    public function testdelete()
+    {
+        $res = $this->withHeaders([
+            'Authorization' => 'Bearer '. $this->token,
+        ])->json('post','http://localhost/api/wechatPlatform/kfMessage/delete', [
+            'id' => 3
+        ]);
+        $this->dumpJson($res);
+    }
+}