Fans.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. public $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('tags', $tags);
  64. }
  65. /**
  66. * 黑名单列表
  67. */
  68. public function back() {
  69. $this->title = '微信粉丝黑名单管理';
  70. $db = Db::name($this->table)->where('is_back', '1')->order('id desc');
  71. $get = $this->request->get();
  72. !empty($get['sex']) && $db->where('sex', $get['sex']);
  73. foreach (['nickname', 'country', 'province', 'city'] as $key) {
  74. if (isset($get[$key]) && $get[$key] !== '') {
  75. $db->where($key, 'like', "%{$get[$key]}%");
  76. }
  77. }
  78. return parent::_list($db);
  79. }
  80. /**
  81. * 设置黑名单
  82. */
  83. public function backadd() {
  84. $wechat = & load_wechat('User');
  85. $openids = $this->_getActionOpenids();
  86. if (false !== $wechat->addBacklist($openids)) {
  87. Db::name($this->table)->where('openid', 'in', $openids)->setField('is_back', '1');
  88. $this->success("已成功将 " . count($openids) . " 名粉丝移到黑名单!", '');
  89. }
  90. $this->error("设备黑名单失败,请稍候再试!{$wechat->errMsg}[{$wechat->errCode}]");
  91. }
  92. /**
  93. * 标签选择
  94. */
  95. public function tagset() {
  96. $tags = $this->request->post('tags', '');
  97. $fans_id = $this->request->post('fans_id', '');
  98. $fans = Db::name('WechatFans')->where('id', $fans_id)->find();
  99. empty($fans) && $this->error('需要操作的数据不存在!');
  100. $wechat = & load_wechat('User');
  101. foreach (explode(',', $fans['tagid_list']) as $tagid) {
  102. is_numeric($tagid) && $wechat->batchDeleteUserTag($tagid, [$fans['openid']]);
  103. }
  104. foreach (explode(',', $tags) as $tagid) {
  105. is_numeric($tagid) && $wechat->batchAddUserTag($tagid, [$fans['openid']]);
  106. }
  107. if (false !== Db::name('WechatFans')->where('id', $fans_id)->setField('tagid_list', $tags)) {
  108. $this->success('粉丝标签成功!', '');
  109. }
  110. $this->error('粉丝标签设置失败, 请稍候再试!');
  111. }
  112. /**
  113. * 取消黑名
  114. */
  115. public function backdel() {
  116. $wechat = & load_wechat('User');
  117. $openids = $this->_getActionOpenids();
  118. if (false !== $wechat->delBacklist($openids)) {
  119. Db::name($this->table)->where('openid', 'in', $openids)->setField('is_back', '0');
  120. $this->success("已成功将 " . count($openids) . " 名粉丝从黑名单中移除!", '');
  121. }
  122. $this->error("设备黑名单失败,请稍候再试!{$wechat->errMsg}[{$wechat->errCode}]");
  123. }
  124. /**
  125. * 给粉丝增加标签
  126. */
  127. public function tagadd() {
  128. $tagid = $this->request->post('tag_id', 0);
  129. empty($tagid) && $this->error('没有可能操作的标签ID');
  130. $openids = $this->_getActionOpenids();
  131. $wechat = & load_wechat('User');
  132. if (false !== $wechat->batchAddUserTag($tagid, $openids)) {
  133. $this->success('设置粉丝标签成功!', '');
  134. }
  135. $this->error("设置粉丝标签失败, 请稍候再试! {$wechat->errMsg}[{$wechat->errCode}]");
  136. }
  137. /**
  138. * 移除粉丝标签
  139. */
  140. public function tagdel() {
  141. $tagid = $this->request->post('tag_id', 0);
  142. empty($tagid) && $this->error('没有可能操作的标签ID');
  143. $openids = $this->_getActionOpenids();
  144. $wechat = & load_wechat('User');
  145. if (false !== $wechat->batchDeleteUserTag($tagid, $openids)) {
  146. $this->success('删除粉丝标签成功!', '');
  147. }
  148. $this->error("删除粉丝标签失败, 请稍候再试! {$wechat->errMsg}[{$wechat->errCode}]");
  149. }
  150. /**
  151. * 获取当前操作用户openid数组
  152. * @return array
  153. */
  154. private function _getActionOpenids() {
  155. $ids = $this->request->post('id', '');
  156. empty($ids) && $this->error('没有需要操作的数据!');
  157. $openids = Db::name($this->table)->where('id', 'in', explode(',', $ids))->column('openid');
  158. empty($openids) && $this->error('没有需要操作的数据!');
  159. return $openids;
  160. }
  161. /**
  162. * 同步粉丝列表
  163. */
  164. public function sync() {
  165. Db::name($this->table)->where('1=1')->delete();
  166. if (WechatService::syncAllFans('')) {
  167. WechatService::syncBlackFans('');
  168. LogService::write('微信管理', '同步全部微信粉丝成功');
  169. $this->success('同步获取所有粉丝成功!', '');
  170. }
  171. $this->error('同步获取粉丝失败,请稍候再试!');
  172. }
  173. }