作者 李忠强

更新

@@ -7,6 +7,8 @@ namespace app\api\controller; @@ -7,6 +7,8 @@ namespace app\api\controller;
7 use app\api\model\GoodsSpec; 7 use app\api\model\GoodsSpec;
8 use app\api\model\SpecValue; 8 use app\api\model\SpecValue;
9 use app\common\controller\Api; 9 use app\common\controller\Api;
  10 +use think\Db;
  11 +use think\exception\PDOException;
10 12
11 /** 13 /**
12 * 订单 14 * 订单
@@ -18,7 +20,6 @@ class Order extends Api @@ -18,7 +20,6 @@ class Order extends Api
18 20
19 /** 21 /**
20 * @ApiTitle (订单列表) 22 * @ApiTitle (订单列表)
21 - * @ApiSummary ([{"goods_id":22,"goods_sku_id":106,"number":2},{"goods_id":23,"goods_sku_id":66,"number":2}])  
22 * @ApiMethod (POST) 23 * @ApiMethod (POST)
23 * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") 24 * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
24 * @ApiParams (name="type", type="integer", required=true, description="类型1全部2待付款3待发货4待收货5待评价") 25 * @ApiParams (name="type", type="integer", required=true, description="类型1全部2待付款3待发货4待收货5待评价")
@@ -26,6 +27,41 @@ class Order extends Api @@ -26,6 +27,41 @@ class Order extends Api
26 * @ApiReturn ({ 27 * @ApiReturn ({
27 'code':'1', 28 'code':'1',
28 'msg':'返回成功' 29 'msg':'返回成功'
  30 + 'data':
  31 + "total": 1, 总条数
  32 + "per_page": 5, 每页数量
  33 + "current_page": 1, 当前页
  34 + "last_page": 1, 最后一页
  35 + "data": [
  36 + {
  37 + "id": 1, 订单id
  38 + "order_no": "adsadasdas", 订单编号
  39 + "discount_price": "0.00", 优惠金额
  40 + "pay_price": "0.00", 实付价格
  41 + "pay_status": "10", 10未支付20已支付
  42 + "freight_status": "10", 10未发货20已发货
  43 + "receipt_status": "10", 10未收货20已收货
  44 + "order_status": "10", 10进行中20已取消30已完成
  45 + "total_sum": 0, 商品总数量
  46 + "goods": [
  47 + {
  48 + "goods_name": "asdasd", 商品名
  49 + "goods_attr": "1", 规格名
  50 + "total_num": 0, 数量
  51 + "total_price": "0.00", 价格
  52 + "image_text": "" 图片
  53 + },
  54 + {
  55 + "goods_name": "asdasd",
  56 + "goods_attr": "1",
  57 + "total_num": 0,
  58 + "total_price": "0.00",
  59 + "image_text": ""
  60 + }
  61 + ],
  62 + "createtime_text": "" 时间
  63 + }
  64 + ]
29 }) 65 })
30 */ 66 */
31 public function orderList() 67 public function orderList()
@@ -34,12 +70,11 @@ class Order extends Api @@ -34,12 +70,11 @@ class Order extends Api
34 $page = $this->request->post('page',1); 70 $page = $this->request->post('page',1);
35 if (!in_array($type,[1,2,3,4,5])) $this->error('type参数不合法'); 71 if (!in_array($type,[1,2,3,4,5])) $this->error('type参数不合法');
36 if (!is_numeric($page)) $this->error('页数不合法'); 72 if (!is_numeric($page)) $this->error('页数不合法');
37 - $model = new \app\api\model\Order();  
38 switch ($type){ 73 switch ($type){
39 case 1: 74 case 1:
40 $where = [ 75 $where = [
41 'fa_litestore_order.status' => 'normal', 76 'fa_litestore_order.status' => 'normal',
42 - 'fa_litestore_order.user_id' => 1, 77 + 'fa_litestore_order.user_id' => $this->auth->id,
43 ]; 78 ];
44 break; 79 break;
45 case 2: 80 case 2:
@@ -71,11 +106,26 @@ class Order extends Api @@ -71,11 +106,26 @@ class Order extends Api
71 ]; 106 ];
72 break; 107 break;
73 } 108 }
  109 +
  110 + $model = new \app\api\model\Order();
74 $list = $model 111 $list = $model
75 ->with(['goods']) 112 ->with(['goods'])
76 ->where($where) 113 ->where($where)
77 ->order('id','desc') 114 ->order('id','desc')
78 - ->paginate(5,false,['page'=>$page]); 115 + ->paginate(5,false,['page'=>$page])
  116 + ->each(function ($item,$key){
  117 + $sum = 0;
  118 + foreach ($item->getRelation('goods')as $key => $value){
  119 + $sum += $value['total_num'];
  120 + $value->visible(['goods_name','goods_attr','total_num','total_price']);
  121 + }
  122 + $item['total_sum'] = $sum;
  123 + $item->visible([
  124 + 'goods','total_sum','order_no','id','pay_price',
  125 + 'discount_price','pay_status','order_status','receipt_status',
  126 + 'freight_status'
  127 + ]);
  128 + });
79 $this->success('订单列表',$list); 129 $this->success('订单列表',$list);
80 } 130 }
81 131
@@ -91,36 +141,64 @@ class Order extends Api @@ -91,36 +141,64 @@ class Order extends Api
91 * @ApiReturn ({ 141 * @ApiReturn ({
92 'code':'1', 142 'code':'1',
93 'msg':'返回成功' 143 'msg':'返回成功'
  144 + 'data':
  145 + "list": [
  146 + {
  147 + "goods_id": 22, 商品id
  148 + "goods_name": "Mate 20 华为 HUAWEI ", 商品名称
  149 + "spec_type": "20", 20多规格10单规格
  150 + "sku_id": 106, 规格id
  151 + "sku_price": "6099.00", 规格单价
  152 + "sku_name": "极光色 8GB+128GB", 规格名称
  153 + "number": 2, 数量
  154 + "image_text": "", 图片
  155 + },
  156 + {
  157 + "goods_id": 23,
  158 + "goods_name": "MacBook Pro 13寸",
  159 + "spec_type": "20",
  160 + "sku_id": 66,
  161 + "sku_price": "12688.00",
  162 + "sku_name": "天空灰",
  163 + "number": 2,
  164 + "image_text": "",
  165 + }
  166 + ],
  167 + "price": "37574.00" 总价
94 }) 168 })
95 */ 169 */
96 public function orderCalculation() 170 public function orderCalculation()
97 { 171 {
98 $json = $this->request->post('data_json'); 172 $json = $this->request->post('data_json');
99 if (!$json) $this->error('data_json参数不能为空'); 173 if (!$json) $this->error('data_json参数不能为空');
100 - $json = '[{"goods_id":22,"goods_sku_id":106,"number":2},{"goods_id":23,"goods_sku_id":66,"number":2}]'; 174 +// $json = '[{"goods_id":22,"goods_sku_id":106,"number":2},{"goods_id":23,"goods_sku_id":66,"number":2}]';
101 $data = json_decode($json,true); 175 $data = json_decode($json,true);
102 $goodsmodel = new \app\api\model\Goods(); 176 $goodsmodel = new \app\api\model\Goods();
103 $skumodel = new GoodsSpec(); 177 $skumodel = new GoodsSpec();
104 $specmodel = new SpecValue(); 178 $specmodel = new SpecValue();
105 - $goods_array = [];  
106 - $sum_price = 0; 179 + $goods_array = []; //商品列表
  180 + $sum_price = 0; //总价格
107 foreach ($data as $key => $value){ 181 foreach ($data as $key => $value){
108 if (!is_numeric($value['goods_id']) || !is_numeric($value['goods_sku_id']) || !is_numeric($value['number'])){ 182 if (!is_numeric($value['goods_id']) || !is_numeric($value['goods_sku_id']) || !is_numeric($value['number'])){
109 $this->error('参数不合法'); 183 $this->error('参数不合法');
110 } 184 }
111 $goods = $goodsmodel->where('goods_id',$value['goods_id'])->field('goods_id,goods_name,image,spec_type')->find(); 185 $goods = $goodsmodel->where('goods_id',$value['goods_id'])->field('goods_id,goods_name,image,spec_type')->find();
112 if (!$goods) $this->error('商品不存在'); 186 if (!$goods) $this->error('商品不存在');
  187 + $goods->visibleAppend(['image_text']);
  188 + $goods->hidden(['image']);
113 $sku = $skumodel->where('goods_spec_id',$value['goods_sku_id']) 189 $sku = $skumodel->where('goods_spec_id',$value['goods_sku_id'])
114 ->field('goods_spec_id,spec_sku_id,goods_price')->find(); 190 ->field('goods_spec_id,spec_sku_id,goods_price')->find();
115 if (!$sku) $this->error('商品规格不存在'); 191 if (!$sku) $this->error('商品规格不存在');
  192 + $sku->unsetAppend();
  193 +
116 $goods['sku_id'] = $sku['goods_spec_id']; 194 $goods['sku_id'] = $sku['goods_spec_id'];
117 $goods['sku_price'] = $sku['goods_price']; 195 $goods['sku_price'] = $sku['goods_price'];
118 if ($goods['spec_type'] == 10){ 196 if ($goods['spec_type'] == 10){
119 - $goods['sku_name'] =''; 197 + $goods['sku_name'] =''; //规格名
120 }else{ 198 }else{
121 $ids = explode('_',$sku['spec_sku_id']); 199 $ids = explode('_',$sku['spec_sku_id']);
122 $sku_name = $specmodel->whereIn('id',$ids)->column('spec_value'); 200 $sku_name = $specmodel->whereIn('id',$ids)->column('spec_value');
123 - $goods['sku_name'] = implode(' ',$sku_name); 201 + $goods['sku_name'] = implode(' ',$sku_name); //规格名
124 } 202 }
125 $goods['number'] = $value['number']; 203 $goods['number'] = $value['number'];
126 $goods_array[] = $goods; 204 $goods_array[] = $goods;
@@ -128,4 +206,129 @@ class Order extends Api @@ -128,4 +206,129 @@ class Order extends Api
128 } 206 }
129 $this->success('下单页详情',['list'=>$goods_array,'price'=>$sum_price]); 207 $this->success('下单页详情',['list'=>$goods_array,'price'=>$sum_price]);
130 } 208 }
  209 +
  210 +
  211 + /**
  212 + * @ApiTitle (下单)
  213 + * @ApiMethod (POST)
  214 + * @ApiSummary ([{"goods_id":22,"goods_sku_id":106,"number":2},{"goods_id":23,"goods_sku_id":66,"number":2}])
  215 + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  216 + * @ApiParams (name="data_json", type="string", required=true, description="下单的商品json数据")
  217 + * @ApiParams (name="goods_id", type="integer", required=false, description="商品id 此值不传 json数组注释用")
  218 + * @ApiParams (name="goods_sku_id", type="integer", required=false, description="规格id 此值不传 json数组注释用")
  219 + * @ApiParams (name="number", type="integer", required=false, description="购买数量 此值不传 json数组注释用")
  220 + * @ApiParams (name="coupon_id", type="integer", required=true, description="优惠券id 无优惠券传0")
  221 + * @ApiParams (name="address_id", type="integer", required=true, description="地址id")
  222 + * @ApiReturn ({
  223 + 'code':'1',
  224 + 'msg':'返回成功'
  225 + 'data':
  226 +
  227 + })
  228 + */
  229 + public function addOrder()
  230 + {
  231 + $json = $this->request->post('data_json');
  232 + $coupon_id = $this->request->post('coupon_id');
  233 + $address_id = $this->request->post('address_id');
  234 +// if (!$json) $this->error('data_json参数不能为空');
  235 + $json = '[{"goods_id":22,"goods_sku_id":106,"number":2},{"goods_id":23,"goods_sku_id":66,"number":2}]';
  236 + $address = Db::name('user_address')
  237 + ->where('id',$address_id)
  238 + ->where('user_id',1)
  239 + ->find();
  240 + if (!$address) $this->error('地址不存在');
  241 + $coupon = [];
  242 + if ($coupon_id > 0){
  243 + $coupon = Db::name('user_coupon')
  244 + ->where('id',$coupon_id)
  245 + ->where('user_id',$this->auth->id)
  246 + ->find();
  247 + if (!$coupon) $this->error('优惠券不存在');
  248 + }
  249 +
  250 + $data = json_decode($json,true);
  251 + $goodsmodel = new \app\api\model\Goods();
  252 + $skumodel = new GoodsSpec();
  253 + $ordermodel = new \app\api\model\Order();
  254 + $ordergoodsmodel = new \app\api\model\OrderGoods();
  255 + $orderaddressmodel = new \app\api\model\OrderAddress();
  256 + $specmodel = new SpecValue();
  257 + $goods_array = []; //商品列表
  258 + $sum_price = 0; //总价格
  259 +
  260 + foreach ($data as $key => $value){
  261 + if (!is_numeric($value['goods_id']) || !is_numeric($value['goods_sku_id']) || !is_numeric($value['number'])){
  262 + $this->error('参数不合法');
  263 + }
  264 + $goods = $goodsmodel->where('goods_id',$value['goods_id'])->find();
  265 + if (!$goods) $this->error('商品不存在');
  266 + $sku = $skumodel->where('goods_spec_id',$value['goods_sku_id'])->find();
  267 + if (!$sku) $this->error('商品规格不存在');
  268 +
  269 + if ($goods['spec_type'] == 10){
  270 + $sku_name =''; //规格名
  271 + }else{
  272 + $ids = explode('_',$sku['spec_sku_id']);
  273 + $sku_name = $specmodel->whereIn('id',$ids)->column('spec_value');
  274 + $sku_name = implode(' ',$sku_name); //规格名
  275 + }
  276 + $goods_array[] = [
  277 + 'goods_id' => $goods['goods_id'],
  278 + 'goods_name' => $goods['goods_name'],
  279 + 'image' => $goods['image'],
  280 + 'deduct_stock_type' => $goods['deduct_stock_type'],
  281 + 'spec_type' => $goods['spec_type'],
  282 + 'spec_sku_id' => $sku['spec_sku_id'],
  283 + 'goods_spec_id' => $sku['goods_spec_id'],
  284 + 'goods_attr' => $sku_name,
  285 + 'content' => $goods['content'],
  286 + 'goods_no' => $goods['number'],
  287 + 'goods_price' => $sku['goods_price'],
  288 + 'goods_weight' => $sku['goods_weight'],
  289 + 'total_num' => $value['number'],
  290 + 'total_price' => bcmul($sku['goods_price'],$value['number'],2),
  291 + 'user_id' => 1,
  292 + ];
  293 + $sum_price = bcadd($sum_price,bcmul($sku['goods_price'],$value['number'],2),2);
  294 + }
  295 +
  296 + if ($coupon !== [] && $coupon['full_price'] < $sum_price) $this->error('优惠券不可使用');
  297 +
  298 + $order_address = [
  299 + 'name' => $address['username'],
  300 + 'phone' => $address['mobile'],
  301 + 'address' => $address['address'],
  302 + 'detail' => $address['address_detail'],
  303 + 'lng' => $address['lng'],
  304 + 'lat' => $address['lat'],
  305 + 'user_id' => 1,
  306 + ];
  307 + $order_no = 'LQ-'.time().mt_rand(1000,9999);
  308 + $couponprice = isset($coupon['price'])?$coupon['price']:0;
  309 + $order = [
  310 + 'order_no' => $order_no,
  311 + 'total_price' => $sum_price,
  312 + 'discount_price' => $couponprice,
  313 + 'pay_price' => bcadd($sum_price,$couponprice,2),
  314 + 'user_id' => 1
  315 + ];
  316 +
  317 + try {
  318 + Db::startTrans();
  319 + $ordermodel->save($order);
  320 + foreach ($goods_array as $key => &$value){
  321 + $value['order_id'] = $ordermodel->id;
  322 + }
  323 + $ordergoodsmodel->saveAll($goods_array);
  324 + $order_address['order_id'] = $ordermodel->id;
  325 + $orderaddressmodel->save($order_address);
  326 + Db::commit();
  327 + }catch (PDOException $e){
  328 + Db::rollback();
  329 + $this->error($e->getMessage());
  330 + }
  331 +
  332 + $this->success('下单页详情',$ordermodel->id);
  333 + }
131 } 334 }
@@ -11,6 +11,15 @@ class Order extends Model @@ -11,6 +11,15 @@ class Order extends Model
11 protected $name = 'litestore_order'; 11 protected $name = 'litestore_order';
12 protected $createTime = 'createtime'; 12 protected $createTime = 'createtime';
13 protected $updateTime = 'updatetime'; 13 protected $updateTime = 'updatetime';
  14 + protected $append = [
  15 + 'createtime_text'
  16 + ];
  17 +
  18 + public function getCreatetimeTextAttr($value,$data)
  19 + {
  20 + $value = !empty($data['createtime']) ? date('Y-m-d',$data['createtime']) : '';
  21 + return $value;
  22 + }
14 23
15 public function goods() 24 public function goods()
16 { 25 {
  1 +<?php
  2 +
  3 +
  4 +namespace app\api\model;
  5 +
  6 +
  7 +use think\Model;
  8 +
  9 +class OrderAddress extends Model
  10 +{
  11 + protected $name = 'litestore_order_address';
  12 + protected $createTime = 'createtime';
  13 + protected $updateTime = '';
  14 +}
@@ -11,4 +11,13 @@ class OrderGoods extends Model @@ -11,4 +11,13 @@ class OrderGoods extends Model
11 protected $name = 'litestore_order_goods'; 11 protected $name = 'litestore_order_goods';
12 protected $createTime = 'createtime'; 12 protected $createTime = 'createtime';
13 protected $updateTime = ''; 13 protected $updateTime = '';
  14 + protected $append = [
  15 + 'image_text'
  16 + ];
  17 +
  18 + public function getImageTextAttr($value,$data)
  19 + {
  20 + $value = !empty($data['image']) ? cdnurl($data['image'],true) : '';
  21 + return $value;
  22 + }
14 } 23 }
@@ -2347,4 +2347,83 @@ abstract class Model implements \JsonSerializable, \ArrayAccess @@ -2347,4 +2347,83 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
2347 self::event('after_delete', $callback, $override); 2347 self::event('after_delete', $callback, $override);
2348 } 2348 }
2349 2349
  2350 +
  2351 + /**
  2352 + * 删除附加属性
  2353 + * @author xchen
  2354 + */
  2355 + public function unsetAppend($append = []){
  2356 + if(!count($append)){
  2357 + $this->append = [];
  2358 + }else{
  2359 + foreach ($this->append as $key => $value){
  2360 + if (in_array($value,$append)){
  2361 + unset($this->append[$key]);
  2362 + }
  2363 + }
  2364 + }
  2365 + }
  2366 +
  2367 + /**
  2368 + * 展示附加属性
  2369 + * @author xchen
  2370 + */
  2371 + public function visibleAppend($append = []){
  2372 + if(!count($append)){
  2373 + return;
  2374 + }else{
  2375 + foreach ($this->append as $key => $value){
  2376 + if (!in_array($value,$append)){
  2377 + unset($this->append[$key]);
  2378 + }
  2379 + }
  2380 + }
  2381 + }
  2382 +
  2383 + /**
  2384 + * 显示关联附加属性
  2385 + * @author xchen
  2386 + */
  2387 + public function visibleRelationAppend($name = null,$append = []){
  2388 + if(is_null($name)){
  2389 + return;
  2390 + }
  2391 + elseif ($append == []){
  2392 + return;
  2393 + }
  2394 + elseif(array_key_exists($name, $this->relation)){
  2395 + foreach($this->relation[$name]->append as $k => $v){
  2396 + if (!in_array($v,$append)){
  2397 + unset($this->relation[$name]->append[$k]);
  2398 + }
  2399 + }
  2400 + }
  2401 + }
  2402 +
  2403 + /**
  2404 + * 删除关联附加属性
  2405 + * @author xchen
  2406 + */
  2407 + public function unsetRelationAppend($name = null,$append = []){
  2408 + if(is_null($name)){
  2409 + return;
  2410 + }
  2411 + else if(array_key_exists($name, $this->relation)){
  2412 + if ($this->relation[$name]){
  2413 + if ($append){
  2414 + foreach($this->relation[$name]->append as $k => $v){
  2415 + if (in_array($v,$append)){
  2416 + unset($this->relation[$name]->append[$k]);
  2417 + }
  2418 + }
  2419 + }else{
  2420 + $this->relation[$name]->append = [];
  2421 + }
  2422 +
  2423 + }
  2424 + }
  2425 + else{
  2426 + return;
  2427 + }
  2428 + }
2350 } 2429 }