Fans.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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\ToolsService;
  17. use service\WechatService;
  18. use think\Db;
  19. /**
  20. * 微信粉丝管理
  21. * Class Fans
  22. * @package app\wechat\controller
  23. * @author Anyon <zoujingli@qq.com>
  24. * @date 2017/03/27 14:43
  25. */
  26. class Fans extends BasicAdmin
  27. {
  28. /**
  29. * 定义当前默认数据表
  30. * @var string
  31. */
  32. public $table = 'WechatFans';
  33. /**
  34. * 显示粉丝列表
  35. * @return array|string
  36. */
  37. public function index()
  38. {
  39. $this->title = '微信粉丝管理';
  40. $get = $this->request->get();
  41. $db = Db::name($this->table)->where('is_back', '0')->order('subscribe_time desc');
  42. (isset($get['sex']) && $get['sex'] !== '') && $db->where('sex', $get['sex']);
  43. foreach (['nickname', 'country', 'province', 'city'] as $key) {
  44. (isset($get[$key]) && $get[$key] !== '') && $db->where($key, 'like', "%{$get[$key]}%");
  45. }
  46. if (isset($get['tag']) && $get['tag'] !== '') {
  47. $db->where("concat(',',tagid_list,',') like :tag", ['tag' => "%,{$get['tag']},%"]);
  48. }
  49. return parent::_list($db);
  50. }
  51. /**
  52. * 列表数据处理
  53. * @param type $list
  54. */
  55. protected function _data_filter(&$list)
  56. {
  57. $tags = Db::name('WechatFansTags')->column('id,name');
  58. foreach ($list as &$vo) {
  59. list($vo['tags_list'], $vo['nickname']) = [[], ToolsService::emojiDecode($vo['nickname'])];
  60. foreach (explode(',', $vo['tagid_list']) as $tag) {
  61. if ($tag !== '' && isset($tags[$tag])) {
  62. $vo['tags_list'][$tag] = $tags[$tag];
  63. } elseif ($tag !== '') {
  64. $vo['tags_list'][$tag] = $tag;
  65. }
  66. }
  67. }
  68. $this->assign('tags', $tags);
  69. }
  70. /**
  71. * 黑名单列表
  72. */
  73. public function back()
  74. {
  75. $this->title = '微信粉丝黑名单管理';
  76. $get = $this->request->get();
  77. $db = Db::name($this->table)->where('is_back', '1')->order('subscribe_time desc');
  78. (isset($get['sex']) && $get['sex'] !== '') && $db->where('sex', $get['sex']);
  79. foreach (['nickname', 'country', 'province', 'city'] as $key) {
  80. (isset($get[$key]) && $get[$key] !== '') && $db->where($key, 'like', "%{$get[$key]}%");
  81. }
  82. if (isset($get['tag']) && $get['tag'] !== '') {
  83. $db->where("concat(',',tagid_list,',') like :tag", ['tag' => "%,{$get['tag']},%"]);
  84. }
  85. return parent::_list($db);
  86. }
  87. /**
  88. * 设置黑名单
  89. */
  90. public function backadd()
  91. {
  92. $wechat = load_wechat('User');
  93. $openids = $this->_getActionOpenids();
  94. if (false !== $wechat->addBacklist($openids)) {
  95. Db::name($this->table)->where('openid', 'in', $openids)->setField('is_back', '1');
  96. $this->success("已成功将 " . count($openids) . " 名粉丝移到黑名单!", '');
  97. }
  98. $this->error("设备黑名单失败,请稍候再试!{$wechat->errMsg}[{$wechat->errCode}]");
  99. }
  100. /**
  101. * 标签选择
  102. */
  103. public function tagset()
  104. {
  105. $tags = $this->request->post('tags', '');
  106. $fans_id = $this->request->post('fans_id', '');
  107. $fans = Db::name('WechatFans')->where('id', $fans_id)->find();
  108. empty($fans) && $this->error('需要操作的数据不存在!');
  109. $wechat = load_wechat('User');
  110. foreach (explode(',', $fans['tagid_list']) as $tagid) {
  111. is_numeric($tagid) && $wechat->batchDeleteUserTag($tagid, [$fans['openid']]);
  112. }
  113. foreach (explode(',', $tags) as $tagid) {
  114. is_numeric($tagid) && $wechat->batchAddUserTag($tagid, [$fans['openid']]);
  115. }
  116. if (false !== Db::name('WechatFans')->where('id', $fans_id)->setField('tagid_list', $tags)) {
  117. $this->success('粉丝标签成功!', '');
  118. }
  119. $this->error('粉丝标签设置失败, 请稍候再试!');
  120. }
  121. /**
  122. * 取消黑名
  123. */
  124. public function backdel()
  125. {
  126. $wechat = load_wechat('User');
  127. $openids = $this->_getActionOpenids();
  128. if (false !== $wechat->delBacklist($openids)) {
  129. Db::name($this->table)->where('openid', 'in', $openids)->setField('is_back', '0');
  130. $this->success("已成功将 " . count($openids) . " 名粉丝从黑名单中移除!", '');
  131. }
  132. $this->error("设备黑名单失败,请稍候再试!{$wechat->errMsg}[{$wechat->errCode}]");
  133. }
  134. /**
  135. * 给粉丝增加标签
  136. */
  137. public function tagadd()
  138. {
  139. $tagid = $this->request->post('tag_id', 0);
  140. empty($tagid) && $this->error('没有可能操作的标签ID');
  141. $openids = $this->_getActionOpenids();
  142. $wechat = load_wechat('User');
  143. if (false !== $wechat->batchAddUserTag($tagid, $openids)) {
  144. $this->success('设置粉丝标签成功!', '');
  145. }
  146. $this->error("设置粉丝标签失败, 请稍候再试! {$wechat->errMsg}[{$wechat->errCode}]");
  147. }
  148. /**
  149. * 移除粉丝标签
  150. */
  151. public function tagdel()
  152. {
  153. $tagid = $this->request->post('tag_id', 0);
  154. empty($tagid) && $this->error('没有可能操作的标签ID');
  155. $openids = $this->_getActionOpenids();
  156. $wechat = load_wechat('User');
  157. if (false !== $wechat->batchDeleteUserTag($tagid, $openids)) {
  158. $this->success('删除粉丝标签成功!', '');
  159. }
  160. $this->error("删除粉丝标签失败, 请稍候再试! {$wechat->errMsg}[{$wechat->errCode}]");
  161. }
  162. /**
  163. * 获取当前操作用户openid数组
  164. * @return array
  165. */
  166. private function _getActionOpenids()
  167. {
  168. $ids = $this->request->post('id', '');
  169. empty($ids) && $this->error('没有需要操作的数据!');
  170. $openids = Db::name($this->table)->where('id', 'in', explode(',', $ids))->column('openid');
  171. empty($openids) && $this->error('没有需要操作的数据!');
  172. return $openids;
  173. }
  174. /**
  175. * 同步粉丝列表
  176. */
  177. public function sync()
  178. {
  179. Db::name($this->table)->where('1=1')->delete();
  180. if (WechatService::syncAllFans('')) {
  181. WechatService::syncBlackFans('');
  182. LogService::write('微信管理', '同步全部微信粉丝成功');
  183. $this->success('同步获取所有粉丝成功!', '');
  184. }
  185. $this->error('同步获取粉丝失败,请稍候再试!');
  186. }
  187. }