Media.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace app\admin\controller\booklistch;
  3. use app\admin\service\ExclusiveService;
  4. use app\common\controller\Backend;
  5. use think\Collection;
  6. use think\Controller;
  7. use think\Request;
  8. use app\main\constants\AdminConstants;
  9. /**
  10. * 书单素材库
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Media extends Backend
  15. {
  16. /**
  17. * BookListMedia模型对象
  18. */
  19. protected $model = null;
  20. protected $vipAdminBindModel = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = model('BookListMediaCh');
  25. $this->vipAdminBindModel = model('VipAdminBind');
  26. $this->view->assign("statusList", $this->model->getStatusList());
  27. }
  28. /**
  29. * 查看
  30. */
  31. public function index()
  32. {
  33. //设置过滤方法
  34. $this->request->filter(['strip_tags']);
  35. if ($this->request->isAjax())
  36. {
  37. //如果发送的来源是Selectpage,则转发到Selectpage
  38. if ($this->request->request('pkey_name'))
  39. {
  40. return $this->selectpage();
  41. }
  42. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  43. $maps = [];
  44. $maps['admin_id'] = $this->auth->id;
  45. //判断角色
  46. if (in_array($this->group, [7, 8])) {
  47. //vip 或 vip运营只显示自己
  48. $maps['admin_id'] = $this->auth->id;
  49. } else {
  50. //找账号绑定信息
  51. $bindModel = $this->vipAdminBindModel
  52. ->where('admin_id_slave', 'eq', $this->auth->id)
  53. ->select();
  54. if ($bindModel) {
  55. $vipAdminIds = array_column($bindModel, 'admin_id_master');
  56. $vipAdminIds[] = $this->auth->id;
  57. $maps['admin_id'] = ['in', $vipAdminIds];
  58. }
  59. }
  60. $total = $this->model
  61. ->where($where)
  62. ->where($maps)
  63. ->order($sort, $order)
  64. ->count();
  65. $list = $this->model
  66. ->where($where)
  67. ->where($maps)
  68. ->order($sort, $order)
  69. ->limit($offset, $limit)
  70. ->select();
  71. foreach ($list as &$row) {
  72. $row['self'] = $row['admin_id'] == $this->auth->id ? 1: 0;
  73. }
  74. $result = array("total" => $total, "rows" => $list);
  75. return json($result);
  76. }
  77. return $this->view->fetch();
  78. }
  79. /**
  80. * 素材选择列表
  81. */
  82. public function select()
  83. {
  84. if ($this->request->isAjax())
  85. {
  86. //如果发送的来源是Selectpage,则转发到Selectpage
  87. if ($this->request->request('pkey_name'))
  88. {
  89. return $this->selectpage();
  90. }
  91. $maps = [];
  92. $maps['status'] = ['EQ', 'normal'];
  93. $maps['admin_id'] = $this->auth->id;
  94. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  95. //判断角色
  96. if (in_array($this->group, [7, 8])) {
  97. //vip 或 vip运营只显示自己
  98. $maps['admin_id'] = $this->auth->id;
  99. } else {
  100. //找账号绑定信息
  101. $bindModel = $this->vipAdminBindModel
  102. ->where('admin_id_slave', 'eq', $this->auth->id)
  103. ->select();
  104. if ($bindModel) {
  105. $vipAdminIds = array_column($bindModel, 'admin_id_master');
  106. $vipAdminIds[] = $this->auth->id;
  107. $maps['admin_id'] = ['in', $vipAdminIds];
  108. }
  109. }
  110. $total = $this->model
  111. ->where($where)
  112. ->where($maps)
  113. ->order($sort, $order)
  114. ->count();
  115. $list = $this->model
  116. ->where($where)
  117. ->where($maps)
  118. ->order($sort, $order)
  119. ->limit($offset, $limit)
  120. ->select();
  121. if ($list) {
  122. foreach ($list as &$row) {
  123. $row['self'] = $row['admin_id'] == $this->auth->id ? 1: 0;
  124. }
  125. }
  126. $result = array("total" => $total, "rows" => $list);
  127. return json($result);
  128. }
  129. return $this->view->fetch();
  130. }
  131. /**
  132. * 添加
  133. */
  134. public function add()
  135. {
  136. $from = $this->request->get('from_window', 'list');
  137. if ($this->request->isPost())
  138. {
  139. $params = $this->request->post("row/a");
  140. if ($params)
  141. {
  142. $params['admin_id'] = $this->auth->id;
  143. /*
  144. * 已经弃用,如果为了兼容老版可取消注释
  145. foreach ($params as $k => &$v)
  146. {
  147. $v = is_array($v) ? implode(',', $v) : $v;
  148. }
  149. */
  150. if ($this->dataLimit)
  151. {
  152. $params[$this->dataLimitField] = $this->auth->id;
  153. }
  154. try
  155. {
  156. //是否采用模型验证
  157. if ($this->modelValidate)
  158. {
  159. $name = basename(str_replace('\\', '/', get_class($this->model)));
  160. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  161. $this->model->validate($validate);
  162. }
  163. $result = $this->model->allowField(true)->save($params);
  164. if ($result !== false)
  165. {
  166. $res = $this->model->getData();
  167. $res['self'] = 1;
  168. $this->success('', null, $res);
  169. }
  170. else
  171. {
  172. $this->error($this->model->getError());
  173. }
  174. }
  175. catch (\think\exception\PDOException $e)
  176. {
  177. $this->error($e->getMessage());
  178. }
  179. }
  180. $this->error(__('Parameter %s can not be empty', ''));
  181. }
  182. $this->assignconfig('from_window', $from);
  183. return $this->view->fetch();
  184. }
  185. /**
  186. * 编辑
  187. */
  188. public function edit($ids = NULL)
  189. {
  190. $from = $this->request->get('from_window', 'list');
  191. $row = $this->model->get($ids);
  192. if (!$row)
  193. $this->error(__('No Results were found'));
  194. $adminIds = $this->getDataLimitAdminIds();
  195. if (is_array($adminIds))
  196. {
  197. if (!in_array($row[$this->dataLimitField], $adminIds))
  198. {
  199. $this->error(__('You have no permission'));
  200. }
  201. }
  202. if ($this->request->isPost())
  203. {
  204. $params = $this->request->post("row/a");
  205. if ($params)
  206. {
  207. if ($row['admin_id'] != $this->auth->id) {
  208. $this->error(__('您没有权限进行修改'));
  209. }
  210. /*
  211. * 已经弃用,如果为了兼容老版可取消注释
  212. foreach ($params as $k => &$v)
  213. {
  214. $v = is_array($v) ? implode(',', $v) : $v;
  215. }
  216. */
  217. try
  218. {
  219. //是否采用模型验证
  220. if ($this->modelValidate)
  221. {
  222. $name = basename(str_replace('\\', '/', get_class($this->model)));
  223. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  224. $row->validate($validate);
  225. }
  226. $result = $row->allowField(true)->save($params);
  227. if ($result !== false)
  228. {
  229. $res = $row->getData();
  230. $res['self'] = 1;
  231. $this->success('', null, $res);
  232. }
  233. else
  234. {
  235. $this->error($row->getError());
  236. }
  237. }
  238. catch (\think\exception\PDOException $e)
  239. {
  240. $this->error($e->getMessage());
  241. }
  242. }
  243. $this->error(__('Parameter %s can not be empty', ''));
  244. }
  245. $this->view->assign("row", $row);
  246. //获取独家书籍id
  247. $bnotin= [];
  248. ExclusiveService::instance()->getExclusiveNotidsWithoutWhere($this->group, $this->auth->id,$bnotin);
  249. $this->assignconfig('bnotin', $bnotin);
  250. $this->assignconfig('from_window', $from);
  251. return $this->view->fetch();
  252. }
  253. }