auth->id; $OrderModel = new OrderModel(); $OrderGoods = new OrderGoods(); $Books = new Books(); $Video = new Video(); if(!isset($data['type']) || !$data['goods_id']) $this->error('参数错误'); if($data['type'] == '0'){ $total_price = $Books->where('id','in',$data['goods_id'])->sum('price'); }else if($data['type'] == '1'){ $total_price = $Video->where('id','in',$data['goods_id'])->sum('price'); } // return $total_price; $order_arr = [ 'user_id' => $uid, 'order_on' => $this->get_order_sn(), 'type' => $data['type'], 'total_price' => $total_price ]; Db::startTrans(); try { $save = $OrderModel->save($order_arr); $goods_id = explode(',',$data['goods_id']); if(count($goods_id) >= 1){ foreach ($goods_id as $k => $v){ if($data['type'] == 0){ $price = $Books->where('id',$v)->column('price'); $arr = [ 'order_id' => $OrderModel->id, 'goods_type' => $data['type'], 'goods_id' => $v, 'price' => (int)$price[0], ]; $OrderGoods = new OrderGoods(); $OrderGoods->save($arr); }else if($data['type'] == 1){ $price = $Video->where('id',$v)->column('price'); $arr = [ 'order_id' => $OrderModel->id, 'goods_type' => $data['type'], 'goods_id' => $v, 'price' => (int)$price[0], ]; $OrderGoods = new OrderGoods(); $OrderGoods->save($arr); } } } Db::commit(); } catch (Exception $e) { $this->setError($e->getMessage()); Db::rollback(); return false; } $this->success('操作成功',$OrderModel); } /** * 订单列表 * */ public function getOrderList(){ $data = input(); $where = []; $uid = $this->auth->id; $OrderModel = new OrderModel(); if(!isset($data['page']) || $data['page'] == '' || $data['page'] == null){ $page = 1; }else{ $page = $data['page']; } if(!isset($data['limit']) || $data['limit'] == '' || $data['limit'] == null){ $limit = 20; }else{ $limit = $data['limit']; } if(isset($data['status']) && $data['status'] != '' && $data['status'] != null){ $where['status'] = $data['status']; } $where['user_id'] = $uid; $where['is_deleted'] = 1; $list = $OrderModel->where($where)->with(['goods'])->paginate([ 'list_rows'=> $limit, 'page' => $page, ]); $lists = $list->toArray(); foreach ($lists['data'] as $key => $item){ foreach ($item['goods'] as $k => $v){ if($v['goods_type'] == 0){ $Books = new Books(); $good = $Books->where('id',$v['goods_id'])->field('id,title,image,book_type,is_new_recommend,author,is_awards,price,entity_price,createtime,updatetime,level_id,arrangement_id,speciality_id,series_id,is_series')->find(); $good['is_free'] = 1; if($good['price'] <= 0){ $good['is_free'] = 0; } $lists['data'][$key]['goods'][$k]['good_item'] = $good; }else if($v['goods_type'] == 1){ $Video = new Video(); $lists['data'][$key]['goods'][$k]['good_item'] = $Video->where('id',$v['goods_id'])->find(); } } } $this->success('操作成功',$lists); } /** * 订单详情 * */ public function getOrderInfo(){ $data = input(); $where = []; $uid = $this->auth->id; $OrderModel = new OrderModel(); if(!isset($data['id']) || $data['id'] == '' || $data['id'] == null){ $this->error('参数错误'); } $list = $OrderModel->where('id',$data['id'])->where('user_id',$uid)->where('is_deleted',1)->with(['goods'])->find(); if(!$list){ $this->error('订单不存在'); } $lists = $list->toArray(); foreach ($lists['goods'] as $k => $v){ if($v['goods_type'] == 0){ $Books = new Books(); $good = $Books->where('id',$v['goods_id'])->field('id,title,image,book_type,is_new_recommend,author,is_awards,price,entity_price,createtime,updatetime,level_id,arrangement_id,speciality_id,series_id,is_series')->find(); $good['is_free'] = 1; if($good['price'] <= 0){ $good['is_free'] = 0; } $lists['goods'][$k]['good_item'] = $good; }else if($v['goods_type'] == 1){ $Video = new Video(); $lists['goods'][$k]['good_item'] = $Video->where('id',$v['goods_id'])->find(); } } $lists['createtime'] = date('Y-m-d H:i:s', $lists['createtime']); $this->success('操作成功',$lists); } /** * 取消订单 * */ public function cancellationOrder(){ $data = input(); $where = []; $uid = $this->auth->id; $OrderModel = new OrderModel(); if(!isset($data['id']) || $data['id'] == '' || $data['id'] == null){ $this->error('参数错误'); } $arr = [ 'status' => -1 ]; if($OrderModel->where('user_id',$uid)->where('id',$data['id'])->update($arr)){ $this->success('操作成功'); }else{ $this->error('操作失败'); }; } /** * 删除订单 * */ public function delOrder(){ $data = input(); $where = []; $uid = $this->auth->id; $OrderModel = new OrderModel(); if(!isset($data['id']) || $data['id'] == '' || $data['id'] == null){ $this->error('参数错误'); } $arr = [ 'is_deleted' => 0 ]; if($OrderModel->where('user_id',$uid)->where('id',$data['id'])->update($arr)){ $this->success('操作成功'); }else{ $this->error('操作失败'); }; } /** * 订单支付 * */ public function orderPay(){ $data = input(); $OrderModel = new OrderModel(); $order = $OrderModel->where('status',0)->where('id',$data['order_id'])->find(); if(!$order){ $this->error('订单不存在'); } $Pay = new Pay(); if($data['pay_type'] == 0){ //微信 $arr = [ 'total_fee' => $order['total_price']*100, 'out_trade_no' => $order['order_on'], 'trade_type' => $data['trade_type'], ]; return $Pay->wxpay($arr); }else if($data['pay_type'] == 1){ //支付宝 $arr = [ 'total_amount' => $order['total_price'], 'out_trade_no' => $order['order_on'], 'subject' => '购买', 'type' => $data['trade_type'], ]; return $Pay->alipay($arr); } } /** * 生成订单号 * */ public function get_order_sn(){ $order_id_main = date('YmdHis') . rand(10000000,99999999); $order_id_len = strlen($order_id_main); $order_id_sum = 0; for($i=0; $i<$order_id_len; $i++){ $order_id_sum += (int)(substr($order_id_main,$i,1)); } $osn = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100,2,'0',STR_PAD_LEFT); return $osn; } }