StoreProduct.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\merchant\controller\store;
  12. use app\merchant\controller\AuthController;
  13. use app\admin\model\system\SystemConfig;
  14. use service\JsonService as Json;
  15. use service\SystemConfigService;
  16. use traits\CurdControllerTrait;
  17. use think\Request;
  18. use app\merchant\model\store\StoreCategory as CategoryModel;
  19. use app\merchant\model\store\StoreProduct as ProductModel;
  20. use think\Url;
  21. use app\merchant\model\system\Relation;
  22. /**
  23. * 产品管理
  24. * Class StoreProduct
  25. * @package app\merchant\controller\store
  26. */
  27. class StoreProduct extends AuthController
  28. {
  29. use CurdControllerTrait;
  30. protected $bindModel = ProductModel::class;
  31. /**
  32. * 显示资源列表
  33. *
  34. * @return \think\Response
  35. */
  36. public function index()
  37. {
  38. $type = $this->request->param('type');
  39. $mer_id = $this->merchantId;
  40. //获取分类
  41. $this->assign('cate', CategoryModel::getTierList());
  42. //全部产品
  43. $all = ProductModel::where(['is_del' => 0, 'mer_id' => $mer_id])->count();
  44. //出售中产品
  45. $onsale = ProductModel::where(['is_show' => 1, 'is_del' => 0, 'status' => 1, 'mer_id' => $mer_id])->count();
  46. //待上架产品
  47. $forsale = ProductModel::where(['is_show' => 0, 'is_del' => 0, 'status' => 1, 'mer_id' => $mer_id])->count();
  48. //仓库中产品
  49. $warehouse = ProductModel::where(['is_del' => 0, 'status' => 1, 'mer_id' => $mer_id])->count();
  50. //已经售馨产品
  51. $outofstock = ProductModel::getModelObject(['mer_id' => $mer_id])->where(ProductModel::setData(4))->count();
  52. //警戒库存
  53. $policeforce = ProductModel::getModelObject(['mer_id' => $mer_id])->where(ProductModel::setData(5))->count();
  54. //回收站
  55. $recycle = ProductModel::where(['is_del' => 1, 'status' => 1, 'mer_id' => $mer_id])->count();
  56. $this->assign(compact('type', 'all', 'onsale', 'forsale', 'warehouse', 'outofstock', 'policeforce', 'recycle'));
  57. return $this->fetch();
  58. }
  59. /**
  60. * 异步查找产品
  61. *
  62. * @return json
  63. */
  64. public function product_ist()
  65. {
  66. $where = parent::getMore([
  67. ['page', 1],
  68. ['limit', 20],
  69. ['store_name', ''],
  70. ['cate_id', ''],
  71. ['excel', 0],
  72. ['status', ''],
  73. ['type', $this->request->param('type')]
  74. ]);
  75. $where['mer_id'] = $this->merchantId;
  76. return Json::successlayui(ProductModel::ProductList($where));
  77. }
  78. /**商品审核
  79. * @return mixed
  80. */
  81. public function examine()
  82. {
  83. $this->assign('cate', CategoryModel::getTierList());
  84. return $this->fetch();
  85. }
  86. /**
  87. * 异步查找产品
  88. *
  89. * @return json
  90. */
  91. public function product_examine_ist()
  92. {
  93. $where = parent::getMore([
  94. ['page', 1],
  95. ['limit', 20],
  96. ['store_name', ''],
  97. ['cate_id', ''],
  98. ]);
  99. $where['mer_id'] = $this->merchantId;
  100. return Json::successlayui(ProductModel::productExamineList($where));
  101. }
  102. /**
  103. * 设置单个产品上架|下架
  104. *
  105. * @return json
  106. */
  107. public function set_show($is_show = '', $id = '')
  108. {
  109. ($is_show == '' || $id == '') && Json::fail('缺少参数');
  110. $res = parent::getDataModification('product', $id, 'is_show', (int)$is_show);
  111. if ($res) {
  112. return Json::successful($is_show == 1 ? '上架成功' : '下架成功');
  113. } else {
  114. return Json::fail($is_show == 1 ? '上架失败' : '下架失败');
  115. }
  116. }
  117. /**
  118. * 快速编辑
  119. *
  120. * @return json
  121. */
  122. public function set_product($field = '', $id = '', $value = '')
  123. {
  124. ($field == '' || $id == '' || $value == '') && Json::fail('缺少参数');
  125. $res = parent::getDataModification('product', $id, $field, $value);
  126. if ($res)
  127. return Json::successful('保存成功');
  128. else
  129. return Json::fail('保存失败');
  130. }
  131. /**
  132. * 设置批量产品上架
  133. *
  134. * @return json
  135. */
  136. public function product_show()
  137. {
  138. $post = parent::postMore([
  139. ['ids', []]
  140. ]);
  141. if (empty($post['ids'])) {
  142. return Json::fail('请选择需要上架的产品');
  143. } else {
  144. $res = ProductModel::where('id', 'in', $post['ids'])->update(['is_show' => 1]);
  145. if ($res)
  146. return Json::successful('上架成功');
  147. else
  148. return Json::fail('上架失败');
  149. }
  150. }
  151. public function create($id = 0)
  152. {
  153. $gold_name = SystemConfigService::get('gold_name');//虚拟币名称
  154. if ($id) {
  155. $product = ProductModel::get($id);
  156. if (!$product) return Json::fail('数据不存在!');
  157. $slider_image = [];
  158. if ($product['slider_image']) {
  159. foreach (json_decode($product['slider_image']) as $key => $value) {
  160. $image['pic'] = $value;
  161. $image['is_show'] = false;
  162. array_push($slider_image, $image);
  163. }
  164. }
  165. $product['slider_image'] = $slider_image;
  166. } else {
  167. $product = [];
  168. }
  169. $this->assign(['id' => $id, 'product' => json_encode($product), 'gold_name' => $gold_name]);
  170. return $this->fetch();
  171. }
  172. /**商品分类
  173. * @throws \think\db\exception\DataNotFoundException
  174. * @throws \think\db\exception\ModelNotFoundException
  175. * @throws \think\exception\DbException
  176. */
  177. public function getCateList()
  178. {
  179. $list = CategoryModel::where(['is_show' => 1, 'pid' => 0])->order('sort desc,add_time desc')->select();
  180. $list = count($list) > 0 ? $list->toArray() : [];
  181. return Json::successful($list);
  182. }
  183. /**
  184. * 保存新建的资源
  185. *
  186. * @param \think\Request $request
  187. * @return \think\Response
  188. */
  189. public function save(Request $request, $id = 0)
  190. {
  191. $data = parent::postMore([
  192. ['cate_id', ""],
  193. 'store_name',
  194. 'store_info',
  195. 'keyword',
  196. ['unit_name', '件'],
  197. ['image', []],
  198. ['slider_image', []],
  199. 'postage',
  200. 'price',
  201. 'vip_price',
  202. 'ot_price',
  203. 'free_shipping',
  204. 'sort',
  205. 'stock',
  206. 'ficti',
  207. 'description',
  208. ['is_show', 0],
  209. ['cost', 0],
  210. ['is_postage', 0],
  211. ['member_pay_type', 0]
  212. ], $request);
  213. $data['mer_id'] = $this->merchantId;
  214. if ($data['cate_id'] == "") return Json::fail('请选择商品分类');
  215. if (!$data['store_name']) return Json::fail('请输入商品名称');
  216. if (!$data['description']) return Json::fail('请输入商品详情');
  217. if (count($data['image']) < 1) return Json::fail('请上传商品图片');
  218. if (count($data['slider_image']) < 1) return Json::fail('请上传商品轮播图');
  219. if ($data['price'] == '' || $data['price'] < 0) return Json::fail('请输入商品售价');
  220. if ($data['ot_price'] == '' || $data['ot_price'] < 0) return Json::fail('请输入商品划线价');
  221. if ($data['vip_price'] == '' || $data['vip_price'] < 0) return Json::fail('请输入商品会员价');
  222. if ($data['postage'] == '' || $data['postage'] < 0) return Json::fail('请输入邮费');
  223. if ($data['stock'] == '' || $data['stock'] < 0) return Json::fail('请输入商品库存');
  224. if ($data['cost'] == '' || $data['ot_price'] < 0) return Json::fail('请输入商品成本价');
  225. $data['image'] = $data['image'][0];
  226. $slider_image = [];
  227. foreach ($data['slider_image'] as $item) {
  228. $slider_image[] = $item['pic'];
  229. }
  230. $data['slider_image'] = json_encode($slider_image);
  231. if ($id) {
  232. $data['status'] = $this->isAudit == 1 ? 0 : 1;
  233. ProductModel::edit($data, $id);
  234. return Json::successful('修改产品成功!');
  235. } else {
  236. $data['add_time'] = time();
  237. $data['status'] = $this->isAudit == 1 ? 0 : 1;
  238. ProductModel::set($data);
  239. return Json::successful('添加产品成功!');
  240. }
  241. }
  242. /**
  243. * 删除指定资源
  244. *
  245. * @param int $id
  246. * @return \think\Response
  247. */
  248. public function delete($id)
  249. {
  250. if (!$id) return $this->failed('数据不存在');
  251. if (!ProductModel::be(['id' => $id])) return $this->failed('产品数据不存在');
  252. if (ProductModel::be(['id' => $id, 'is_del' => 1])) {
  253. $res = parent::getDataModification('product', $id, 'is_del', 0);
  254. if (!$res)
  255. return Json::fail(ProductModel::getErrorInfo('恢复失败,请稍候再试!'));
  256. else
  257. return Json::successful('成功恢复产品!');
  258. } else {
  259. $res = parent::getDataModification('product', $id, 'is_del', 1);
  260. if (!$res)
  261. return Json::fail(ProductModel::getErrorInfo('删除失败,请稍候再试!'));
  262. else
  263. return Json::successful('删除成功!');
  264. }
  265. }
  266. /**
  267. * 点赞
  268. * @param $id
  269. * @return mixed|\think\response\Json|void
  270. */
  271. public function collect($id)
  272. {
  273. if (!$id) return $this->failed('数据不存在');
  274. $product = ProductModel::get($id);
  275. if (!$product) return Json::fail('数据不存在!');
  276. $this->assign(StoreProductRelation::getCollect($id));
  277. return $this->fetch();
  278. }
  279. /**
  280. * 收藏
  281. * @param $id
  282. * @return mixed|\think\response\Json|void
  283. */
  284. public function like($id)
  285. {
  286. if (!$id) return $this->failed('数据不存在');
  287. $product = ProductModel::get($id);
  288. if (!$product) return Json::fail('数据不存在!');
  289. $this->assign(StoreProductRelation::getLike($id));
  290. return $this->fetch();
  291. }
  292. /**
  293. * 修改产品价格
  294. * @param Request $request
  295. */
  296. public function edit_product_price(Request $request)
  297. {
  298. $data = parent::postMore([
  299. ['id', 0],
  300. ['price', 0],
  301. ], $request);
  302. if (!$data['id']) return Json::fail('参数错误');
  303. $res = ProductModel::edit(['price' => $data['price']], $data['id']);
  304. if ($res) return Json::successful('修改成功');
  305. else return Json::fail('修改失败');
  306. }
  307. /**
  308. * 修改产品库存
  309. * @param Request $request
  310. */
  311. public function edit_product_stock(Request $request)
  312. {
  313. $data = parent::postMore([
  314. ['id', 0],
  315. ['stock', 0],
  316. ], $request);
  317. if (!$data['id']) return Json::fail('参数错误');
  318. $res = ProductModel::edit(['stock' => $data['stock']], $data['id']);
  319. if ($res) return Json::successful('修改成功');
  320. else return Json::fail('修改失败');
  321. }
  322. /**关联课程
  323. * @param int $id
  324. * @return mixed
  325. */
  326. public function knowledge($id = 0)
  327. {
  328. if (!$id) Json::fail('缺少参数');
  329. $this->assign(['id' => $id]);
  330. return $this->fetch();
  331. }
  332. /**获取商品关联的专题
  333. * @param int $id
  334. * @throws \think\Exception
  335. * @throws \think\db\exception\DataNotFoundException
  336. * @throws \think\db\exception\ModelNotFoundException
  337. * @throws \think\exception\DbException
  338. */
  339. public function knowledge_points($id = 0)
  340. {
  341. if (!$id) Json::fail('缺少参数');
  342. $data = Relation::getQuestionsRelationSpecial($id, 5);
  343. foreach ($data['data'] as $k => &$v) {
  344. if ($v['type'] == 6) $v['type'] = $v['light_type'];
  345. $v['types'] = parent::specialTaskType($v['type']);
  346. }
  347. return Json::successlayui($data);
  348. }
  349. /**关联专题
  350. * @param int $id
  351. */
  352. public function relation($id = 0)
  353. {
  354. if (!$id) Json::fail('缺少参数');
  355. $this->assign(['id' => $id]);
  356. return $this->fetch('relation');
  357. }
  358. /**商品关联课程
  359. * @param int $id
  360. */
  361. public function add_knowledge_points($id, $special_ids)
  362. {
  363. if (!$id) Json::fail('缺少参数');
  364. $res = Relation::setRelations($id, $special_ids, 5);
  365. if ($res)
  366. return Json::successful('关联成功');
  367. else
  368. return Json::fail('关联失败');
  369. }
  370. /**商品关联课程排序
  371. * @param int $id
  372. * @param int $special_id
  373. * @param $value
  374. */
  375. public function up_knowledge_points_sort($id, $special_id, $value)
  376. {
  377. if (!$id || !$special_id) Json::fail('缺少参数');
  378. $res = Relation::updateRelationSort($id, $special_id, 5, $value);
  379. if ($res)
  380. return Json::successful('修改成功');
  381. else
  382. return Json::fail('修改失败');
  383. }
  384. /**删除关联专题
  385. * @param int $id
  386. * @param int $special_id
  387. * @throws \think\exception\DbException
  388. */
  389. public function delete_knowledge_points($id = 0, $special_id = 0)
  390. {
  391. if (!$id || !$special_id) Json::fail('缺少参数');
  392. $res = Relation::delRelation($id, $special_id, 5);
  393. if ($res)
  394. return Json::successful('删除成功');
  395. else
  396. return Json::fail('删除失败');
  397. }
  398. }