Product.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\Product as Request;
  3. use App\Models\AdminHistory;
  4. use App\Models\Business;
  5. use App\Models\City;
  6. use App\Models\OrdersProduct;
  7. use App\Models\Producer;
  8. use App\Models\Product as Model;
  9. use Illuminate\Support\Carbon;
  10. use App\Models\Product\Spec as ProductSpec;
  11. use App\Models\Product\Type as ProductType;
  12. use App\Models\Product\Attr as ProductAttr;
  13. use App\Models\Product\Skus as ProductSkus;
  14. use App\Models\Product\City as ProductCity;
  15. use App\Models\ProductPhoto;
  16. use Illuminate\Support\Facades\DB;
  17. use App\Models\WeiBan\Tags as WeiBanTags;
  18. use Intervention\Image\Facades\Image;
  19. use Intervention\Image\Gd\Font;
  20. use App\Facades\Servers\WechatMini\Mini;
  21. /**
  22. * 产品管理
  23. *
  24. * @author 刘相欣
  25. *
  26. */
  27. class Product extends Auth{
  28. protected function _initialize(){
  29. parent::_initialize();
  30. $this->assign('breadcrumb1','基础信息');
  31. $this->assign('breadcrumb2','产品管理');
  32. }
  33. /**
  34. * 首页列表
  35. *
  36. * */
  37. public function index(Model $Model,City $City,ProductCity $ProductCity,OrdersProduct $OrdersProduct,AdminHistory $AdminHistory){
  38. // 接受参数
  39. $code = request('product_code','');
  40. $name = request('name','');
  41. $status = request('status');
  42. $startTime = request('start_time','');
  43. $cityIds = request('city_ids',[]);
  44. // 编码转ID
  45. $id = $code ? $Model->codeToId($code) : 0;
  46. // 查询条件
  47. $map = [];
  48. // 是否有城市
  49. $whereIn = $cityIds ? $cityIds : [];
  50. // 编码ID
  51. if( $id ) $map[] = ['product.id','=',$id];
  52. if( $name ) $map[] = ['product.name','like','%'.$name.'%'];
  53. if( $startTime ) $map[] = ['product.insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
  54. if( $startTime ) $map[] = ['product.insert_time','<=',Carbon::createFromFormat('Y-m-d',$startTime)->endOfDay()->getTimestamp()];
  55. if( !is_null($status) ) $map[] = ['product.status','=',$status];
  56. // 查询数据
  57. $list = $Model->query();
  58. if( $whereIn ) $list = $list->join('product_city','product_city.product_id','=','product.id')->whereIn('product_city.city_id',$whereIn);
  59. $list = $list->groupBy('product.id')
  60. ->where($map)
  61. ->orderBy('product.sort')
  62. ->orderByDesc('product.id')
  63. ->select(['product.*'])
  64. ->paginate(request('limit',config('page_num',10)))->appends(request()->all());
  65. // 统计销量
  66. $salesList = $OrdersProduct->query()->where([['status','<>',4]])->whereIn('product_id',array_column($list->items(),'id'))->groupBy('product_id')->select([DB::raw('sum(`buy_num`) as sales_total'),'product_id'])->pluck('sales_total','product_id')->toArray();
  67. // 循环处理数据
  68. foreach ($list as $key => $value) {
  69. // 判断总库存
  70. if( !$value['stock_total'] ) {
  71. // 以首次添加时的为准
  72. $value['stock_total']= (int) $AdminHistory->query()->where([['table_name','=','product'],['primary_id','=',$value['id']],['notes_type','=',1],['column_name','=','stock']])->orderBy('update_time')->value('after_value');
  73. // 修改库存
  74. $Model->edit($value['id'],['stock_total'=>$value['stock_total']]);
  75. }
  76. // id转编号
  77. $value['product_code'] = $Model->idToCode($value['id']);
  78. // id转编号
  79. $cityName = [];
  80. // 获取城市列表
  81. $cityIds = $ProductCity->query()->where([['product_id','=',$value['id']]])->pluck('city_id');
  82. // 如果有的话
  83. if( $cityIds ) {
  84. // 返回结果
  85. foreach ($cityIds as $cityId) {
  86. // 城市ID
  87. if( $cityId == 1 ) continue;
  88. // 获取城市名
  89. $cityName[] = $City->getOne($cityId,'name');
  90. }
  91. }
  92. // 销量统计
  93. $value['sales_total'] = isset($salesList[$value['id']])?$salesList[$value['id']]:0;
  94. // 城市
  95. $value['city_name'] = implode('、',$cityName);
  96. // 重组
  97. $list[$key] = $value;
  98. }
  99. // 获取列表
  100. $cityList = $City->getCityList();
  101. // 分配数据
  102. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  103. $this->assign('cityList',$cityList);
  104. $this->assign('list', $list);
  105. // 加载模板
  106. return $this->fetch();
  107. }
  108. /**
  109. * 获取小程序海报
  110. *
  111. */
  112. public function get_poster(Model $Model){
  113. // 接收参数
  114. $id = request('id',0);
  115. // 查询用户
  116. $oldData = $Model->where(['id'=>$id])->first();
  117. // 错误告知
  118. if( !$oldData ) return $this->error('查无数据');
  119. // 获取分享海报图片
  120. $result = $this->getShareImage('id='.$id,$oldData);
  121. // 错误提示
  122. if( isset($result['error']) ) return $this->error($result['error']);
  123. // 分配数据
  124. $this->assign('image',$result);
  125. $this->assign('oldData',$oldData);
  126. $this->assign('crumbs','海报');
  127. // 加载模板
  128. return $this->fetch();
  129. }
  130. /**
  131. * 获取分享海报图片
  132. * @param int $scene 场景值
  133. *
  134. */
  135. private function getShareImage($scene,$oldData){
  136. // 尝试执行
  137. try {
  138. // 加载图片
  139. $image = Image::make(public_path('uploads/images/poster/').'product.png');
  140. // 产品缩略图
  141. $thumb = Image::make(path_compat($oldData['thumb']))->resize(1000,1000);
  142. // 设置文字样式(字体、大小、颜色等)
  143. $fontPath = public_path().'/fonts/msyh14.ttf';// 指定字体文件路径
  144. // 生成小程序二维码
  145. $qrcode = Mini::getUnlimit($scene,['page'=>'pages/product/index','width'=>280,'is_hyaline'=>true]);
  146. // 错误提示
  147. if( isset($qrcode['error']) ) return $qrcode;
  148. // 加载图片
  149. $qrcode = Image::make($qrcode)->resize(250,250);
  150. // 插入图片
  151. $image->insert($thumb,'top-center',0,130);
  152. // 插入图片
  153. $image->insert($qrcode,'bottom-left',120,120);
  154. // 给图片写入文字
  155. $image->text('¥'.$oldData['price'], 360,1280,function (Font $font) use ($fontPath) {
  156. $font->file($fontPath); // 字体文件地址
  157. $font->size(88); // 字体大小
  158. $font->color('#333333');
  159. $font->align('left');
  160. });
  161. // 给图片写入文字
  162. $image->text($oldData['name'], 120,1380,function (Font $font) use ($fontPath) {
  163. $font->file($fontPath); // 字体文件地址
  164. $font->size(48); // 字体大小
  165. $font->color('#333333');
  166. $font->align('left');
  167. });
  168. // 转码成字符串
  169. $image = $image->encode('jpg', 90)->__toString();
  170. // 转base64
  171. $base64 = 'data:image/jpg;base64,' . base64_encode($image);
  172. // 返回结果
  173. return $base64;
  174. } catch (\Throwable $th) {
  175. // 错误提示
  176. return ['error'=>$th->getMessage()];
  177. }
  178. }
  179. /**
  180. * 添加
  181. *
  182. * */
  183. public function add( Request $request, Model $Model,WeiBanTags $WeiBanTags,ProductPhoto $ProductPhoto,Producer $Producer,Business $Business,ProductType $ProductType,ProductSpec $ProductSpec,ProductAttr $ProductAttr,ProductSkus $ProductSkus,City $City,ProductCity $ProductCity){
  184. if( request()->isMethod('post') ){
  185. // 验证参数
  186. $request->scene('add')->validate();
  187. // 组合数据
  188. $data['name'] = request('name','');
  189. $data['thumb'] = request('thumb','');
  190. $data['poster'] = request('poster','');
  191. $data['spec'] = request('spec','');
  192. $data['price'] = request('price',0);
  193. $data['market_price'] = request('market_price',0);
  194. $data['producer_id'] = request('producer_id',0);
  195. $data['business_id'] = request('business_id',0);
  196. $data['quota'] = request('quota',0);
  197. $data['min_quota'] = request('min_quota',0);
  198. $data['quota_start'] = request('quota_start','');
  199. $data['quota_end'] = request('quota_end','');
  200. $data['puton_time'] = request('puton_time','');
  201. $data['putoff_time'] = request('putoff_time','');
  202. $data['stock'] = request('stock',0);
  203. $data['status'] = 1;
  204. $data['admin_uid'] = admin('uid');
  205. $description = request('description','');
  206. $attr = request('attr',[]);
  207. $skuList = request('sku',[]);
  208. $cityIds = request('city_ids',[]);
  209. $photoList = request('photo_list',[]);
  210. $tagScope = request('tag_scope',[]);
  211. $data['tag_scope'] = implode(',',$tagScope);
  212. $tagExclude = request('tag_exclude',[]);
  213. $data['tag_exclude'] = implode(',',$tagExclude);
  214. // 循环
  215. foreach ($photoList as $key => $value) {
  216. if( !$value ) unset($photoList[$key]);
  217. }
  218. $photoList = array_values($photoList);
  219. // 如果没有选择,则意味着全部
  220. $cityIds = $cityIds ? $cityIds : [1];
  221. $data['quota_start'] = $data['quota_start'] ? strtotime($data['quota_start']) : 0;
  222. $data['quota_end'] = $data['quota_end'] ? strtotime($data['quota_end']) : 0;
  223. $data['puton_time'] = $data['puton_time'] ? strtotime($data['puton_time']) : 0;
  224. $data['putoff_time'] = $data['putoff_time'] ? strtotime($data['putoff_time']) : 0;
  225. // 限购提示
  226. if( !$data['thumb'] ) return json_send(['code'=>'error','msg'=>'请上传产品主图','data'=>['error'=>'请上传产品主图']]);
  227. // 限购提示
  228. if( $attr && !$skuList ) return json_send(['code'=>'error','msg'=>'规格属性存在时,请填写SKU','data'=>['error'=>'规格属性存在时,请填写SKU']]);
  229. // 限购提示
  230. if( $data['quota'] && ( !$data['quota_start'] || !$data['quota_end'] )) return json_send(['code'=>'error','msg'=>'限购必填限购时间','data'=>['error'=>'限购必填限购时间']]);
  231. // 限购提示
  232. if( $data['min_quota'] > $data['quota'] ) return json_send(['code'=>'error','msg'=>'起购数量请勿大于限购数量','data'=>['error'=>'起购数量请勿大于限购数量']]);
  233. // SKU存在的时候,判断限购数量
  234. if( $skuList ) {
  235. // 循环处理
  236. foreach ($skuList as $attrNames => $value) {
  237. // SKU限购,所以限购时间也必须填
  238. if( $value['quota'] && ( !$data['quota_start'] || !$data['quota_end'] )) return json_send(['code'=>'error','msg'=>'SKU限购时,请填限购时间','data'=>['error'=>'SKU限购时,请填限购时间']]);
  239. // 限购提示
  240. if( $value['min_quota'] > $value['quota'] ) return json_send(['code'=>'error','msg'=>$attrNames.'起购数量请勿大于限购数量','data'=>['error'=>$attrNames.'起购数量请勿大于限购数量']]);
  241. // 提示起购数量异常,如果总起购数量小于SKU起购数量,提示
  242. if( $value['min_quota'] && $value['min_quota'] < $data['min_quota'] ) return json_send(['code'=>'error','msg'=>'SKU的起购数量不能小于总起购数量','data'=>['error'=>'SKU的起购数量不能小于总起购数量']]);
  243. }
  244. // 限购数量
  245. $quota = array_sum(array_column($skuList,'quota'));
  246. // 提示限购数量异常
  247. if( $data['quota']&& $data['quota'] < $quota ) return json_send(['code'=>'error','msg'=>'总限购数量不能小于SKU的限购数量','data'=>['error'=>'总限购数量不能小于SKU的限购数量']]);
  248. }
  249. // 上下架
  250. if( $data['puton_time'] ) {
  251. // 下架时间必填
  252. if( !$data['putoff_time'] ) return json_send(['code'=>'error','msg'=>'请填写自动下架时间','data'=>['error'=>'自动上架请填写下架时间']]);
  253. // 下架时间必填
  254. if( $data['putoff_time'] <= $data['puton_time'] ) return json_send(['code'=>'error','msg'=>'下架时间请晚于上架时间','data'=>['error'=>'下架时间请晚于上架时间']]);
  255. }
  256. // 总库存
  257. if( $skuList ) $data['stock'] = array_sum(array_column($skuList,'stock'));
  258. // 总库存
  259. $data['stock_total'] = $data['stock'];
  260. // 获取规格属性
  261. $specAttr = $this->getSpecAttr($attr,$ProductSpec,true);
  262. // 开启事务
  263. DB::beginTransaction();
  264. try {
  265. // 写入
  266. $id = $Model->add($data);
  267. // 提示新增失败
  268. if( !$id ) {
  269. // 回滚
  270. DB::rollBack();
  271. // 提示
  272. return json_send(['code'=>'error','msg'=>'新增失败']);
  273. }
  274. // 组合数据
  275. foreach ($specAttr as $key => $value) {
  276. // 查询结果
  277. $value['id'] = $ProductAttr->upsertByName($value,$id);
  278. // 提示新增失败
  279. if( !$value['id'] ) {
  280. // 回滚
  281. DB::rollBack();
  282. // 提示
  283. return json_send(['code'=>'error','msg'=>'商品属性新增失败']);
  284. }
  285. // 重组
  286. $specAttr[$key] = $value;
  287. }
  288. // 图册
  289. foreach ($photoList as $key => $value) {
  290. // 整理数据
  291. $value = ['sort'=>$key,'thumb'=>$value,'product_id'=>$id,'insert_time'=>time(),'update_time'=>time()];
  292. // 重新整理
  293. $photoList[$key] = $value;
  294. }
  295. // 存在图册
  296. if( $photoList ) {
  297. // 写入失败
  298. $result = $ProductPhoto->query()->insert($photoList);
  299. // 提示新增失败
  300. if( !$result ) {
  301. // 回滚
  302. DB::rollBack();
  303. // 提示
  304. return json_send(['code'=>'error','msg'=>'商品图册写入失败']);
  305. }
  306. }
  307. // 循环SKU
  308. foreach ($skuList as $attrNames => $value) {
  309. // 属性ID值
  310. $value['attr_ids'] = [];
  311. // 规格属性值组合
  312. $value['attr_names']= $attrNames;
  313. // 切割成数据
  314. $names = explode(',',$attrNames);
  315. // 循环处理
  316. foreach ($names as $name) {
  317. foreach ($specAttr as $vv) {
  318. if( $name == $vv['name'] ) {
  319. // 提示新增失败
  320. if( !$vv['id'] ) {
  321. // 回滚
  322. DB::rollBack();
  323. // 提示
  324. return json_send(['code'=>'error','msg'=>'属性SKU匹配失败']);
  325. }
  326. $value['attr_ids'][] = $vv['id'];
  327. }
  328. }
  329. }
  330. // 转成好存储的数据
  331. $value['attr_ids'] = implode(',',$value['attr_ids']);
  332. // 转成好存储的数据
  333. $value['product_id'] = $id;
  334. // 转成好存储的数据
  335. $value['sku_thumb'] = (string)$value['sku_thumb'];
  336. // 转成好存储的数据
  337. $value['stock_total']= $value['stock'];
  338. // 转成好存储的数据
  339. $value['insert_time']= time();
  340. // 转成好存储的数据
  341. $value['update_time']= time();
  342. // SKU结果
  343. $skuList[$attrNames] = $value;
  344. }
  345. // 循环城市范围
  346. foreach ($cityIds as $key => $value) {
  347. // 重组数据
  348. $cityIds[$key] = ['city_id'=>$value,'product_id'=>$id,'insert_time'=>time(),'update_time'=>time()];
  349. }
  350. // 写入城市范围
  351. $ProductCity->query()->insert($cityIds);
  352. // 返回结果
  353. $result = $ProductSkus->insert($skuList);
  354. // 提示新增失败
  355. if( !$result ) {
  356. // 回滚
  357. DB::rollBack();
  358. // 提示
  359. return json_send(['code'=>'error','msg'=>'SKU插入失败']);
  360. }
  361. // 更新内容
  362. $Model->updateDesc($id,$description);
  363. // 提交
  364. DB::commit();
  365. // 记录行为
  366. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  367. // 告知结果
  368. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  369. } catch (\Throwable $th) {
  370. // 回滚
  371. DB::rollBack();
  372. // 提示
  373. return json_send(['code'=>'error','msg'=>'系统异常,写入失败','data'=>$th->getMessage()]);
  374. }
  375. }
  376. // 获取类型数据
  377. $typeList = $ProductType->getList();
  378. $cityList = $City->getCityList();
  379. $businessList = $Business->getList();
  380. $producerList = $Producer->getList();
  381. // 标签列表
  382. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  383. // 标签列表
  384. $tagList = [];
  385. // 循环数据
  386. foreach ($tagData as $value) {
  387. $tagList[$value['group']][] = $value['name'];
  388. }
  389. // 分配数据
  390. $this->assign('crumbs','新增');
  391. $this->assign('tagList',$tagList);
  392. $this->assign('typeList',$typeList);
  393. $this->assign('cityList',$cityList);
  394. $this->assign('businessList',$businessList);
  395. $this->assign('producerList',$producerList);
  396. // 加载模板
  397. return $this->fetch();
  398. }
  399. /**
  400. * 编辑
  401. *
  402. * */
  403. public function edit( Request $request, Model $Model,WeiBanTags $WeiBanTags,ProductPhoto $ProductPhoto,Producer $Producer,Business $Business,ProductType $ProductType,ProductSpec $ProductSpec,ProductAttr $ProductAttr,ProductSkus $ProductSkus,City $City,ProductCity $ProductCity){
  404. if(request()->isMethod('post')){
  405. // 验证参数
  406. $request->scene('edit')->validate();
  407. // 组合数据
  408. $id = request('id',0);
  409. $data['name'] = request('name','');
  410. $data['thumb'] = request('thumb','');
  411. $data['poster'] = request('poster','');
  412. $data['spec'] = request('spec','');
  413. $data['price'] = request('price',0);
  414. $data['market_price'] = request('market_price',0);
  415. $data['quota'] = request('quota',0);
  416. $data['min_quota'] = request('min_quota',0);
  417. $data['quota_start'] = request('quota_start','');
  418. $data['quota_end'] = request('quota_end','');
  419. $data['puton_time'] = request('puton_time','');
  420. $data['putoff_time'] = request('putoff_time','');
  421. $data['producer_id'] = request('producer_id',0);
  422. $data['business_id'] = request('business_id',0);
  423. $data['stock'] = request('stock',0);
  424. $data['status'] = 1;
  425. $description = request('description','');
  426. $attr = request('attr',[]);
  427. $skuList = request('sku',[]);
  428. $cityIds = request('city_ids',[]);
  429. $photoList = request('photo_list',[]);
  430. $tagScope = request('tag_scope',[]);
  431. $data['tag_scope'] = implode(',',$tagScope);
  432. $tagExclude = request('tag_exclude',[]);
  433. $data['tag_exclude'] = implode(',',$tagExclude);
  434. // 循环
  435. foreach ($photoList as $key => $value) {
  436. if( !$value ) unset($photoList[$key]);
  437. }
  438. $photoList = array_values($photoList);
  439. // 如果没有选择,则意味着全部
  440. $cityIds = $cityIds ? $cityIds : [1];
  441. $data['quota_start'] = $data['quota_start'] ? strtotime($data['quota_start']) : 0;
  442. $data['quota_end'] = $data['quota_end'] ? strtotime($data['quota_end']) : 0;
  443. $data['puton_time'] = $data['puton_time'] ? strtotime($data['puton_time']) : 0;
  444. $data['putoff_time'] = $data['putoff_time'] ? strtotime($data['putoff_time']) : 0;
  445. // 限购提示
  446. if( !$data['thumb'] ) return json_send(['code'=>'error','msg'=>'请上传产品主图','data'=>['error'=>'请上传产品主图']]);
  447. // 限购提示
  448. if( $attr && !$skuList ) return json_send(['code'=>'error','msg'=>'规格属性存在时,请填写SKU','data'=>['error'=>'规格属性存在时,请填写SKU']]);
  449. // 限购提示
  450. if( $data['quota'] && ( !$data['quota_start'] || !$data['quota_end'] )) return json_send(['code'=>'error','msg'=>'限购必填限购时间','data'=>['error'=>'限购必填限购时间']]);
  451. // 限购提示
  452. if( $data['min_quota'] > $data['quota'] ) return json_send(['code'=>'error','msg'=>'起购数量请勿大于限购数量','data'=>['error'=>'起购数量请勿大于限购数量']]);
  453. // SKU存在的时候,判断限购数量
  454. if( $skuList ) {
  455. // 循环处理
  456. foreach ($skuList as $attrNames => $value) {
  457. // SKU限购,所以限购时间也必须填
  458. if( $value['quota'] && ( !$data['quota_start'] || !$data['quota_end'] )) return json_send(['code'=>'error','msg'=>'SKU限购时,请填限购时间','data'=>['error'=>'SKU限购时,请填限购时间']]);
  459. // 限购提示
  460. if( $value['min_quota'] > $value['quota'] ) return json_send(['code'=>'error','msg'=>$attrNames.'起购数量请勿大于限购数量','data'=>['error'=>$attrNames.'起购数量请勿大于限购数量']]);
  461. // 提示起购数量异常,如果总起购数量小于SKU起购数量,提示
  462. if( $value['min_quota'] && $value['min_quota'] < $data['min_quota'] ) return json_send(['code'=>'error','msg'=>'SKU的起购数量不能小于总起购数量','data'=>['error'=>'SKU的起购数量不能小于总起购数量']]);
  463. }
  464. // 限购数量
  465. $quota = array_sum(array_column($skuList,'quota'));
  466. // 提示限购数量异常
  467. if( $data['quota']&& $data['quota'] < $quota ) return json_send(['code'=>'error','msg'=>'总限购数量不能小于SKU的限购数量','data'=>['error'=>'总限购数量不能小于SKU的限购数量']]);
  468. }
  469. // 上下架
  470. if( $data['puton_time'] ) {
  471. // 下架时间必填
  472. if( !$data['putoff_time'] ) return json_send(['code'=>'error','msg'=>'请填写自动下架时间','data'=>['error'=>'自动上架请填写下架时间']]);
  473. // 下架时间必填
  474. if( $data['putoff_time'] <= $data['puton_time'] ) return json_send(['code'=>'error','msg'=>'下架时间请晚于上架时间','data'=>['error'=>'下架时间请晚于上架时间']]);
  475. }
  476. // 总库存
  477. if( $skuList ) $data['stock'] = array_sum(array_column($skuList,'stock'));
  478. // 总库存
  479. $data['stock_total'] = $data['stock'];
  480. // 获取规格属性
  481. $specAttr = $this->getSpecAttr($attr,$ProductSpec,true);
  482. // 开启事务
  483. DB::beginTransaction();
  484. try {
  485. // 写入
  486. $result = $Model->edit($id,$data);
  487. // 提示新增失败
  488. if( !$result ) {
  489. // 回滚
  490. DB::rollBack();
  491. // 提示
  492. return json_send(['code'=>'error','msg'=>'修改失败']);
  493. }
  494. // 获取旧图册
  495. $oldPhoto = $ProductPhoto->getListByProductId($id);
  496. // 循环个数
  497. for ($i=0; $i < 4; $i++) {
  498. // 如果存在旧数据与新数据,修改
  499. if( isset($oldPhoto[$i]) && isset($photoList[$i]) ) $ProductPhoto->query()->where([['product_id','=',$id],['sort','=',$i]])->update(['thumb'=>(string)$photoList[$i],'update_time'=>time()]);
  500. // 如果存在旧数据,不存在新数据,删除
  501. if( isset($oldPhoto[$i]) && !isset($photoList[$i]) ) $ProductPhoto->query()->where([['product_id','=',$id],['sort','=',$i]])->delete();
  502. // 如果不存在旧数据,存在新数据,新增
  503. if( !isset($oldPhoto[$i]) && isset($photoList[$i]) ) $ProductPhoto->add(['sort'=>$i,'product_id'=>$id,'thumb'=>(string)$photoList[$i],'insert_time'=>time(),'update_time'=>time()]);
  504. }
  505. // 组合数据
  506. foreach ($specAttr as $key => $value) {
  507. // 查询结果
  508. $value['id'] = $ProductAttr->upsertByName($value,$id);
  509. // 提示新增失败
  510. if( !$value['id'] ) {
  511. // 回滚
  512. DB::rollBack();
  513. // 提示
  514. return json_send(['code'=>'error','msg'=>'商品属性新增失败']);
  515. }
  516. // 重组
  517. $specAttr[$key] = $value;
  518. }
  519. // 如果不在这个属性列表中的数据删除
  520. $ProductAttr->query()->where([['product_id','=',$id]])->whereNotIn('id',array_column($specAttr,'id'))->delete();
  521. // 循环SKU
  522. foreach ($skuList as $attrNames => $value) {
  523. // 属性ID值
  524. $value['attr_ids'] = [];
  525. // 规格属性值组合
  526. $value['attr_names']= $attrNames;
  527. // 切割成数据
  528. $names = explode(',',$attrNames);
  529. // 循环处理
  530. foreach ($names as $name) {
  531. foreach ($specAttr as $vv) {
  532. if( $name == $vv['name'] ) {
  533. // 提示新增失败
  534. if( !$vv['id'] ) {
  535. // 回滚
  536. DB::rollBack();
  537. // 提示
  538. return json_send(['code'=>'error','msg'=>'属性SKU匹配失败']);
  539. }
  540. $value['attr_ids'][] = $vv['id'];
  541. }
  542. }
  543. }
  544. // 转成好存储的数据
  545. $value['attr_ids'] = implode(',',$value['attr_ids']);
  546. // 转成好存储的数据
  547. $value['sku_thumb'] = (string)$value['sku_thumb'];
  548. // 转成好存储的数据
  549. $value['stock_total']= $value['stock'];
  550. // 转成好存储的数据
  551. $value['product_id']= $id;
  552. // 查询
  553. $oldSkuId = $ProductSkus->query()->where([['product_id','=',$id],['attr_ids','=',$value['attr_ids']]])->value('id');
  554. // 判断是否存在ID
  555. $value['id'] = $oldSkuId ? $ProductSkus->edit($oldSkuId,$value) : $ProductSkus->add($value);
  556. // SKU结果
  557. $skuList[$attrNames]= $value;
  558. }
  559. // 如果不在这个属性列表中的数据删除
  560. $ProductSkus->query()->where([['product_id','=',$id]])->whereNotIn('id',array_column($skuList,'id'))->delete();
  561. // 获取之前的城市ID
  562. $oldCityIds = $ProductCity->getListByProductId($id);
  563. // 循环城市范围
  564. foreach ($cityIds as $key => $value) {
  565. // 不存在则新增
  566. if( !in_array($value,$oldCityIds) ) $ProductCity->add(['city_id'=>$value,'product_id'=>$id]);
  567. }
  568. // 不存在的城市删除
  569. $ProductCity->query()->where([['product_id','=',$id]])->whereNotIn('city_id',$cityIds)->delete();
  570. // 更新内容
  571. $Model->updateDesc($id,$description);
  572. // 提交
  573. DB::commit();
  574. // 记录行为
  575. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],$data);
  576. // 告知结果
  577. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  578. } catch (\Throwable $th) {
  579. // 回滚
  580. DB::rollBack();
  581. // 提示
  582. return json_send(['code'=>'error','msg'=>'系统异常,写入失败','data'=>$th->getMessage() .'=>'.$th->getLine()]);
  583. }
  584. }
  585. // 接收参数
  586. $id = request('id',0);
  587. // 查询数据
  588. $oldData = $Model->where(['id'=>$id])->first();
  589. // 如果是没有数据
  590. if( !$oldData ) return $this->error('查无数据');
  591. // 产品信息转格式
  592. $oldData = $oldData->toArray();
  593. $oldData['description'] = $Model->getDesc($id);
  594. $oldData['city_ids'] = $ProductCity->getListByProductId($id);
  595. $oldData['tag_scope'] = explode(',',$oldData['tag_scope']);
  596. $oldData['tag_exclude'] = explode(',',$oldData['tag_exclude']);
  597. $photoList = $ProductPhoto->getListByProductId($id);
  598. // 获取产品属性
  599. $attrList = $ProductAttr->getListByProductId($id);
  600. // 规格属性
  601. $specList = [];
  602. // 获取数据
  603. foreach ($attrList as $value) {
  604. $value['active'] = 0;
  605. if( !isset($specList[$value['spec_id']]) ) $specList[$value['spec_id']] = $ProductSpec->getOne($value['spec_id']);
  606. $specList[$value['spec_id']]['attr_list'][] = $value;
  607. }
  608. // 产品类型
  609. $skuList = $ProductSkus->getListByProductId($id);
  610. // 循环处理
  611. foreach ($skuList as $key => $value) {
  612. // 数据解析
  613. $value['attr_ids'] = explode(',',$value['attr_ids']);
  614. // 循环处理
  615. foreach ( $value['attr_ids'] as $k=>$attrId ) {
  616. // 获取ids对应的name
  617. $value['attr_ids'][$k] = $ProductAttr->getValueById($attrId,'name');
  618. }
  619. // 数据合并
  620. $value['attr_ids'] = implode(',',$value['attr_ids']);
  621. // 重组
  622. $skuList[$key] = $value;
  623. }
  624. // 获取类型数据
  625. $typeList = $ProductType->getList();
  626. $cityList = $City->getCityList();
  627. $businessList = $Business->getList();
  628. $producerList = $Producer->getList();
  629. // 标签列表
  630. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  631. // 标签列表
  632. $tagList = [];
  633. // 循环数据
  634. foreach ($tagData as $value) {
  635. $tagList[$value['group']][] = $value['name'];
  636. }
  637. // 分配数据
  638. $this->assign('tagList',$tagList);
  639. $this->assign('typeList',$typeList);
  640. $this->assign('cityList',$cityList);
  641. $this->assign('businessList',$businessList);
  642. $this->assign('producerList',$producerList);
  643. $this->assign('oldData',$oldData);
  644. $this->assign('photoList',$photoList);
  645. $this->assign('skuList',$skuList);
  646. $this->assign('specList',$specList);
  647. $this->assign('crumbs','修改');
  648. // 加载模板
  649. return $this->fetch();
  650. }
  651. /**
  652. * 编辑
  653. *
  654. * */
  655. public function copy( Request $request, Model $Model,WeiBanTags $WeiBanTags,ProductPhoto $ProductPhoto,Producer $Producer,Business $Business,ProductType $ProductType,ProductSpec $ProductSpec,ProductAttr $ProductAttr,ProductSkus $ProductSkus,City $City,ProductCity $ProductCity){
  656. if( request()->isMethod('post') ){
  657. // 验证参数
  658. $request->scene('add')->validate();
  659. // 组合数据
  660. $data['name'] = request('name','');
  661. $data['thumb'] = request('thumb','');
  662. $data['poster'] = request('poster','');
  663. $data['spec'] = request('spec','');
  664. $data['price'] = request('price',0);
  665. $data['market_price'] = request('market_price',0);
  666. $data['producer_id'] = request('producer_id',0);
  667. $data['business_id'] = request('business_id',0);
  668. $data['quota'] = request('quota',0);
  669. $data['min_quota'] = request('min_quota',0);
  670. $data['quota_start'] = request('quota_start','');
  671. $data['quota_end'] = request('quota_end','');
  672. $data['stock'] = request('stock',0);
  673. $data['status'] = 1;
  674. $data['admin_uid'] = admin('uid');
  675. $description = request('description','');
  676. $attr = request('attr',[]);
  677. $skuList = request('sku',[]);
  678. $cityIds = request('city_ids',[]);
  679. $photoList = request('photo_list',[]);
  680. $tagScope = request('tag_scope',[]);
  681. $data['tag_scope'] = implode(',',$tagScope);
  682. $tagExclude = request('tag_exclude',[]);
  683. $data['tag_exclude'] = implode(',',$tagExclude);
  684. // 循环
  685. foreach ($photoList as $key => $value) {
  686. if( !$value ) unset($photoList[$key]);
  687. }
  688. $photoList = array_values($photoList);
  689. // 如果没有选择,则意味着全部
  690. $cityIds = $cityIds ? $cityIds : [1];
  691. $data['quota_start'] = $data['quota_start'] ? strtotime($data['quota_start']) : 0;
  692. $data['quota_end'] = $data['quota_end'] ? strtotime($data['quota_end']) : 0;
  693. // 限购提示
  694. if( !$data['thumb'] ) return json_send(['code'=>'error','msg'=>'请上传产品主图','data'=>['error'=>'请上传产品主图']]);
  695. // 限购提示
  696. if( $attr && !$skuList ) return json_send(['code'=>'error','msg'=>'规格属性存在时,请填写SKU','data'=>['error'=>'规格属性存在时,请填写SKU']]);
  697. // 限购提示
  698. if( $data['quota'] && ( !$data['quota_start'] || !$data['quota_end'] )) return json_send(['code'=>'error','msg'=>'限购必填限购时间','data'=>['error'=>'限购必填限购时间']]);
  699. // 限购提示
  700. if( $data['min_quota'] > $data['quota'] ) return json_send(['code'=>'error','msg'=>'起购数量请勿大于限购数量','data'=>['error'=>'起购数量请勿大于限购数量']]);
  701. // 总库存
  702. if( $skuList ) $data['stock'] = array_sum(array_column($skuList,'stock'));
  703. // SKU存在的时候,判断限购数量
  704. if( $skuList ) {
  705. // 循环处理
  706. foreach ($skuList as $attrNames => $value) {
  707. // SKU限购,所以限购时间也必须填
  708. if( $value['quota'] && ( !$data['quota_start'] || !$data['quota_end'] )) return json_send(['code'=>'error','msg'=>'SKU限购时,请填限购时间','data'=>['error'=>'SKU限购时,请填限购时间']]);
  709. // 限购提示
  710. if( $value['min_quota'] > $value['quota'] ) return json_send(['code'=>'error','msg'=>$attrNames.'起购数量请勿大于限购数量','data'=>['error'=>$attrNames.'起购数量请勿大于限购数量']]);
  711. // 提示起购数量异常,如果总起购数量小于SKU起购数量,提示
  712. if( $value['min_quota'] && $value['min_quota'] < $data['min_quota'] ) return json_send(['code'=>'error','msg'=>'SKU的起购数量不能小于总起购数量','data'=>['error'=>'SKU的起购数量不能小于总起购数量']]);
  713. }
  714. // 限购数量
  715. $quota = array_sum(array_column($skuList,'quota'));
  716. // 提示限购数量异常
  717. if( $data['quota'] && $data['quota'] < $quota ) return json_send(['code'=>'error','msg'=>'总限购数量不能小于SKU的限购数量','data'=>['error'=>'总限购数量不能小于SKU的限购数量']]);
  718. }
  719. // 总库存
  720. $data['stock_total'] = $data['stock'];
  721. // 获取规格属性
  722. $specAttr = $this->getSpecAttr($attr,$ProductSpec,true);
  723. // 开启事务
  724. DB::beginTransaction();
  725. try {
  726. // 写入
  727. $id = $Model->add($data);
  728. // 提示新增失败
  729. if( !$id ) {
  730. // 回滚
  731. DB::rollBack();
  732. // 提示
  733. return json_send(['code'=>'error','msg'=>'新增失败']);
  734. }
  735. // 图册
  736. foreach ($photoList as $key => $value) {
  737. // 整理数据
  738. $value = ['sort'=>$key,'thumb'=>$value,'product_id'=>$id,'insert_time'=>time(),'update_time'=>time()];
  739. // 重新整理
  740. $photoList[$key] = $value;
  741. }
  742. // 存在图册
  743. if( $photoList ) {
  744. // 写入失败
  745. $result = $ProductPhoto->query()->insert($photoList);
  746. // 提示新增失败
  747. if( !$result ) {
  748. // 回滚
  749. DB::rollBack();
  750. // 提示
  751. return json_send(['code'=>'error','msg'=>'商品图册写入失败']);
  752. }
  753. }
  754. // 组合数据
  755. foreach ($specAttr as $key => $value) {
  756. // 查询结果
  757. $value['id'] = $ProductAttr->upsertByName($value,$id);
  758. // 提示新增失败
  759. if( !$value['id'] ) {
  760. // 回滚
  761. DB::rollBack();
  762. // 提示
  763. return json_send(['code'=>'error','msg'=>'商品属性新增失败']);
  764. }
  765. // 重组
  766. $specAttr[$key] = $value;
  767. }
  768. // 循环SKU
  769. foreach ($skuList as $attrNames => $value) {
  770. // 属性ID值
  771. $value['attr_ids'] = [];
  772. // 规格属性值组合
  773. $value['attr_names']= $attrNames;
  774. // 切割成数据
  775. $names = explode(',',$attrNames);
  776. // 循环处理
  777. foreach ($names as $name) {
  778. foreach ($specAttr as $vv) {
  779. if( $name == $vv['name'] ) {
  780. // 提示新增失败
  781. if( !$vv['id'] ) {
  782. // 回滚
  783. DB::rollBack();
  784. // 提示
  785. return json_send(['code'=>'error','msg'=>'属性SKU匹配失败']);
  786. }
  787. $value['attr_ids'][] = $vv['id'];
  788. }
  789. }
  790. }
  791. // 转成好存储的数据
  792. $value['attr_ids'] = implode(',',$value['attr_ids']);
  793. // 转成好存储的数据
  794. $value['product_id']= $id;
  795. // 转成好存储的数据
  796. $value['sku_thumb'] = (string) $value['sku_thumb'];
  797. // 转成好存储的数据
  798. $value['stock_total']= $value['stock'];
  799. // 转成好存储的数据
  800. $value['insert_time']= time();
  801. // 转成好存储的数据
  802. $value['update_time']= time();
  803. // SKU结果
  804. $skuList[$attrNames] = $value;
  805. }
  806. // 循环城市范围
  807. foreach ($cityIds as $key => $value) {
  808. // 重组数据
  809. $cityIds[$key] = ['city_id'=>$value,'product_id'=>$id,'insert_time'=>time(),'update_time'=>time()];
  810. }
  811. // 写入城市范围
  812. $ProductCity->query()->insert($cityIds);
  813. // 返回结果
  814. $result = $ProductSkus->insert($skuList);
  815. // 提示新增失败
  816. if( !$result ) {
  817. // 回滚
  818. DB::rollBack();
  819. // 提示
  820. return json_send(['code'=>'error','msg'=>'SKU插入失败']);
  821. }
  822. // 更新内容
  823. $Model->updateDesc($id,$description);
  824. // 提交
  825. DB::commit();
  826. // 记录行为
  827. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  828. // 告知结果
  829. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  830. } catch (\Throwable $th) {
  831. // 回滚
  832. DB::rollBack();
  833. // 提示
  834. return json_send(['code'=>'error','msg'=>'系统异常,写入失败','data'=>$th->getMessage()]);
  835. }
  836. }
  837. // 接收参数
  838. $id = request('id',0);
  839. // 查询数据
  840. $oldData = $Model->where(['id'=>$id])->first();
  841. // 如果是没有数据
  842. if( !$oldData ) return $this->error('查无数据');
  843. // 产品信息转格式
  844. $oldData = $oldData->toArray();
  845. $oldData['description'] = $Model->getDesc($id);
  846. $oldData['city_ids'] = $ProductCity->getListByProductId($id);
  847. $oldData['tag_scope'] = explode(',',$oldData['tag_scope']);
  848. $oldData['tag_exclude'] = explode(',',$oldData['tag_exclude']);
  849. $photoList = $ProductPhoto->getListByProductId($id);
  850. // 获取产品属性
  851. $attrList = $ProductAttr->getListByProductId($id);
  852. // 规格属性
  853. $specList = [];
  854. // 获取数据
  855. foreach ($attrList as $value) {
  856. $value['active'] = 0;
  857. if( !isset($specList[$value['spec_id']]) ) $specList[$value['spec_id']] = $ProductSpec->getOne($value['spec_id']);
  858. $specList[$value['spec_id']]['attr_list'][] = $value;
  859. }
  860. // 产品类型
  861. $skuList = $ProductSkus->getListByProductId($id);
  862. // 循环处理
  863. foreach ($skuList as $key => $value) {
  864. // 数据解析
  865. $value['attr_ids'] = explode(',',$value['attr_ids']);
  866. // 循环处理
  867. foreach ( $value['attr_ids'] as $k=>$attrId ) {
  868. // 获取ids对应的name
  869. $value['attr_ids'][$k] = $ProductAttr->getValueById($attrId,'name');
  870. }
  871. // 数据合并
  872. $value['attr_ids'] = implode(',',$value['attr_ids']);
  873. // 重组
  874. $skuList[$key] = $value;
  875. }
  876. // 标签列表
  877. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  878. // 标签列表
  879. $tagList = [];
  880. // 循环数据
  881. foreach ($tagData as $value) {
  882. $tagList[$value['group']][] = $value['name'];
  883. }
  884. // 获取类型数据
  885. $typeList = $ProductType->getList();
  886. $cityList = $City->getCityList();
  887. $businessList = $Business->getList();
  888. $producerList = $Producer->getList();
  889. // 分配数据
  890. $this->assign('tagList',$tagList);
  891. $this->assign('typeList',$typeList);
  892. $this->assign('cityList',$cityList);
  893. $this->assign('businessList',$businessList);
  894. $this->assign('producerList',$producerList);
  895. $this->assign('oldData',$oldData);
  896. $this->assign('photoList',$photoList);
  897. $this->assign('skuList',$skuList);
  898. $this->assign('specList',$specList);
  899. $this->assign('crumbs','复制');
  900. // 加载模板
  901. return $this->fetch();
  902. }
  903. /**
  904. * 状态
  905. *
  906. * */
  907. public function set_status( Request $request, Model $Model ){
  908. // 验证参数
  909. $request->scene('set_status')->validate();
  910. // 接收参数
  911. $id = request('id',0);
  912. $status = request('status',0);
  913. // 查询数据
  914. $result = $Model->edit($id,['status'=>$status]);
  915. // 提示新增失败
  916. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  917. // 记录行为
  918. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],['status'=>$status]);
  919. // 告知结果
  920. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  921. }
  922. /**
  923. * 获取某个类目下的规格
  924. *
  925. */
  926. public function get_spec_html( ProductSpec $ProductSpec){
  927. // 接收参数
  928. $typeId = request('type_id',0);
  929. // 查询数据
  930. $specList = $ProductSpec->getList();
  931. // 循环处理
  932. foreach ($specList as $key => $value) {
  933. if( $value['type_id'] != $typeId ) unset($specList[$key]);
  934. }
  935. // 分配数据
  936. $this->assign('specList',$specList);
  937. // 加载模板
  938. return $this->fetch();
  939. }
  940. /**
  941. * 获取某个类目下的规格
  942. *
  943. */
  944. public function get_sku_html( ProductSpec $ProductSpec,ProductSkus $ProductSkus){
  945. // 接收参数
  946. $attr = request('attr',[]);
  947. $id = request('product_id',0);
  948. // 产品类型
  949. $oldSkus = $id ? $ProductSkus->getListByProductId($id) : [];
  950. // 获取规格属性
  951. $specAttr = $this->getSpecAttr($attr,$ProductSpec);
  952. // 组合SKU
  953. $brushList = [];
  954. // 循环规格属性
  955. foreach ($specAttr as $specId => $value) {
  956. // 获取SKU组合
  957. $brushList[$specId] = array_column($value['attr_list'],'name');
  958. }
  959. // sku列表
  960. $skuList = $this->brush([],$brushList);
  961. // 循环规格属性
  962. foreach ($skuList as $newKey => $new) {
  963. // 获取新数据
  964. $new = ['attr_names'=>$new,'price'=>0,'market_price'=>0,'stock'=>0,'min_quota'=>0,'quota'=>0,'status'=>0,'sku_thumb'=>''];
  965. // 循环旧的sku
  966. foreach ($oldSkus as $old) {
  967. // 如果有相等的规格
  968. if( $old['attr_names'] == $new['attr_names']) {
  969. $new['sku_thumb'] = $old['sku_thumb'];
  970. $new['market_price']= $old['market_price'];
  971. $new['price'] = $old['price'];
  972. $new['stock'] = $old['stock'];
  973. $new['status'] = $old['status'];
  974. $new['min_quota'] = $old['min_quota'];
  975. $new['quota'] = $old['quota'];
  976. }
  977. }
  978. $skuList[$newKey] = $new;
  979. }
  980. // 分配数据
  981. $this->assign('specAttr',$specAttr);
  982. $this->assign('skuList',$skuList);
  983. // 加载模板
  984. return $this->fetch();
  985. }
  986. /**
  987. * 获取规格属性
  988. * @param ProductSpec $ProductSpec
  989. *
  990. */
  991. private function getSpecAttr($attr,$ProductSpec,$onlyList=false){
  992. // 组合参数
  993. $specAttr = [];
  994. // 循环处理数据
  995. foreach ($attr as $specId => $value) {
  996. $specAttr[$specId]['spec_id'] = $specId;
  997. $specAttr[$specId]['spec_name'] = $ProductSpec->getOne($specId,'name');
  998. $specAttr[$specId]['attr_list'] = [];
  999. foreach ($value['name'] as $key => $name) {
  1000. // 如果没有名称的,跳过
  1001. if( !$name ) continue;
  1002. // 结果
  1003. $temp = ['id'=>0,'spec_id'=>$specId,'thumb'=>'','name'=>'','remark'=>''];
  1004. // 获取名称
  1005. $temp['name'] = str_ireplace(',','',$name);
  1006. // 获取对应的备注信息
  1007. if( !empty($value['remark'][$key]) ) $temp['remark'] = $value['remark'][$key];
  1008. if( !empty($value['thumb'][$key]) ) $temp['thumb'] = $value['thumb'][$key];
  1009. // 获取对应的数据
  1010. $specAttr[$specId]['attr_list'][]= $temp;
  1011. }
  1012. // 没有属性的规格,移除
  1013. if( !$specAttr[$specId]['attr_list'] ) unset($specAttr[$specId]);
  1014. }
  1015. // 仅获取属性列表
  1016. if( $onlyList ) {
  1017. // 属性列表
  1018. $attrList = [];
  1019. // 判断结果
  1020. foreach ($specAttr as $value) {
  1021. // 获取数据
  1022. array_push($attrList,...$value['attr_list']);
  1023. }
  1024. // 返回结果
  1025. $specAttr = $attrList;
  1026. }
  1027. // 返回结果
  1028. return $specAttr;
  1029. }
  1030. /**
  1031. * 组合SKU
  1032. *
  1033. */
  1034. private function brush($res = [], $arr = []){
  1035. // 获取第一个规格的属性
  1036. if (empty($res)) $res = (array) array_shift($arr);
  1037. // 如果只有一个规格,返回该规格的属性即可
  1038. if (empty($arr)) return $res;
  1039. // 接下来要参与计算的一组属性
  1040. $current = array_shift($arr);
  1041. // 计算的列表
  1042. $last = [];
  1043. // 循环上一次已经算出的集合
  1044. foreach ($res as $attr) {
  1045. foreach ($current as $col_val) {
  1046. $last[] = $attr . ',' . $col_val;
  1047. }
  1048. }
  1049. return $this->brush($last,$arr); # 递归处理, 直到$arr滚到最后一组属性
  1050. }
  1051. /**
  1052. * 排序
  1053. *
  1054. * */
  1055. public function set_sort( Request $request, Model $Model ){
  1056. // 验证参数
  1057. $request->scene('set_sort')->validate();
  1058. // 接收参数
  1059. $id = request('id',0);
  1060. $sort = request('sort',0);
  1061. // 查询数据
  1062. $result = $Model->edit($id,['sort'=>$sort]);
  1063. // 提示新增失败
  1064. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  1065. // 记录行为
  1066. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],['sort'=>$sort]);
  1067. // 告知结果
  1068. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  1069. }
  1070. }