Vipreply.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2020/3/23
  6. * Time: 下午5:51
  7. */
  8. namespace app\admin\controller\wechat;
  9. use app\common\controller\Backend;
  10. use app\common\library\Redis;
  11. use app\common\model\AutoreplyCollect;
  12. use app\common\model\WechatAutoreply;
  13. use app\main\constants\AdminConstants;
  14. use app\main\constants\CacheConstants;
  15. use app\main\constants\ErrorCodeConstants;
  16. use app\main\constants\SubscribeDelayConstants;
  17. use app\main\service\AdminService;
  18. use app\main\service\AutoreplyService;
  19. use app\main\service\ReplyVipService;
  20. use app\main\service\SubscribeVipDelayService;
  21. use app\main\service\UrlService;
  22. use app\main\service\VisitLimitService;
  23. use think\Collection;
  24. class Vipreply extends Backend
  25. {
  26. /**
  27. * @var WechatAutoreply
  28. */
  29. protected $model = null;
  30. protected $noNeedRight = ['detail'];
  31. public function _initialize()
  32. {
  33. parent::_initialize();
  34. $this->model = model('WechatAutoreply');
  35. }
  36. /**
  37. * 查看
  38. */
  39. public function index()
  40. {
  41. //设置过滤方法
  42. $this->request->filter(['strip_tags']);
  43. if ($this->request->isAjax())
  44. {
  45. //如果发送的来源是Selectpage,则转发到Selectpage
  46. if ($this->request->request('pkey_name'))
  47. {
  48. return $this->selectpage();
  49. }
  50. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  51. $total = $this->model
  52. ->where($where)
  53. ->whereNotIn('title', ['签到', 'subscribe'])
  54. ->where('admin_id', $this->auth->id)
  55. ->order($sort, $order)
  56. ->count();
  57. $list = $this->model
  58. ->where($where)
  59. ->whereNotIn('title', ['签到', 'subscribe'])
  60. ->where('admin_id', $this->auth->id)
  61. ->order($sort, $order)
  62. ->limit($offset, $limit)
  63. ->select();
  64. if ($list) {
  65. $collect = new AutoreplyCollect();
  66. $ids = array_column($list, 'id');
  67. $rs = model('wechat_autoreply')->whereIn('pid', $ids)->column('id,pid');
  68. $ids = array_keys($rs);
  69. if (VisitLimitService::instance()->checkMigratedV2()) {
  70. $collect_list = AutoreplyService::instance()->getMigrateData($ids)->data;
  71. } else {
  72. $collect_list = $collect->whereIn('id', $ids)->column('id,uv,subscribe,money');
  73. }
  74. $list = Collection::make($list)->toArray();
  75. foreach ($list as $index => $item) {
  76. $list[$index]['uv'] = $list[$index]['subscribe'] = $list[$index]['money'] = 0;
  77. foreach ($rs as $id => $pid) {
  78. if ($pid == $item['id'] && array_key_exists($id, $collect_list)) {
  79. $list[$index]['uv'] += $collect_list[$id]['uv'];
  80. $list[$index]['subscribe'] += $collect_list[$id]['subscribe'];
  81. $list[$index]['money'] += $collect_list[$id]['money'];
  82. }
  83. }
  84. if (array_key_exists('books', $item) && $item['books']) {
  85. $list[$index]['books'] = json_decode($item['books'], true);
  86. }
  87. }
  88. }
  89. $result = array("total" => $total, "rows" => $list);
  90. return json($result);
  91. }
  92. return $this->view->fetch();
  93. }
  94. /**
  95. * 编辑
  96. */
  97. public function edit($ids = NULL)
  98. {
  99. $row = $this->model->get(['id' => $ids, 'admin_id' => $this->auth->id]);
  100. if (!$row)
  101. $this->error(__('No Results were found'));
  102. if ($row['title'] == 'subscribe')
  103. $this->error(__('请在新版被关注回复中设置关注回复内容'));
  104. if ($this->request->isPost())
  105. {
  106. $params = $this->request->post("row/a");
  107. if ($params)
  108. {
  109. $params['title'] = $params['text'];
  110. $row->save($params);
  111. $this->success();
  112. }
  113. $this->error();
  114. }
  115. $channel_id = $this->auth->agent_id ? $this->auth->agent_id : $this->auth->channel_id;
  116. if ($row['type'] == 'text') {
  117. $data = json_decode($row['text_content'], true);
  118. $data['news_content'] = [];
  119. } else {
  120. $data = json_decode($row['news_content'], true);
  121. $data['text_content'] = [];
  122. }
  123. if ($data['deploy_type'] == SubscribeDelayConstants::VIP_DEPLOY_GROUP) {
  124. $groupList = model('vip_group')->whereIn('id', $data['admin_group'])->column('id,id,name,createtime,updatetime');
  125. foreach($data['admin_group'] as $groupId){
  126. if (array_key_exists($groupId, $groupList)) {
  127. $data['selected'][$groupId] = $groupList[$groupId];
  128. }
  129. }
  130. }
  131. $data['text_tip_word'] = json_decode($data['text_tip_word'], true);
  132. $data['id'] = $row['id'];
  133. $data = UrlService::instance()->replaceReferralHost($channel_id, $data, false)->data;
  134. // echo \GuzzleHttp\json_encode($data, JSON_UNESCAPED_UNICODE);exit;
  135. $this->assignconfig("row", $data);
  136. return $this->view->fetch();
  137. }
  138. /**
  139. * 添加
  140. */
  141. public function add()
  142. {
  143. if ($this->request->isPost())
  144. {
  145. $params = json_decode($this->request->param('data'), true);
  146. $result = ReplyVipService::instance()->createReply($params, $this->auth->id);
  147. if ($result->code == ErrorCodeConstants::SUCCESS) {
  148. $this->success();
  149. } else {
  150. $this->error($result->msg);
  151. }
  152. }
  153. return $this->view->fetch();
  154. }
  155. public function detail($ids)
  156. {
  157. if ($this->request->isAjax()) {
  158. list($where, $sort, $order, $offset, $limit) = $this->buildparams(false,true);
  159. $total = $this->model->where('pid', $ids)
  160. ->alias('wa')
  161. ->join('admin a', 'a.id=wa.admin_id')
  162. ->where($where)
  163. ->limit($offset, $limit)
  164. ->count();
  165. if ($total) {
  166. $list = $this->model->where('pid', $ids)
  167. ->alias('wa')
  168. ->join('admin a', 'a.id=wa.admin_id')
  169. ->where($where)
  170. ->limit($offset, $limit)
  171. ->field('wa.id,wa.title,wa.createtime,wa.status,a.nickname')
  172. ->select();
  173. $collect = new AutoreplyCollect();
  174. $ids = array_column($list, 'id');
  175. $collect_list = $collect->whereIn('id', $ids)->column('id,uv,subscribe,money');
  176. foreach ($list as $index => $item) {
  177. $list[$index]['uv'] = $list[$index]['subscribe'] = $list[$index]['money'] = 0;
  178. if (array_key_exists($item['id'], $collect_list)) {
  179. $list[$index] = array_merge($list[$index]->getData(), $collect_list[$item['id']]);
  180. }
  181. }
  182. } else {
  183. $list = [];
  184. }
  185. return json(['total'=>$total, 'rows'=>$list]);
  186. }
  187. $this->assignconfig('ids', $ids);
  188. return $this->view->fetch();
  189. }
  190. }