Product.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------------------------------
  4. * GouGuOPEN [ 左手研发,右手开源,未来可期!]
  5. +-----------------------------------------------------------------------------------------------
  6. * @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
  7. +-----------------------------------------------------------------------------------------------
  8. * @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
  9. +-----------------------------------------------------------------------------------------------
  10. * @Author 勾股工作室 <hdm58@qq.com>
  11. +-----------------------------------------------------------------------------------------------
  12. */
  13. declare (strict_types = 1);
  14. namespace app\contract\controller;
  15. use app\base\BaseController;
  16. use app\contract\model\Product as ProductModel;
  17. use app\contract\validate\ProductValidate;
  18. use think\exception\ValidateException;
  19. use think\facade\Db;
  20. use think\facade\View;
  21. class Product extends BaseController
  22. {
  23. /**
  24. * 构造函数
  25. */
  26. protected $model;
  27. public function __construct()
  28. {
  29. parent::__construct(); // 调用父类构造函数
  30. $this->model = new ProductModel();
  31. }
  32. public function datalist()
  33. {
  34. if (request()->isAjax()) {
  35. $param = get_params();
  36. $where = [];
  37. if (!empty($param['keywords'])) {
  38. $where[] = ['p.title', 'like', '%' . $param['keywords'] . '%'];
  39. }
  40. if (isset($param['status']) && $param['status']!='') {
  41. $where[] = ['p.status', '=', $param['status']];
  42. }
  43. if (!empty($param['cate_id'])) {
  44. $cate_id_array = get_cate_son('ProductCate',$param['cate_id']);
  45. $where[] = ['p.cate_id', 'in', $cate_id_array];
  46. }
  47. $where[] = ['p.delete_time', '=', 0];
  48. $list = $this->model->datalist($where, $param);
  49. return table_assign(0, '', $list);
  50. } else {
  51. return view();
  52. }
  53. }
  54. //新建编辑
  55. public function add()
  56. {
  57. $param = get_params();
  58. if (request()->isAjax()) {
  59. $param['sale_price'] = empty($param['sale_price'])?0:$param['sale_price'];
  60. $param['base_price'] = empty($param['base_price'])?0:$param['base_price'];
  61. $param['purchase_price'] = empty($param['purchase_price'])?0:$param['purchase_price'];
  62. if (!empty($param['id']) && $param['id'] > 0) {
  63. try {
  64. validate(ProductValidate::class)->scene('edit')->check($param);
  65. } catch (ValidateException $e) {
  66. // 验证失败 输出错误信息
  67. return to_assign(1, $e->getError());
  68. }
  69. $this->model->edit($param);
  70. } else {
  71. try {
  72. validate(ProductValidate::class)->scene('add')->check($param);
  73. } catch (ValidateException $e) {
  74. // 验证失败 输出错误信息
  75. return to_assign(1, $e->getError());
  76. }
  77. $param['admin_id'] = $this->uid;
  78. $this->model->add($param);
  79. }
  80. } else {
  81. $id = isset($param['id']) ? $param['id'] : 0;
  82. if($id>0){
  83. $detail = $this->model->getById($id);
  84. if($detail['file_ids'] !=''){
  85. $file_array = Db::name('File')->where('id','in',$detail['file_ids'])->select();
  86. $detail['file_array'] = $file_array;
  87. }
  88. View::assign('detail', $detail);
  89. return view('edit');
  90. }
  91. return view();
  92. }
  93. }
  94. //查看
  95. public function view()
  96. {
  97. $param = get_params();
  98. $id = isset($param['id']) ? $param['id'] : 0;
  99. $detail = $this->model->getById($id);
  100. if($detail['cate_id']>0){
  101. $detail['cate'] = Db::name('ProductCate')->where('id',$detail['cate_id'])->value('title');
  102. }
  103. if($detail['file_ids'] !=''){
  104. $file_array = Db::name('File')->where('id','in',$detail['file_ids'])->select();
  105. $detail['file_array'] = $file_array;
  106. }
  107. View::assign('detail', $detail);
  108. return view();
  109. }
  110. //设置
  111. public function set()
  112. {
  113. $param = get_params();
  114. $res = $this->model->strict(false)->field('id,status')->update($param);
  115. if ($res) {
  116. add_log('set', $param['id'], $param);
  117. return to_assign();
  118. }
  119. else{
  120. return to_assign(0, '操作失败');
  121. }
  122. }
  123. /**
  124. * 删除
  125. */
  126. public function del()
  127. {
  128. $param = get_params();
  129. $id = isset($param['id']) ? $param['id'] : 0;
  130. if (request()->isDelete()) {
  131. $this->model->delById($id);
  132. } else {
  133. return to_assign(1, "错误的请求");
  134. }
  135. }
  136. }