'number', 'product_number'=>'string', 'product_name'=>'string', 'market_price'=>'number', 'sales_price'=>'number', 'remain_num'=>'number', 'product_value_id'=>'array', ]; HelperService::diffParam($param_list,$this->post); $ProductService = new ProductService(); $id = $ProductService->addProduct($this->post); $this->returnJson(['id'=>$id]); } /** * 添加商品分类的接口 */ public function addProductCategory(){ $param_list = [ 'category_name'=>'string', 'parent_id'=>'int', ]; HelperService::diffParam($param_list,$this->post); if(isset($this->post['category_img']) && is_array($this->post['category_img'])){ $category_img = json_encode($this->post['category_img']); } $data = [ 'category_name'=>$this->post['category_name'], 'parent_id'=>$this->post['parent_id'], 'category_img'=>$category_img, 'status'=>0 ]; $ProductService = new ProductService(); $product = $ProductService->getProductCategoryByCondition(['category_name'=>$this->post['category_name']]); if($product){ $this->returnJson('category_name is duplicate'); } $id=$ProductService->addProductCategory($data); $this->returnJson(['id'=>$id]); } /** * 更新商品分类接口 */ public function upProductCategory(){ $param_list = [ 'category_id'=>'number', ]; HelperService::diffParam($param_list,$this->post); $update_list = ['category_id','category_name','parent_id','status','category_img']; $data = []; foreach($this->post as $key=>$item){ if(!in_array($key,$update_list)){ continue; } $data[$key] = $item; } if(isset($this->post['category_img']) && is_array($this->post['category_img'])){ $data['category_img'] = json_encode($this->post['category_img']); } $ProductService = new ProductService(); $row=$ProductService->upProductCategory($data); $this->returnJson(['row'=>$row]); } public function addProductAttr(){ $param_list = [ 'attr_name'=>'string', ]; HelperService::diffParam($param_list,$this->post); $ProductService = new ProductService(); $id=$ProductService->addProductAttr($this->post); $this->returnJson(['id'=>$id]); } public function upProductAttr(){ $param_list = [ 'attr_id'=>'number', ]; HelperService::diffParam($param_list,$this->post); $ProductService = new ProductService(); $row=$ProductService->upProductAttr($this->post); $this->returnJson(['row'=>$row]); } public function addProductAttrValue(){ $param_list = [ 'attr_id'=>'number', 'value_name'=>'string', ]; HelperService::diffParam($param_list,$this->post); $ProductService = new ProductService(); $id=$ProductService->addProductAttrValue($this->post); $this->returnJson(['id'=>$id]); } public function upProductAttrValue(){ $param_list = [ 'product_value_id'=>'number', ]; HelperService::diffParam($param_list,$this->post); $ProductService = new ProductService(); $row=$ProductService->upProductAttrValue($this->post); $this->returnJson(['row'=>$row]); } public function addProductImg(){ $param_list = [ 'product_id'=>'number', 'img'=>'string', ]; HelperService::diffParam($param_list,$this->post); $ProductService = new ProductService(); $id=$ProductService->addProductImg($this->post); $this->returnJson(['id'=>$id]); } public function upProductImg(){ $param_list = [ 'id'=>'number', ]; HelperService::diffParam($param_list,$this->post); $ProductService = new ProductService(); $row=$ProductService->upProductImg($this->post); $this->returnJson(['row'=>$row]); } /** * 获取分类列表 */ public function getCategoryList(){ $productCategoryModel = new ProductCategoryModel(); $productCategory = $productCategoryModel->getList(['status'=>0]); $this->returnJson($productCategory); } /** * 根据分类id获取分类信息 */ public function getCategoryInfoById(){ $param_list = [ 'category_id'=>'number', ]; HelperService::diffParam($param_list,$this->post); $productCategoryModel = new ProductCategoryModel(); $productCategory = $productCategoryModel->where([ 'category_id'=>$this->post['category_id'], ])->find(); $this->returnJson($productCategory); } /** * 获取当前分类下的产品列表 */ public function getProductListByCategoryId(){ $param_list = [ 'category_id'=>'number', ]; HelperService::diffParam($param_list,$this->post); $productModel = new ProductModel(); if(!isset($this->post['product_id'])){ $productList = $productModel->getList(['category_id'=>$this->post['category_id'],'remain_num'=>['gt',0],'status'=>0],'add_time desc',300); }else{ $product_id = $this->post['product_id']; $productList = $productModel->getList(['category_id'=>$this->post['category_id'],'remain_num'=>['gt',0],'status'=>0],"product_id <> {$product_id},add_time desc"); } $this->returnJson($productList); } /** * 将产品加入购物 */ public function addCar(){ $param_list = [ 'product_id'=>'number', 'user_id'=>'number', 'num'=>'number', ]; HelperService::diffParam($param_list,$this->post); $buyCarModel = new BuyCarModel(); $buyCar = $buyCarModel->getOne(['user_id'=>$this->post['user_id'],'product_id'=>$this->post['product_id']]); if(empty($buyCar)){ $data = [ 'user_id'=>$this->post['user_id'], 'product_id'=>$this->post['product_id'], 'num'=>$this->post['num'], 'add_time'=>time() ]; $id = $buyCarModel->addInfo($data); }else{ $id = $buyCarModel->upInfo(['id'=>$buyCar['id'],'user_id'=>$this->post['user_id'],'num'=>$buyCar['num']],['num'=>$this->post['num']+$buyCar['num']]); } $this->returnJson(['id'=>$id]); } /** * 将产品自增 */ public function incCar(){ $param_list = [ 'product_id'=>'number', 'user_id'=>'number', ]; HelperService::diffParam($param_list,$this->post); $buyCarModel = new BuyCarModel(); $buyCar = $buyCarModel->getOne(['user_id'=>$this->post['user_id'],'product_id'=>$this->post['product_id']]); if(empty($buyCar)){ $data = [ 'user_id'=>$this->post['user_id'], 'product_id'=>$this->post['product_id'], 'num'=>1, 'add_time'=>time() ]; $id = $buyCarModel->addInfo($data); }else{ $id = $buyCarModel->where(['id'=>$buyCar['id']])->setInc('num',1); } $this->returnJson(['id'=>$id]); } /** * 获取购物车列表 */ public function getCarInfo(){ $param_list = [ 'user_id'=>'number', ]; HelperService::diffParam($param_list,$this->post); $buyCarModel = new BuyCarModel(); $list = $buyCarModel->getCarAndProductInfo(['user_id'=>$this->post['user_id'],'buy_car.status'=>0]); $priceList = []; $total_num = 0; foreach($list as $item){ $total_num += $item['num']; $priceList[] = $item['sales_price'] * $item['num']; } $data = [ 'count'=>$total_num, 'total_price'=>array_sum($priceList) ]; $this->returnJson($data); } /** * 获取购物车列表 */ public function getCarList(){ $param_list = [ 'user_id'=>'number', ]; HelperService::diffParam($param_list,$this->post); $buyCarModel = new BuyCarModel(); $list = $buyCarModel->getCarAndProductInfo(['user_id'=>$this->post['user_id'],'buy_car.status'=>0]); $priceList = []; foreach($list as $item){ $priceList[] = $item['sales_price'] * $item['num']; } $data = [ 'total_price'=>array_sum($priceList), 'list'=>$list ]; $this->returnJson($data); } /** *批量查询勾选的商品 */ public function getCarListByProductIds(){ $param_list = [ 'user_id'=>'number', 'productIds'=>'array', ]; HelperService::diffParam($param_list,$this->post); $buyCarModel = new BuyCarModel(); $list = $buyCarModel->getCarAndProductInfo([ 'buy_car.user_id'=>$this->post['user_id'], 'buy_car.status'=>0, 'buy_car.product_id'=>['in',$this->post['productIds']] ]); $priceList = []; foreach($list as $item){ $priceList[] = $item['sales_price'] * $item['num']; } $data = [ 'total_price'=>array_sum($priceList), 'list'=>$list, ]; $this->returnJson($data); } /** *批量更新勾选的商品 */ public function updateCarByProductIds(){ $param_list = [ 'user_id'=>'number', 'productIdAndNum'=>'array', ]; HelperService::diffParam($param_list,$this->post); $buyCarModel = new BuyCarModel(); $row = 0; foreach($this->post['productIdAndNum'] as $product_id=>$num){ $row += $buyCarModel->upInfo(['product_id'=>$product_id,'user_id'=>$this->post['user_id']],['num'=>$num]); } $this->returnJson(['row'=>$row]); } /** * 批量删除被勾选的商品 */ public function delCarBatch(){ $param_list = [ 'user_id'=>'number', 'product_ids'=>'array', ]; HelperService::diffParam($param_list,$this->post); $buyCarModel = new BuyCarModel(); $row = $buyCarModel->where([ 'user_id'=>$this->post['user_id'], 'product_id'=>['in',$this->post['product_ids']] ])->delete(); $this->returnJson(['row'=>$row]); } /** * 保存 用户收货地址 */ public function saveAddress(){ $param_list = [ 'user_id'=>'number', 'name'=>'string', 'mobile'=>'mobile', 'city'=>'string', 'address'=>'string', 'address_name'=>'string', ]; HelperService::diffParam($param_list,$this->post); $data = [ 'user_id' => $this->post['user_id'], 'name' => $this->post['name'], 'mobile' => $this->post['mobile'], 'city' => $this->post['city'], 'address' => $this->post['address'], 'address_name' =>$this->post['address_name'], 'status' => 0 ]; $userAddressModel = new UserAddressModel(); if(isset($this->post['address_id']) && !empty($this->post['address_id']) && is_numeric($this->post['address_id'])) { $where['address_id'] = $this->post['address_id']; $row = $userAddressModel->upInfo($where,$data); $this->returnJson(['row'=>$row]); }else{ $data['add_datetime'] = date('Y-m-d H:i:s'); $id = $userAddressModel->addInfo($data); $this->returnJson(['id'=>$id]); } } /** * 删除 用户的收货地址 */ public function delAddress(){ $param_list = [ 'address_id'=>'number', 'user_id'=>'number' ]; HelperService::diffParam($param_list,$this->post); $userAddressModel = new UserAddressModel(); $row = $userAddressModel ->where([ 'address_id'=>$this->post['address_id'], 'user_id'=>$this->post['user_id'] ])->delete(); $this->returnJson(['row'=>$row]); } /** * 将收货地址设置为默认地址 */ public function setDefaultAddress(){ $param_list = [ 'user_id'=>'number', 'address_id'=>'number', ]; HelperService::diffParam($param_list,$this->post); $userAddressModel = new UserAddressModel(); $where = [ 'user_id' => $this->post['user_id'], 'address_id'=> $this->post['address_id'], ]; $row = 0; $res = $userAddressModel->upInfo(['user_id'=>$this->post['user_id']],['default_address'=>0]); if($res){ $row += 1; } $res = $userAddressModel->upInfo($where,['default_address'=>1]); if($res){ $row += 1; } $this->returnJson(['row'=>$row]); } /** * 获取默认地址的信息 */ public function getDefaultAddress(){ $param_list = [ 'user_id'=>'number', ]; HelperService::diffParam($param_list,$this->post); $userAddressModel = new UserAddressModel(); $userAddress = $userAddressModel->getOne([ 'user_id'=>$this->post['user_id'], 'default_address'=>1, ]); $this->returnJson($userAddress); } /** *通过地址的id获取地址信息 */ public function getUserAddressById(){ $param_list = [ 'address_id'=>'int', ]; HelperService::diffParam($param_list,$this->post); $userAddressModel = new UserAddressModel(); $userAddress = $userAddressModel->getOne([ 'address_id'=>$this->post['address_id'] ]); $this->returnJson($userAddress); } /** * 根据用户id获取收货地址的列表 */ public function getUserAddressListByUserId(){ $param_list = [ 'user_id'=>'number', ]; HelperService::diffParam($param_list,$this->post); $userAddressModel = new UserAddressModel(); $userAddress = $userAddressModel->getList([ 'user_id'=>$this->post['user_id'], ],"default_address desc,address_id desc"); $this->returnJson($userAddress); } /** * 获取产品的分页列表 */ public function getProductPage(){ $param_list = [ 'pageSize'=>'number', 'page'=>'number', 'condition'=>'array', ]; HelperService::diffParam($param_list,$this->post); $search_key = ['product_id','category_id','product_name','origin']; $condition = $this->post['condition']; $where = []; foreach($condition as $key=>$item){ if(!in_array($key,$search_key)){ continue; } switch($key){ case "product_id": case "category_id": $where[$key] = $item; break; case 'product_name': case 'origin': $where[$key] = ['like',"%{$item}%"]; break; } } $page = $this->post['page']; $pageSize = $this->post['pageSize']; $page = $page>=1?intval($page):1; $pageSize = $pageSize>2?intval($pageSize):2; $productModel = new ProductModel(); if($where) { $productCount = $productModel->where($where)->count(); $productList = $productModel->where($where)->page($page, $pageSize)->select(); }else{ $productCount = $productModel->count(); $productList = $productModel->page($page, $pageSize)->select(); } $data = [ 'count' => $productCount, 'list'=>$productList ]; $this->returnJson($data); } /** * 获取产品订单列表 */ public function getProductOrderPageByType(){ $param_list = [ 'page'=>'number', 'pageSize'=>'number', 'status'=>'int', ]; HelperService::diffParam($param_list,$this->post); if(!in_array($this->post['status'],[0,1,2,3,10])){ $this->returnJson('',400,'status is error'); } //判断要查询的订单 $where = ['status'=>$this->post['status']]; if($this->post['status'] == 10){ //全部订单 $where = []; } //可选条件user_id $user_id = isset($this->post['user_id'])?$this->post['user_id']:0; if(!empty($user_id) && is_numeric($user_id)){ $where['user_id'] = $this->post['user_id']; } //可选条件 $condition = isset($this->post['condition'])?$this->post['condition']:[]; if(!empty($condition) && is_array($condition)){ foreach($condition as $key=>$value){ switch($key){ case 'order_no': case 'pay_style': case 'logistic_company': case 'logistic_number': $where[$key] = $value; break; case 'add_time': case 'pay_time': //确保是时间戳 $time = intval($value); //日期当天的数据 $where[$key] = ['between',[$time,$time+86400]]; break; } } } $page = $this->post['page'] >1?$this->post['page']:1; $pageSize = $this->post['pageSize'] >1?$this->post['pageSize']:2; $productOrderModel = new ProductOrderModel(); $res = $productOrderModel->getPage($where,"order_id desc",$page,$pageSize); $this->returnJson($res); } /** * 修改 */ /** * 无额外属性的产品添加接口 */ public function saveProductNotAttr(){ $param_list = [ 'category_id'=>'number', 'product_name'=>'string', 'product_number'=>'', 'market_price'=>'int', 'sales_price'=>'int', 'remain_num'=>'int', ]; HelperService::diffParam($param_list,$this->post); $productModel = new ProductModel(); $data = [ 'category_id'=>$this->post['category_id'], 'product_number'=>$this->post['product_number'], 'product_name'=>$this->post['product_name'], 'market_price'=>$this->post['market_price'], 'sales_price'=>$this->post['sales_price'], 'remain_num'=>$this->post['remain_num'], 'add_time'=>time(), 'add_datetime'=>date('Y-m-d H:i:s'), 'content'=>isset($this->post['content'])?$this->post['content']:'', 'weight'=>isset($this->post['weight'])?$this->post['weight']:'', 'origin'=>isset($this->post['origin'])?$this->post['origin']:'', 'Unit'=>isset($this->post['Unit'])?$this->post['Unit']:'', 'img'=>isset($this->post['img'])?$this->post['img']:'', ]; if(isset($this->post['product_id'])) { $product_id = intval($this->post['product_id'])>0?$this->post['product_id']:0; $row = $productModel->upInfo(['product_id'=>$product_id],$data); $this->returnJson(['row' => $row]); }else{ $id = $productModel->addInfo($data); $this->returnJson(['id' => $id]); } } /** * 根据productId获取ProductInfo */ public function getProductDetailById(){ $param_list = [ 'product_id'=>'number' ]; HelperService::diffParam($param_list,$this->post); $productModel = new ProductModel(); $product = $productModel->where(['product_id'=>$this->post['product_id']])->find(); $this->returnJson($product); } /** * 根据productId修改产品信息 */ public function upProductInfoByProductId(){ $param_list = [ 'product_id'=>'number', ]; HelperService::diffParam($param_list,$this->post); $data = [ 'status'=>isset($this->post['status'])?$this->post['status']:0, ]; $productModel = new ProductModel(); $row = $productModel->upInfo(['product_id'=>$this->post['product_id']],$data); $this->returnJson(['row'=>$row]); } /** * 通过条件获取订单信息 */ public function getUserOrderByType(){ $param_list = [ 'user_id'=>'number', 'status'=>'int' ]; HelperService::diffParam($param_list,$this->post); if(!in_array($this->post['status'],[0,1,2,3,4,10])){ $this->returnJson([],400,'status is error'); } $params = []; $params['user_id'] = $this->post['user_id']; if($this->post['status']!=10){ $params['status'] = $this->post['status']; } $productOrderModel = new ProductOrderModel(); $productOrder = $productOrderModel->getProductOrder($params); $this->returnJson($productOrder); } /** * 用户每种类型订单的数量 */ public function getUserOrderCountByType(){ $param_list = [ 'user_id'=>'number', 'status'=>'int' ]; HelperService::diffParam($param_list,$this->post); if(!in_array($this->post['status'],[0,1,2,3,4,10])){ $this->returnJson([],400,'status is error'); } $params = []; $params['user_id'] = $this->post['user_id']; if($this->post['status']!=10){ $params['status'] = $this->post['status']; } $productOrderModel = new ProductOrderModel(); $productOrderCount = $productOrderModel->getProductOrderCount($params); $this->returnJson($productOrderCount); } /** * 根据订单编号或许订单详情 */ public function getOrderByOrderNo(){ $param_list = [ 'order_no'=>'string' ]; HelperService::diffParam($param_list,$this->post); $params = []; $params['order_no'] = $this->post['order_no']; $productOrderModel = new ProductOrderModel(); $productOrder = $productOrderModel->getOne($params); $order_no = isset($productOrder['order_no'])?$productOrder['order_no']:0; $productOrderDetailModel = new ProductOrderDetailModel(); $productOrderDetail = $productOrderDetailModel->getList(['order_no'=>$order_no],'',1000); if($productOrder){ $productOrder['detail'] = $productOrderDetail; } $this->returnJson($productOrder); } /** * 前台用户取消订单(暂时不用这个接口,因为和功能有点重复) */ private function userCancelOrder(){ $param_list = [ 'order_no'=>'string', 'user_id'=>'number' ]; HelperService::diffParam($param_list,$this->post); $orderModel = new ProductOrderModel(); $orderModel->upInfo([ 'order_no'=>$this->post['order_no'], 'user_id'=>$this->post['user_id'] ],['status'=>3]); $this->returnJson('success'); } /** * 后台需要订单 */ public function saveOrderStatus(){ $param_list = [ 'order_no'=>'string', 'status'=>'number', ]; HelperService::diffParam($param_list,$this->post); if(!in_array($this->post['status'],[0,1,2,3,4])){ $this->returnJson([],400,'status is error'); } $orderModel = new ProductOrderModel(); $orderModel->upInfo( ['order_no'=>$this->post['order_no']], ['status'=>$this->post['status']] ); $this->returnJson('success'); } /** * 保存订单信息 */ public function saveOrderInfo(){ $param_list = [ 'order_no'=>'string', 'status'=>'int' ]; HelperService::diffParam($param_list,$this->post); $data = []; if(isset($this->post['logistic_company_name'])){ $data['logistic_company_name'] = $this->post['logistic_company_name']; } if(isset($this->post['back_note'])){ $data['back_note'] = $this->post['back_note']; } if(isset($this->post['logistic_number'])){ $data['logistic_number'] = $this->post['logistic_number']; } if(!in_array($this->post['status'],[0,1,2,3,4])){ $this->returnJson([],400,'status is error'); } $data['status'] = $this->post['status']; $orderModel = new ProductOrderModel(); $orderModel->upInfo(['order_no'=>$this->post['order_no']],$data); $this->returnJson('success'); } /** * 提交订单信息 */ public function PayOrder(){ $param_list = [ 'user_id'=>'number', 'productIds'=>'array', 'send_style'=>'int', 'pay_style'=>'int', ]; HelperService::diffParam($param_list,$this->post); $productService = new ProductService(); $orderModel = new ProductOrderModel(); $order_no = $this->createOrderNum(); $total_price = $productService->getOrderPriceByProductIds($order_no,$this->post['productIds'],$this->post['user_id']); if(empty($total_price)){ HelperService::returnJson(['code'=>400,'msg'=>'Payorder is error']); } switch($this->post['send_style']){ //快递方式 case 0: $order =[ 'order_no'=>$order_no, 'address_id'=>$this->post['address_id']?:0, 'add_time'=>time(), 'user_id'=>$this->post['user_id'], 'total_price'=> $total_price, 'pay_style'=>$this->post['pay_style'], 'send_style'=>$this->post['send_style'], 'note'=>$this->post['note']?:'', 'status'=>0 ]; $order_id = $orderModel->addInfo($order); if($order_id === false){ HelperService::returnJson(['code'=>400,'msg'=>'insert order fail']); } break; //自提方式 case 1: $order =[ 'order_no'=>$order_no, 'address_id'=>$this->post['address_id']?:0, 'add_time'=>time(), 'add_date'=>date('Y-m-d'), 'user_id'=>$this->post['user_id'], 'total_price'=> $total_price, 'pay_style'=>$this->post['pay_style'], 'send_style'=>$this->post['send_style'], 'note'=>$this->post['note']?:'', 'status'=>0, 'get_datetime'=>$this->post['get_datetime'], ]; $order_id = $orderModel->addInfo($order); if($order_id === false){ HelperService::returnJson(['code'=>400,'msg'=>'insert order fail']); } break; default: HelperService::returnJson(['code'=>400,'msg'=>'send_style is error']); break; } $this->returnJson(['order_no'=>$order_no]); } /** *商品每日订单数量 */ public function ProductOrderDayNum(){ $param = $this->request->param(); $end_date = isset($param['end_date'])?strtotime($param['end_date']):strtotime("-1 seconds",strtotime(date('Y-m-d'))); $start_date = isset($param['start_date'])?strtotime($param['start_date']):0; //控制时间,以免增加服务器压力 if(empty($start_date) || $end_date-$start_date>86400*20){ $start_date = $end_date-86400*20; } $productOrderModel = new ProductOrderModel(); $productOrder = $productOrderModel ->field('count(order_id) as total_count,add_date') ->where(['add_date'=>['between',[date('Y-m-d',$start_date),date('Y-m-d',$end_date)]]])->group('add_date')->cache(3600*2)->select(); $this->returnJson($productOrder); } /** *订单总数量 */ public function totalOrderNum(){ $productOrderModel = new ProductOrderModel(); $productOrder = $productOrderModel->cache(10)->count(); $this->returnJson(['count'=>$productOrder]); } }