Fans.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. */
  50. public function back() {
  51. $this->title = '微信粉丝黑名单管理';
  52. $db = Db::name($this->table)->where('is_back', '1')->order('id desc');
  53. $get = $this->request->get();
  54. !empty($get['sex']) && $db->where('sex', $get['sex']);
  55. foreach (['nickname', 'country', 'province', 'city'] as $key) {
  56. if (isset($get[$key]) && $get[$key] !== '') {
  57. $db->where($key, 'like', "%{$get[$key]}%");
  58. }
  59. }
  60. return parent::_list($db);
  61. }
  62. /**
  63. * 设置黑名单
  64. */
  65. public function backadd() {
  66. $ids = $this->request->post('id', '');
  67. empty($ids) && $this->error('没有需要操作的数据!');
  68. $openids = Db::name($this->table)->where('id', 'in', explode(',', $ids))->column('openid');
  69. empty($openids) && $this->error('没有需要操作的数据!');
  70. $wechat = & load_wechat('User');
  71. if (false !== $wechat->addBacklist($openids)) {
  72. Db::name($this->table)->where('openid', 'in', $openids)->setField('is_back', '1');
  73. $this->success("已成功将 " . count($openids) . " 名粉丝移到黑名单!", '');
  74. }
  75. $this->error("设备黑名单失败,请稍候再试!{$wechat->errMsg}[{$wechat->errCode}]");
  76. }
  77. /**
  78. * 取消黑名
  79. */
  80. public function backdel() {
  81. $ids = $this->request->post('id', '');
  82. empty($ids) && $this->error('没有需要操作的数据!');
  83. $openids = Db::name($this->table)->where('id', 'in', explode(',', $ids))->column('openid');
  84. empty($openids) && $this->error('没有需要操作的数据!');
  85. $wechat = & load_wechat('User');
  86. if (false !== $wechat->delBacklist($openids)) {
  87. Db::name($this->table)->where('openid', 'in', $openids)->setField('is_back', '0');
  88. $this->success("已成功将 " . count($openids) . " 名粉丝从黑名单中移除!", '');
  89. }
  90. $this->error("设备黑名单失败,请稍候再试!{$wechat->errMsg}[{$wechat->errCode}]");
  91. }
  92. /**
  93. * 同步粉丝列表
  94. */
  95. public function sync() {
  96. Db::name($this->table)->where('1=1')->delete();
  97. if (WechatService::syncAllFans('')) {
  98. WechatService::syncBlackFans('');
  99. LogService::write('微信管理', '同步全部微信粉丝成功');
  100. $this->success('同步获取所有粉丝成功!', '');
  101. }
  102. $this->error('同步获取粉丝失败,请稍候再试!');
  103. }
  104. }