verify_sign(); // 验证参数 $request->scene('create')->validate(); // 检查登录 $uid = $this->checkLogin(); // 接收参数 $productId = request('product_id',0); $buyNum = request('buy_num',0); $addrId = request('addr_id',0); $buyType = 3; $payType = 1; // 获取地址 $addr = $CustomAddr->getOne($addrId); // 如果不存在数据 if( !$addr ) return json_send(['code'=>'error','msg'=>'地址有误,请核对','data'=>['error'=>'没有找到对应的地址']]); // 重组数据 $addr = ['contact_name'=>$addr['contact_name'],'contact_shop'=>$addr['contact_shop'],'shop_type'=>$addr['shop_type'],'contact_phone'=>$addr['contact_phone'],'contact_province'=>$addr['contact_province'],'contact_city'=>$addr['contact_city'],'contact_area'=>$addr['contact_area'],'contact_addr'=>$addr['contact_addr']]; // 解码 $productList = $Product->getOne($productId); // 如果产品不存在 if( !$productList ) return json_send(['code'=>'error','msg'=>'产品不存在或已下架','data'=>['error'=>'产品ID不存在'.$productId]]); // 如果产品库存不足主产品 if( $productList['stock'] < $buyNum ) return json_send(['code'=>'error','msg'=>'产品库存不足','data'=>['error'=>'产品库存不足']]); // 获取积分信息 $balance = $CustomScore->getBalanceByUid($uid); // 如果剩余积分不足 if( $balance < $buyNum * $productList['score'] ) return json_send(['code'=>'error','msg'=>'积分不足','data'=>['error'=>'积分不足']]); // 总共积分 $order['score_total'] = $buyNum * $productList['score']; $order['product_id'] = $productId; $order['buy_num'] = $buyNum; $order['custom_uid'] = $uid; $order['status'] = 1; // 组合数据,写入订单表,子表 DB::beginTransaction(); // 写入数据 try { // 扣减库存 $result = $Product->edit($productId,['stock'=>DB::raw('stock+-'.$buyNum)]); // 如果扣减失败 if( !$result ) { // 回退数据 DB::rollBack(); // 提示信息 return json_send(['code'=>'error','msg'=>'兑换失败','data'=>['error'=>'产品库存扣减失败']]); } // 创建总订单 $orderId = $Model->add($order); // 如果扣减失败 if( !$orderId ) { // 回退数据 DB::rollBack(); // 提示信息 return json_send(['code'=>'error','msg'=>'兑换失败','data'=>['error'=>'订单创建失败']]); } // 积分扣减 $result = $CustomScore->trade($uid,$orderId,($order['score_total']*-1),$buyType,$payType); // 地址写入失败 if( isset($result['error']) ) { // 回退数据 DB::rollBack(); // 提示信息 return json_send(['code'=>'error','msg'=>'兑换失败','data'=>['error'=>$result['error']]]); } // 订单ID $addr['order_id'] = $orderId; // 写入订单地址表 $result = $OrdersAddr->add($addr); // 地址写入失败 if( !$result ) { // 回退数据 DB::rollBack(); // 提示信息 return json_send(['code'=>'error','msg'=>'兑换失败','data'=>['error'=>'地址写入失败']]); } // 提交数据 DB::commit(); // 返回结果 return json_send(['code'=>'success','msg'=>'兑换成功','data'=>['id'=>$orderId]]); } catch (\Throwable $th) { // 回退数据 DB::rollBack(); // 判断结果,如果库存扣减失败的话 if( stripos($th->getMessage(),'UNSIGNED') ) return json_send(['code'=>'error','msg'=>'兑换失败','data'=>['error'=>'产品库存扣减失败']]); // 下单失败提示 return json_send(['code'=>'error','msg'=>'兑换失败','data'=>['error'=>$th->getMessage()]]); } } /** * 获取列表 /api/score_order/get_list * * @param string $name 产品名称 * @param int $page 页码,默认1 * @param int $limit 每页条数,默认10条 * * */ public function get_list(Request $request,Model $Model){ // 接口验签 // $this->verify_sign(); // 验证参数 $request->scene('get_list')->validate(); // 检查登录 $uid = $this->checkLogin(); // 接收参数 $status = request('status',0); $limit = request('limit',10); // 显示 $map = [['score_orders.custom_uid','=',$uid]]; // 查询状态 if( $status ) $map[] = ['score_orders.status','=',$status]; // 查询 $Paginator = $Model->query()->join('score_product','score_orders.product_id','=','score_product.id')->where($map)->orderByDesc('score_orders.id')->paginate($limit,['score_orders.id','score_orders.product_id','score_orders.score_total','score_orders.buy_num','score_orders.status','score_orders.insert_time','score_product.name as product_name','score_product.thumb as product_thumb','score_product.spec as product_spec']); // 循环处理数据 foreach ($Paginator as $key => $value) { // 产品图路径 $value['state'] = $Model->getState($value['status'],'state'); // 产品图路径 $value['product_thumb'] = path_compat($value['product_thumb']); // 重组 $Paginator[$key] = $value; } // 获取数据 $data['total'] = $Paginator->total(); $data['current_page'] = $Paginator->currentPage(); $data['per_page'] = $Paginator->perPage(); $data['last_page'] = $Paginator->lastPage(); $data['data'] = $Paginator->items(); // 返回结果 return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]); } /** * 订单详情 * * @pamam int $id 订单ID * * */ public function get_detail( Request $request, Model $Model,OrdersAddr $OrdersAddr){ // 验证参数 $request->scene('get_detail')->validate(); // 接受参数 $id = request('id',0); // 查询数据 $order = $Model->query()->join('score_product','score_orders.product_id','=','score_product.id')->where([['score_orders.id','=',$id]])->first(['score_orders.id','score_orders.remark','score_orders.track_number','score_orders.product_id','score_orders.score_total','score_orders.buy_num','score_orders.status','score_orders.insert_time','score_product.name as product_name','score_product.thumb as product_thumb','score_product.spec as product_spec']); // 查询不到订单 if( !$order ) return json_send(['code'=>'error','msg'=>'订单不存在']); // 数据转换 $order = $order->toArray(); // id转编号 $order['insert_time'] = date('Y-m-d H:i:s',$order['insert_time']); $order['order_code'] = $Model->idToCode($order['id']); $order['state'] = $Model->getState($order['status'],'state'); $order['remark'] = $order['remark'] ? $order['remark'] : '暂无'; $order['product_thumb'] = path_compat($order['product_thumb']); // 地址 $order['order_addr'] = $OrdersAddr->query()->where([['order_id','=',$id]])->first(['contact_name','contact_phone','contact_province','contact_city','contact_area','contact_addr','contact_shop']); // 加载模板 return json_send(['code'=>'success','msg'=>'获取成功','data'=>$order]); } }