178 lines
6.9 KiB
PHP
178 lines
6.9 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Api\Miniprogram;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Http\Response;
|
||
use App\Models\{Good, GoodsCategory, GoodsAttr, GoodsCart};
|
||
use App\Http\Resources\{GoodsCategoryResource, GoodsResource, GoodsInfoResource, GoodsCartResource};
|
||
use App\Services\GoodsService;
|
||
|
||
class GoodsController extends Controller
|
||
{
|
||
public function cateList(Request $request){
|
||
$list = GoodsCategory::orderBy('sort', 'asc')->get();
|
||
return GoodsCategoryResource::collection($list)->additional(['code' => Response::HTTP_OK, 'message' => '']);
|
||
}
|
||
|
||
/**
|
||
* 获取菜单以及商品
|
||
*/
|
||
public function goodsList(Request $request){
|
||
//热销商品
|
||
$data['hot_data'] = [
|
||
'cate_name'=>'热销',
|
||
'cate_goods'=> GoodsInfoResource::collection(
|
||
Good::with(['activityExchange', 'useGoodsAttrs'])
|
||
->where('is_hot', 1)
|
||
->where('is_sell', 1)
|
||
->orderBy('goods_price', 'desc')
|
||
->orderBy('created_at', 'desc')
|
||
->get()),
|
||
];
|
||
|
||
$list = GoodsCategory::with(['goods', 'goods.activityExchange', 'goods.useGoodsAttrs'])->orderBy('sort', 'asc')->get();
|
||
$data['goods_List'] = GoodsCategoryResource::collection($list);
|
||
// [
|
||
// 'cate_name'=>"",
|
||
// "cate_goods"=>[
|
||
// [
|
||
// 'goods_id'=>1,
|
||
// 'goods_name'=>'ddd',
|
||
// 'goods_cover'=>'',
|
||
// 'goods_price'=>'10.00',
|
||
// 'sell_num'=>0,
|
||
// 'exchange_array'=>[
|
||
// 1,2,3,4
|
||
// ],
|
||
// 'goods_attr'=>[
|
||
// [
|
||
// 'attr_name'=>'规格名称',
|
||
// 'attr_values'=>[
|
||
// [
|
||
// 'value_name'=>'属性名称',
|
||
// 'value_peice'=>'属性价格',
|
||
// ]
|
||
// ]
|
||
// ]
|
||
// ]
|
||
// ]
|
||
// ]
|
||
// ]
|
||
|
||
return $this->success($data);
|
||
}
|
||
|
||
public function toCart(Request $request){
|
||
//返回最新购物车
|
||
return GoodsCartResource::collection(auth('api')->user()->getUserCart())->additional(['code' => Response::HTTP_OK, 'message' => '']);
|
||
}
|
||
|
||
public function toCartPrice(Request $request){
|
||
$goodsService = new GoodsService();
|
||
return $this->success(['cart_price'=>$goodsService->goodsCartPrice()], '请求成功');
|
||
}
|
||
|
||
/**修改购物车 */
|
||
public function editCart(Request $request){
|
||
$user = auth('api')->user();
|
||
$user_id = $user->id;
|
||
//检测是否绑定昵称头像
|
||
if(!$user->avatar || !$user->nick_name){
|
||
return $this->error('未绑定昵称头像', 501);
|
||
}
|
||
//检测是否绑定手机号
|
||
if(!$user->phone){
|
||
return $this->error('未绑定手机号', 502);
|
||
}
|
||
//
|
||
$init_num = $request->input('edit_num', 0);
|
||
$goods_info = $request->input('goods_info', '{}');//{"goods_id":1,"goods_attr":[1,2,3]}
|
||
//按给到的goods_info,改变购物车内容
|
||
$goods_info = json_decode($goods_info, true);
|
||
$goods = Good::findOrFail($goods_info['goods_id']);
|
||
// 判断是否下架
|
||
if(!$goods->is_sell){
|
||
return $this->error('该商品已下架');
|
||
}
|
||
|
||
$attr_data['goods_info'] = $goods_info;
|
||
$info_name = [];
|
||
$attr_price = 0;
|
||
// 处理有属性情况
|
||
if(isset($goods_info['goods_attr'])){
|
||
|
||
$goods_attrs = GoodsAttr::select('id')->where('is_use', 1)->where('goods_id', $goods->id)->groupBy('attr_id')->get();
|
||
|
||
//判断属性
|
||
if($goods_attrs->count() != count($goods_info['goods_attr'])){
|
||
return $this->error('请选择属性');
|
||
}
|
||
|
||
$goods_attrs = GoodsAttr::with('attr')->where('is_use', 1)->whereIn('id', $goods_info['goods_attr'])->get();
|
||
|
||
//判断属性是否均可用
|
||
if($goods_attrs->count() != count($goods_info['goods_attr'])){
|
||
return $this->error('属性错误');
|
||
}
|
||
foreach($goods_attrs as $goods_attr){
|
||
//判断属性是否有重复选择
|
||
if($goods_attr->attr->attr_type == 1 && isset($info_name[$goods_attr->attr_id])){
|
||
return $this->error('属性错误');
|
||
}
|
||
if(isset($info_name[$goods_attr->attr_id])){
|
||
$info_name[$goods_attr->attr_id] .= '|'.$goods_attr->attr_value;
|
||
}else{
|
||
$info_name[$goods_attr->attr_id] = $goods_attr->attr_name.":".$goods_attr->attr_value;
|
||
}
|
||
|
||
$attr_price += $goods_attr->value_price;
|
||
}
|
||
$attr_data['info_name'] = $info_name;
|
||
}
|
||
$attr_json = json_encode($attr_data, JSON_UNESCAPED_UNICODE);
|
||
//改变购物车
|
||
$code = md5($attr_json);
|
||
$cart_has = GoodsCart::where(['user_id'=>$user_id, 'goods_encode'=>$code])->exists();
|
||
$goods_num = abs($init_num);
|
||
if($cart_has){
|
||
$cart_goods = GoodsCart::where(['user_id'=>$user_id, 'goods_encode'=>$code])->first();
|
||
if($init_num > 0){
|
||
$cart_goods->increment('goods_num', $goods_num);
|
||
}elseif($init_num < 0){
|
||
$cart_goods->decrement('goods_num');
|
||
//如果数量为0 则删除
|
||
if($cart_goods->goods_num == 0){
|
||
$cart_goods->delete();
|
||
}
|
||
}
|
||
}else{
|
||
if($init_num > 0){
|
||
GoodsCart::create([
|
||
'user_id' => $user_id,
|
||
'goods_id' => $goods->id,
|
||
'goods_name' => $goods->goods_name,
|
||
'goods_encode' => $code,
|
||
'goods_attrs' => $attr_json,
|
||
'goods_price' => $goods->goods_price+$attr_price,
|
||
'goods_num' => $goods_num,
|
||
'is_too' => $goods->is_too,
|
||
]);
|
||
}else{
|
||
return $this->error('加入购物车失败:参数错误');
|
||
}
|
||
}
|
||
//返回最新购物车
|
||
return GoodsCartResource::collection(auth('api')->user()->getUserCart())->additional(['code' => Response::HTTP_OK, 'message' => '修改购物车成功']);
|
||
}
|
||
|
||
/**
|
||
* 清空购物车
|
||
*/
|
||
public function cleanCart(){
|
||
auth('api')->user()->cleanUserCart();
|
||
return $this->success('清空成功');
|
||
}
|
||
|
||
} |