Fans.php 4.0 KB

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