StoreProduct.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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\admin\controller\store;
  12. use app\admin\controller\AuthController;
  13. use app\admin\model\system\Recommend;
  14. use app\admin\model\system\RecommendRelation;
  15. use service\FormBuilder as Form;
  16. use app\admin\model\store\StoreProductRelation;
  17. use app\admin\model\system\SystemConfig;
  18. use service\JsonService as Json;
  19. use service\SystemConfigService;
  20. use traits\CurdControllerTrait;
  21. use service\UploadService as Upload;
  22. use think\Request;
  23. use app\admin\model\store\StoreCategory as CategoryModel;
  24. use app\admin\model\store\StoreProduct as ProductModel;
  25. use think\Url;
  26. use app\admin\model\system\SystemAttachment;
  27. use app\admin\model\questions\Relation;
  28. use app\admin\model\merchant\Merchant;
  29. /**
  30. * 产品管理
  31. * Class StoreProduct
  32. * @package app\admin\controller\store
  33. */
  34. class StoreProduct extends AuthController
  35. {
  36. use CurdControllerTrait;
  37. protected $bindModel = ProductModel::class;
  38. /**
  39. * 显示资源列表
  40. *
  41. * @return \think\Response
  42. */
  43. public function index()
  44. {
  45. $type = $this->request->param('type');
  46. //获取分类
  47. $this->assign('cate', CategoryModel::getTierList());
  48. $this->assign('mer_list', Merchant::getMerchantList());
  49. //出售中产品
  50. $onsale = ProductModel::where(['is_show' => 1, 'status' => 1, 'is_del' => 0])->count();
  51. //待上架产品
  52. $forsale = ProductModel::where(['is_show' => 0, 'status' => 1, 'is_del' => 0])->count();
  53. //仓库中产品
  54. $warehouse = ProductModel::where(['is_del' => 0, 'status' => 1])->count();
  55. //已经售馨产品
  56. $outofstock = ProductModel::getModelObject()->where(ProductModel::setData(4))->count();
  57. //警戒库存
  58. $policeforce = ProductModel::getModelObject()->where(ProductModel::setData(5))->count();
  59. //回收站
  60. $recycle = ProductModel::where(['is_del' => 1, 'status' => 1])->count();
  61. $this->assign(compact('type', 'onsale', 'forsale', 'warehouse', 'outofstock', 'policeforce', 'recycle'));
  62. return $this->fetch();
  63. }
  64. /**
  65. * 异步查找产品
  66. *
  67. * @return json
  68. */
  69. public function product_ist()
  70. {
  71. $where = parent::getMore([
  72. ['page', 1],
  73. ['limit', 20],
  74. ['store_name', ''],
  75. ['cate_id', ''],
  76. ['mer_id', ''],
  77. ['excel', 0],
  78. ['status', 1],
  79. ['type', $this->request->param('type')]
  80. ]);
  81. return Json::successlayui(ProductModel::ProductList($where));
  82. }
  83. /**商品审核
  84. * @return mixed
  85. */
  86. public function examine()
  87. {
  88. $this->assign('cate', CategoryModel::getTierList());
  89. $this->assign('mer_list', Merchant::getMerchantList());
  90. return $this->fetch();
  91. }
  92. /**
  93. * 异步查找产品
  94. *
  95. * @return json
  96. */
  97. public function product_examine_ist()
  98. {
  99. $where = parent::getMore([
  100. ['page', 1],
  101. ['limit', 20],
  102. ['store_name', ''],
  103. ['cate_id', ''],
  104. ['mer_id', ''],
  105. ['status', ''],
  106. ]);
  107. return Json::successlayui(ProductModel::productExamineList($where));
  108. }
  109. /**不通过
  110. * @param $id
  111. * @throws \think\exception\DbException
  112. */
  113. public function fail($id)
  114. {
  115. $fail_msg = parent::postMore([
  116. ['message', ''],
  117. ]);
  118. if (!ProductModel::be(['id' => $id, 'status' => 0])) return Json::fail('操作记录不存在或状态错误!');
  119. $special = ProductModel::get($id);
  120. if (!$special) return Json::fail('操作记录不存!');
  121. if ($special->status != 0) return Json::fail('您已审核,请勿重复操作');
  122. ProductModel::beginTrans();
  123. $res = ProductModel::changeFail($id, $special['mer_id'], $fail_msg['message']);
  124. if ($res) {
  125. ProductModel::commitTrans();
  126. return Json::successful('操作成功!');
  127. } else {
  128. ProductModel::rollbackTrans();
  129. return Json::fail('操作失败!');
  130. }
  131. }
  132. /**通过
  133. * @param $id
  134. * @throws \think\exception\DbException
  135. */
  136. public function succ($id)
  137. {
  138. if (!ProductModel::be(['id' => $id, 'status' => 0])) return Json::fail('操作记录不存在或状态错误!');
  139. $special = ProductModel::get($id);
  140. if (!$special) return Json::fail('操作记录不存!');
  141. if ($special->status != 0) return Json::fail('您已审核,请勿重复操作');
  142. ProductModel::beginTrans();
  143. $res = ProductModel::changeSuccess($id, $special['mer_id']);
  144. if ($res) {
  145. ProductModel::commitTrans();
  146. return Json::successful('操作成功!');
  147. } else {
  148. ProductModel::rollbackTrans();
  149. return Json::fail('操作失败!');
  150. }
  151. }
  152. public function examineDetails($id)
  153. {
  154. if (!$id) return Json::fail('参数错误');
  155. $details = ProductModel::get($id);
  156. if (!$details) return Json::fail('商品不存在');
  157. $this->assign(['details' => json_encode($details)]);
  158. return $this->fetch('product');
  159. }
  160. /**
  161. * 设置单个产品上架|下架
  162. *
  163. * @return json
  164. */
  165. public function set_show($is_show = '', $id = '')
  166. {
  167. ($is_show == '' || $id == '') && Json::fail('缺少参数');
  168. $res = parent::getDataModification('product', $id, 'is_show', (int)$is_show);
  169. if ($res) {
  170. return Json::successful($is_show == 1 ? '上架成功' : '下架成功');
  171. } else {
  172. return Json::fail($is_show == 1 ? '上架失败' : '下架失败');
  173. }
  174. }
  175. /**
  176. * 快速编辑
  177. *
  178. * @return json
  179. */
  180. public function set_product($field = '', $id = '', $value = '')
  181. {
  182. ($field == '' || $id == '' || $value == '') && Json::fail('缺少参数');
  183. $res = parent::getDataModification('product', $id, $field, $value);
  184. if ($res)
  185. return Json::successful('保存成功');
  186. else
  187. return Json::fail('保存失败');
  188. }
  189. /**
  190. * 设置批量产品上架
  191. *
  192. * @return json
  193. */
  194. public function product_show()
  195. {
  196. $post = parent::postMore([
  197. ['ids', []]
  198. ]);
  199. if (empty($post['ids'])) {
  200. return Json::fail('请选择需要上架的产品');
  201. } else {
  202. $res = ProductModel::where('id', 'in', $post['ids'])->update(['is_show' => 1]);
  203. if ($res)
  204. return Json::successful('上架成功');
  205. else
  206. return Json::fail('上架失败');
  207. }
  208. }
  209. public function create($id = 0)
  210. {
  211. $gold_name = SystemConfigService::get('gold_name');//虚拟币名称
  212. if ($id) {
  213. $product = ProductModel::get($id);
  214. if (!$product) return Json::fail('数据不存在!');
  215. $slider_image = [];
  216. if ($product['slider_image']) {
  217. foreach (json_decode($product['slider_image']) as $key => $value) {
  218. $image['pic'] = $value;
  219. $image['is_show'] = false;
  220. array_push($slider_image, $image);
  221. }
  222. }
  223. $product['slider_image'] = $slider_image;
  224. } else {
  225. $product = [];
  226. }
  227. $this->assign(['id' => $id, 'product' => json_encode($product), 'gold_name' => $gold_name]);
  228. return $this->fetch();
  229. }
  230. /**商品分类
  231. * @throws \think\db\exception\DataNotFoundException
  232. * @throws \think\db\exception\ModelNotFoundException
  233. * @throws \think\exception\DbException
  234. */
  235. public function getCateList()
  236. {
  237. $list = CategoryModel::where(['is_show' => 1, 'pid' => 0])->order('sort desc,add_time desc')->select();
  238. $list = count($list) > 0 ? $list->toArray() : [];
  239. return Json::successful($list);
  240. }
  241. /**
  242. * 保存新建的资源
  243. *
  244. * @param \think\Request $request
  245. * @return \think\Response
  246. */
  247. public function save(Request $request, $id = 0)
  248. {
  249. $data = parent::postMore([
  250. ['cate_id', ""],
  251. 'store_name',
  252. 'store_info',
  253. 'keyword',
  254. ['unit_name', '件'],
  255. ['image', []],
  256. ['slider_image', []],
  257. 'postage',
  258. 'price',
  259. 'vip_price',
  260. 'ot_price',
  261. 'free_shipping',
  262. 'sort',
  263. 'stock',
  264. 'give_gold_num',
  265. 'ficti',
  266. 'description',
  267. ['is_show', 0],
  268. ['cost', 0],
  269. ['is_postage', 0],
  270. ['member_pay_type', 0],
  271. ['is_alone', 0],
  272. ['brokerage_ratio', 0],
  273. ['brokerage_two', 0]
  274. ], $request);
  275. if ($data['cate_id'] == "") return Json::fail('请选择商品分类');
  276. if (!$data['store_name']) return Json::fail('请输入商品名称');
  277. if (!$data['description']) return Json::fail('请输入商品详情');
  278. if (count($data['image']) < 1) return Json::fail('请上传商品图片');
  279. if (count($data['slider_image']) < 1) return Json::fail('请上传商品轮播图');
  280. if ($data['price'] == '' || $data['price'] < 0) return Json::fail('请输入商品售价');
  281. if ($data['ot_price'] == '' || $data['ot_price'] < 0) return Json::fail('请输入商品原价');
  282. if ($data['vip_price'] == '' || $data['vip_price'] < 0) return Json::fail('请输入商品会员价');
  283. if ($data['postage'] == '' || $data['postage'] < 0) return Json::fail('请输入邮费');
  284. if ($data['stock'] == '' || $data['stock'] < 0) return Json::fail('请输入商品库存');
  285. if ($data['cost'] == '' || $data['ot_price'] < 0) return Json::fail('请输入商品成本价');
  286. $data['image'] = $data['image'][0];
  287. $slider_image = [];
  288. foreach ($data['slider_image'] as $item) {
  289. $slider_image[] = $item['pic'];
  290. }
  291. $data['slider_image'] = json_encode($slider_image);
  292. if ($data['is_alone'] && bcadd($data['brokerage_ratio'], $data['brokerage_two'], 2) > 100) return Json::fail('两级返佣比例之和不能大于100');
  293. if ($id) {
  294. ProductModel::edit($data, $id);
  295. return Json::successful('修改产品成功!');
  296. } else {
  297. $data['add_time'] = time();
  298. ProductModel::set($data);
  299. return Json::successful('添加产品成功!');
  300. }
  301. }
  302. /**
  303. * 添加推荐
  304. * @param int $product_id
  305. * @return mixed
  306. * @throws \think\exception\DbException
  307. */
  308. public function recommend($product_id = 0)
  309. {
  310. if (!$product_id) $this->failed('缺少参数');
  311. $special = ProductModel::get($product_id);
  312. if (!$special) $this->failed('没有查到此专题');
  313. if ($special->is_del) $this->failed('此专题已删除');
  314. $form = Form::create(Url::build('save_recommend', ['product_id' => $product_id]), [
  315. Form::select('recommend_id', '推荐')->setOptions(function () use ($product_id) {
  316. $list = Recommend::where(['is_show' => 1, 'type' => 4])->where('is_fixed', 0)->field('title,id')->order('sort desc,add_time desc')->select();
  317. $menus = [];
  318. foreach ($list as $menu) {
  319. $menus[] = ['value' => $menu['id'], 'label' => $menu['title']];
  320. }
  321. return $menus;
  322. })->filterable(1),
  323. Form::number('sort', '排序'),
  324. ]);
  325. $form->setMethod('post')->setTitle('推荐设置')->setSuccessScript('parent.$(".J_iframe:visible")[0].contentWindow.location.reload(); setTimeout(function(){parent.layer.close(parent.layer.getFrameIndex(window.name));},800);');
  326. $this->assign(compact('form'));
  327. return $this->fetch('public/form-builder');
  328. }
  329. /**
  330. * 保存推荐
  331. * @param int $special_id
  332. * @throws \think\exception\DbException
  333. */
  334. public function save_recommend($product_id = 0)
  335. {
  336. if (!$product_id) $this->failed('缺少参数');
  337. $data = parent::postMore([
  338. ['recommend_id', 0],
  339. ['sort', 0],
  340. ]);
  341. if (!$data['recommend_id']) return Json::fail('请选择推荐');
  342. $recommend = Recommend::get($data['recommend_id']);
  343. if (!$recommend) return Json::fail('导航菜单不存在');
  344. $data['add_time'] = time();
  345. $data['type'] = $recommend->type;
  346. $data['link_id'] = $product_id;
  347. if (RecommendRelation::be(['type' => $recommend->type, 'link_id' => $product_id, 'recommend_id' => $data['recommend_id']])) return Json::fail('已推荐,请勿重复推荐');
  348. if (RecommendRelation::set($data))
  349. return Json::successful('推荐成功');
  350. else
  351. return Json::fail('推荐失败');
  352. }
  353. /**取消推荐
  354. * @param int $id
  355. */
  356. public function cancel_recommendation($id = 0, $product_id = 0)
  357. {
  358. if (!$id || !$product_id) $this->failed('缺少参数');
  359. if (RecommendRelation::be(['id' => $id, 'link_id' => $product_id])) {
  360. $res = RecommendRelation::where(['id' => $id, 'link_id' => $product_id])->delete();
  361. if ($res)
  362. return Json::successful('取消推荐成功');
  363. else
  364. return Json::fail('取消推荐失败');
  365. } else {
  366. return Json::fail('推荐不存在');
  367. }
  368. }
  369. /**
  370. * 删除指定资源
  371. *
  372. * @param int $id
  373. * @return \think\Response
  374. */
  375. public function delete($id)
  376. {
  377. if (!$id) return $this->failed('数据不存在');
  378. if (!ProductModel::be(['id' => $id])) return $this->failed('产品数据不存在');
  379. if (ProductModel::be(['id' => $id, 'is_del' => 1])) {
  380. $res = parent::getDataModification('product', $id, 'is_del', 0);
  381. if (!$res)
  382. return Json::fail(ProductModel::getErrorInfo('恢复失败,请稍候再试!'));
  383. else
  384. return Json::successful('成功恢复产品!');
  385. } else {
  386. $res = parent::getDataModification('product', $id, 'is_del', 1);
  387. if (!$res)
  388. return Json::fail(ProductModel::getErrorInfo('删除失败,请稍候再试!'));
  389. else
  390. return Json::successful('删除成功!');
  391. }
  392. }
  393. /**
  394. * 点赞
  395. * @param $id
  396. * @return mixed|\think\response\Json|void
  397. */
  398. public function collect($id)
  399. {
  400. if (!$id) return $this->failed('数据不存在');
  401. $product = ProductModel::get($id);
  402. if (!$product) return Json::fail('数据不存在!');
  403. $this->assign(StoreProductRelation::getCollect($id));
  404. return $this->fetch();
  405. }
  406. /**
  407. * 收藏
  408. * @param $id
  409. * @return mixed|\think\response\Json|void
  410. */
  411. public function like($id)
  412. {
  413. if (!$id) return $this->failed('数据不存在');
  414. $product = ProductModel::get($id);
  415. if (!$product) return Json::fail('数据不存在!');
  416. $this->assign(StoreProductRelation::getLike($id));
  417. return $this->fetch();
  418. }
  419. /**
  420. * 修改产品价格
  421. * @param Request $request
  422. */
  423. public function edit_product_price(Request $request)
  424. {
  425. $data = parent::postMore([
  426. ['id', 0],
  427. ['price', 0],
  428. ], $request);
  429. if (!$data['id']) return Json::fail('参数错误');
  430. $res = ProductModel::edit(['price' => $data['price']], $data['id']);
  431. if ($res) return Json::successful('修改成功');
  432. else return Json::fail('修改失败');
  433. }
  434. /**
  435. * 修改产品库存
  436. * @param Request $request
  437. */
  438. public function edit_product_stock(Request $request)
  439. {
  440. $data = parent::postMore([
  441. ['id', 0],
  442. ['stock', 0],
  443. ], $request);
  444. if (!$data['id']) return Json::fail('参数错误');
  445. $res = ProductModel::edit(['stock' => $data['stock']], $data['id']);
  446. if ($res) return Json::successful('修改成功');
  447. else return Json::fail('修改失败');
  448. }
  449. /**关联课程
  450. * @param int $id
  451. * @return mixed
  452. */
  453. public function knowledge($id = 0)
  454. {
  455. if (!$id) Json::fail('缺少参数');
  456. $this->assign(['id' => $id]);
  457. return $this->fetch();
  458. }
  459. /**获取商品关联的专题
  460. * @param int $id
  461. * @throws \think\Exception
  462. * @throws \think\db\exception\DataNotFoundException
  463. * @throws \think\db\exception\ModelNotFoundException
  464. * @throws \think\exception\DbException
  465. */
  466. public function knowledge_points($id = 0)
  467. {
  468. if (!$id) Json::fail('缺少参数');
  469. $data = Relation::getQuestionsRelationSpecial($id, 5);
  470. foreach ($data['data'] as $k => &$v) {
  471. if ($v['type'] == 6) $v['type'] = $v['light_type'];
  472. $v['types'] = parent::specialTaskType($v['type']);
  473. }
  474. return Json::successlayui($data);
  475. }
  476. /**关联专题
  477. * @param int $id
  478. */
  479. public function relation($id = 0)
  480. {
  481. if (!$id) Json::fail('缺少参数');
  482. $mer_list = Merchant::getMerchantList();
  483. $this->assign(['id' => $id,'mer_list' => $mer_list]);
  484. return $this->fetch('relation');
  485. }
  486. /**商品关联课程
  487. * @param int $id
  488. */
  489. public function add_knowledge_points($id, $special_ids)
  490. {
  491. if (!$id) Json::fail('缺少参数');
  492. $res = Relation::setRelations($id, $special_ids, 5);
  493. if ($res)
  494. return Json::successful('关联成功');
  495. else
  496. return Json::fail('关联失败');
  497. }
  498. /**商品关联课程排序
  499. * @param int $id
  500. * @param int $special_id
  501. * @param $value
  502. */
  503. public function up_knowledge_points_sort($id, $special_id, $value)
  504. {
  505. if (!$id || !$special_id) Json::fail('缺少参数');
  506. $res = Relation::updateRelationSort($id, $special_id, 5, $value);
  507. if ($res)
  508. return Json::successful('修改成功');
  509. else
  510. return Json::fail('修改失败');
  511. }
  512. /**删除关联专题
  513. * @param int $id
  514. * @param int $special_id
  515. * @throws \think\exception\DbException
  516. */
  517. public function delete_knowledge_points($id = 0, $special_id = 0)
  518. {
  519. if (!$id || !$special_id) Json::fail('缺少参数');
  520. $res = Relation::delRelation($id, $special_id, 5);
  521. if ($res)
  522. return Json::successful('删除成功');
  523. else
  524. return Json::fail('删除失败');
  525. }
  526. }