Fans.php 3.8 KB

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