StoreProduct.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. <?php
  2. namespace app\admin\controller\store;
  3. use app\admin\controller\AuthController;
  4. use app\models\store\SystemStockBill;
  5. use app\admin\model\store\{
  6. StoreDescription,
  7. StoreProductAttrValue,
  8. StoreProductAttr,
  9. StoreProductAttrResult,
  10. StoreProductCate,
  11. StoreProductRelation,
  12. StoreCategory as CategoryModel,
  13. StoreProduct as ProductModel
  14. };
  15. use app\admin\model\ump\StoreBargain;
  16. use app\admin\model\ump\StoreCombination;
  17. use app\admin\model\ump\StoreSeckill;
  18. use crmeb\services\{
  19. JsonService, UtilService as Util, JsonService as Json, FormBuilder as Form
  20. };
  21. use crmeb\traits\CurdControllerTrait;
  22. use think\facade\Route as Url;
  23. use app\admin\model\system\{SystemAttachment, ShippingTemplates, SystemStore};
  24. /**
  25. * 产品管理
  26. * Class StoreProduct
  27. * @package app\admin\controller\store
  28. */
  29. class StoreProduct extends AuthController
  30. {
  31. use CurdControllerTrait;
  32. protected $bindModel = ProductModel::class;
  33. protected $store;
  34. protected $main_where = [];
  35. public function initialize()
  36. {
  37. parent::initialize(); // TODO: Change the autogenerated stub
  38. $store_id = $this->adminInfo['store_id'];
  39. $this->store = SystemStore::where('id', $store_id)->find();
  40. if ($this->store && $this->store['is_triple']) {
  41. $this->main_where['store_id'] = $store_id;
  42. }
  43. }
  44. /**
  45. * 显示资源列表
  46. *
  47. * @return \think\Response
  48. */
  49. public function index()
  50. {
  51. $type = $this->request->param('type', 1);
  52. //获取分类
  53. $this->assign('cate', CategoryModel::getTierList(null, 1));
  54. //出售中产品
  55. $onsale = ProductModel::where('is_del', 0)->where($this->main_where)->where('is_consumer', 0)->where('is_show', 1)->count();
  56. //待上架产品
  57. $forsale = ProductModel::where('is_del', 0)->where($this->main_where)->where('is_consumer', 0)->where('is_show', 0)->count();
  58. //仓库中产品
  59. $warehouse = ProductModel::where('is_del', 0)->where($this->main_where)->where('is_consumer', 0)->count();
  60. //已经售馨产品
  61. $outofstock = ProductModel::getModelObject(['type' => 4])->where($this->main_where)->where('is_consumer', 0)->count();
  62. //警戒库存
  63. $policeforce = ProductModel::getModelObject(['type' => 5])->where($this->main_where)->where('is_consumer', 0)->count();
  64. //回收站
  65. $recycle = ProductModel::where('is_del', 1)->where($this->main_where)->where('is_consumer', 0)->count();
  66. $this->assign(compact('type', 'onsale', 'forsale', 'warehouse', 'outofstock', 'policeforce', 'recycle'));
  67. return $this->fetch();
  68. }
  69. public function consumer()
  70. {
  71. $type = $this->request->param('type', 1);
  72. //获取分类
  73. $this->assign('cate', CategoryModel::getTierList(null, 1, 1));
  74. //出售中产品
  75. $onsale = ProductModel::where('is_del', 0)->where('is_show', 1)->where('is_consumer', 1)->count();
  76. //待上架产品
  77. $forsale = ProductModel::where('is_del', 0)->where('is_show', 0)->where('is_consumer', 1)->count();
  78. //仓库中产品
  79. $warehouse = ProductModel::where('is_del', 0)->where('is_consumer', 1)->count();
  80. //已经售馨产品
  81. $outofstock = ProductModel::getModelObject(['type' => 4])->where('is_consumer', 1)->count();
  82. //警戒库存
  83. $policeforce = ProductModel::getModelObject(['type' => 5])->where('is_consumer', 1)->count();
  84. //回收站
  85. $recycle = ProductModel::where('is_del', 1)->where('is_consumer', 1)->count();
  86. $this->assign(compact('type', 'onsale', 'forsale', 'warehouse', 'outofstock', 'policeforce', 'recycle'));
  87. return $this->fetch();
  88. }
  89. /**
  90. * 异步查找产品
  91. *
  92. * @return json
  93. */
  94. public function product_ist()
  95. {
  96. $where = Util::getMore([
  97. ['page', 1],
  98. ['limit', 20],
  99. ['store_name', ''],
  100. ['cate_id', ''],
  101. ['excel', 0],
  102. ['order', ''],
  103. ['bar_code',''],
  104. [['type', 'd'], $this->request->param('type/d')],
  105. ['is_consumer', 0]
  106. ]);
  107. Json::successlayui(ProductModel::ProductList(array_merge($where, $this->main_where)));
  108. }
  109. /**
  110. * 设置单个产品上架|下架
  111. *
  112. * @return json
  113. */
  114. public function set_show($is_show = '', $id = '')
  115. {
  116. ($is_show == '' || $id == '') && Json::fail('缺少参数');
  117. $res = ProductModel::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  118. if ($res) {
  119. return Json::successful($is_show == 1 ? '上架成功' : '下架成功');
  120. } else {
  121. return Json::fail($is_show == 1 ? '上架失败' : '下架失败');
  122. }
  123. }
  124. /**
  125. * 快速编辑
  126. *
  127. * @return json
  128. */
  129. public function set_product($field = '', $id = '', $value = '')
  130. {
  131. $field == '' || $id == '' || $value == '' && Json::fail('缺少参数');
  132. if (ProductModel::where(['id' => $id])->update([$field => $value]))
  133. return Json::successful('保存成功');
  134. else
  135. return Json::fail('保存失败');
  136. }
  137. /**
  138. * 设置批量产品上架
  139. *
  140. * @return json
  141. */
  142. public function product_show()
  143. {
  144. $post = Util::postMore([
  145. ['ids', []]
  146. ]);
  147. if (empty($post['ids'])) {
  148. return Json::fail('请选择需要上架的产品');
  149. } else {
  150. $res = ProductModel::where('id', 'in', $post['ids'])->update(['is_show' => 1]);
  151. if ($res)
  152. return Json::successful('上架成功');
  153. else
  154. return Json::fail('上架失败');
  155. }
  156. }
  157. /**
  158. * 显示创建资源表单页.
  159. *
  160. * @return \think\Response
  161. */
  162. public function create($id = 0, $consumer = 0)
  163. {
  164. $this->assign('id', (int)$id);
  165. $this->assign('consumer', (int)$consumer);
  166. return $this->fetch();
  167. }
  168. /**
  169. * 获取规则属性模板
  170. * @throws \think\db\exception\DataNotFoundException
  171. * @throws \think\db\exception\DbException
  172. * @throws \think\db\exception\ModelNotFoundException
  173. */
  174. public function get_rule()
  175. {
  176. return Json::successful(\app\models\store\StoreProductRule::field(['rule_name', 'rule_value'])->select()->each(function ($item) {
  177. $item['rule_value'] = json_decode($item['rule_value'], true);
  178. })->toArray());
  179. }
  180. /**
  181. * 获取产品详细信息
  182. * @param int $id
  183. * @throws \think\db\exception\DataNotFoundException
  184. * @throws \think\db\exception\DbException
  185. * @throws \think\db\exception\ModelNotFoundException
  186. */
  187. public function get_product_info($id = 0, $type = 0)
  188. {
  189. $list = CategoryModel::getTierList(null, 1, $type);
  190. $menus = [];
  191. foreach ($list as $menu) {
  192. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name'], 'disabled' => $menu['pid'] == 0 ? 0 : 1];//,'disabled'=>$menu['pid']== 0];
  193. }
  194. $data['tempList'] = ShippingTemplates::order('sort', 'desc')->field(['id', 'name'])->select()->toArray();
  195. $data['cateList'] = $menus;
  196. $data['productInfo'] = [];
  197. if ($id) {
  198. $productInfo = ProductModel::get($id);
  199. if (!$productInfo) {
  200. return Json::fail('修改的产品不存在');
  201. }
  202. $productInfo['cate_id'] = explode(',', $productInfo['cate_id']);
  203. $productInfo['description'] = htmlspecialchars_decode(StoreDescription::getDescription($id));
  204. $productInfo['slider_image'] = is_string($productInfo['slider_image']) ? json_decode($productInfo['slider_image'], true) : [];
  205. if ($productInfo['spec_type'] == 1) {
  206. $result = StoreProductAttrResult::getResult($id, 0);
  207. foreach ($result['value'] as $k => $v) {
  208. $num = 1;
  209. foreach ($v['detail'] as $dv) {
  210. $result['value'][$k]['value' . $num] = $dv;
  211. $num++;
  212. }
  213. }
  214. $productInfo['items'] = $result['attr'];
  215. $productInfo['attrs'] = $result['value'];
  216. $productInfo['attr'] = ['pic' => '', 'price' => 0, 'cost' => 0, 'ot_price' => 0, 'stock' => 0, 'bar_code' => '', 'weight' => 0, 'volume' => 0, 'brokerage' => 0, 'brokerage_two' => 0, 'brokerage_suit' => 0];
  217. } else {
  218. $result = StoreProductAttrValue::where('product_id', $id)->where('type', 0)->find();
  219. if ($result) {
  220. $single = $result->toArray();
  221. } else {
  222. $single = [];
  223. }
  224. $productInfo['items'] = [];
  225. $productInfo['attrs'] = [];
  226. $productInfo['attr'] = [
  227. 'pic' => $single['image'] ?? '',
  228. 'price' => $single['price'] ?? 0,
  229. 'cost' => $single['cost'] ?? 0,
  230. 'ot_price' => $single['ot_price'] ?? 0,
  231. 'stock' => $single['stock'] ?? 0,
  232. 'bar_code' => $single['bar_code'] ?? '',
  233. 'weight' => $single['weight'] ?? 0,
  234. 'volume' => $single['volume'] ?? 0,
  235. 'brokerage' => $single['brokerage'] ?? 0,
  236. 'brokerage_two' => $single['brokerage_two'] ?? 0,
  237. 'brokerage_suit' => $single['brokerage_suit'] ?? 0,
  238. ];
  239. }
  240. if ($productInfo['activity']) {
  241. $activity = explode(',', $productInfo['activity']);
  242. foreach ($activity as $k => $v) {
  243. if ($v == 1) {
  244. $activity[$k] = '秒杀';
  245. } elseif ($v == 2) {
  246. $activity[$k] = '砍价';
  247. } elseif ($v == 3) {
  248. $activity[$k] = '拼团';
  249. }
  250. }
  251. $productInfo['activity'] = $activity;
  252. } else {
  253. $productInfo['activity'] = ['秒杀', '砍价', '拼团'];
  254. }
  255. $time_area_discount = json_decode($productInfo['time_area_discount'] ?: '[]', true);
  256. $selectTime = [];
  257. foreach ($time_area_discount as $k => $v) {
  258. $times = explode('-', $k);
  259. $selectTime[] = ['start' => $times[0], 'end' => $times[1], 'value' => $v];
  260. }
  261. $productInfo['selectTime'] = $selectTime;
  262. $data['productInfo'] = $productInfo;
  263. }
  264. return JsonService::successful($data);
  265. }
  266. /**
  267. * 保存新建的资源
  268. *
  269. *
  270. */
  271. public function save($id)
  272. {
  273. $data = Util::postMore([
  274. ['cate_id', []],
  275. 'store_name',
  276. 'store_info',
  277. 'keyword',
  278. ['unit_name', '件'],
  279. ['image', []],
  280. ['slider_image', []],
  281. ['postage', 0],
  282. ['is_sub', 0],
  283. ['sort', 0],
  284. ['sales', 0],
  285. ['ficti', 100],
  286. ['give_integral', 0],
  287. ['max_use_integral', 0],
  288. ['is_show', 0],
  289. ['temp_id', 0],
  290. ['is_hot', 0],
  291. ['is_benefit', 0],
  292. ['is_suit', 0],
  293. ['is_bind', 0],
  294. ['is_award', 0],
  295. ['is_best', 0],
  296. ['is_new', 0],
  297. ['mer_use', 0],
  298. ['is_postage', 0],
  299. ['is_good', 0],
  300. ['description', ''],
  301. ['spec_type', 0],
  302. ['video_link', ''],
  303. ['items', []],
  304. ['attrs', []],
  305. ['activity', []],
  306. ['is_consumer', 0],
  307. ['bar_code', ''],
  308. ['store_bag', 0],
  309. ['store_spread', 0],
  310. ['selectTime', []]
  311. ]);
  312. foreach ($data['activity'] as $k => $v) {
  313. if ($v == '秒杀') {
  314. $data['activity'][$k] = 1;
  315. } elseif ($v == '砍价') {
  316. $data['activity'][$k] = 2;
  317. } else {
  318. $data['activity'][$k] = 3;
  319. }
  320. }
  321. $data['time_area_discount'] = [];
  322. foreach ($data['selectTime'] as $v) {
  323. if (strtotime(date('Y-m-d') . ' ' . ($v['start'] ?? '00:00')) >= strtotime(date('Y-m-d') . ' ' . ($v['end'] ?? '00:00'))) {
  324. return Json::fail('区间折扣的开始时间需要小于结束时间');
  325. }
  326. $data['time_area_discount'][$v['start'] . '-' . $v['end']] = $v['value'];
  327. }
  328. $data['time_area_discount'] = json_encode($data['time_area_discount']);
  329. unset($data['selectTime']);
  330. $data['activity'] = implode(',', $data['activity']);
  331. $detail = $data['attrs'];
  332. $data['price'] = min(array_column($detail, 'price'));
  333. $data['ot_price'] = min(array_column($detail, 'ot_price'));
  334. $data['cost'] = min(array_column($detail, 'cost'));
  335. $attr = $data['items'];
  336. unset($data['items'], $data['video'], $data['attrs']);
  337. if (count($data['cate_id']) < 1) return Json::fail('请选择产品分类');
  338. $cate_id = $data['cate_id'];
  339. $data['cate_id'] = implode(',', $data['cate_id']);
  340. if (!$data['store_name']) return Json::fail('请输入产品名称');
  341. if (count($data['image']) < 1) return Json::fail('请上传产品图片');
  342. if (count($data['slider_image']) < 1) return Json::fail('请上传产品轮播图');
  343. $data['image'] = $data['image'][0];
  344. $data['slider_image'] = json_encode($data['slider_image']);
  345. $data['stock'] = array_sum(array_column($detail, 'stock'));
  346. ProductModel::beginTrans();
  347. foreach ($detail as &$item) {
  348. if (($item['brokerage'] + $item['brokerage_two']) > $item['price']) {
  349. return Json::fail('一二级返佣相加不能大于商品售价');
  350. }
  351. }
  352. if ($id) {
  353. unset($data['sales']);
  354. ProductModel::edit($data, $id);
  355. $description = $data['description'];
  356. unset($data['description']);
  357. StoreDescription::saveDescription($description, $id);
  358. StoreProductCate::where('product_id', $id)->delete();
  359. $cateData = [];
  360. foreach ($cate_id as $cid) {
  361. $cateData[] = ['product_id' => $id, 'cate_id' => $cid, 'add_time' => time()];
  362. }
  363. StoreProductCate::insertAll($cateData);
  364. if ($data['spec_type'] == 0) {
  365. $attr = [
  366. [
  367. 'value' => '规格',
  368. 'detailValue' => '',
  369. 'attrHidden' => '',
  370. 'detail' => ['默认']
  371. ]
  372. ];
  373. $detail[0]['value1'] = '规格';
  374. $detail[0]['detail'] = ['规格' => '默认'];
  375. }
  376. $attr_res = StoreProductAttr::createProductAttr($attr, $detail, $id);
  377. if ($attr_res) {
  378. ProductModel::commitTrans();
  379. return Json::success('修改成功!');
  380. } else {
  381. ProductModel::rollbackTrans();
  382. return Json::fail(StoreProductAttr::getErrorInfo());
  383. }
  384. } else {
  385. $data['add_time'] = time();
  386. $data['code_path'] = '';
  387. $res = ProductModel::create($data);
  388. $description = $data['description'];
  389. StoreDescription::saveDescription($description, $res['id']);
  390. $cateData = [];
  391. foreach ($cate_id as $cid) {
  392. $cateData[] = ['product_id' => $res['id'], 'cate_id' => $cid, 'add_time' => time()];
  393. }
  394. StoreProductCate::insertAll($cateData);
  395. if ($data['spec_type'] == 0) {
  396. $attr = [
  397. [
  398. 'value' => '规格',
  399. 'detailValue' => '',
  400. 'attrHidden' => '',
  401. 'detail' => ['默认']
  402. ]
  403. ];
  404. $detail[0]['value1'] = '规格';
  405. $detail[0]['detail'] = ['规格' => '默认'];
  406. }
  407. $attr_res = StoreProductAttr::createProductAttr($attr, $detail, $res['id']);
  408. if ($attr_res) {
  409. ProductModel::commitTrans();
  410. return Json::success('添加产品成功!');
  411. } else {
  412. ProductModel::rollbackTrans();
  413. return Json::fail(StoreProductAttr::getErrorInfo());
  414. }
  415. }
  416. }
  417. public function attr_save()
  418. {
  419. $where = Util::getMore([
  420. ['bar_code', 20],
  421. ['in_stock', 0],
  422. ]);
  423. if ($where['in_stock'] < 0.001) return app('json')->fail('补货数量不能少于0.001');
  424. $info = StoreProductAttrValue::alias('a')->join("StoreProduct b", "a.product_id=b.id", "left")->where('a.type', 0)->where('a.bar_code', $where['bar_code'])->field('a.price,a.unique,a.stock as in_stock,a.product_id,b.store_name,b.image,a.bar_code')->find();
  425. if (!$info) return app('json')->fail('商品条形码不存在');
  426. $info = $info->toarray();
  427. $info['in_stock'] = $where['in_stock'];
  428. $info['admin_id'] = $this->adminId;
  429. if (SystemStockBill::order_create($info)) {
  430. return Json::successful('入库审核提交成功');
  431. } else {
  432. return Json::fail('入库审核提交失败');
  433. }
  434. }
  435. public function bill()
  436. {
  437. return $this->fetch();
  438. }
  439. public function bill_lst()
  440. {
  441. $where = Util::getMore([
  442. ['key', ''],
  443. ['page', 0],
  444. ['limit', 0],
  445. ['status', -2],
  446. ]);
  447. return Json::successlayui(SystemStockBill::lst($where));
  448. }
  449. public function audit($id)
  450. {
  451. if (!$id) return $this->failed('数据不存在');
  452. $Bill = SystemStockBill::where('id', $id)->find();
  453. if (!$Bill) return Json::fail('数据不存在!');
  454. $f = array();
  455. $f[] = Form::radio('status', '状态', $Bill->getData('status'))->options([['value' => 1, 'label' => '通过'], ['value' => -1, 'label' => '拒绝']]);
  456. $f[] = Form::textarea('refuse_msg', '备注');
  457. $form = Form::make_post_form('入库审核', $f, Url::buildUrl('audit_save', array('id' => $id)), 2);
  458. $this->assign(compact('form'));
  459. return $this->fetch('public/form-builder');
  460. }
  461. public function batch_audit($id)
  462. {
  463. if (!$id) return $this->failed('数据不存在');
  464. $f = array();
  465. $f[] = Form::radio('status', '状态')->options([['value' => 1, 'label' => '通过'], ['value' => -1, 'label' => '拒绝']]);
  466. $f[] = Form::textarea('refuse_msg', '备注');
  467. $form = Form::make_post_form('入库审核', $f, Url::buildUrl('batch_audit_save', array('id' => $id)), 2);
  468. $this->assign(compact('form'));
  469. return $this->fetch('public/form-builder');
  470. }
  471. public function audit_save($id)
  472. {
  473. $where = Util::postMore([
  474. ['refuse_msg', ''],
  475. ['status', 1],
  476. ]);
  477. if (!SystemStockBill::be(['id' => $id, 'status' => 0])) return $this->failed('数据不存在或已经审核');
  478. $where['audit_time'] = time();
  479. $where['audit_admin'] = $this->adminId;
  480. SystemStockBill::edit($where, $id);
  481. if ($where['status'] == 1) {
  482. $info = SystemStockBill::find($id)->toArray();
  483. $rs = StoreProductAttrValue::in_Stock($info);
  484. }
  485. return Json::successful('审核成功');
  486. }
  487. /**
  488. * 批量审核
  489. */
  490. public function batch_audit_save()
  491. {
  492. $where = Util::postMore([
  493. ['refuse_msg', ''],
  494. ['status', 1],
  495. ]);
  496. $ids = explode(',', input('id'));
  497. if ($where['status'] == 1) {
  498. foreach ($ids as $v) {
  499. $info = SystemStockBill::where('id', $v)->where('status', 0)->find();
  500. if ($info) {
  501. $rs = StoreProductAttrValue::in_Stock($info);
  502. }
  503. }
  504. }
  505. $where['audit_time'] = time();
  506. $where['audit_admin'] = $this->adminId;
  507. SystemStockBill::where('id', 'in', join(",", $ids))->update($where);
  508. return Json::successful('批量审核成功');
  509. }
  510. public function edit_content($id)
  511. {
  512. if (!$id) return $this->failed('数据不存在');
  513. $product = ProductModel::get($id);
  514. if (!$product) return Json::fail('数据不存在!');
  515. $this->assign([
  516. 'content' => $product->description,
  517. 'field' => 'description',
  518. 'action' => Url::buildUrl('change_field', ['id' => $id, 'field' => 'description'])
  519. ]);
  520. return $this->fetch('public/edit_content');
  521. }
  522. /**
  523. * 显示编辑资源表单页.
  524. *
  525. * @param int $id
  526. * @return \think\Response
  527. */
  528. public function edit($id)
  529. {
  530. if (!$id) return $this->failed('数据不存在');
  531. $product = ProductModel::get($id);
  532. if (!$product) return Json::fail('数据不存在!');
  533. $field = [
  534. Form::select('cate_id', '产品分类', explode(',', $product->getData('cate_id')))->setOptions(function () {
  535. $list = CategoryModel::getTierList(null, 1);
  536. $menus = [];
  537. foreach ($list as $menu) {
  538. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name'], 'disabled' => $menu['pid'] == 0];//,'disabled'=>$menu['pid']== 0];
  539. }
  540. return $menus;
  541. })->filterable(1)->multiple(1),
  542. Form::input('store_name', '产品名称', $product->getData('store_name')),
  543. Form::input('store_info', '产品简介', $product->getData('store_info'))->type('textarea'),
  544. Form::input('keyword', '产品关键字', $product->getData('keyword'))->placeholder('多个用英文状态下的逗号隔开'),
  545. Form::input('unit_name', '产品单位', $product->getData('unit_name'))->col(12),
  546. Form::input('bar_code', '产品条码', $product->getData('bar_code'))->col(12),
  547. Form::frameImageOne('image', '产品主图片(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), $product->getData('image'))->icon('image')->width('100%')->height('500px'),
  548. Form::frameImages('slider_image', '产品轮播图(640*640px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'slider_image')), json_decode($product->getData('slider_image'), 1) ?: [])->maxLength(5)->icon('images')->width('100%')->height('500px'),
  549. Form::number('price', '产品售价', $product->getData('price'))->min(0)->col(8),
  550. Form::number('ot_price', '产品市场价', $product->getData('ot_price'))->min(0)->col(8),
  551. Form::number('give_integral', '赠送积分', $product->getData('give_integral'))->min(0)->col(8),
  552. Form::number('postage', '邮费', $product->getData('postage'))->min(0)->col(8),
  553. Form::number('sales', '销量', $product->getData('sales'))->min(0)->precision(0)->col(8)->readonly(1),
  554. Form::number('ficti', '虚拟销量', $product->getData('ficti'))->min(0)->precision(0)->col(8),
  555. Form::number('stock', '库存', ProductModel::getStock($id) > 0 ? ProductModel::getStock($id) : $product->getData('stock'))->min(0)->precision(0)->col(8),
  556. Form::number('cost', '产品成本价', $product->getData('cost'))->min(0)->col(8),
  557. Form::number('sort', '排序', $product->getData('sort'))->col(8),
  558. Form::radio('is_show', '产品状态', $product->getData('is_show'))->options([['label' => '上架', 'value' => 1], ['label' => '下架', 'value' => 0]])->col(8),
  559. Form::radio('is_hot', '热卖单品', $product->getData('is_hot'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  560. Form::radio('is_benefit', '促销单品', $product->getData('is_benefit'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  561. Form::radio('is_best', '精品推荐', $product->getData('is_best'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  562. Form::radio('is_new', '首发新品', $product->getData('is_new'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  563. Form::radio('is_postage', '是否包邮', $product->getData('is_postage'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  564. Form::radio('is_good', '是否优品推荐', $product->getData('is_good'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  565. ];
  566. $form = Form::make_post_form('编辑产品', $field, Url::buildUrl('update', array('id' => $id)), 2);
  567. $this->assign(compact('form'));
  568. return $this->fetch('public/form-builder');
  569. }
  570. /**
  571. * 保存更新的资源
  572. *
  573. * @param $id
  574. */
  575. public function update($id)
  576. {
  577. $data = Util::postMore([
  578. ['cate_id', []],
  579. 'store_name',
  580. 'store_info',
  581. 'keyword',
  582. 'bar_code',
  583. ['unit_name', '件'],
  584. ['image', []],
  585. ['slider_image', []],
  586. ['postage', 0],
  587. ['ot_price', 0],
  588. ['price', 0],
  589. ['sort', 0],
  590. ['stock', 0],
  591. ['temp_id', 0],
  592. ['ficti', 100],
  593. ['give_integral', 0],
  594. ['is_show', 0],
  595. ['cost', 0],
  596. ['is_hot', 0],
  597. ['is_benefit', 0],
  598. ['is_best', 0],
  599. ['is_new', 0],
  600. ['mer_use', 0],
  601. ['is_postage', 0],
  602. ['is_good', 0],
  603. ]);
  604. if (count($data['cate_id']) < 1) return Json::fail('请选择产品分类');
  605. $cate_id = $data['cate_id'];
  606. $data['cate_id'] = implode(',', $data['cate_id']);
  607. if (!$data['store_name']) return Json::fail('请输入产品名称');
  608. if (count($data['image']) < 1) return Json::fail('请上传产品图片');
  609. if (count($data['slider_image']) < 1) return Json::fail('请上传产品轮播图');
  610. // if(count($data['slider_image'])>8) return Json::fail('轮播图最多5张图');
  611. if ($data['price'] == '' || $data['price'] < 0) return Json::fail('请输入产品售价');
  612. if ($data['ot_price'] == '' || $data['ot_price'] < 0) return Json::fail('请输入产品市场价');
  613. if ($data['stock'] == '' || $data['stock'] < 0) return Json::fail('请输入库存');
  614. $data['image'] = $data['image'][0];
  615. $data['slider_image'] = json_encode($data['slider_image']);
  616. ProductModel::edit($data, $id);
  617. StoreProductCate::where('product_id', $id)->delete();
  618. foreach ($cate_id as $cid) {
  619. StoreProductCate::insert(['product_id' => $id, 'cate_id' => $cid, 'add_time' => time()]);
  620. }
  621. return Json::successful('修改成功!');
  622. }
  623. public function attr($id)
  624. {
  625. if (!$id) return $this->failed('数据不存在!');
  626. // $result = StoreProductAttrResult::getResult($id);
  627. $result = StoreProductAttrValue::getStoreProductAttrResult($id);
  628. $image = ProductModel::where('id', $id)->value('image');
  629. $this->assign(compact('id', 'result', 'image'));
  630. return $this->fetch();
  631. }
  632. /**
  633. * 生成属性
  634. * @param int $id
  635. */
  636. public function is_format_attr($id = 0, $type = 0)
  637. {
  638. $data = Util::postMore([
  639. ['attrs', []],
  640. ['items', []]
  641. ]);
  642. $attr = $data['attrs'];
  643. $value = attr_format($attr)[1];
  644. $valueNew = [];
  645. $count = 0;
  646. foreach ($value as $key => $item) {
  647. $detail = $item['detail'];
  648. sort($item['detail'], SORT_STRING);
  649. $suk = implode(',', $item['detail']);
  650. $types = 1;
  651. if ($id) {
  652. $sukValue = StoreProductAttrValue::where('product_id', $id)->where('type', 0)->where('suk', $suk)->column('bar_code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two,brokerage_suit', 'suk');
  653. if (!count($sukValue)) {
  654. if ($type == 0) $types = 0; //编辑商品时,将没有规格的数据不生成默认值
  655. $sukValue[$suk]['pic'] = '';
  656. $sukValue[$suk]['price'] = 0;
  657. $sukValue[$suk]['cost'] = 0;
  658. $sukValue[$suk]['ot_price'] = 0;
  659. $sukValue[$suk]['stock'] = 0;
  660. $sukValue[$suk]['bar_code'] = '';
  661. $sukValue[$suk]['weight'] = 0;
  662. $sukValue[$suk]['volume'] = 0;
  663. $sukValue[$suk]['brokerage'] = 0;
  664. $sukValue[$suk]['brokerage_two'] = 0;
  665. $sukValue[$suk]['brokerage_suit'] = 0;
  666. }
  667. } else {
  668. $sukValue[$suk]['pic'] = '';
  669. $sukValue[$suk]['price'] = 0;
  670. $sukValue[$suk]['cost'] = 0;
  671. $sukValue[$suk]['ot_price'] = 0;
  672. $sukValue[$suk]['stock'] = 0;
  673. $sukValue[$suk]['bar_code'] = '';
  674. $sukValue[$suk]['weight'] = 0;
  675. $sukValue[$suk]['volume'] = 0;
  676. $sukValue[$suk]['brokerage'] = 0;
  677. $sukValue[$suk]['brokerage_two'] = 0;
  678. $sukValue[$suk]['brokerage_suit'] = 0;
  679. }
  680. if ($types) { //编辑商品时,将没有规格的数据不生成默认值
  681. foreach (array_keys($detail) as $k => $title) {
  682. $header[$k]['title'] = $title;
  683. $header[$k]['align'] = 'center';
  684. $header[$k]['minWidth'] = 130;
  685. }
  686. foreach (array_values($detail) as $k => $v) {
  687. $valueNew[$count]['value' . ($k + 1)] = $v;
  688. $header[$k]['key'] = 'value' . ($k + 1);
  689. }
  690. $valueNew[$count]['detail'] = $detail;
  691. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  692. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  693. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  694. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  695. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  696. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  697. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ?? 0;
  698. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ?? 0;
  699. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ?? 0;
  700. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ?? 0;
  701. $valueNew[$count]['brokerage_suit'] = $sukValue[$suk]['brokerage_suit'] ?? 0;
  702. $count++;
  703. }
  704. }
  705. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 80];
  706. $header[] = ['title' => '售价', 'slot' => 'price', 'align' => 'center', 'minWidth' => 120];
  707. $header[] = ['title' => '成本价', 'slot' => 'cost', 'align' => 'center', 'minWidth' => 140];
  708. $header[] = ['title' => '原价', 'slot' => 'ot_price', 'align' => 'center', 'minWidth' => 140];
  709. $header[] = ['title' => '库存', 'slot' => 'stock', 'align' => 'center', 'minWidth' => 140];
  710. $header[] = ['title' => '产品编号', 'slot' => 'bar_code', 'align' => 'center', 'minWidth' => 140];
  711. $header[] = ['title' => '重量(KG)', 'slot' => 'weight', 'align' => 'center', 'minWidth' => 140];
  712. $header[] = ['title' => '体积(m³)', 'slot' => 'volume', 'align' => 'center', 'minWidth' => 140];
  713. $header[] = ['title' => '操作', 'slot' => 'action', 'align' => 'center', 'minWidth' => 70];
  714. $info = ['attr' => $attr, 'value' => $valueNew, 'header' => $header];
  715. return Json::successful($info);
  716. }
  717. public function set_attr($id)
  718. {
  719. if (!$id) return $this->failed('产品不存在!');
  720. list($attr, $detail) = Util::postMore([
  721. ['items', []],
  722. ['attrs', []]
  723. ], null, true);
  724. $res = StoreProductAttr::createProductAttr($attr, $detail, $id);
  725. if ($res)
  726. return $this->successful('编辑属性成功!');
  727. else
  728. return $this->failed(StoreProductAttr::getErrorInfo());
  729. }
  730. public function clear_attr($id)
  731. {
  732. if (!$id) return $this->failed('产品不存在!');
  733. if (false !== StoreProductAttr::clearProductAttr($id) && false !== StoreProductAttrResult::clearResult($id))
  734. return $this->successful('清空产品属性成功!');
  735. else
  736. return $this->failed(StoreProductAttr::getErrorInfo('清空产品属性失败!'));
  737. }
  738. /**
  739. * 删除指定资源
  740. *
  741. * @param int $id
  742. * @return \think\Response
  743. */
  744. public function delete($id)
  745. {
  746. if (!$id) return $this->failed('数据不存在');
  747. if (!ProductModel::be(['id' => $id])) return $this->failed('产品数据不存在');
  748. if (ProductModel::be(['id' => $id, 'is_del' => 1])) {
  749. $data['is_del'] = 0;
  750. if (!ProductModel::edit($data, $id))
  751. return Json::fail(ProductModel::getErrorInfo('恢复失败,请稍候再试!'));
  752. else
  753. return Json::successful('成功恢复产品!');
  754. } else {
  755. $res1 = StoreSeckill::where('product_id', $id)->where('is_del', 0)->find();
  756. $res2 = StoreBargain::where('product_id', $id)->where('is_del', 0)->find();
  757. $res3 = StoreCombination::where('product_id', $id)->where('is_del', 0)->find();
  758. if ($res1 || $res2 || $res3) {
  759. return Json::fail(ProductModel::getErrorInfo('该商品已参加活动,无法删除!'));
  760. } else {
  761. $data['is_del'] = 1;
  762. if (!ProductModel::edit($data, $id))
  763. return Json::fail(ProductModel::getErrorInfo('删除失败,请稍候再试!'));
  764. else
  765. return Json::successful('成功移到回收站!');
  766. }
  767. }
  768. }
  769. /**
  770. * 点赞
  771. * @param $id
  772. * @return mixed|\think\response\Json|void
  773. */
  774. public function collect($id)
  775. {
  776. if (!$id) return $this->failed('数据不存在');
  777. $product = ProductModel::get($id);
  778. if (!$product) return Json::fail('数据不存在!');
  779. $this->assign(StoreProductRelation::getCollect($id));
  780. return $this->fetch();
  781. }
  782. /**
  783. * 收藏
  784. * @param $id
  785. * @return mixed|\think\response\Json|void
  786. */
  787. public function like($id)
  788. {
  789. if (!$id) return $this->failed('数据不存在');
  790. $product = ProductModel::get($id);
  791. if (!$product) return Json::fail('数据不存在!');
  792. $this->assign(StoreProductRelation::getLike($id));
  793. return $this->fetch();
  794. }
  795. /**
  796. * 修改产品价格
  797. */
  798. public function edit_product_price()
  799. {
  800. $data = Util::postMore([
  801. ['id', 0],
  802. ['price', 0],
  803. ]);
  804. if (!$data['id']) return Json::fail('参数错误');
  805. $res = ProductModel::edit(['price' => $data['price']], $data['id']);
  806. if ($res) return Json::successful('修改成功');
  807. else return Json::fail('修改失败');
  808. }
  809. /**
  810. * 修改产品库存
  811. *
  812. */
  813. public function edit_product_stock()
  814. {
  815. $data = Util::postMore([
  816. ['id', 0],
  817. ['stock', 0],
  818. ]);
  819. if (!$data['id']) return Json::fail('参数错误');
  820. $res = ProductModel::edit(['stock' => $data['stock']], $data['id']);
  821. if ($res) return Json::successful('修改成功');
  822. else return Json::fail('修改失败');
  823. }
  824. /**
  825. * 检测商品是否开活动
  826. * @param $id
  827. * @throws \think\db\exception\DataNotFoundException
  828. * @throws \think\db\exception\DbException
  829. * @throws \think\db\exception\ModelNotFoundException
  830. */
  831. public function check_activity($id)
  832. {
  833. if ($id != 0) {
  834. $res1 = StoreSeckill::where('product_id', $id)->where('is_del', 0)->find();
  835. $res2 = StoreBargain::where('product_id', $id)->where('is_del', 0)->find();
  836. $res3 = StoreCombination::where('product_id', $id)->where('is_del', 0)->find();
  837. if ($res1 || $res2 || $res3) {
  838. return Json::successful('该商品有活动开启,无法删除属性');
  839. } else {
  840. return Json::fail('删除成功');
  841. }
  842. } else {
  843. return Json::fail('没有参数ID');
  844. }
  845. }
  846. public function product_br()
  847. {
  848. $list = StoreProductAttrValue::where('bar_code', '<', '99999')->where('bar_code', '>', '0')->where('volume', '>', '0')->where('volume', '<', '99999')->column('product_id,bar_code,price,volume');
  849. $str = "";
  850. foreach ($list as $v) {
  851. $title = ProductModel::where('id', $v['product_id'])->value('store_name');
  852. $str .= sprintf("PLU\t%d\t%s\t\t%s\t%s\t1\t1\t999\t\t\r\n", $v['volume'], $v['bar_code'], $title, bcadd($v['price'], 0, 2));
  853. }
  854. $str = $this->charsetToGBK($str);
  855. header("Content-type: text/plain; charset=gbk");
  856. header("Accept-Ranges: bytes");
  857. header("Content-Disposition: attachment; filename=NewFile.txt");
  858. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  859. header("Pragma: no-cache");
  860. header("Expires: 0");
  861. exit($str);
  862. }
  863. /**
  864. * 将非GBK字符集的编码转为GBK
  865. *
  866. * @param mixed $mixed 源数据
  867. *
  868. * @return mixed GBK格式数据
  869. */
  870. function charsetToGBK($mixed)
  871. {
  872. if (is_array($mixed)) {
  873. foreach ($mixed as $k => $v) {
  874. if (is_array($v)) {
  875. $mixed[$k] = charsetToGBK($v);
  876. } else {
  877. $encode = mb_detect_encoding($v, array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'));
  878. if ($encode == 'UTF-8') {
  879. $mixed[$k] = iconv('UTF-8', 'GBK', $v);
  880. }
  881. }
  882. }
  883. } else {
  884. $encode = mb_detect_encoding($mixed, array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'));
  885. //var_dump($encode);
  886. if ($encode == 'UTF-8') {
  887. $mixed = iconv('UTF-8', 'GBK', $mixed);
  888. }
  889. }
  890. return $mixed;
  891. }
  892. }