Lists.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace app\admin\controller\award;
  3. use app\common\controller\Backend;
  4. use app\common\library\Redis;
  5. use app\main\constants\ActivityConstants;
  6. use think\Controller;
  7. use think\Request;
  8. /**
  9. * 奖品列管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Lists extends Backend
  14. {
  15. /**
  16. * AwardLists模型对象
  17. */
  18. protected $model = null;
  19. protected $noNeedRight = ['ajaxlapse','show','initcache'];
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = model('AwardLists');
  24. $this->view->assign("typeList", $this->model->getTypeList());
  25. $this->view->assign("stateList", $this->model->getStateList());
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. /**
  33. * 添加
  34. */
  35. public function add()
  36. {
  37. if ($this->request->isPost())
  38. {
  39. $params = $this->request->post("row/a");
  40. if ($params)
  41. {
  42. /*
  43. * 已经弃用,如果为了兼容老版可取消注释
  44. foreach ($params as $k => &$v)
  45. {
  46. $v = is_array($v) ? implode(',', $v) : $v;
  47. }
  48. */
  49. if ($this->dataLimit)
  50. {
  51. $params[$this->dataLimitField] = $this->auth->id;
  52. }
  53. try
  54. {
  55. //是否采用模型验证
  56. if ($this->modelValidate)
  57. {
  58. $name = basename(str_replace('\\', '/', get_class($this->model)));
  59. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  60. $this->model->validate($validate);
  61. }
  62. if ($params['type'] == 2){
  63. $params['content'] = str_replace(array("\r\n", "\r", "\n"), ",", $params['book_content']);
  64. if (count(explode(',',$params['content'])) >50){
  65. $this->error('添加的书籍ID不能超过50本');
  66. }
  67. }
  68. $result = $this->model->allowField(true)->save($params);
  69. if ($result !== false)
  70. {
  71. $this->success();
  72. }
  73. else
  74. {
  75. $this->error($this->model->getError());
  76. }
  77. }
  78. catch (\think\exception\PDOException $e)
  79. {
  80. $this->error($e->getMessage());
  81. }
  82. }
  83. $this->error(__('Parameter %s can not be empty', ''));
  84. }
  85. return $this->view->fetch();
  86. }
  87. /**
  88. * 编辑
  89. */
  90. public function edit($ids = NULL)
  91. {
  92. $row = $this->model->get($ids);
  93. if (!$row)
  94. $this->error(__('No Results were found'));
  95. $adminIds = $this->getDataLimitAdminIds();
  96. if (is_array($adminIds))
  97. {
  98. if (!in_array($row[$this->dataLimitField], $adminIds))
  99. {
  100. $this->error(__('You have no permission'));
  101. }
  102. }
  103. if ($this->request->isPost())
  104. {
  105. $params = $this->request->post("row/a");
  106. if ($params)
  107. {
  108. /*
  109. * 已经弃用,如果为了兼容老版可取消注释
  110. foreach ($params as $k => &$v)
  111. {
  112. $v = is_array($v) ? implode(',', $v) : $v;
  113. }
  114. */
  115. try
  116. {
  117. //是否采用模型验证
  118. if ($this->modelValidate)
  119. {
  120. $name = basename(str_replace('\\', '/', get_class($this->model)));
  121. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  122. $row->validate($validate);
  123. }
  124. if ($params['type'] == 2){
  125. $params['content'] = str_replace(array("\r\n", "\r", "\n"), ",", $params['book_content']);
  126. if (count(explode(',',$params['content'])) >50){
  127. $this->error('添加的书籍ID不能超过50本');
  128. }
  129. }
  130. $result = $row->allowField(true)->save($params);
  131. if ($result !== false)
  132. {
  133. $this->success();
  134. }
  135. else
  136. {
  137. $this->error($row->getError());
  138. }
  139. }
  140. catch (\think\exception\PDOException $e)
  141. {
  142. $this->error($e->getMessage());
  143. }
  144. }
  145. $this->error(__('Parameter %s can not be empty', ''));
  146. }
  147. $this->view->assign("row", $row);
  148. return $this->view->fetch();
  149. }
  150. public function ajaxLapse()
  151. {
  152. $id = $this->request->param('id',0);
  153. $state = $this->request->param('state',0);
  154. if ( !$id || !$state ){
  155. $this->error('操作失败');
  156. }
  157. $state = $state == 2 ? 1 : 2;
  158. $obj = $this->model->find($id);
  159. $obj->state=$state;
  160. $obj->save();
  161. $this->success('操作成功');
  162. }
  163. public function show()
  164. {
  165. //设置过滤方法
  166. $this->request->filter(['strip_tags']);
  167. if ($this->request->isAjax()){
  168. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  169. $total = $this->model
  170. ->where($where)
  171. ->order($sort, $order)
  172. ->count();
  173. $list = $this->model
  174. ->where($where)
  175. ->order($sort, $order)
  176. ->limit($offset, $limit)
  177. ->select();
  178. $result = array("total" => $total, "rows" => $list);
  179. return json($result);
  180. }
  181. return $this->view->fetch();
  182. }
  183. public function InitCache()
  184. {
  185. $list = $this->model->select();
  186. if ($list){
  187. $rdList = [];
  188. foreach($list as $k => $v){
  189. $temp = json_encode($v);
  190. $rdList[] = $temp;
  191. }
  192. unset($list);
  193. $redisKey = ActivityConstants::AWARD_LIST_SET;
  194. Redis::instance()->del($redisKey);
  195. Redis::instance()->sAddArray($redisKey,$rdList);
  196. }
  197. $this->success('操作成功');
  198. }
  199. }