Attribute.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace app\admin\controller\shop;
  3. use app\common\controller\Backend;
  4. use Exception;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 商品属性
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Attribute extends Backend
  14. {
  15. /**
  16. * Attribute模型对象
  17. * @var \app\admin\model\shop\Attribute
  18. */
  19. protected $model = null;
  20. protected $noNeedRight = ["attrs"];
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\shop\Attribute;
  25. $this->view->assign("typeList", $this->model->getTypeList());
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. public function index()
  33. {
  34. $category_id = $this->request->param('category_id');
  35. $this->assignconfig('category_id', $category_id);
  36. return parent::index();
  37. }
  38. /**
  39. * 添加
  40. */
  41. public function add()
  42. {
  43. if ($this->request->isPost()) {
  44. $params = $this->request->post("row/a");
  45. $params['category_id'] = $this->request->param('category_id');
  46. if ($params) {
  47. $params = $this->preExcludeFields($params);
  48. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  49. $params[$this->dataLimitField] = $this->auth->id;
  50. }
  51. $result = false;
  52. Db::startTrans();
  53. try {
  54. //是否采用模型验证
  55. if ($this->modelValidate) {
  56. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  57. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  58. $this->model->validateFailException(true)->validate($validate);
  59. }
  60. $result = $this->model->allowField(true)->save($params);
  61. Db::commit();
  62. } catch (ValidateException $e) {
  63. Db::rollback();
  64. $this->error($e->getMessage());
  65. } catch (PDOException $e) {
  66. Db::rollback();
  67. $this->error($e->getMessage());
  68. } catch (Exception $e) {
  69. Db::rollback();
  70. $this->error($e->getMessage());
  71. }
  72. if ($result !== false) {
  73. $this->success();
  74. } else {
  75. $this->error(__('No rows were inserted'));
  76. }
  77. }
  78. $this->error(__('Parameter %s can not be empty', ''));
  79. }
  80. return $this->view->fetch();
  81. }
  82. public function attrs()
  83. {
  84. $category_id = $this->request->param('category_id');
  85. if (empty($category_id)) {
  86. $this->error('分类不能为空');
  87. }
  88. $list = $this->model->with(['AttributeValue'])->where('category_id', $category_id)->select();
  89. $this->success('获取成功', '', $list);
  90. }
  91. }