Exportfans.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\library\Redis;
  5. use app\main\constants\CacheConstants;
  6. use think\Controller;
  7. use think\Request;
  8. /**
  9. * 粉丝导量配置管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Exportfans extends Backend
  14. {
  15. /**
  16. * @var \app\common\model\ExportFans
  17. */
  18. protected $model = null;
  19. protected $noNeedRight = ['upload'];
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = model('ExportFans');
  24. $this->view->assign('type', $this->request->param('type', 1));
  25. $this->assignconfig('type', $this->request->param('type', 1));
  26. $this->view->assign("statusList", $this->model->getStatusList());
  27. $this->view->assign("payStatusList", $this->model->getPayStatusList());
  28. }
  29. /**
  30. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  31. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  32. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  33. */
  34. /**
  35. * 查看
  36. */
  37. public function index()
  38. {
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags']);
  41. if ($this->request->isAjax())
  42. {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('pkey_name'))
  45. {
  46. return $this->selectpage();
  47. }
  48. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  49. $where_u = [
  50. 'type' => $this->request->get('type', 0)
  51. ];
  52. $total = $this->model
  53. ->where($where)
  54. ->where($where_u)
  55. ->order($sort, $order)
  56. ->count();
  57. $list = $this->model
  58. ->where($where)
  59. ->where($where_u)
  60. ->order($sort, $order)
  61. ->limit($offset, $limit)
  62. ->select();
  63. $result = array("total" => $total, "rows" => $list);
  64. return json($result);
  65. }
  66. return $this->view->fetch();
  67. }
  68. public function upload($ids)
  69. {
  70. if ($this->request->isPost()) {
  71. $filePath = ROOT_PATH . DS . 'public' . DS . $this->request->param('file');
  72. $list = file($filePath);
  73. $list = array_map(function ($v) {
  74. $v = trim($v);
  75. return $v;
  76. }, $list);
  77. $config = $this->model->field("limittime, type, starttime")->where('id', $ids)->find();
  78. $limittime = $config['limittime'];
  79. $starttime = $config['starttime'];
  80. $type = $config['type'];
  81. $count = 0;
  82. if ($type == '1') {
  83. //二期设置用户写入
  84. $expire_time = $limittime;
  85. foreach ($list as $id) {
  86. $key = CacheConstants::getLeadUser($id);
  87. $hashKey = 'id_'.$ids;
  88. $list = [];
  89. if ($data = Redis::instance()->hGetAll($key)) {
  90. //判断过期时间
  91. foreach ($data as $k => $row) {
  92. $item = json_decode($row, true);
  93. if ($item['expire_time'] <= time()) {
  94. //删除
  95. continue;
  96. }
  97. $expire_time = $expire_time < $item['expire_time'] ? $item['expire_time'] : $expire_time;
  98. $list[$k] = $row;
  99. }
  100. }
  101. //追加
  102. $list[$hashKey] = json_encode(['id' => $ids, 'expire_time' => $limittime, 'start_time'=>$starttime]);
  103. Redis::instance()->hMSet($key, $list);
  104. Redis::instance()->expire($key, $expire_time - time());
  105. $count++;
  106. }
  107. } else {
  108. foreach ($list as $id) {
  109. $key = CacheConstants::getExportFansUserCache($id);
  110. if ($exists = Redis::instance()->hGetAll($key)) {
  111. if ($exists['id'] > $ids) {
  112. continue;
  113. }
  114. }
  115. Redis::instance()->hMSet($key, ['id' => $ids, 'chapter' => '', 'expire' => $limittime]);
  116. Redis::instance()->expire($key, $limittime - time());
  117. $count++;
  118. }
  119. }
  120. if ($count) {
  121. $this->success('上传成功用户数' . $count);
  122. } else {
  123. $this->error();
  124. }
  125. }
  126. return $this->view->fetch();
  127. }
  128. /**
  129. * 编辑
  130. */
  131. public function edit($ids = NULL)
  132. {
  133. $row = $this->model->get($ids);
  134. if (!$row)
  135. $this->error(__('No Results were found'));
  136. $adminIds = $this->getDataLimitAdminIds();
  137. if (is_array($adminIds))
  138. {
  139. if (!in_array($row[$this->dataLimitField], $adminIds))
  140. {
  141. $this->error(__('You have no permission'));
  142. }
  143. }
  144. if ($this->request->isPost())
  145. {
  146. $params = $this->request->post("row/a");
  147. if ($params)
  148. {
  149. /*
  150. * 已经弃用,如果为了兼容老版可取消注释
  151. foreach ($params as $k => &$v)
  152. {
  153. $v = is_array($v) ? implode(',', $v) : $v;
  154. }
  155. */
  156. try
  157. {
  158. //是否采用模型验证
  159. if ($this->modelValidate)
  160. {
  161. $name = basename(str_replace('\\', '/', get_class($this->model)));
  162. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  163. $row->validate($validate);
  164. }
  165. $result = $row->allowField(true)->save($params);
  166. if ($result !== false)
  167. {
  168. $cache = CacheConstants::getExportFansRow($ids);
  169. Redis::instance()->del($cache);
  170. $this->success();
  171. }
  172. else
  173. {
  174. $this->error($row->getError());
  175. }
  176. }
  177. catch (\think\exception\PDOException $e)
  178. {
  179. $this->error($e->getMessage());
  180. }
  181. }
  182. $this->error(__('Parameter %s can not be empty', ''));
  183. }
  184. $this->view->assign("row", $row);
  185. return $this->view->fetch();
  186. }
  187. /**
  188. * 删除
  189. */
  190. public function del($ids = "")
  191. {
  192. if ($ids)
  193. {
  194. $pk = $this->model->getPk();
  195. $adminIds = $this->getDataLimitAdminIds();
  196. if (is_array($adminIds))
  197. {
  198. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  199. }
  200. $list = $this->model->where($pk, 'in', $ids)->select();
  201. $count = 0;
  202. foreach ($list as $k => $v)
  203. {
  204. $cache = CacheConstants::getExportFansRow($ids);
  205. Redis::instance()->del($cache);
  206. $count += $v->delete();
  207. }
  208. if ($count)
  209. {
  210. $this->success();
  211. }
  212. else
  213. {
  214. $this->error(__('No rows were deleted'));
  215. }
  216. }
  217. $this->error(__('Parameter %s can not be empty', 'ids'));
  218. }
  219. }