Fans.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. namespace app\wechat\controller;
  14. use controller\BasicAdmin;
  15. use service\LogService;
  16. use service\WechatService;
  17. use think\Db;
  18. /**
  19. * 微信粉丝管理
  20. * Class Fans
  21. * @package app\wechat\controller
  22. * @author Anyon <zoujingli@qq.com>
  23. * @date 2017/03/27 14:43
  24. */
  25. class Fans extends BasicAdmin {
  26. /**
  27. * 定义当前默认数据表
  28. * @var string
  29. */
  30. protected $table = 'WechatFans';
  31. /**
  32. * 显示粉丝列表
  33. * @return array|string
  34. */
  35. public function index() {
  36. $this->title = '微信粉丝管理';
  37. $db = Db::name($this->table)->where('is_back', '0')->order('id desc');
  38. $get = $this->request->get();
  39. !empty($get['sex']) && $db->where('sex', $get['sex']);
  40. foreach (['nickname', 'country', 'province', 'city'] as $key) {
  41. if (isset($get[$key]) && $get[$key] !== '') {
  42. $db->where($key, 'like', "%{$get[$key]}%");
  43. }
  44. }
  45. return parent::_list($db);
  46. }
  47. /**
  48. * 列表数据处理
  49. * @param type $list
  50. */
  51. protected function _data_filter(&$list) {
  52. $tags = Db::name('WechatFansTags')->column('id,name');
  53. foreach ($list as &$vo) {
  54. $vo['tags_list'] = [];
  55. foreach (explode(',', $vo['tagid_list']) as $tag) {
  56. if ($tag !== '' && isset($tags[$tag])) {
  57. $vo['tags_list'][$tag] = $tags[$tag];
  58. } elseif ($tag !== '') {
  59. $vo['tags_list'][$tag] = $tag;
  60. }
  61. }
  62. }
  63. $this->assign('alert', [
  64. 'type' => 'success',
  65. 'title' => '开发中',
  66. 'content' => '请稍候...'
  67. ]);
  68. }
  69. /**
  70. * 黑名单列表
  71. */
  72. public function back() {
  73. $this->title = '微信粉丝黑名单管理';
  74. $db = Db::name($this->table)->where('is_back', '1')->order('id desc');
  75. $get = $this->request->get();
  76. !empty($get['sex']) && $db->where('sex', $get['sex']);
  77. foreach (['nickname', 'country', 'province', 'city'] as $key) {
  78. if (isset($get[$key]) && $get[$key] !== '') {
  79. $db->where($key, 'like', "%{$get[$key]}%");
  80. }
  81. }
  82. return parent::_list($db);
  83. }
  84. /**
  85. * 设置黑名单
  86. */
  87. public function backadd() {
  88. $ids = $this->request->post('id', '');
  89. empty($ids) && $this->error('没有需要操作的数据!');
  90. $openids = Db::name($this->table)->where('id', 'in', explode(',', $ids))->column('openid');
  91. empty($openids) && $this->error('没有需要操作的数据!');
  92. $wechat = & load_wechat('User');
  93. if (false !== $wechat->addBacklist($openids)) {
  94. Db::name($this->table)->where('openid', 'in', $openids)->setField('is_back', '1');
  95. $this->success("已成功将 " . count($openids) . " 名粉丝移到黑名单!", '');
  96. }
  97. $this->error("设备黑名单失败,请稍候再试!{$wechat->errMsg}[{$wechat->errCode}]");
  98. }
  99. /**
  100. * 取消黑名
  101. */
  102. public function backdel() {
  103. $ids = $this->request->post('id', '');
  104. empty($ids) && $this->error('没有需要操作的数据!');
  105. $openids = Db::name($this->table)->where('id', 'in', explode(',', $ids))->column('openid');
  106. empty($openids) && $this->error('没有需要操作的数据!');
  107. $wechat = & load_wechat('User');
  108. if (false !== $wechat->delBacklist($openids)) {
  109. Db::name($this->table)->where('openid', 'in', $openids)->setField('is_back', '0');
  110. $this->success("已成功将 " . count($openids) . " 名粉丝从黑名单中移除!", '');
  111. }
  112. $this->error("设备黑名单失败,请稍候再试!{$wechat->errMsg}[{$wechat->errCode}]");
  113. }
  114. /**
  115. * 同步粉丝列表
  116. */
  117. public function sync() {
  118. Db::name($this->table)->where('1=1')->delete();
  119. if (WechatService::syncAllFans('')) {
  120. WechatService::syncBlackFans('');
  121. LogService::write('微信管理', '同步全部微信粉丝成功');
  122. $this->success('同步获取所有粉丝成功!', '');
  123. }
  124. $this->error('同步获取粉丝失败,请稍候再试!');
  125. }
  126. }