Order.php
2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
namespace app\api\controller;
use app\api\model\GoodsSpec;
use app\api\model\SpecValue;
use app\common\controller\Api;
/**
* 订单
*/
class Order extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
/**
* @ApiTitle (下单页面)
* @ApiSummary ([{"goods_id":22,"goods_sku_id":106,"number":2},{"goods_id":23,"goods_sku_id":66,"number":2}])
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="data_json", type="string", required=true, description="下单的商品json数据")
* @ApiParams (name="goods_id", type="integer", required=false, description="商品id 此值不传 json数组注释用")
* @ApiParams (name="goods_sku_id", type="integer", required=false, description="规格id 此值不传 json数组注释用")
* @ApiParams (name="number", type="integer", required=false, description="购买数量 此值不传 json数组注释用")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function orderCalculation()
{
$json = $this->request->post('data_json');
if (!$json) $this->error('data_json参数不能为空');
$json = '[{"goods_id":22,"goods_sku_id":106,"number":2},{"goods_id":23,"goods_sku_id":66,"number":2}]';
$data = json_decode($json,true);
$goodsmodel = new \app\api\model\Goods();
$skumodel = new GoodsSpec();
$specmodel = new SpecValue();
$goods_array = [];
$sum_price = 0;
foreach ($data as $key => $value){
if (!is_numeric($value['goods_id']) || !is_numeric($value['goods_sku_id']) || !is_numeric($value['number'])){
$this->error('参数不合法');
}
$goods = $goodsmodel->where('goods_id',$value['goods_id'])->field('goods_id,goods_name,image,spec_type')->find();
if (!$goods) $this->error('商品不存在');
$sku = $skumodel->where('goods_spec_id',$value['goods_sku_id'])
->field('goods_spec_id,spec_sku_id,goods_price')->find();
if (!$sku) $this->error('商品规格不存在');
$goods['sku_id'] = $sku['goods_spec_id'];
$goods['sku_price'] = $sku['goods_price'];
if ($goods['spec_type'] == 10){
$goods['sku_name'] ='';
}else{
$ids = explode('_',$sku['spec_sku_id']);
$sku_name = $specmodel->whereIn('id',$ids)->column('spec_value');
$goods['sku_name'] = implode(' ',$sku_name);
}
$goods['number'] = $value['number'];
$goods_array[] = $goods;
$sum_price = bcadd($sum_price,bcmul($sku['goods_price'],$value['number'],2),2);
}
$this->success('下单页详情',['list'=>$goods_array,'price'=>$sum_price]);
}
}