Fans.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2022 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免费声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  13. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  14. // +----------------------------------------------------------------------
  15. namespace app\wechat\controller;
  16. use app\wechat\model\WechatFans;
  17. use app\wechat\service\WechatService;
  18. use think\admin\Controller;
  19. use think\admin\helper\QueryHelper;
  20. use think\exception\HttpResponseException;
  21. /**
  22. * 微信用户管理
  23. * Class Fans
  24. * @package app\wechat\controller
  25. */
  26. class Fans extends Controller
  27. {
  28. /**
  29. * 微信用户管理
  30. * @auth true
  31. * @menu true
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. */
  36. public function index()
  37. {
  38. WechatFans::mQuery()->layTable(function () {
  39. $this->title = '微信用户管理';
  40. }, function (QueryHelper $query) {
  41. $query->where(['appid' => WechatService::instance()->getAppid()]);
  42. $query->like('nickname')->equal('subscribe,is_black')->dateBetween('subscribe_at');
  43. });
  44. }
  45. /**
  46. * 列表数据处理
  47. * @param array $data
  48. */
  49. protected function _index_page_filter(array &$data)
  50. {
  51. foreach ($data as &$vo) $vo['subscribe_at'] = format_datetime($vo['subscribe_at']);
  52. }
  53. /**
  54. * 同步用户数据
  55. * @auth true
  56. */
  57. public function sync()
  58. {
  59. sysoplog('微信授权配置', '创建粉丝用户同步任务');
  60. $this->_queue('同步微信用户数据', "xadmin:fansall");
  61. }
  62. /**
  63. * 黑名单列表操作
  64. * @auth true
  65. */
  66. public function black()
  67. {
  68. try {
  69. $data = $this->_vali([
  70. 'black.require' => '操作类型不能为空!',
  71. 'openid.require' => '操作用户不能为空!',
  72. ]);
  73. foreach (array_chunk(str2arr($data['openid']), 20) as $openids) {
  74. if ($data['black']) {
  75. WechatService::WeChatUser()->batchBlackList($openids);
  76. WechatFans::mk()->whereIn('openid', $openids)->update(['is_black' => 1]);
  77. } else {
  78. WechatService::WeChatUser()->batchUnblackList($openids);
  79. WechatFans::mk()->whereIn('openid', $openids)->update(['is_black' => 0]);
  80. }
  81. }
  82. if (empty($data['black'])) {
  83. $this->success('移出黑名单成功!');
  84. } else {
  85. $this->success('拉入黑名单成功!');
  86. }
  87. } catch (HttpResponseException $exception) {
  88. throw $exception;
  89. } catch (\Exception $exception) {
  90. $this->error("黑名单操作失败,请稍候再试!<br>{$exception->getMessage()}");
  91. }
  92. }
  93. /**
  94. * 删除用户信息
  95. * @auth true
  96. */
  97. public function remove()
  98. {
  99. WechatFans::mDelete();
  100. }
  101. /**
  102. * 清空用户数据
  103. * @auth true
  104. */
  105. public function truncate()
  106. {
  107. try {
  108. WechatFans::mQuery()->empty();
  109. WechatFans::mQuery()->empty();
  110. $this->success('清空用户数据成功!');
  111. } catch (HttpResponseException $exception) {
  112. throw $exception;
  113. } catch (\Exception $exception) {
  114. $this->error("清空用户数据失败,{$exception->getMessage()}");
  115. }
  116. }
  117. }