From f4c4329a456f77fe6581b836825b13cc0b40b08b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=AF=E7=AB=8B?= Date: Wed, 15 Sep 2021 23:30:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BE=AE=E4=BF=A1=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/wechat/command/Auto.php | 3 ++- app/wechat/command/Fans.php | 10 ++++----- app/wechat/controller/Auto.php | 17 ++++++--------- app/wechat/controller/Fans.php | 29 +++++++++++--------------- app/wechat/controller/Keys.php | 23 ++++++++------------ app/wechat/controller/Menu.php | 3 +-- app/wechat/controller/News.php | 29 ++++++++++---------------- app/wechat/controller/api/View.php | 5 +++-- app/wechat/model/WechatAuto.php | 28 +++++++++++++++++++++++++ app/wechat/model/WechatFans.php | 28 +++++++++++++++++++++++++ app/wechat/model/WechatFansTags.php | 28 +++++++++++++++++++++++++ app/wechat/model/WechatKeys.php | 28 +++++++++++++++++++++++++ app/wechat/model/WechatNews.php | 28 +++++++++++++++++++++++++ app/wechat/model/WechatNewsArticle.php | 28 +++++++++++++++++++++++++ app/wechat/service/AutoService.php | 3 ++- app/wechat/service/FansService.php | 7 ++++--- app/wechat/service/MediaService.php | 6 ++++-- app/wechat/service/WechatService.php | 2 +- 18 files changed, 228 insertions(+), 77 deletions(-) create mode 100644 app/wechat/model/WechatAuto.php create mode 100644 app/wechat/model/WechatFans.php create mode 100644 app/wechat/model/WechatFansTags.php create mode 100644 app/wechat/model/WechatKeys.php create mode 100644 app/wechat/model/WechatNews.php create mode 100644 app/wechat/model/WechatNewsArticle.php diff --git a/app/wechat/command/Auto.php b/app/wechat/command/Auto.php index 62e396be7..dabf7abf5 100644 --- a/app/wechat/command/Auto.php +++ b/app/wechat/command/Auto.php @@ -16,6 +16,7 @@ namespace app\wechat\command; +use app\wechat\model\WechatAuto; use app\wechat\service\MediaService; use app\wechat\service\WechatService; use think\admin\Command; @@ -63,7 +64,7 @@ class Auto extends Command // 查询微信消息对象 $map = ['code' => $code, 'status' => 1]; - $data = $this->app->db->name('WechatAuto')->where($map)->find(); + $data = WechatAuto::mk()->where($map)->find(); if (empty($data)) $this->setQueueError("Message Data Query failed"); // 发送微信客服消息 diff --git a/app/wechat/command/Fans.php b/app/wechat/command/Fans.php index 9b11199dd..2efd62e13 100644 --- a/app/wechat/command/Fans.php +++ b/app/wechat/command/Fans.php @@ -16,6 +16,8 @@ namespace app\wechat\command; +use app\wechat\model\WechatFans; +use app\wechat\model\WechatFansTags; use app\wechat\service\FansService; use app\wechat\service\WechatService; use think\admin\Command; @@ -98,7 +100,6 @@ class Fans extends Command * @return string * @throws \WeChat\Exceptions\InvalidResponseException * @throws \WeChat\Exceptions\LocalCacheException - * @throws \think\db\exception\DbException */ public function _black(string $next = '', int $done = 0): string { @@ -108,7 +109,7 @@ class Fans extends Command foreach (array_chunk($result['data']['openid'], 100) as $chunk) { $done += count($chunk); $map = [['is_black', '=', 0], ['openid', 'in', $chunk]]; - $this->app->db->name('WechatFans')->where($map)->update(['is_black' => 1]); + WechatFans::mk()->where($map)->update(['is_black' => 1]); } $next = $result['total'] > $done ? $result['next_openid'] : null; } @@ -142,12 +143,11 @@ class Fans extends Command $tag['appid'] = $appid; $this->queue->message($count, ++$done, "-> {$tag['name']}"); } - $this->app->db->name('WechatFansTags')->where(['appid' => $appid])->delete(); - $this->app->db->name('WechatFansTags')->insertAll($list['tags']); + WechatFansTags::mk()->where(['appid' => $appid])->delete(); + WechatFansTags::mk()->insertAll($list['tags']); } $this->output->comment($done > 0 ? '微信用户标签数据获取完成' : '未获取到微信用户标签数据'); $this->output->newLine(); return ", 获取到 {$done} 个标签"; } - } \ No newline at end of file diff --git a/app/wechat/controller/Auto.php b/app/wechat/controller/Auto.php index 3b7623b3d..14d06aa97 100644 --- a/app/wechat/controller/Auto.php +++ b/app/wechat/controller/Auto.php @@ -16,6 +16,7 @@ namespace app\wechat\controller; +use app\wechat\model\WechatAuto; use think\admin\Controller; use think\admin\extend\CodeExtend; @@ -26,12 +27,6 @@ use think\admin\extend\CodeExtend; */ class Auto extends Controller { - /** - * 绑定数据表 - * @var string - */ - private $table = 'WechatAuto'; - /** * 消息类型 * @var array @@ -53,7 +48,7 @@ class Auto extends Controller public function index() { $this->title = '关注自动回复'; - $query = $this->_query($this->table)->like('code,type'); + $query = WechatAuto::mQuery()->like('code,type'); $query->equal('status')->dateBetween('create_at')->order('time asc')->page(); } @@ -78,7 +73,7 @@ class Auto extends Controller public function add() { $this->title = '添加自动回复'; - $this->_form($this->table, 'form'); + WechatAuto::mForm('form'); } /** @@ -91,7 +86,7 @@ class Auto extends Controller public function edit() { $this->title = '编辑自动回复'; - $this->_form($this->table, 'form'); + WechatAuto::mForm('form'); } /** @@ -129,7 +124,7 @@ class Auto extends Controller */ public function state() { - $this->_save($this->table, $this->_vali([ + WechatAuto::mSave($this->_vali([ 'status.in:0,1' => '状态值范围异常!', 'status.require' => '状态值不能为空!', ])); @@ -142,6 +137,6 @@ class Auto extends Controller */ public function remove() { - $this->_delete($this->table); + WechatAuto::mDelete(); } } \ No newline at end of file diff --git a/app/wechat/controller/Fans.php b/app/wechat/controller/Fans.php index 420639e2e..75f459911 100644 --- a/app/wechat/controller/Fans.php +++ b/app/wechat/controller/Fans.php @@ -16,6 +16,7 @@ namespace app\wechat\controller; +use app\wechat\model\WechatFans; use app\wechat\service\WechatService; use think\admin\Controller; use think\admin\helper\QueryHelper; @@ -28,12 +29,6 @@ use think\exception\HttpResponseException; */ class Fans extends Controller { - /** - * 绑定数据表 - * @var string - */ - private $table = 'WechatFans'; - /** * 微信用户管理 * @auth true @@ -44,7 +39,7 @@ class Fans extends Controller */ public function index() { - $this->_query($this->table)->layTable(function () { + WechatFans::mQuery()->layTable(function () { $this->title = '微信用户管理'; }, function (QueryHelper $query) { $query->where(['appid' => WechatService::instance()->getAppid()]); @@ -77,18 +72,18 @@ class Fans extends Controller */ public function black() { - $data = $this->_vali([ - 'black.require' => '操作类型不能为空!', - 'openid.require' => '操作用户不能为空!', - ]); try { + $data = $this->_vali([ + 'black.require' => '操作类型不能为空!', + 'openid.require' => '操作用户不能为空!', + ]); foreach (array_chunk(str2arr($data['openid']), 20) as $openids) { if ($data['black']) { WechatService::WeChatUser()->batchBlackList($openids); - $this->app->db->name('WechatFans')->whereIn('openid', $openids)->update(['is_black' => 1]); + WechatFans::mk()->whereIn('openid', $openids)->update(['is_black' => 1]); } else { WechatService::WeChatUser()->batchUnblackList($openids); - $this->app->db->name('WechatFans')->whereIn('openid', $openids)->update(['is_black' => 0]); + WechatFans::mk()->whereIn('openid', $openids)->update(['is_black' => 0]); } } if (empty($data['black'])) { @@ -97,7 +92,7 @@ class Fans extends Controller $this->success('拉入黑名单成功!'); } } catch (HttpResponseException $exception) { - throw $exception; + throw $exception; } catch (\Exception $exception) { $this->error("黑名单操作失败,请稍候再试!
{$exception->getMessage()}"); } @@ -110,7 +105,7 @@ class Fans extends Controller */ public function remove() { - $this->_delete($this->table); + WechatFans::mDelete(); } /** @@ -120,8 +115,8 @@ class Fans extends Controller public function truncate() { try { - $this->_query('WechatFans')->empty(); - $this->_query('WechatFansTags')->empty(); + WechatFans::mQuery()->empty(); + WechatFans::mQuery()->empty(); $this->success('清空用户数据成功!'); } catch (HttpResponseException $exception) { throw $exception; diff --git a/app/wechat/controller/Keys.php b/app/wechat/controller/Keys.php index 52d82c82e..a32bc1c0a 100644 --- a/app/wechat/controller/Keys.php +++ b/app/wechat/controller/Keys.php @@ -16,6 +16,7 @@ namespace app\wechat\controller; +use app\wechat\model\WechatKeys; use app\wechat\service\WechatService; use think\admin\Controller; use think\exception\HttpResponseException; @@ -27,12 +28,6 @@ use think\exception\HttpResponseException; */ class Keys extends Controller { - /** - * 绑定数据表 - * @var string - */ - private $table = 'WechatKeys'; - /** * 消息类型 * @var array @@ -64,7 +59,7 @@ class Keys extends Controller } // 数据列表分页处理 $this->title = '回复规则管理'; - $query = $this->_query($this->table)->whereNotIn('keys', ['subscribe', 'default']); + $query = WechatKeys::mQuery()->whereNotIn('keys', ['subscribe', 'default']); $query->equal('status')->like('keys,type')->dateBetween('create_at')->order('sort desc,id desc')->page(); } @@ -91,7 +86,7 @@ class Keys extends Controller { $this->_applyFormToken(); $this->title = '添加回复规则'; - $this->_form($this->table, 'form'); + WechatKeys::mForm('form'); } /** @@ -105,7 +100,7 @@ class Keys extends Controller { $this->_applyFormToken(); $this->title = '编辑回复规则'; - $this->_form($this->table, 'form'); + WechatKeys::mForm('form'); } /** @@ -116,7 +111,7 @@ class Keys extends Controller public function state() { $this->_applyFormToken(); - $this->_save($this->table, $this->_vali([ + WechatKeys::mSave($this->_vali([ 'status.in:0,1' => '状态值范围异常!', 'status.require' => '状态值不能为空!', ])); @@ -130,7 +125,7 @@ class Keys extends Controller public function remove() { $this->_applyFormToken(); - $this->_delete($this->table); + WechatKeys::mDelete(); } /** @@ -145,7 +140,7 @@ class Keys extends Controller { $this->_applyFormToken(); $this->title = '编辑订阅回复规则'; - $this->_form($this->table, 'form', 'keys', [], ['keys' => 'subscribe']); + WechatKeys::mForm('form', 'keys', [], ['keys' => 'subscribe']); } /** @@ -160,7 +155,7 @@ class Keys extends Controller { $this->_applyFormToken(); $this->title = '编辑默认回复规则'; - $this->_form($this->table, 'form', 'keys', [], ['keys' => 'default']); + WechatKeys::mForm('form', 'keys', [], ['keys' => 'default']); } /** @@ -171,7 +166,7 @@ class Keys extends Controller { if ($this->request->isPost()) { $map = [['keys', '=', $data['keys']], ['id', '<>', $data['id'] ?? 0]]; - if ($this->app->db->name($this->table)->where($map)->count() > 0) { + if (WechatKeys::mk()->where($map)->count() > 0) { $this->error('该关键字已经存在!'); } } elseif ($this->request->isGet()) { diff --git a/app/wechat/controller/Menu.php b/app/wechat/controller/Menu.php index 6644444f0..9d72981f3 100644 --- a/app/wechat/controller/Menu.php +++ b/app/wechat/controller/Menu.php @@ -165,5 +165,4 @@ class Menu extends Controller return []; } } - -} +} \ No newline at end of file diff --git a/app/wechat/controller/News.php b/app/wechat/controller/News.php index 015040223..fdb54e0d7 100644 --- a/app/wechat/controller/News.php +++ b/app/wechat/controller/News.php @@ -16,6 +16,8 @@ namespace app\wechat\controller; +use app\wechat\model\WechatNews; +use app\wechat\model\WechatNewsArticle; use app\wechat\service\MediaService; use think\admin\Controller; use think\admin\service\AdminService; @@ -27,13 +29,6 @@ use think\admin\service\AdminService; */ class News extends Controller { - - /** - * 设置默认操作表 - * @var string - */ - private $table = 'WechatNews'; - /** * 微信图文管理 * @auth true @@ -45,7 +40,7 @@ class News extends Controller public function index() { $this->title = '微信图文列表'; - $this->_query($this->table)->where(['is_deleted' => 0])->order('id desc')->page(); + WechatNews::mQuery()->where(['is_deleted' => 0])->order('id desc')->page(); } /** @@ -89,7 +84,7 @@ class News extends Controller 'create_by' => AdminService::instance()->getUserId(), 'article_id' => $this->_buildArticle($this->request->post('data', [])), ]; - if ($this->app->db->name($this->table)->insert($update) !== false) { + if (WechatNews::mk()->insert($update) !== false) { $this->success('图文添加成功!', 'javascript:history.back()'); } else { $this->error('图文添加失败,请稍候再试!'); @@ -118,7 +113,7 @@ class News extends Controller } else { $ids = $this->_buildArticle($this->request->post('data', [])); [$map, $data] = [['id' => $this->id], ['article_id' => $ids]]; - if ($this->app->db->name($this->table)->where($map)->update($data) !== false) { + if (WechatNews::mk()->where($map)->update($data) !== false) { $this->success('更新成功!', 'javascript:history.back()'); } else { $this->error('更新失败,请稍候再试!'); @@ -133,32 +128,30 @@ class News extends Controller */ public function remove() { - $this->_delete($this->table); + WechatNews::mDelete(); } /** * 图文更新操作 * @param array $data - * @param array $ids * @return string - * @throws \think\db\exception\DbException */ - private function _buildArticle(array $data, array $ids = []): string + private function _buildArticle(array $data): string { + $ids = []; foreach ($data as $vo) { if (empty($vo['digest'])) { $vo['digest'] = mb_substr(strip_tags(str_replace(["\s", ' '], '', $vo['content'])), 0, 120); } $vo['create_at'] = date('Y-m-d H:i:s'); if (empty($vo['id'])) { - $result = $id = $this->app->db->name('WechatNewsArticle')->insertGetId($vo); + $result = $id = WechatNewsArticle::mk()->insertGetId($vo); } else { $id = intval($vo['id']); - $result = $this->app->db->name('WechatNewsArticle')->where('id', $id)->update($vo); + $result = WechatNewsArticle::mk()->where('id', $id)->update($vo); } if ($result !== false) array_push($ids, $id); } return join(',', $ids); } - -} +} \ No newline at end of file diff --git a/app/wechat/controller/api/View.php b/app/wechat/controller/api/View.php index fbcdfcd2e..fc58891bd 100644 --- a/app/wechat/controller/api/View.php +++ b/app/wechat/controller/api/View.php @@ -16,6 +16,7 @@ namespace app\wechat\controller\api; +use app\wechat\model\WechatNewsArticle; use app\wechat\service\MediaService; use think\admin\Controller; @@ -51,10 +52,10 @@ class View extends Controller public function item($id = 0) { $map = ['id' => $id ?: input('id', 0)]; - $this->app->db->name('WechatNewsArticle')->where($map)->update([ + WechatNewsArticle::mk()->where($map)->update([ 'read_num' => $this->app->db->raw('read_num+1'), ]); - $this->info = $this->app->db->name('WechatNewsArticle')->where($map)->find(); + $this->info = WechatNewsArticle::mk()->where($map)->find(); $this->fetch(); } diff --git a/app/wechat/model/WechatAuto.php b/app/wechat/model/WechatAuto.php new file mode 100644 index 000000000..945ad3810 --- /dev/null +++ b/app/wechat/model/WechatAuto.php @@ -0,0 +1,28 @@ +app->db->name('WechatAuto')->where(['status' => 1])->order('time asc')->cursor() as $vo) { + foreach (WechatAuto::mk()->where(['status' => 1])->order('time asc')->cursor() as $vo) { [$name, $time] = ["推送客服消息 {$vo['code']}#{$openid}", $this->parseTimeString($vo['time'])]; QueueService::instance()->register($name, "xadmin:fansmsg {$openid} {$vo['code']}", $time, []); } diff --git a/app/wechat/service/FansService.php b/app/wechat/service/FansService.php index 5b4e84aeb..1a65108dc 100644 --- a/app/wechat/service/FansService.php +++ b/app/wechat/service/FansService.php @@ -16,6 +16,7 @@ namespace app\wechat\service; +use app\wechat\model\WechatFans; use think\admin\Service; /** @@ -46,7 +47,7 @@ class FansService extends Service if ($appid !== '') $user['appid'] = $appid; unset($user['privilege'], $user['groupid']); $this->app->event->trigger('WechatFansUpdate', $user); - return !!data_save('WechatFans', $user, 'openid'); + return !!data_save(WechatFans::class, $user, 'openid'); } /** @@ -59,7 +60,7 @@ class FansService extends Service */ public function get(string $openid): array { - return $this->app->db->name('WechatFans')->where(['openid' => $openid])->find() ?: []; + $user = WechatFans::mk()->where(['openid' => $openid])->find(); + return empty($user) ? [] : $user->toArray(); } - } diff --git a/app/wechat/service/MediaService.php b/app/wechat/service/MediaService.php index 2989cc35f..efe89f4e0 100644 --- a/app/wechat/service/MediaService.php +++ b/app/wechat/service/MediaService.php @@ -16,6 +16,8 @@ namespace app\wechat\service; +use app\wechat\model\WechatNews; +use app\wechat\model\WechatNewsArticle; use think\admin\Service; use think\admin\Storage; use WeChat\Contracts\MyCurlFile; @@ -40,14 +42,14 @@ class MediaService extends Service { // 文章主体数据 $where = ['id' => $id, 'is_deleted' => 0]; - $data = $this->app->db->name('WechatNews')->where($where)->where($map)->find(); + $data = WechatNews::mk()->where($where)->where($map)->find(); if (empty($data)) return []; // 文章内容编号 $data['articles'] = []; $data['articleids'] = str2arr($data['article_id']); if (empty($data['articleids'])) return $data; // 文章内容集合 - $query = $this->app->db->name('WechatNewsArticle'); + $query = WechatNewsArticle::mk(); $query->whereIn('id', $data['articleids'])->orderField('id', $data['articleids']); $data['articles'] = $query->withoutField('create_by,create_at')->select()->toArray(); return $data; diff --git a/app/wechat/service/WechatService.php b/app/wechat/service/WechatService.php index e4f5fd0a6..4e2c19e82 100644 --- a/app/wechat/service/WechatService.php +++ b/app/wechat/service/WechatService.php @@ -213,7 +213,7 @@ class WechatService extends Service * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function getWebOauthInfo(string $source, $isfull = 0, $redirect = true): array + public function getWebOauthInfo(string $source, int $isfull = 0, bool $redirect = true): array { $appid = $this->getAppid(); $openid = $this->app->session->get("{$appid}_openid");