Fans.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2021 广州楚才信息科技有限公司 [ 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\service\WechatService;
  17. use think\admin\Controller;
  18. use think\exception\HttpResponseException;
  19. /**
  20. * 微信用户管理
  21. * Class Fans
  22. * @package app\wechat\controller
  23. */
  24. class Fans extends Controller
  25. {
  26. /**
  27. * 绑定数据表
  28. * @var string
  29. */
  30. private $table = 'WechatFans';
  31. /**
  32. * 微信用户管理
  33. * @auth true
  34. * @menu true
  35. * @throws \think\admin\Exception
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function index()
  41. {
  42. $this->title = '微信用户管理';
  43. $this->where = ['appid' => WechatService::instance()->getAppid()];
  44. $query = $this->_query($this->table)->like('nickname')->equal('subscribe,is_black');
  45. $query->dateBetween('subscribe_at')->where($this->where)->order('subscribe_time desc')->page();
  46. }
  47. /**
  48. * 列表数据处理
  49. * @param array $data
  50. */
  51. protected function _index_page_filter(array &$data)
  52. {
  53. $tags = $this->app->db->name('WechatFansTags')->column('name', 'id');
  54. foreach ($data as &$vo) {
  55. $vo['tags'] = [];
  56. foreach (explode(',', $vo['tagid_list']) as $tagid) {
  57. if (isset($tags[$tagid])) $vo['tags'][] = $tags[$tagid];
  58. }
  59. }
  60. }
  61. /**
  62. * 同步用户数据
  63. * @auth true
  64. */
  65. public function sync()
  66. {
  67. sysoplog('微信授权配置', '创建粉丝用户同步任务');
  68. $this->_queue('同步微信用户数据', "xadmin:fansall");
  69. }
  70. /**
  71. * 清空用户数据
  72. * @auth true
  73. */
  74. public function truncate()
  75. {
  76. try {
  77. $this->_query('WechatFans')->empty();
  78. $this->_query('WechatFansTags')->empty();
  79. $this->success('清空用户数据成功!');
  80. } catch (\think\exception\HttpResponseException $exception) {
  81. throw $exception;
  82. } catch (\Exception $exception) {
  83. $this->error("清空用户数据失败,{$exception->getMessage()}");
  84. }
  85. }
  86. /**
  87. * 删除用户信息
  88. * @auth true
  89. * @throws \think\db\exception\DbException
  90. */
  91. public function remove()
  92. {
  93. $this->_applyFormToken();
  94. $this->_delete($this->table);
  95. }
  96. /**
  97. * 用户拉入黑名单
  98. * @auth true
  99. */
  100. public function blackAdd()
  101. {
  102. try {
  103. $this->_applyFormToken();
  104. foreach (array_chunk(explode(',', $this->request->post('openid')), 20) as $openids) {
  105. WechatService::WeChatUser()->batchBlackList($openids);
  106. $this->app->db->name('WechatFans')->whereIn('openid', $openids)->update(['is_black' => '1']);
  107. }
  108. $this->success('拉入黑名单成功!');
  109. } catch (HttpResponseException $exception) {
  110. throw $exception;
  111. } catch (\Exception $exception) {
  112. $this->error("拉入黑名单失败,请稍候再试!<br>{$exception->getMessage()}");
  113. }
  114. }
  115. /**
  116. * 用户移出黑名单
  117. * @auth true
  118. */
  119. public function blackDel()
  120. {
  121. try {
  122. $this->_applyFormToken();
  123. foreach (array_chunk(explode(',', $this->request->post('openid')), 20) as $openids) {
  124. WechatService::WeChatUser()->batchUnblackList($openids);
  125. $this->app->db->name('WechatFans')->whereIn('openid', $openids)->update(['is_black' => '0']);
  126. }
  127. $this->success('移出黑名单成功!');
  128. } catch (HttpResponseException $exception) {
  129. throw $exception;
  130. } catch (\Exception $exception) {
  131. $this->error("移出黑名单失败,请稍候再试!<br>{$exception->getMessage()}");
  132. }
  133. }
  134. }