Fans.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.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\queue\WechatQueue;
  16. use app\wechat\service\WechatService;
  17. use library\Controller;
  18. use think\Db;
  19. use think\exception\HttpResponseException;
  20. /**
  21. * 微信粉丝管理
  22. * Class Fans
  23. * @package app\wechat\controller
  24. */
  25. class Fans extends Controller
  26. {
  27. /**
  28. * 绑定数据表
  29. * @var string
  30. */
  31. protected $table = 'WechatFans';
  32. /**
  33. * 微信粉丝管理
  34. * @auth true
  35. * @menu true
  36. * @throws \think\Exception
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. * @throws \think\exception\DbException
  40. * @throws \think\exception\PDOException
  41. */
  42. public function index()
  43. {
  44. $this->title = '微信粉丝管理';
  45. $this->where = ['appid' => WechatService::getAppid()];
  46. $query = $this->_query($this->table)->like('nickname')->equal('subscribe,is_black');
  47. $query->dateBetween('subscribe_at')->where($this->where)->order('subscribe_time desc')->page();
  48. }
  49. /**
  50. * 列表数据处理
  51. * @param array $data
  52. */
  53. protected function _index_page_filter(array &$data)
  54. {
  55. $tags = Db::name('WechatFansTags')->column('id,name');
  56. foreach ($data as &$user) {
  57. $user['tags'] = [];
  58. foreach (explode(',', $user['tagid_list']) as $tagid) {
  59. if (isset($tags[$tagid])) $user['tags'][] = $tags[$tagid];
  60. }
  61. }
  62. }
  63. /**
  64. * 批量拉黑粉丝
  65. * @auth true
  66. */
  67. public function setBlack()
  68. {
  69. try {
  70. $this->applyCsrfToken();
  71. foreach (array_chunk(explode(',', $this->request->post('openid')), 20) as $openids) {
  72. WechatService::WeChatUser()->batchBlackList($openids);
  73. Db::name('WechatFans')->whereIn('openid', $openids)->update(['is_black' => '1']);
  74. }
  75. $this->success('拉黑粉丝信息成功!');
  76. } catch (HttpResponseException $exception) {
  77. throw $exception;
  78. } catch (\Exception $e) {
  79. $this->error("拉黑粉丝信息失败,请稍候再试!{$e->getMessage()}");
  80. }
  81. }
  82. /**
  83. * 取消拉黑粉丝
  84. * @auth true
  85. */
  86. public function delBlack()
  87. {
  88. try {
  89. $this->applyCsrfToken();
  90. foreach (array_chunk(explode(',', $this->request->post('openid')), 20) as $openids) {
  91. WechatService::WeChatUser()->batchUnblackList($openids);
  92. Db::name('WechatFans')->whereIn('openid', $openids)->update(['is_black' => '0']);
  93. }
  94. $this->success('取消拉黑粉丝信息成功!');
  95. } catch (HttpResponseException $exception) {
  96. throw $exception;
  97. } catch (\Exception $e) {
  98. $this->error("取消拉黑粉丝信息失败,请稍候再试!{$e->getMessage()}");
  99. }
  100. }
  101. /**
  102. * 同步粉丝列表
  103. * @auth true
  104. */
  105. public function sync()
  106. {
  107. try {
  108. $this->appid = WechatService::getAppid();
  109. sysoplog('微信管理', "创建微信[{$this->appid}]粉丝同步任务");
  110. sysqueue("同步[{$this->appid}]粉丝列表", WechatQueue::URI, 0, ['appid' => $this->appid], 0);
  111. $this->success('创建同步粉丝任务成功,需要时间来完成。<br>请到 系统管理 > 任务管理 查看执行进度!');
  112. } catch (HttpResponseException $exception) {
  113. throw $exception;
  114. } catch (\Exception $e) {
  115. $this->error("创建同步粉丝任务失败,请稍候再试!<br> {$e->getMessage()}");
  116. }
  117. }
  118. /**
  119. * 删除粉丝信息
  120. * @auth true
  121. * @throws \think\Exception
  122. * @throws \think\exception\PDOException
  123. */
  124. public function remove()
  125. {
  126. $this->applyCsrfToken();
  127. $this->_delete($this->table);
  128. }
  129. }