Manage.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace app\admin\controller\cp\vip;
  3. use app\common\controller\Backend;
  4. use app\common\service\VipCpService;
  5. use think\Controller;
  6. use think\db\Query;
  7. use think\Request;
  8. /**
  9. * VIP用户付费书籍管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Manage extends Backend
  14. {
  15. /**
  16. * CpVipList模型对象
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = model('CpVipList');
  23. }
  24. /**
  25. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  26. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  27. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  28. */
  29. /**
  30. * 查看
  31. */
  32. public function index()
  33. {
  34. //设置过滤方法
  35. $this->request->filter(['strip_tags']);
  36. if ($this->request->isAjax())
  37. {
  38. //如果发送的来源是Selectpage,则转发到Selectpage
  39. if ($this->request->request('pkey_name'))
  40. {
  41. return $this->selectpage();
  42. }
  43. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  44. $sort = 'cp_vip_list.id';
  45. $total = $this->model
  46. ->field('cp_vip_list.*, book.name')
  47. ->join('book', 'cp_vip_list.book_id = book.id', 'left')
  48. ->where($where)
  49. ->order($sort, $order)
  50. ->count();
  51. $list = $this->model
  52. ->field('cp_vip_list.*, book.name')
  53. ->join('book', 'cp_vip_list.book_id = book.id', 'left')
  54. ->where($where)
  55. ->order($sort, $order)
  56. ->limit($offset, $limit)
  57. ->select();
  58. $result = array("total" => $total, "rows" => $list);
  59. return json($result);
  60. }
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 删除
  65. */
  66. public function del($ids = "")
  67. {
  68. if ($ids)
  69. {
  70. $pk = $this->model->getPk();
  71. $adminIds = $this->getDataLimitAdminIds();
  72. if (is_array($adminIds))
  73. {
  74. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  75. }
  76. $list = $this->model->where($pk, 'in', $ids)->select();
  77. $count = 0;
  78. foreach ($list as $k => $v)
  79. {
  80. $count += $v->delete();
  81. VipCpService::instance()->delCpRedis($v->book_id);
  82. }
  83. if ($count)
  84. {
  85. $this->success();
  86. }
  87. else
  88. {
  89. $this->error(__('No rows were deleted'));
  90. }
  91. }
  92. $this->error(__('Parameter %s can not be empty', 'ids'));
  93. }
  94. public function ajaxSaveData()
  95. {
  96. if ($this->request->isPost())
  97. {
  98. $params = $this->request->post("row/a");
  99. if ($params)
  100. {
  101. /*
  102. * 已经弃用,如果为了兼容老版可取消注释
  103. foreach ($params as $k => &$v)
  104. {
  105. $v = is_array($v) ? implode(',', $v) : $v;
  106. }
  107. */
  108. if ($this->dataLimit)
  109. {
  110. $params[$this->dataLimitField] = $this->auth->id;
  111. }
  112. try
  113. {
  114. //是否采用模型验证
  115. if ($this->modelValidate)
  116. {
  117. $name = basename(str_replace('\\', '/', get_class($this->model)));
  118. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  119. $this->model->validate($validate);
  120. }
  121. $obj = $this->model->where('book_id', 'eq', $params['book_id'])->find();
  122. if($obj){
  123. $this->success();
  124. }
  125. $result = $this->model->allowField(true)->save($params);
  126. if ($result !== false)
  127. {
  128. VipCpService::instance()->saveCpRedis($params['book_id']);
  129. $this->success();
  130. }
  131. else
  132. {
  133. $this->error($this->model->getError());
  134. }
  135. }
  136. catch (\think\exception\PDOException $e)
  137. {
  138. $this->error($e->getMessage());
  139. }
  140. }
  141. $this->error(__('Parameter %s can not be empty', ''));
  142. }
  143. }
  144. public function ajaxBatchSave()
  145. {
  146. $params = $this->request->post();
  147. $ids_arr = $params['ids'];
  148. if ($ids_arr) {
  149. foreach ($ids_arr as $k => $id) {
  150. $obj = $this->model->where('book_id', 'eq', $id)->find();
  151. if (!$obj) {
  152. $book = model('book')->field(['id', 'cp_id', 'cp_name'])->where('id', 'eq', $id)->find();
  153. if ($book) {
  154. $book_id = $book->id;
  155. $data = [
  156. 'book_id' => $book->id,
  157. 'cp_id' => $book->cp_id,
  158. 'cp_name' => $book->cp_name,
  159. 'createtime' => time(),
  160. 'updatetime' => time(),
  161. ];
  162. $result = $this->model->allowField(true)->insert($data);
  163. if ($result !== false)
  164. {
  165. VipCpService::instance()->saveCpRedis($book_id);
  166. }
  167. else
  168. {
  169. $this->error($this->model->getError());
  170. }
  171. }
  172. }
  173. }
  174. }
  175. $this->success();
  176. }
  177. /**
  178. * 查看
  179. */
  180. public function bookCpList()
  181. {
  182. //设置过滤方法
  183. $this->request->filter(['strip_tags']);
  184. if ($this->request->isAjax())
  185. {
  186. //如果发送的来源是Selectpage,则转发到Selectpage
  187. if ($this->request->request('pkey_name'))
  188. {
  189. return $this->selectpage();
  190. }
  191. $notinBookids = $this->model->getBookIds();
  192. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  193. $total = model('book')
  194. ->where('id', 'not in', $notinBookids)
  195. ->where('cp_id', 'neq', '')
  196. ->where($where)
  197. ->order($sort, $order)
  198. ->count();
  199. $list = model('book')->field(['id', 'cp_id','cp_name','name'])
  200. ->where('id', 'not in', $notinBookids)
  201. ->where('cp_id', 'neq', '')
  202. ->where($where)
  203. ->order($sort, $order)
  204. ->limit($offset, $limit)
  205. ->select();
  206. $result = array("total" => $total, "rows" => $list);
  207. return json($result);
  208. }
  209. return $this->view->fetch();
  210. }
  211. }