Resource.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\library\Redis;
  5. use think\Controller;
  6. use think\Log;
  7. use think\Request;
  8. /**
  9. * 活动资源管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Resource extends Backend
  14. {
  15. /**
  16. * @var \app\common\model\Resource
  17. */
  18. protected $model = null;
  19. /**
  20. * @var int
  21. */
  22. protected $aid = 0;
  23. protected $resource_type = null;
  24. protected $business_line = 0;
  25. protected $admin_id;
  26. public $noNeedRight =['select'];
  27. public function _initialize()
  28. {
  29. parent::_initialize();
  30. $this->model = model('Resource');
  31. $this->view->assign("showTypeList", $this->model->getShowTypeList());
  32. $this->view->assign("typeList", $this->model->getTypeList());
  33. $this->view->assign("statusList", $this->model->getStatusList());
  34. $this->view->assign("popRangeList", $this->model->getPopRangeList());
  35. $this->view->assign("classifyList", $this->model->getClassifyList());
  36. $this->aid = $this->request->get('aid');
  37. if ($this->request->has('resource_type')) {
  38. $this->resource_type = intval($this->request->get('resource_type'));
  39. }
  40. $this->assign('aid', $this->aid);
  41. $this->assign('resource_type', $this->resource_type ?? '');
  42. $this->assignconfig('aid', $this->aid);
  43. $this->assignconfig('resource_type', $this->resource_type ?? '');
  44. //业务线
  45. if ($this->request->has('business_line')) {
  46. $this->business_line = intval($this->request->get('business_line'));
  47. }
  48. $this->assignconfig('business_line', $this->business_line);
  49. $this->assign('business_line', $this->business_line);
  50. }
  51. /**
  52. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  53. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  54. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  55. */
  56. /**
  57. * 查看
  58. */
  59. public function index()
  60. {
  61. $map['activity_id'] = $this->aid;
  62. //设置过滤方法
  63. $this->request->filter(['strip_tags']);
  64. if ($this->request->isAjax()) {
  65. //如果发送的来源是Selectpage,则转发到Selectpage
  66. if ($this->request->request('pkey_name')) {
  67. return $this->selectpage();
  68. }
  69. if ($this->resource_type !== null) {
  70. $map['resource_type'] = $this->resource_type;
  71. }
  72. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  73. $total = $this->model
  74. ->where($where)
  75. ->where($map)
  76. ->count();
  77. $list = $this->model->alias('r')
  78. ->join(['goods' => 'g'], 'r.goods_id = g.id', 'LEFT')
  79. ->field([
  80. 'r.*',
  81. 'g.show_type' => 'g_show_type',
  82. 'g.type' => 'g_type',
  83. 'g.money' => 'g_money',
  84. 'g.kandian' => 'g_kandian',
  85. 'g.free_kandian' => 'g_free_kandian',
  86. 'g.icon' => 'g_icon',
  87. 'g.free_day' => 'g_free_day',
  88. 'g.day' => 'g_day',
  89. ])
  90. ->where($where)
  91. ->where($map)
  92. ->order($sort, $order)
  93. ->limit($offset, $limit)
  94. ->select();
  95. $result = array("total" => $total, "rows" => $list);
  96. return json($result);
  97. }
  98. return $this->view->fetch();
  99. }
  100. public function select(){
  101. $map['activity_id'] = $this->aid;
  102. //设置过滤方法
  103. $this->request->filter(['strip_tags']);
  104. if ($this->request->isAjax())
  105. {
  106. //如果发送的来源是Selectpage,则转发到Selectpage
  107. if ($this->request->request('pkey_name'))
  108. {
  109. return $this->selectpage();
  110. }
  111. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  112. if ($this->resource_type !== null) {
  113. $map['resource_type'] = $this->resource_type;
  114. }
  115. $total = $this->model
  116. ->where($where)
  117. ->where($map)
  118. ->count();
  119. $list = $this->model->alias('r')
  120. ->join(['goods' => 'g'], 'r.goods_id = g.id', 'LEFT')
  121. ->field([
  122. 'r.*',
  123. 'g.show_type' => 'g_show_type',
  124. 'g.type' => 'g_type',
  125. 'g.money' => 'g_money',
  126. 'g.kandian' => 'g_kandian',
  127. 'g.free_kandian' => 'g_free_kandian',
  128. 'g.icon' => 'g_icon',
  129. 'g.free_day' => 'g_free_day',
  130. 'g.day' => 'g_day',
  131. ])
  132. ->where($where)
  133. ->where($map)
  134. ->order($sort, $order)
  135. ->limit($offset, $limit)
  136. ->select();
  137. $result = array("total" => $total, "rows" => $list);
  138. return json($result);
  139. }
  140. return $this->view->fetch();
  141. }
  142. /**
  143. * 添加
  144. */
  145. public function add()
  146. {
  147. if ($this->request->isPost())
  148. {
  149. $params = $this->request->post("row/a");
  150. if ($params)
  151. {
  152. if ($this->dataLimit)
  153. {
  154. $params[$this->dataLimitField] = $this->auth->id;
  155. }
  156. try
  157. {
  158. //是否采用模型验证
  159. if ($this->modelValidate)
  160. {
  161. $name = basename(str_replace('\\', '/', get_class($this->model)));
  162. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  163. $this->model->validate($validate);
  164. }
  165. if (isset($params['activity_id']) && $params['activity_id'] > 0) {
  166. //验证平台活动 是否已经添加了资源
  167. if (model("Resource")->where('activity_id', 'eq', $params['activity_id'])->count() > 0) {
  168. $this->error("活动仅支持配置一条资源");
  169. }
  170. }
  171. $result = $this->model->allowField(true)->save($params);
  172. if ($result !== false)
  173. {
  174. $this->success();
  175. }
  176. else
  177. {
  178. $this->error($this->model->getError());
  179. }
  180. }
  181. catch (\think\exception\PDOException $e)
  182. {
  183. $this->error($e->getMessage());
  184. }
  185. }
  186. $this->error(__('Parameter %s can not be empty', ''));
  187. }
  188. if (in_array($this->resource_type, [0, 1, 2])) {
  189. return $this->view->fetch('addgoodsresource');
  190. } else {
  191. return $this->view->fetch('add');
  192. }
  193. }
  194. /**
  195. * 编辑
  196. */
  197. public function edit($ids = NULL)
  198. {
  199. $row = $this->model->get($ids);
  200. if (!$row)
  201. $this->error(__('No Results were found'));
  202. $adminIds = $this->getDataLimitAdminIds();
  203. if (is_array($adminIds))
  204. {
  205. if (!in_array($row[$this->dataLimitField], $adminIds))
  206. {
  207. $this->error(__('You have no permission'));
  208. }
  209. }
  210. if ($this->request->isPost())
  211. {
  212. $params = $this->request->post("row/a");
  213. if ($params)
  214. {
  215. //$params['warning'] = str_replace("/n","<br/>",$params['warning']);die;
  216. Log::write($params['warning'],'congcongtest');
  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. //清一下缓存
  230. $key = 'A-R-R:'.$ids;
  231. $key2 = 'R-I:'.$ids;
  232. Redis::instance()->del($key);
  233. Redis::instance()->del($key2);
  234. $this->success();
  235. }
  236. else
  237. {
  238. $this->error($row->getError());
  239. }
  240. }
  241. catch (\think\exception\PDOException $e)
  242. {
  243. $this->error($e->getMessage());
  244. }
  245. }
  246. $this->error(__('Parameter %s can not be empty', ''));
  247. }
  248. $goods = [
  249. 'show_type_text' => '',
  250. 'type_text' => '',
  251. 'money' => '',
  252. 'kandian' => '',
  253. 'free_kandian' => '',
  254. 'icon' => '',
  255. 'id' => '',
  256. 'free_day' => '',
  257. 'type' => '',
  258. 'day' => '',
  259. ];
  260. if (!empty($row['goods_id'])) {
  261. $goodsModel = model('goods');
  262. $goods = $goodsModel->where('id', $row['goods_id'])->find();
  263. }
  264. $this->view->assign("goods", $goods);
  265. $this->view->assign("row", $row);
  266. if (in_array($this->resource_type, [0, 1])) {
  267. return $this->view->fetch('editgoodsresource');
  268. } else {
  269. return $this->view->fetch('edit');
  270. }
  271. }
  272. }