Floattips.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\service\LogService;
  4. use app\common\controller\Backend;
  5. use app\common\library\Redis;
  6. use app\main\constants\CacheConstants;
  7. use app\main\service\FloatTipsService;
  8. use think\Controller;
  9. use think\Request;
  10. /**
  11. * 浮动条
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class Floattips extends Backend
  16. {
  17. /**
  18. * @var \app\common\model\Floattips
  19. */
  20. protected $model = null;
  21. protected $noNeedRight = ['upload'];
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = model('Floattips');
  26. $this->view->assign("typeList", $this->model->getTypeList());
  27. $this->view->assign("statusList", $this->model->getStatusList());
  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. $total = $this->model
  50. ->where($where)
  51. ->order($sort, $order)
  52. ->count();
  53. $list = $this->model
  54. ->where($where)
  55. ->order($sort, $order)
  56. ->limit($offset, $limit)
  57. ->select();
  58. if ($list) {
  59. foreach ($list as $index => $item) {
  60. $item['recharge_user_day'] = (int)Redis::instance()->pfCount(CacheConstants::getFloatTipsOrderUserDay($item['id']));
  61. $item['recharge_money_day'] = (float)Redis::instance()->get(CacheConstants::getFloatTipsOrderMoneyDay($item['id']));
  62. }
  63. }
  64. $result = array("total" => $total, "rows" => $list);
  65. return json($result);
  66. }
  67. return $this->view->fetch();
  68. }
  69. /**
  70. * 添加
  71. */
  72. public function add()
  73. {
  74. if ($this->request->isPost())
  75. {
  76. $params = $this->request->post("row/a");
  77. switch ($params['type']){
  78. case 'activity':
  79. $params['link'] = '/s/'.$params['activity_id'];
  80. $params['book_id'] = $params['chapter_id'] = '';
  81. break;
  82. case 'book':
  83. $params['link'] = '/index/book/chapter?book_id='.$params['book_id'].'&sid='.$params['chapter_id'];
  84. break;
  85. case 'url':
  86. $params['book_id'] = $params['chapter_id'] = '';
  87. break;
  88. }
  89. if ($params)
  90. {
  91. /*
  92. * 已经弃用,如果为了兼容老版可取消注释
  93. foreach ($params as $k => &$v)
  94. {
  95. $v = is_array($v) ? implode(',', $v) : $v;
  96. }
  97. */
  98. if ($this->dataLimit)
  99. {
  100. $params[$this->dataLimitField] = $this->auth->id;
  101. }
  102. try
  103. {
  104. //是否采用模型验证
  105. if ($this->modelValidate)
  106. {
  107. $name = basename(str_replace('\\', '/', get_class($this->model)));
  108. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  109. $this->model->validate($validate);
  110. }
  111. $result = $this->model->allowField(true)->save($params);
  112. if ($result !== false)
  113. {
  114. $this->success();
  115. }
  116. else
  117. {
  118. $this->error($this->model->getError());
  119. }
  120. }
  121. catch (\think\exception\PDOException $e)
  122. {
  123. $this->error($e->getMessage());
  124. }
  125. }
  126. $this->error(__('Parameter %s can not be empty', ''));
  127. }
  128. $this->view->assign('activity', FloatTipsService::instance()->getSysActivity()->data);
  129. return $this->view->fetch();
  130. }
  131. /**
  132. * 编辑
  133. */
  134. public function edit($ids = NULL)
  135. {
  136. $row = $this->model->get($ids);
  137. if (!$row)
  138. $this->error(__('No Results were found'));
  139. $adminIds = $this->getDataLimitAdminIds();
  140. if (is_array($adminIds))
  141. {
  142. if (!in_array($row[$this->dataLimitField], $adminIds))
  143. {
  144. $this->error(__('You have no permission'));
  145. }
  146. }
  147. if ($this->request->isPost())
  148. {
  149. $params = $this->request->post("row/a");
  150. switch ($params['type']){
  151. case 'activity':
  152. $params['link'] = '/s/'.$params['activity_id'];
  153. $params['book_id'] = $params['chapter_id'] = '';
  154. break;
  155. case 'book':
  156. $params['link'] = '/index/book/chapter?book_id='.$params['book_id'].'&sid='.$params['chapter_id'];
  157. break;
  158. case 'url':
  159. $params['book_id'] = $params['chapter_id'] = '';
  160. break;
  161. }
  162. if ($params)
  163. {
  164. /*
  165. * 已经弃用,如果为了兼容老版可取消注释
  166. foreach ($params as $k => &$v)
  167. {
  168. $v = is_array($v) ? implode(',', $v) : $v;
  169. }
  170. */
  171. try
  172. {
  173. //是否采用模型验证
  174. if ($this->modelValidate)
  175. {
  176. $name = basename(str_replace('\\', '/', get_class($this->model)));
  177. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  178. $row->validate($validate);
  179. }
  180. $result = $row->allowField(true)->save($params);
  181. if ($result !== false)
  182. {
  183. Redis::instance()->del(CacheConstants::getFloatTipsInfo($ids));
  184. $this->success();
  185. }
  186. else
  187. {
  188. $this->error($row->getError());
  189. }
  190. }
  191. catch (\think\exception\PDOException $e)
  192. {
  193. $this->error($e->getMessage());
  194. }
  195. }
  196. $this->error(__('Parameter %s can not be empty', ''));
  197. }
  198. if ($row['type'] == 'book') {
  199. $row['chapter_name'] = model('Book')::getChapterInfo($row['book_id'], $row['chapter_id'])['data']['name'];
  200. } else {
  201. $row['chapter_name'] = '';
  202. }
  203. $this->view->assign("row", $row);
  204. $this->view->assign('activity', FloatTipsService::instance()->getSysActivity()->data);
  205. return $this->view->fetch();
  206. }
  207. public function upload($ids)
  208. {
  209. if ($this->request->isPost()) {
  210. $filePath = ROOT_PATH . DS . 'public' . DS . $this->request->param('file');
  211. $list = file($filePath);
  212. $list = array_map(function ($v) {
  213. $v = trim($v);
  214. return $v;
  215. }, $list);
  216. $count = 0;
  217. $row = $this->model->where('id', $ids)->find();
  218. $end_time = $row['endtime'];
  219. $expire = $end_time - time();
  220. foreach ($list as $user_id) {
  221. $uCache = CacheConstants::getFloatTipsIdsByUserIds($user_id);
  222. $success = (int)Redis::instance()->sAdd($uCache, $ids);
  223. if ($success) {
  224. $count++;
  225. $ttl = Redis::instance()->ttl($uCache);
  226. if ($expire > $ttl) {
  227. Redis::instance()->expire($uCache, $expire);
  228. }
  229. }
  230. }
  231. if ($count) {
  232. $this->success('上传用户数:' . $count);
  233. } else {
  234. $this->error('未查询到可上传信息');
  235. }
  236. }
  237. return $this->view->fetch();
  238. }
  239. /**
  240. * 删除
  241. */
  242. public function del($ids = "")
  243. {
  244. if ($ids)
  245. {
  246. $adminIds = $this->getDataLimitAdminIds();
  247. if (is_array($adminIds))
  248. {
  249. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  250. }
  251. $ids = explode(',', $ids);
  252. model('floattipsuser')->whereIn('float_id', $ids)->delete();
  253. model('floattips')->whereIn('id', $ids)->delete();
  254. foreach ($ids as $id) {
  255. Redis::instance()->del(CacheConstants::getFloatTipsInfo($id));
  256. }
  257. $this->success();
  258. }
  259. $this->error(__('Parameter %s can not be empty', 'ids'));
  260. }
  261. }