Bookchargesetting.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\service\ExclusiveService;
  4. use app\common\controller\Backend;
  5. use app\main\constants\AdminConstants;
  6. use app\main\constants\BookConstants;
  7. use app\main\service\AdminService;
  8. use app\main\service\BookService;
  9. use think\Controller;
  10. use think\Request;
  11. /**
  12. * 书籍收费配置
  13. *
  14. * @icon fa fa-circle-o
  15. */
  16. class Bookchargesetting extends Backend
  17. {
  18. protected $noNeedRight = ['setting'];
  19. /**
  20. * @var \app\common\model\BookChargeSetting
  21. */
  22. protected $model = null;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = model('BookChargeSetting');
  27. $this->view->assign("statusList", $this->model->getStatusList());
  28. $this->view->assign("levelList", BookConstants::$chapter_price);
  29. $this->view->assign("startList", BookConstants::$chapter_charge_start);
  30. }
  31. /**
  32. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  33. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  34. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  35. */
  36. /**
  37. * 查看
  38. */
  39. public function index()
  40. {
  41. //设置过滤方法
  42. $this->request->filter(['strip_tags']);
  43. if ($this->request->isAjax())
  44. {
  45. //如果发送的来源是Selectpage,则转发到Selectpage
  46. if ($this->request->request('pkey_name'))
  47. {
  48. return $this->selectpage();
  49. }
  50. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  51. $total = $this->model
  52. ->where('admin_id', $this->auth->id)
  53. ->where($where)
  54. ->order($sort, $order)
  55. ->count();
  56. $list = $this->model
  57. ->where('admin_id', $this->auth->id)
  58. ->where($where)
  59. ->join('book', 'book.id=book_charge_setting.book_id')
  60. ->order($sort, $order)
  61. ->limit($offset, $limit)
  62. ->field('book_charge_setting.*,book.name')
  63. ->select();
  64. $result = array("total" => $total, "rows" => $list);
  65. return json($result);
  66. }
  67. return $this->view->fetch();
  68. }
  69. /**
  70. * 编辑
  71. */
  72. public function edit($ids = NULL)
  73. {
  74. $row = $this->model->get($ids);
  75. if (!$row)
  76. $this->error(__('No Results were found'));
  77. $adminIds = $this->getDataLimitAdminIds();
  78. if (is_array($adminIds))
  79. {
  80. if (!in_array($row[$this->dataLimitField], $adminIds))
  81. {
  82. $this->error(__('You have no permission'));
  83. }
  84. }
  85. if ($this->request->isPost())
  86. {
  87. $params = $this->request->post("row/a");
  88. if ($params)
  89. {
  90. /*
  91. * 已经弃用,如果为了兼容老版可取消注释
  92. foreach ($params as $k => &$v)
  93. {
  94. $v = is_array($v) ? implode(',', $v) : $v;
  95. }
  96. */
  97. try
  98. {
  99. //是否采用模型验证
  100. if ($this->modelValidate)
  101. {
  102. $name = basename(str_replace('\\', '/', get_class($this->model)));
  103. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  104. $row->validate($validate);
  105. }
  106. $result = $row->allowField(true)->save($params);
  107. if ($result !== false)
  108. {
  109. $this->model->clearInfo($this->auth->id, $params['book_id']);
  110. $this->success();
  111. }
  112. else
  113. {
  114. $this->error($row->getError());
  115. }
  116. }
  117. catch (\think\exception\PDOException $e)
  118. {
  119. $this->error($e->getMessage());
  120. }
  121. }
  122. $this->error(__('Parameter %s can not be empty', ''));
  123. }
  124. $bnotin = [];
  125. ExclusiveService::instance()->getExclusiveNotidsWithoutWhere($this->group, $this->auth->id,$bnotin);
  126. $this->assignconfig('bnotin',$bnotin);
  127. $this->view->assign("row", $row);
  128. return $this->view->fetch();
  129. }
  130. /**
  131. * 删除
  132. */
  133. public function del($ids = "")
  134. {
  135. if ($ids)
  136. {
  137. $pk = $this->model->getPk();
  138. $adminIds = $this->getDataLimitAdminIds();
  139. if (is_array($adminIds))
  140. {
  141. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  142. }
  143. $list = $this->model->where($pk, 'in', $ids)->select();
  144. $count = 0;
  145. foreach ($list as $k => $v)
  146. {
  147. $this->model->clearInfo($this->auth->id, $v['book_id']);
  148. $count += $v->delete();
  149. }
  150. if ($count)
  151. {
  152. $this->success();
  153. }
  154. else
  155. {
  156. $this->error(__('No rows were deleted'));
  157. }
  158. }
  159. $this->error(__('Parameter %s can not be empty', 'ids'));
  160. }
  161. /**
  162. * 添加
  163. */
  164. public function add()
  165. {
  166. if ($this->request->isPost())
  167. {
  168. $params = $this->request->post("row/a");
  169. if ($params)
  170. {
  171. /*
  172. * 已经弃用,如果为了兼容老版可取消注释
  173. foreach ($params as $k => &$v)
  174. {
  175. $v = is_array($v) ? implode(',', $v) : $v;
  176. }
  177. */
  178. if ($this->dataLimit)
  179. {
  180. $params[$this->dataLimitField] = $this->auth->id;
  181. }
  182. try
  183. {
  184. //是否采用模型验证
  185. if ($this->modelValidate)
  186. {
  187. $name = basename(str_replace('\\', '/', get_class($this->model)));
  188. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  189. $this->model->validate($validate);
  190. }
  191. $params['admin_id'] = $this->auth->id;
  192. $exists = [
  193. 'admin_id' => $params['admin_id'],
  194. 'book_id' => $params['book_id']
  195. ];
  196. if ($this->model->where($exists)->find()) {
  197. $this->error('书籍配置已存在');
  198. }
  199. $result = $this->model->allowField(true)->save($params);
  200. if ($result !== false)
  201. {
  202. $this->success();
  203. }
  204. else
  205. {
  206. $this->error($this->model->getError());
  207. }
  208. }
  209. catch (\think\exception\PDOException $e)
  210. {
  211. $this->error($e->getMessage());
  212. }
  213. }
  214. $this->error(__('Parameter %s can not be empty', ''));
  215. }
  216. return $this->view->fetch();
  217. }
  218. /**
  219. * 全局设置
  220. * @return string
  221. * @throws \think\Exception
  222. */
  223. public function setting()
  224. {
  225. if ($this->request->isPost()) {
  226. $update = [];
  227. $update['book_chapter_price'] = $this->request->param('book_chapter_price', 0);
  228. $update['book_chapter_charge_start'] = $this->request->param('book_chapter_charge_start', 0);
  229. if ($update) {
  230. AdminService::instance()->getAdminConfigModel()->update($update, ['admin_id'=>$this->auth->id]);
  231. AdminService::instance()->getAdminConfigModel()->delAdminInfoAllCache($this->auth->id);
  232. }
  233. $this->success();
  234. }
  235. $row = AdminService::instance()->getAdminConfigModel()->where('admin_id', $this->auth->id)->find();
  236. $this->view->assign('row', $row);
  237. return $this->view->fetch();
  238. }
  239. }