Product.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /**
  3. * Author: luzheng.liu
  4. * Time: 2020/12/16 23:06
  5. */
  6. namespace app\api\controller;
  7. use app\api\BaseController;
  8. use app\api\model\AdminModel;
  9. use app\api\model\BrandModel;
  10. use app\api\model\CompanyModel;
  11. use app\api\model\GroupModel;
  12. use app\api\model\PayModel;
  13. use app\api\model\ProductModel;
  14. use app\api\model\ProductTypeModel;
  15. use app\common\until\Until;
  16. use think\Db;
  17. class Product extends BaseController {
  18. /**
  19. * @OA\Get(path="/api/Product/index",
  20. * tags={"产品管理"},
  21. * summary="产品列表",
  22. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  23. * @OA\Parameter(name="page", in="query", description="页码", @OA\Schema(type="ineger",default="1")),
  24. * @OA\Parameter(name="pageSize", in="query", description="页尺寸", @OA\Schema(type="integer",default="10")),
  25. * @OA\Parameter(name="status", in="query", description="状态 1正常 2删除", @OA\Schema(type="integer",default="1")),
  26. * @OA\Parameter(name="name", in="query", description="产品名称", @OA\Schema(type="string")),
  27. * @OA\Parameter(name="brandId", in="query", description="品牌id", @OA\Schema(type="integer")),
  28. * @OA\Parameter(name="companyId", in="query", description="公司id", @OA\Schema(type="integer")),
  29. * @OA\Parameter(name="productTypeId", in="query", description="产品类型id", @OA\Schema(type="integer")),
  30. * @OA\Parameter(name="type", in="query", description="产品分类 1洗浴 2小食", @OA\Schema(type="integer")),
  31. * @OA\RequestBody(
  32. * ),
  33. * @OA\Response(response="200", description="请求成功")
  34. * )
  35. */
  36. public function index() {
  37. $input = Until::getInput();
  38. $model = new ProductModel();
  39. $model->setPage($input['page'] ?? 1);
  40. $model->setPageSize($input['pageSize'] ?? 10);
  41. $where = [];
  42. if ($this->isAdmin() && !empty($input['status'])) {
  43. $where[] = ['p.status', '=', ];
  44. } else {
  45. $where[] = ['p.status', '=', $model::NORMAL];
  46. }
  47. if (!empty($input['name'])) {
  48. $where[] = ['p.product_name', 'like', "%{$input['name']}%"];
  49. }
  50. if (!empty($input['brandId'])) {
  51. $where[] = ['p.brand_id', '=', "{$input['brandId']}"];
  52. }
  53. if (!empty($input['groupId'])) {
  54. $where[] = ['p.group_id', '=', "{$input['groupId']}"];
  55. }
  56. if (!empty($input['companyId'])) {
  57. $where[] = ['p.company_id', '=', "{$input['companyId']}"];
  58. }
  59. // if (!empty($input['storeId'])) {
  60. // $where[] = ['s.id', '=', "{$input['storeId']}"];
  61. // }
  62. if (!empty($input['productTypeId'])) {
  63. $where[] = ['pt.id', '=', $input['productTypeId']];
  64. $where[] = ['pt.status', '=', 1];
  65. }
  66. if (!empty($input['type'])) {
  67. $where[] = ['p.type', '=', (int)$input['type']];
  68. }else {
  69. $where[] = ['p.type', '=', 1];
  70. }
  71. $model->setWhere($where);
  72. $data = $model->getProductList();
  73. Until::output($data);
  74. }
  75. /**
  76. * @OA\Get(path="/api/Product/typeList",
  77. * tags={"产品管理"},
  78. * summary="产品类型列表",
  79. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  80. * @OA\Parameter(name="page", in="query", description="页码", @OA\Schema(type="ineger",default="1")),
  81. * @OA\Parameter(name="pageSize", in="query", description="页尺寸", @OA\Schema(type="integer",default="10")),
  82. * @OA\Parameter(name="type", in="query", description="1为洗浴 2为小食 3全部", @OA\Schema(type="integer",default="1")),
  83. * @OA\RequestBody(
  84. * ),
  85. * @OA\Response(response="200", description="请求成功")
  86. * )
  87. */
  88. public function typeList() {
  89. $input = Until::getInput();
  90. $model = new ProductTypeModel();
  91. $model->setPage($input['page'] ?? 1);
  92. $model->setPageSize($input['pageSize'] ?? 10);
  93. $where = [];
  94. if (!$this->isAdmin()) {
  95. $where[] = ['status','=', 1];
  96. }
  97. if (!empty($input['type']) && $input['type'] == 2) {
  98. $where[] = ['type','=', 2];
  99. }
  100. if (!empty($input['type']) && $input['type'] == 1) {
  101. $where[] = ['type','=', 1];
  102. }
  103. $model->setWhere($where);
  104. $data = $model->getProductTypeList();
  105. foreach ($data['list'] as &$v) {
  106. $v['title'] = $v['product_type_name'];
  107. }
  108. Until::output($data);
  109. }
  110. /**
  111. * @OA\Post(path="/api/Product/saveType",
  112. * tags={"产品管理"},
  113. * summary="保存产品类型信息",
  114. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  115. * @OA\RequestBody(
  116. * @OA\MediaType(
  117. * mediaType="multipart/form-data",
  118. * @OA\Schema(
  119. * @OA\Property(description="产品类型名称", property="name", type="string", default="精油推背"),
  120. * @OA\Property(description="产品类型图片", property="imgUrl", type="integer", default="http://xxx.com"),
  121. * @OA\Property(description="类型 1位洗浴 2为小食", property="type", type="integer", default="1"),
  122. * required={"name","imgUrl"})
  123. * )
  124. * ),
  125. * @OA\Response(response="200", description="请求成功")
  126. * )
  127. */
  128. public function saveType() {
  129. $input = Until::getInput();
  130. $rule = [
  131. 'name|分类名称' => 'require',
  132. 'imgUrl|产品分类图片' => 'require',
  133. ];
  134. Until::check($rule, $input);
  135. $model = new ProductTypeModel();
  136. if (!empty($input['id'])) {
  137. $id = (int)$input['id'];
  138. $model::where(['id' => $id])->update([
  139. 'product_type_name' => $input['name'],
  140. 'product_type_img' => $input['imgUrl'] ?? '',
  141. 'type' => $input['type'] ?? 1,
  142. ]);
  143. } else {
  144. $model->insertGetId([
  145. 'product_type_name' => $input['name'],
  146. 'product_type_img' => $input['imgUrl'] ?? '',
  147. 'type' => $input['type'] ?? 1
  148. ]);
  149. }
  150. Until::output(['isSuccess' => 1]);
  151. }
  152. /**
  153. * @OA\GET(path="/api/Product/deleteType",
  154. * tags={"产品管理"},
  155. * summary="删除(屏蔽)产品类型信息",
  156. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  157. * @OA\Parameter(name="id", in="query", description="产品id", @OA\Schema(type="ineger",default="1")),
  158. * @OA\Parameter(name="status", in="query", description="1正常 0禁用", @OA\Schema(type="ineger",default="1")),
  159. * @OA\RequestBody(
  160. * ),
  161. * @OA\Response(response="200", description="请求成功")
  162. * )
  163. */
  164. public function deleteType() {
  165. $input = Until::getInput();
  166. $rule = [
  167. 'id' => 'require',
  168. 'status' => 'require',
  169. ];
  170. Until::check($rule, $input);
  171. $model = new ProductTypeModel();
  172. $where[] = ['id', '=', (int)$input['id']];
  173. $data = ['status' => (int)$input['status']];
  174. $isSuccess = $model::where($where)->update($data);
  175. Until::output(['isSuccess' => $isSuccess]);
  176. }
  177. /**
  178. * @OA\Post(path="/api/Product/save",
  179. * tags={"产品管理"},
  180. * summary="保存产品信息",
  181. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  182. * @OA\RequestBody(
  183. * @OA\MediaType(
  184. * mediaType="multipart/form-data",
  185. * @OA\Schema(
  186. * @OA\Property(description="产品名称", property="name", type="string", default="精油推背"),
  187. * @OA\Property(description="产品图片", property="imgUrl", type="integer", default="http://xxx.com"),
  188. * @OA\Property(description="原价", property="oldPrice", type="string", default="88.00"),
  189. * @OA\Property(description="现价", property="currentPrice", type="string", default="66.00"),
  190. * @OA\Property(description="产品介绍(富文本编辑)", property="productContent", type="string", default="本产品由xxx精油。。"),
  191. * @OA\Property(description="公司id", property="companyId", type="string", default="1"),
  192. * @OA\Property(description="品牌id", property="brandId", type="string", default="1"),
  193. * @OA\Property(description="状态 1正常 2下架", property="status", type="integer", default="1"),
  194. * @OA\Property(description="产品类型id", property="productTypeId", type="integer", default="1"),
  195. * @OA\Property(description="产品分类 1 洗浴 2小食", property="type", type="integer", default="1"),
  196. * required={"name","imgUrl","oldPrice","currentPrice","companyId","brandId","productContent"})
  197. * )
  198. * ),
  199. * @OA\Response(response="200", description="请求成功")
  200. * )
  201. */
  202. public function save() {
  203. $input = Until::getInput();
  204. $rule = [
  205. 'name|产品名称' => 'require',
  206. 'imgUrl|产品图片' => 'require',
  207. 'oldPrice|原价' => 'require',
  208. 'currentPrice|现价' => 'require',
  209. 'companyId|公司id' => 'require',
  210. 'brandId|品牌id' => 'require',
  211. 'productContent|产品内容' => 'require',
  212. 'productTypeId|产品类型' => 'require'
  213. ];
  214. Until::check($rule, $input);
  215. $model = new ProductModel();
  216. if (!empty($input['id'])) {
  217. $id = (int)$input['id'];
  218. $model::where(['id' => $id])->update([
  219. 'product_name' => $input['name'],
  220. 'product_img' => $input['imgUrl'],
  221. 'old_price' => $input['oldPrice'],
  222. 'current_price' => $input['currentPrice'],
  223. 'company_id' => $input['companyId'],
  224. 'brand_id' => $input['brandId'],
  225. 'product_content' => $input['productContent'],
  226. 'product_type_id' => $input['productTypeId'],
  227. 'status' => $input['status'] ?? 1,
  228. ]);
  229. } else {
  230. $id = $model->insertGetId([
  231. 'product_name' => $input['name'],
  232. 'product_img' => $input['imgUrl'],
  233. 'old_price' => $input['oldPrice'],
  234. 'current_price' => $input['currentPrice'],
  235. 'company_id' => $input['companyId'],
  236. 'brand_id' => $input['brandId'],
  237. 'product_content' => $input['productContent'],
  238. 'status' => $input['status'] ?? 1,
  239. 'product_type_id' => $input['productTypeId'],
  240. 'type' => $input['type'] ?? 1
  241. ]);
  242. }
  243. $model->setWhere([['p.id', '=', (int)$id]]);
  244. $info = $model->getProductInfo();
  245. Until::output(['info' => $info]);
  246. }
  247. /**
  248. * @OA\GET(path="/api/Product/read",
  249. * tags={"产品管理"},
  250. * summary="查看产品信息",
  251. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  252. * @OA\Parameter(name="id", in="query", description="产品id", @OA\Schema(type="ineger",default="1")),
  253. * @OA\RequestBody(
  254. * ),
  255. * @OA\Response(response="200", description="请求成功")
  256. * )
  257. */
  258. public function read($id) {
  259. $model = new ProductModel();
  260. $where[] = ['p.id', '=', (int)$id];
  261. $model->setWhere($where);
  262. $info = $model->getProductInfo();
  263. Until::output(['info' => $info]);
  264. }
  265. /**
  266. * @OA\GET(path="/api/Product/delete",
  267. * tags={"产品管理"},
  268. * summary="删除产品信息",
  269. * @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
  270. * @OA\Parameter(name="id", in="query", description="产品id", @OA\Schema(type="ineger",default="1")),
  271. * @OA\Parameter(name="status", in="query", description="1正常 2下架 3删除", @OA\Schema(type="ineger",default="1")),
  272. * @OA\RequestBody(
  273. * ),
  274. * @OA\Response(response="200", description="请求成功")
  275. * )
  276. */
  277. public function delete($id, $status) {
  278. $model = new ProductModel();
  279. $where[] = ['id', '=', (int)$id];
  280. $data = ['status' => (int)$status];
  281. $isSuccess = $model::where($where)->update($data);
  282. Until::output(['isSuccess' => $isSuccess]);
  283. }
  284. }