Material.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\cmgadm\controller\shop;
  3. use app\common\controller\Backend;
  4. use app\cmgadm\model\Goodscats;
  5. use Think\Db;
  6. use think\Session;
  7. /**
  8. * 推广素材管理
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Material extends Backend
  13. {
  14. /**
  15. * Material模型对象
  16. * @var \app\cmgadm\model\Material
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\cmgadm\model\Material;
  23. $this->assign('shoptype',config('shopType'));
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. //当前是否为关联查询
  36. $this->relationSearch = false;
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags']);
  39. if ($this->request->isAjax())
  40. {
  41. //如果发送的来源是Selectpage,则转发到Selectpage
  42. if ($this->request->request('keyField'))
  43. {
  44. return $this->selectpage();
  45. }
  46. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  47. $total = $this->model
  48. ->where($where)
  49. ->order($sort, $order)
  50. ->count();
  51. $list = $this->model
  52. ->where($where)
  53. ->order($sort, $order)
  54. ->limit($offset, $limit)
  55. ->select();
  56. $result = array("total" => $total, "rows" => $list);
  57. return json($result);
  58. }
  59. return $this->view->fetch();
  60. }
  61. function add()
  62. {
  63. if ($this->request->isPost()) {
  64. $params = $this->request->post("row/a");
  65. if ($params) {
  66. $params = $this->preExcludeFields($params);
  67. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  68. $params[$this->dataLimitField] = $this->auth->id;
  69. }
  70. $result = false;
  71. $params['create_time'] = time();
  72. Db::startTrans();
  73. try {
  74. //是否采用模型验证
  75. if ($this->modelValidate) {
  76. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  77. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  78. $this->model->validateFailException(true)->validate($validate);
  79. }
  80. $result = $this->model->allowField(true)->insertGetId($params);
  81. Db::commit();
  82. } catch (ValidateException $e) {
  83. Db::rollback();
  84. $this->error($e->getMessage());
  85. } catch (PDOException $e) {
  86. Db::rollback();
  87. $this->error($e->getMessage());
  88. } catch (Exception $e) {
  89. Db::rollback();
  90. $this->error($e->getMessage());
  91. }
  92. if ($result !== false) {
  93. $this->success();
  94. } else {
  95. $this->error(__('No rows were inserted'));
  96. }
  97. }
  98. $this->error(__('Parameter %s can not be empty', ''));
  99. }
  100. return parent::add();
  101. }
  102. public function edit($ids = NULL)
  103. {
  104. $row = $this->model->get($ids);
  105. if (!$row)
  106. {
  107. $this->error(__('No Results were found'));
  108. }
  109. return parent::edit($ids);
  110. }
  111. function multi($ids = '')
  112. {
  113. $ids = $ids ? $ids : $this->request->param("ids");
  114. if ($ids) {
  115. $params=input('params');
  116. $arrs=explode('=', $params);
  117. $data[$arrs[0]]=$arrs[1];
  118. $res=$this->model->where(['id'=>$ids])->update ($data);
  119. if($res)
  120. {
  121. $this->success('操作成功');
  122. }else{
  123. $this->error('操作失败');
  124. }
  125. }else{
  126. $this->error('请选择要操作的数据');
  127. }
  128. return parent::multi($ids);
  129. }
  130. }