StoreProduct.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\StoreProductAttr;
  17. use app\admin\model\store\StoreProductAttrResult;
  18. use app\admin\model\store\StoreProductRelation;
  19. use app\admin\model\system\SystemConfig;
  20. use service\JsonService as Json;
  21. use service\SystemConfigService;
  22. use traits\CurdControllerTrait;
  23. use service\UploadService as Upload;
  24. use think\Request;
  25. use app\admin\model\store\StoreCategory as CategoryModel;
  26. use app\admin\model\store\StoreProduct as ProductModel;
  27. use think\Url;
  28. use app\admin\model\system\SystemAttachment;
  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. //出售中产品
  49. $onsale = ProductModel::where(['is_show' => 1, 'is_del' => 0])->count();
  50. //待上架产品
  51. $forsale = ProductModel::where(['is_show' => 0, 'is_del' => 0])->count();
  52. //仓库中产品
  53. $warehouse = ProductModel::where(['is_del' => 0])->count();
  54. //已经售馨产品
  55. $outofstock = ProductModel::getModelObject()->where(ProductModel::setData(4))->count();
  56. //警戒库存
  57. $policeforce = ProductModel::getModelObject()->where(ProductModel::setData(5))->count();
  58. //回收站
  59. $recycle = ProductModel::where(['is_del' => 1])->count();
  60. $this->assign(compact('type', 'onsale', 'forsale', 'warehouse', 'outofstock', 'policeforce', 'recycle'));
  61. return $this->fetch();
  62. }
  63. /**
  64. * 异步查找产品
  65. *
  66. * @return json
  67. */
  68. public function product_ist()
  69. {
  70. $where = parent::getMore([
  71. ['page', 1],
  72. ['limit', 20],
  73. ['store_name', ''],
  74. ['cate_id', ''],
  75. ['excel', 0],
  76. ['type', $this->request->param('type')]
  77. ]);
  78. return Json::successlayui(ProductModel::ProductList($where));
  79. }
  80. /**
  81. * 设置单个产品上架|下架
  82. *
  83. * @return json
  84. */
  85. public function set_show($is_show = '', $id = '')
  86. {
  87. ($is_show == '' || $id == '') && Json::fail('缺少参数');
  88. $res = ProductModel::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  89. if ($res) {
  90. return Json::successful($is_show == 1 ? '上架成功' : '下架成功');
  91. } else {
  92. return Json::fail($is_show == 1 ? '上架失败' : '下架失败');
  93. }
  94. }
  95. /**
  96. * 快速编辑
  97. *
  98. * @return json
  99. */
  100. public function set_product($field = '', $id = '', $value = '')
  101. {
  102. $field == '' || $id == '' || $value == '' && Json::fail('缺少参数');
  103. if (ProductModel::where(['id' => $id])->update([$field => $value]))
  104. return Json::successful('保存成功');
  105. else
  106. return Json::fail('保存失败');
  107. }
  108. /**
  109. * 设置批量产品上架
  110. *
  111. * @return json
  112. */
  113. public function product_show()
  114. {
  115. $post = parent::postMore([
  116. ['ids', []]
  117. ]);
  118. if (empty($post['ids'])) {
  119. return Json::fail('请选择需要上架的产品');
  120. } else {
  121. $res = ProductModel::where('id', 'in', $post['ids'])->update(['is_show' => 1]);
  122. if ($res)
  123. return Json::successful('上架成功');
  124. else
  125. return Json::fail('上架失败');
  126. }
  127. }
  128. public function create($id=0)
  129. {
  130. $gold_name=SystemConfigService::get('gold_name');//虚拟币名称
  131. if($id){
  132. $product = ProductModel::get($id);
  133. if (!$product) return Json::fail('数据不存在!');
  134. $slider_image=[];
  135. if($product['slider_image']){
  136. foreach (json_decode($product['slider_image']) as $key=>$value){
  137. $image['pic']=$value;
  138. $image['is_show']=false;
  139. array_push($slider_image,$image);
  140. }
  141. }
  142. $product['slider_image']=$slider_image;
  143. }else{
  144. $product=[];
  145. }
  146. $this->assign(['id'=> $id,'product'=>json_encode($product),'gold_name'=>$gold_name]);
  147. return $this->fetch();
  148. }
  149. public function getCateList()
  150. {
  151. $list=CategoryModel::where('is_show',1)->where('pid',0)->select();
  152. $list=count($list) >0 ? $list->toArray() : [];
  153. return Json::successful($list);
  154. }
  155. /**
  156. * 上传图片
  157. * @return \think\response\Json
  158. */
  159. public function upload()
  160. {
  161. $res = Upload::image('file', 'store/product/' . date('Ymd'));
  162. $thumbPath = Upload::thumb($res->dir);
  163. //产品图片上传记录
  164. $fileInfo = $res->fileInfo->getinfo();
  165. SystemAttachment::attachmentAdd($res->fileInfo->getSaveName(), $fileInfo['size'], $fileInfo['type'], $res->dir, $thumbPath, 1);
  166. if ($res->status == 200)
  167. return Json::successful('图片上传成功!', ['name' => $res->fileInfo->getSaveName(), 'url' => Upload::pathToUrl($thumbPath)]);
  168. else
  169. return Json::fail($res->error);
  170. }
  171. /**
  172. * 保存新建的资源
  173. *
  174. * @param \think\Request $request
  175. * @return \think\Response
  176. */
  177. public function save(Request $request,$id=0)
  178. {
  179. $data = parent::postMore([
  180. ['cate_id', ""],
  181. 'store_name',
  182. 'store_info',
  183. 'keyword',
  184. ['unit_name', '件'],
  185. ['image', []],
  186. ['slider_image', []],
  187. 'postage',
  188. 'price',
  189. 'vip_price',
  190. 'ot_price',
  191. 'free_shipping',
  192. 'sort',
  193. 'stock',
  194. 'give_gold_num',
  195. 'ficti',
  196. 'description',
  197. ['is_show', 0],
  198. ['cost', 0],
  199. ['is_postage', 0],
  200. ['member_pay_type', 0],
  201. ], $request);
  202. if ($data['cate_id'] == "") return Json::fail('请选择商品分类');
  203. if (!$data['store_name']) return Json::fail('请输入商品名称');
  204. if (!$data['description']) return Json::fail('请输入商品详情');
  205. if (count($data['image']) < 1) return Json::fail('请上传商品图片');
  206. if (count($data['slider_image']) < 1) return Json::fail('请上传商品轮播图');
  207. if ($data['price'] == '' || $data['price'] < 0) return Json::fail('请输入商品售价');
  208. if ($data['ot_price'] == '' || $data['ot_price'] < 0) return Json::fail('请输入商品原价');
  209. if ($data['vip_price'] == '' || $data['vip_price'] < 0) return Json::fail('请输入商品会员价');
  210. if ($data['postage'] == '' || $data['postage'] < 0) return Json::fail('请输入邮费');
  211. if ($data['stock'] == '' || $data['stock'] < 0) return Json::fail('请输入商品库存');
  212. if ($data['cost'] == '' || $data['ot_price'] < 0) return Json::fail('请输入商品成本价');
  213. $data['image'] = $data['image'][0];
  214. $slider_image=[];
  215. foreach ($data['slider_image'] as $item) {
  216. $slider_image[] = $item['pic'];
  217. }
  218. $data['slider_image'] = json_encode($slider_image);
  219. if($id){
  220. ProductModel::edit($data,$id);
  221. return Json::successful('修改产品成功!');
  222. }else{
  223. $data['add_time'] = time();
  224. ProductModel::set($data);
  225. return Json::successful('添加产品成功!');
  226. }
  227. }
  228. /**
  229. * 添加推荐
  230. * @param int $product_id
  231. * @return mixed
  232. * @throws \think\exception\DbException
  233. */
  234. public function recommend($product_id = 0)
  235. {
  236. if (!$product_id) $this->failed('缺少参数');
  237. $special = ProductModel::get($product_id);
  238. if (!$special) $this->failed('没有查到此专题');
  239. if ($special->is_del) $this->failed('此专题已删除');
  240. $form = Form::create(Url::build('save_recommend', ['product_id' => $product_id]), [
  241. Form::select('recommend_id', '推荐')->setOptions(function () use ($product_id) {
  242. $list = Recommend::where(['is_show' => 1,'type'=>4])->where('is_fixed', 0)->field('title,id')->order('sort desc,add_time desc')->select();
  243. $menus = [];
  244. foreach ($list as $menu) {
  245. $menus[] = ['value' => $menu['id'], 'label' => $menu['title']];
  246. }
  247. return $menus;
  248. })->filterable(1),
  249. Form::number('sort', '排序'),
  250. ]);
  251. $form->setMethod('post')->setTitle('推荐设置')->setSuccessScript('parent.$(".J_iframe:visible")[0].contentWindow.location.reload(); setTimeout(function(){parent.layer.close(parent.layer.getFrameIndex(window.name));},800);');
  252. $this->assign(compact('form'));
  253. return $this->fetch('public/form-builder');
  254. }
  255. /**
  256. * 保存推荐
  257. * @param int $special_id
  258. * @throws \think\exception\DbException
  259. */
  260. public function save_recommend($product_id = 0)
  261. {
  262. if (!$product_id) $this->failed('缺少参数');
  263. $data = parent::postMore([
  264. ['recommend_id', 0],
  265. ['sort', 0],
  266. ]);
  267. if (!$data['recommend_id']) return Json::fail('请选择推荐');
  268. $recommend = Recommend::get($data['recommend_id']);
  269. if (!$recommend) return Json::fail('导航菜单不存在');
  270. $data['add_time'] = time();
  271. $data['type'] = $recommend->type;
  272. $data['link_id'] = $product_id;
  273. if (RecommendRelation::be(['type' => $recommend->type, 'link_id' => $product_id, 'recommend_id' => $data['recommend_id']])) return Json::fail('已推荐,请勿重复推荐');
  274. if (RecommendRelation::set($data))
  275. return Json::successful('推荐成功');
  276. else
  277. return Json::fail('推荐失败');
  278. }
  279. /**取消推荐
  280. * @param int $id
  281. */
  282. public function cancel_recommendation($id=0,$product_id=0)
  283. {
  284. if (!$id || !$product_id) $this->failed('缺少参数');
  285. if (RecommendRelation::be(['id' => $id, 'link_id' => $product_id])){
  286. $res=RecommendRelation::where(['id'=>$id,'link_id'=>$product_id])->delete();
  287. if ($res)
  288. return Json::successful('取消推荐成功');
  289. else
  290. return Json::fail('取消推荐失败');
  291. }else{
  292. return Json::fail('推荐不存在');
  293. }
  294. }
  295. public function attr($id)
  296. {
  297. if (!$id) return $this->failed('数据不存在!');
  298. $result = StoreProductAttrResult::getResult($id);
  299. $image = ProductModel::where('id', $id)->value('image');
  300. $this->assign(compact('id', 'result', 'product', 'image'));
  301. return $this->fetch();
  302. }
  303. /**
  304. * 生成属性
  305. * @param int $id
  306. */
  307. public function is_format_attr($id = 0)
  308. {
  309. if (!$id) return Json::fail('产品不存在');
  310. list($attr, $detail) = parent::postMore([
  311. ['items', []],
  312. ['attrs', []]
  313. ], $this->request, true);
  314. $product = ProductModel::get($id);
  315. if (!$product) return Json::fail('产品不存在');
  316. $attrFormat = attrFormat($attr)[1];
  317. if (count($detail)) {
  318. foreach ($attrFormat as $k => $v) {
  319. foreach ($detail as $kk => $vv) {
  320. if ($v['detail'] == $vv['detail']) {
  321. $attrFormat[$k]['price'] = $vv['price'];
  322. $attrFormat[$k]['vip_price'] = $vv['vip_price'];
  323. $attrFormat[$k]['cost'] = isset($vv['cost']) ? $vv['cost'] : $product['cost'];
  324. $attrFormat[$k]['sales'] = $vv['sales'];
  325. $attrFormat[$k]['pic'] = $vv['pic'];
  326. $attrFormat[$k]['check'] = false;
  327. break;
  328. } else {
  329. $attrFormat[$k]['cost'] = $product['cost'];
  330. $attrFormat[$k]['price'] = '';
  331. $attrFormat[$k]['vip_price'] = '';
  332. $attrFormat[$k]['sales'] = '';
  333. $attrFormat[$k]['pic'] = $product['image'];
  334. $attrFormat[$k]['check'] = true;
  335. }
  336. }
  337. }
  338. } else {
  339. foreach ($attrFormat as $k => $v) {
  340. $attrFormat[$k]['cost'] = $product['cost'];
  341. $attrFormat[$k]['price'] = $product['price'];
  342. $attrFormat[$k]['vip_price'] = $product['vip_price'];
  343. $attrFormat[$k]['sales'] = $product['stock'];
  344. $attrFormat[$k]['pic'] = $product['image'];
  345. $attrFormat[$k]['check'] = false;
  346. }
  347. }
  348. return Json::successful($attrFormat);
  349. }
  350. public function set_attr($id)
  351. {
  352. if (!$id) return $this->failed('产品不存在!');
  353. list($attr, $detail) = parent::postMore([
  354. ['items', []],
  355. ['attrs', []]
  356. ], $this->request, true);
  357. $res = StoreProductAttr::createProductAttr($attr, $detail, $id);
  358. if ($res)
  359. return $this->successful('编辑属性成功!');
  360. else
  361. return $this->failed(StoreProductAttr::getErrorInfo());
  362. }
  363. public function clear_attr($id)
  364. {
  365. if (!$id) return $this->failed('产品不存在!');
  366. if (false !== StoreProductAttr::clearProductAttr($id) && false !== StoreProductAttrResult::clearResult($id))
  367. return $this->successful('清空产品属性成功!');
  368. else
  369. return $this->failed(StoreProductAttr::getErrorInfo('清空产品属性失败!'));
  370. }
  371. /**
  372. * 删除指定资源
  373. *
  374. * @param int $id
  375. * @return \think\Response
  376. */
  377. public function delete($id)
  378. {
  379. if(!$id) return $this->failed('数据不存在');
  380. if(!ProductModel::be(['id'=>$id])) return $this->failed('产品数据不存在');
  381. if(ProductModel::be(['id'=>$id,'is_del'=>1])){
  382. $data['is_del'] = 0;
  383. if(!ProductModel::edit($data,$id))
  384. return Json::fail(ProductModel::getErrorInfo('恢复失败,请稍候再试!'));
  385. else
  386. return Json::successful('成功恢复产品!');
  387. }else{
  388. $data['is_del'] = 1;
  389. if (!ProductModel::edit($data, $id))
  390. return Json::fail(ProductModel::getErrorInfo('删除失败,请稍候再试!'));
  391. else
  392. return Json::successful('删除成功!');
  393. }
  394. }
  395. /**
  396. * 点赞
  397. * @param $id
  398. * @return mixed|\think\response\Json|void
  399. */
  400. public function collect($id)
  401. {
  402. if (!$id) return $this->failed('数据不存在');
  403. $product = ProductModel::get($id);
  404. if (!$product) return Json::fail('数据不存在!');
  405. $this->assign(StoreProductRelation::getCollect($id));
  406. return $this->fetch();
  407. }
  408. /**
  409. * 收藏
  410. * @param $id
  411. * @return mixed|\think\response\Json|void
  412. */
  413. public function like($id)
  414. {
  415. if (!$id) return $this->failed('数据不存在');
  416. $product = ProductModel::get($id);
  417. if (!$product) return Json::fail('数据不存在!');
  418. $this->assign(StoreProductRelation::getLike($id));
  419. return $this->fetch();
  420. }
  421. /**
  422. * 修改产品价格
  423. * @param Request $request
  424. */
  425. public function edit_product_price(Request $request)
  426. {
  427. $data = parent::postMore([
  428. ['id', 0],
  429. ['price', 0],
  430. ], $request);
  431. if (!$data['id']) return Json::fail('参数错误');
  432. $res = ProductModel::edit(['price' => $data['price']], $data['id']);
  433. if ($res) return Json::successful('修改成功');
  434. else return Json::fail('修改失败');
  435. }
  436. /**
  437. * 修改产品库存
  438. * @param Request $request
  439. */
  440. public function edit_product_stock(Request $request)
  441. {
  442. $data = parent::postMore([
  443. ['id', 0],
  444. ['stock', 0],
  445. ], $request);
  446. if (!$data['id']) return Json::fail('参数错误');
  447. $res = ProductModel::edit(['stock' => $data['stock']], $data['id']);
  448. if ($res) return Json::successful('修改成功');
  449. else return Json::fail('修改失败');
  450. }
  451. }