session->get('us'); $userinfo = User::where('id',$user['id'])->find(); if ($userinfo['type'] != 2) return Common::return_error('非商家,无法添加!'); $merchInfo = UserMerchInfo::where('user_id',$user['id'])->where('audit',1)->where('status',1)->find(); if (!$merchInfo) return Common::return_error('商家审核未通过或被禁用,无法添加!'); $cate_id = $merchInfo['category_one_id']; $twocate = Category::get($cate_id); if ($twocate['type'] != 1){ return Common::return_error('商家类别错误,无法添加!'); } $data['label'] = implode(',',$data['label']); $data['imgs'] = implode(',',$data['imgs']); Db::startTrans(); try { if ($data['id']){ $data['update_at'] = date('Y-m-d H:i:s'); self::where('id',$data['id'])->update($data); Db::commit(); self::price_log($data['id'],$data['price'],$data['first_price']); return Common::return_success('编辑成功'); }else{ $data['user_id'] = $userinfo->id; $data['merch_id'] = $merchInfo->id; $product = self::create($data); Db::commit(); self::price_log($product->id,$data['price'],$data['first_price']); return Common::return_success('添加成功'); } } catch (Exception $e) { Db::rollback(); return Common::return_error('失败'); } } /** * 商品价格变动记录 */ public static function price_log($product_id,$o_price,$first_price){ $data['product_id'] = $product_id; $data['o_price'] = $o_price; $data['first_price'] = $first_price; ProductPriceLog::create($data); } /** * 添加、编辑商品信息(加油站商品) * @param $data * @return array */ public static function add_edit_station($data){ $user = app()->session->get('us'); $userinfo = User::where('id',$user['id'])->find(); if ($userinfo['type'] != 2) return Common::return_error('非商家,无法添加!'); $merchInfo = UserMerchInfo::where('user_id',$user['id'])->where('audit',1)->where('status',1)->find(); if (!$merchInfo) return Common::return_error('商家审核未通过或被禁用,无法添加!'); $cate_id = $merchInfo['category_one_id']; $twocate = Category::get($cate_id); if ($twocate['type'] != 2){ return Common::return_error('商家类别错误,无法添加!'); } $data['specifications'] = json_encode($data['specifications'],true); $data['imgs'] = implode(',',$data['imgs']); Db::startTrans(); try { if ($data['id']){ $data['update_at'] = date('Y-m-d H:i:s'); self::where('id',$data['id'])->update($data); Db::commit(); return Common::return_success('编辑成功'); }else{ $data['user_id'] = $userinfo->id; $data['merch_id'] = $merchInfo->id; self::create($data); Db::commit(); return Common::return_success('添加成功'); } } catch (Exception $e) { Db::rollback(); return Common::return_error('失败'); } } /** * 获取商品信息列表 */ public static function product_list($Nowpage,$limits){ $user = app()->session->get('us'); $count = self::where('is_del',1)->where('user_id',$user['id'])->count(); $data['count'] = $count; $list = self::where('is_del',1)->where('user_id',$user['id'])->page($Nowpage, $limits)->order('id desc')->select(); if ($list){ $list = $list->toArray(); foreach ($list as $k=>$v){ $list[$k]['label'] = explode(',',$v['label']); $list[$k]['image'] = explode(',',$v['imgs'])[0]; $list[$k]['imgs'] = explode(',',$v['imgs']); $list[$k]['specifications'] = json_decode($v['specifications'],true); } } else{ $list = []; } $data['list'] = $list; return Common::return_success('成功',$data); } /** * 删除商品信息 */ public static function del_product($id){ $user = app()->session->get('us'); $job = self::get($id); if (!$job) return Common::return_error('信息不存在'); if ($job->user_id!=$user['id']) return Common::return_error('无法删除'); $job->is_del = 0; if ($job->save()) return Common::return_success('删除成功'); else return Common::return_error('删除失败'); } /** * 商品列表 */ public static function productList($merch_id,$Nowpage,$limits){ $user = app()->session->get('us'); $merch = UserMerchInfo::where('id',$merch_id)->where('audit',1)->where('status',1)->find(); if (!$merch) return Common::return_error('商家不存在'); //商品 $product['count'] = self::where('merch_id',$merch_id)->where('is_del',1)->order('id desc')->count(); $product_list = self::where('merch_id',$merch_id)->where('is_del',1) ->order('id desc')->field('id,merch_id,user_id,product_name,price,product_info,imgs,specifications') ->page($Nowpage, $limits) ->select(); if ($product_list){ $product_list = $product_list->toArray(); foreach ($product_list as $k=>$v){ if ($v['price']==0){ $product_list[$k]['price'] = json_decode($v['specifications'],true)[0]['price']; $product[$k]['is_first_buy'] = false; }else{ if ($v['first_price']>0){ $order = Order::where('user_id',$user['id'])->where('product_id',$v['id'])->whereNotIn('status','0,4')->count(); if ($order){ $product[$k]['is_first_buy'] = false; }else{ $product[$k]['is_first_buy'] = true; $product[$k]['price'] = $v['first_price']; } }else{ $product[$k]['is_first_buy'] = false; } } $product_list[$k]['image'] = explode(',',$v['imgs'])[0]; $product_list[$k]['is_collect'] = self::checkUserCollect($v['id']) ? 'true' : 'false'; unset($product_list[$k]['imgs']); } $product['list'] = $product_list; } else{ $product['list'] = []; } return Common::return_success('成功',$product); } /** * 商品详情页 */ public static function productDetail($product_id,$Nowpage,$limits){ $user = app()->session->get('us'); $detail = self::where('id',$product_id)->where('is_del',1)->find(); if (!$detail) return Common::return_error('商品不存在'); $merch = UserMerchInfo::where('id',$detail['merch_id'])->where('audit',1)->where('status',1)->field('id,user_id,name,category_one_id,category_two_id,log,lat,address,phone,capita,imgs,lat,log')->find()->toArray(); $merch['imgs'] = explode(',',$merch['imgs']); $merch['evaluation'] = UserMerchInfo::merchEvaluation($merch['id']); //评价几颗星 $merch['distance'] = Common::getDistance($merch['lat'],$merch['log'])."km"; //距离 $detail = $detail->toArray(); if ($detail['price']==0){ $detail['is_first_buy'] = false; }else{ if ($detail['first_price']>0){ $order = Order::where('user_id',$user['id'])->where('product_id',$product_id)->whereNotIn('status','0,4')->count(); if ($order){ $detail['is_first_buy'] = false; }else{ $detail['is_first_buy'] = true; $detail['price'] = $detail['first_price']; } }else{ $detail['is_first_buy'] = false; } } $d[0]['id'] = $detail['id']; $d[0]['product_name'] = $detail['product_name']; $productlist = self::where('merch_id',$detail['merch_id'])->where('id','<>',$product_id)->where('is_del',1)->field('id,product_name')->select(); if ($productlist){ $product_list = array_merge($d,$productlist->toArray()); }else{ $product_list = $d; } self::browseAdd($product_id); //浏览数 //获取商家的类别 $type = UserMerchInfo::where('a.id',$detail['merch_id'])->alias('a')->join('Category b','a.category_one_id=b.id')->value('b.type'); $detail['imgs'] = $detail['imgs'] ? explode(',',$detail['imgs']) : []; $detail['type'] = $type; $detail['label'] = explode(',',$detail['label']); $detail['specifications'] = json_decode($detail['specifications'],true); $dates = date('Y-m'); $timebegin = strtotime($dates) ; //开始时间戳 $day = date('t',$timebegin); $timeend = $timebegin + 86400 * $day - 1; //结束时间戳 $start = date('Y-m-d H:i:s',$timebegin); $end = date('Y-m-d H:i:s',$timeend); //商品月销量 $detail['sales'] = Order::where('product_id',$product_id)->where('status','in','2,3')->count(); //$detail['sales'] = Order::where('product_id',$product_id)->whereBetweenTime('create_at',$start,$end)->where('status','in','2,3')->count(); //评论 $evaluation['count'] = ProductEvaluation::where('product_id',$product_id)->where('is_del',1)->count(); $evaluation_list = ProductEvaluation::where('a.product_id',$product_id) ->alias('a') ->where('a.is_del',1) ->join('User b','a.user_id=b.id') ->field('a.*,b.nickname,b.headimg') ->order('a.is_optimization desc,a.id desc') ->page($Nowpage, $limits) ->select(); if ($evaluation_list){ $evaluation_list = $evaluation_list->toArray(); foreach ($evaluation_list as $k=>$v){ $evaluation_list[$k]['imgs'] = $v['imgs'] ? explode(',',$v['imgs']) : []; $evaluation_list[$k]['create_at'] = date('Y-m-d',strtotime($v['create_at'])); $evaluation_list[$k]['is_zan'] = ProductEvaluation::checkUserEvaluationZan($v['id']) ? 'true' : 'false'; } $evaluation['list'] = $evaluation_list; }else { $evaluation['list'] = []; } return Common::return_success('成功',compact('product_list','detail','evaluation','merch')); } /** * 判断用户是否收藏商品 */ public static function checkUserCollect($product_id){ $user = app()->session->get('us'); if ($user){ $collect = ProductCollect::where('user_id',$user['id'])->where('product_id',$product_id)->count(); if ($collect) return true; } return false; } /** * 商品浏览量增加 */ public static function browseAdd($product_id){ self::where('id',$product_id)->setInc('browse'); } }