Signrecommand.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace app\admin\controller\clientmanage\recommand;
  3. use app\common\controller\Backend;
  4. use app\common\library\Redis;
  5. use think\Controller;
  6. use think\Request;
  7. /**
  8. * 签到推荐书库
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Signrecommand extends Backend
  13. {
  14. /**
  15. * SignRecommand模型对象
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = model('ClientSignRecommand');
  22. $this->view->assign("sexList", $this->model->getSexList());
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 添加
  32. */
  33. public function add()
  34. {
  35. if ($this->request->isPost())
  36. {
  37. $params = $this->request->post("row/a");
  38. if ($params)
  39. {
  40. if ($this->dataLimit)
  41. {
  42. $params[$this->dataLimitField] = $this->auth->id;
  43. }
  44. try
  45. {
  46. //是否采用模型验证
  47. if ($this->modelValidate)
  48. {
  49. $name = basename(str_replace('\\', '/', get_class($this->model)));
  50. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  51. $this->model->validate($validate);
  52. }
  53. $result = $this->model->allowField(true)->save($params);
  54. if ($result !== false)
  55. {
  56. $redis = Redis::instance();
  57. $key = 'SRB:' . $params['sex'];
  58. $redis->del($key);
  59. $this->success();
  60. }
  61. else
  62. {
  63. $this->error($this->model->getError());
  64. }
  65. }
  66. catch (\think\exception\PDOException $e)
  67. {
  68. $this->error($e->getMessage());
  69. }
  70. }
  71. $this->error(__('Parameter %s can not be empty', ''));
  72. }
  73. return $this->view->fetch();
  74. }
  75. /**
  76. * 编辑
  77. */
  78. public function edit($ids = NULL)
  79. {
  80. $row = $this->model->get($ids);
  81. if (!$row)
  82. $this->error(__('No Results were found'));
  83. $adminIds = $this->getDataLimitAdminIds();
  84. if (is_array($adminIds))
  85. {
  86. if (!in_array($row[$this->dataLimitField], $adminIds))
  87. {
  88. $this->error(__('You have no permission'));
  89. }
  90. }
  91. if ($this->request->isPost())
  92. {
  93. $params = $this->request->post("row/a");
  94. if ($params)
  95. {
  96. /*
  97. * 已经弃用,如果为了兼容老版可取消注释
  98. foreach ($params as $k => &$v)
  99. {
  100. $v = is_array($v) ? implode(',', $v) : $v;
  101. }
  102. */
  103. try
  104. {
  105. //是否采用模型验证
  106. if ($this->modelValidate)
  107. {
  108. $name = basename(str_replace('\\', '/', get_class($this->model)));
  109. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  110. $row->validate($validate);
  111. }
  112. $result = $row->allowField(true)->save($params);
  113. if ($result !== false)
  114. {
  115. $redis = Redis::instance();
  116. $key = 'SRB:' . $params['sex'];
  117. $redis->del($key);
  118. $this->success();
  119. }
  120. else
  121. {
  122. $this->error($row->getError());
  123. }
  124. }
  125. catch (\think\exception\PDOException $e)
  126. {
  127. $this->error($e->getMessage());
  128. }
  129. }
  130. $this->error(__('Parameter %s can not be empty', ''));
  131. }
  132. $this->view->assign("row", $row);
  133. return $this->view->fetch();
  134. }
  135. /**
  136. * 删除
  137. */
  138. public function del($ids = "")
  139. {
  140. if ($ids)
  141. {
  142. $pk = $this->model->getPk();
  143. $adminIds = $this->getDataLimitAdminIds();
  144. if (is_array($adminIds))
  145. {
  146. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  147. }
  148. $list = $this->model->where($pk, 'in', $ids)->select();
  149. $count = 0;
  150. foreach ($list as $k => $v)
  151. {
  152. $count += $v->delete();
  153. }
  154. if ($count)
  155. {
  156. $redis = Redis::instance();
  157. $redis->del('SRB:1');
  158. $redis->del('SRB:2');
  159. $redis->del('SRB:W:2');
  160. $redis->del('SRB:W:1');
  161. $this->success();
  162. }
  163. else
  164. {
  165. $this->error(__('No rows were deleted'));
  166. }
  167. }
  168. $this->error(__('Parameter %s can not be empty', 'ids'));
  169. }
  170. }