Fans.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\admin\controller\shopro\wechat;
  3. use app\common\controller\Backend;
  4. /**
  5. * 微信管理
  6. *
  7. * @icon fa fa-circle-o
  8. */
  9. class Fans extends Backend
  10. {
  11. /**
  12. * Wechat模型对象
  13. * @var \app\admin\model\shopro\wechat\Fans
  14. */
  15. protected $model = null;
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->model = new \app\admin\model\shopro\wechat\Fans;
  20. }
  21. /**
  22. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  23. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  24. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  25. */
  26. /**
  27. * 查看
  28. */
  29. public function index()
  30. {
  31. //当前是否为关联查询
  32. $this->relationSearch = false;
  33. //设置过滤方法
  34. $this->request->filter(['strip_tags', 'trim']);
  35. if ($this->request->isAjax()) {
  36. //如果发送的来源是Selectpage,则转发到Selectpage
  37. if ($this->request->request('keyField')) {
  38. return $this->selectpage();
  39. }
  40. $searchWhere = $this->request->request('searchWhere');
  41. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  42. $total = $this->model
  43. ->where($where)
  44. ->whereOr('id', '=', $searchWhere)
  45. ->whereOr('nickname', 'like', "%$searchWhere%")
  46. ->whereOr('country', 'like', "%$searchWhere%")
  47. ->whereOr('province', 'like', "%$searchWhere%")
  48. ->whereOr('city', 'like', "%$searchWhere%")
  49. ->order($sort, $order)
  50. ->count();
  51. $list = $this->model
  52. ->where($where)
  53. ->whereOr('id', '=', $searchWhere)
  54. ->whereOr('nickname', 'like', "%$searchWhere%")
  55. ->whereOr('country', 'like', "%$searchWhere%")
  56. ->whereOr('province', 'like', "%$searchWhere%")
  57. ->whereOr('city', 'like', "%$searchWhere%")
  58. ->order($sort, $order)
  59. ->limit($offset, $limit)
  60. ->select();
  61. $list = collection($list)->toArray();
  62. $result = array("total" => $total, "rows" => $list);
  63. $this->success('查看粉丝', null, $result);
  64. }
  65. return $this->view->fetch();
  66. }
  67. //同步粉丝
  68. public function syncfans()
  69. {
  70. $wechatFans = new \app\admin\model\shopro\wechat\Fans;
  71. // 检测队列
  72. checkEnv('queue');
  73. try {
  74. //批量更新粉色关注状态为未关注
  75. $this->model->where('subscribe', 1)->update(['subscribe' => 0]);
  76. $wechat = new \addons\shopro\library\Wechat('wxOfficialAccount');
  77. $res = $wechat->asyncFans();
  78. } catch (\Exception $e) { //如书写为(Exception $e)将无效
  79. if (strpos($e->getMessage(), 'ip')) {
  80. $this->error('请将当前IP地址加入公众号后台IP白名单');
  81. }
  82. $this->error($e->getMessage());
  83. }
  84. $this->success($res['msg']);
  85. }
  86. public function user()
  87. {
  88. $get = $this->request->get();
  89. $user = \think\Db::name('shopro_user_oauth')->where([
  90. 'platform' => 'wxOfficialAccount',
  91. 'openid' => $get['openid'],
  92. ])->find();
  93. if ($user) {
  94. $user_id = $user['user_id'];
  95. $this->success('找到用户', null, $user_id);
  96. } else {
  97. $this->error('暂未成为商城用户');
  98. }
  99. }
  100. }