审查视图

application/api/controller/Order.php 15.7 KB
王智 authored
1 2 3 4 5 6 7 8 9 10 11 12
<?php
/**
 * Created by PhpStorm.
 * User: 86132
 * Date: 2020/11/18
 * Time: 10:07
 */

namespace app\api\controller;


use app\common\controller\Api;
王智 authored
13
use think\Db;
王智 authored
14 15 16 17 18 19 20 21 22 23 24 25

/**
 * 订单板块接口
 */
class Order extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * 订单板块接口
王智 authored
26 27
     * @ApiTitle    (生成订单--有更改)
     * @ApiSummary  (生成订单--有更改)
王智 authored
28
     * @ApiMethod   (POST)
王智 authored
29 30 31 32 33
     * @ApiRoute    (/api/Order/InsertOrder)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="address_id", type="integer", required=true, description="地址ID")
     * @ApiParams   (name="json", type="integer", required=true, description="json Goods_id:1,num:10")
     * @ApiParams   (name="total", type="integer", required=true, description="总价")
王智 authored
34 35
     * @ApiParams   (name="water_num", type="integer", required=true, description="使用氧气泡泡【新增参数】")
     * @ApiParams   (name="tick_id", type="integer", required=true, description="优惠券ID 未使用传0 【新增参数】")
王智 authored
36
     * @ApiParams   (name="shopcar_id", type="integer", required=true, description="购物车ID ,拼接成字符串 直接购买购物车ID 传0")
王智 authored
37 38 39 40 41
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
王智 authored
42
    "data": // 返回订单号 调用支付
王智 authored
43 44
    })
     */
王智 authored
45 46 47
    public function InsertOrder()
    {
        $UserId = $this->is_token($this->request->header());
王智 authored
48 49 50 51 52 53 54 55 56 57
//        //是否首次下单
//        $FirstPay = Db::name('order')->where('user_id', $UserId)->where('status', 'IN', '1,2,3,4,5')->find();
//        if (empty($FirstPay)) {
//            Db::name('user')->where('id', $UserId)->update(
//                [
//                    'is_new' => 1,
//                    'updatetime' => time()
//                ]
//            );
//        }
王智 authored
58 59
        $params = $this->request->param();
        //Json 转 Array
王智 authored
60
        $JsonArray = json_decode(htmlspecialchars_decode($params['json']), true);
王智 authored
61 62 63 64 65 66 67 68 69 70 71 72 73 74
        //检查商品库存
        foreach ($JsonArray as $k => $v) {
            $this->StockLook($v['Goods_id'], $v['num']);
        }
        /*添加商品到订单*/
        //查询地址
        $AddressArray = Db::name('address')->where('user_id', $UserId)->where('id', $params['address_id'])->find();
        if (empty($AddressArray)) {
            $this->error('地址获取失败', 0);
            die;
        }
        //生成订单号
        $OrderSn = $this->order_sn();
        //计算订单总价
王智 authored
75
        $Total = $this->Total($UserId, $params['total']);
王智 authored
76
        if ($params['tick_id'] != 0) {
王智 authored
77 78 79 80 81 82 83
            //更改优惠券
            Db::name('user_tick')->where('user_id', $UserId)->where('id', $params['tick_id'])->update(
                [
                    'status' => 1,
                    'updatetime' => time()
                ]
            );
王智 authored
84 85 86 87
            $tick = '是';
        } else {
            $tick = '否';
        }
王智 authored
88
        if ($params['water_num'] != 0) {
王智 authored
89
            echo '1';
王智 authored
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
            //用户消耗氧气泡泡
            $WaterUserNum = Db::name('user')->where('id', $UserId)->find();
            //更改用户积分
            Db::name('user')->where('id', $UserId)->update(['num' => $WaterUserNum['num'] - $params['water_num']]);
            //添加用户积分记录
            $WaterUserNumPay = [
                'user_id' => $UserId,
                'status' => 0,
                'type' => 11,
                'num' => $params['water_num'],
                'up_num' => $WaterUserNum['num'] - $params['water_num'],
                'createtime' => time()
            ];
            Db::name('num')->insert($WaterUserNumPay);
        }
王智 authored
105 106 107 108 109 110 111 112 113
        $data = [
            'OrderSn' => $OrderSn,
            'user_id' => $UserId,
            'name' => $AddressArray['name'],
            'mobile' => $AddressArray['mobile'],
            'city' => $AddressArray['city'],
            'address' => $AddressArray['address'],
            'address_con' => $AddressArray['address_con'],
            'status' => 0,
王智 authored
114
            'you' => $params['total'] - $Total,
王智 authored
115
            'total' => $Total,
王智 authored
116 117 118
            'water_num' => $params['water_num'],
            'tick_id' => $params['tick_id'],
            'tick' => $tick,
王智 authored
119 120
            'createtime' => time(),
            'updatetime' => time()
王智 authored
121
        ];
王智 authored
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
        $InserOrder = Db::name('order')->insert($data);
        if (!$InserOrder) {
            $this->error('订单生成失败', 0);
            die;
        }
        /*添加商品到订单详情*/
        foreach ($JsonArray as $k => $v) {
            $InserOrderCon = Db::name('order_con')->insert(
                [
                    'OrderSn' => $OrderSn,
                    'Goods_id' => $v['Goods_id'],
                    'num' => $v['num'],
                    'createtime' => time(),
                    'updatetime' => time()
                ]
            );
            if (!$InserOrderCon) {
                $this->error('订单详情生成失败', 0);
                die;
            }
        }
        /*删除购物车*/
        if ($params['shopcar_id'] != 0) {
            if (strstr($params['shopcar_id'], ',')) {
                $ShopCarID = explode(',', $params['shopcar_id']);
            } else {
                $ShopCarID = explode(' ', $params['shopcar_id']);
            }
            foreach ($ShopCarID as $k => $v) {
                $DeleteShopCar = Db::name('shopcar')->where('user_id', $UserId)->where('id', $v)->delete();
                if (!$DeleteShopCar) {
                    $this->error('购物车删除失败', 0);
                    die;
                }
            }
        }
王智 authored
158 159 160 161 162 163 164
        //查询新用户优惠
        $UserInfo = Db::name('user')->where('id', $UserId)->find();
        if ($UserInfo['is_new'] == 1) {
            Db::name('user')->where('id', $UserId)->update([
                'is_new' => 0
            ]);
        }
王智 authored
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
        //返回订单号
        $this->success('成功', $OrderSn);
    }


    /**
     * 订单板块接口
     * @ApiTitle    (我的订单)
     * @ApiSummary  (我的订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Order/MyOrder)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="status", type="integer", required=true, description="订单状态:0=待支付,1=待发货,2=待收货,3=已完成,4=退款/售后,5=退款/售后完成,6=已取消,9=全部")
     * @ApiParams   (name="pages", type="integer", required=true, description="pages")
     * @ApiParams   (name="rows", type="integer", required=true, description="rows")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": // 返回订单号 调用支付
    })
     */
    public function MyOrder()
    {
        $UserId = $this->is_token($this->request->header());
        $params = $this->request->param();
        $map = [];
        if ($params['status'] != 9) {
            $map['status'] = ['EQ', $params['status']];
        }
        $OrderArray = Db::name('order')
            ->where('user_id', $UserId)
            ->where($map)
            ->order('updatetime desc')
            ->select();
        if (empty($OrderArray)) {
            $data = [
                'Count' => 0,
                'List' => []
            ];
        } else {
            foreach ($OrderArray as $k => $v) {
王智 authored
208
                $List[$k]['Id'] = $v['id'];
王智 authored
209
                $List[$k]['OrderSn'] = $v['OrderSn'];
王智 authored
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
                $List[$k]['Status'] = $v['status'];
                //支付时间
                $PayTime = Db::name('pay_order')->where('user_id', $UserId)->where('OrderSn', $v['OrderSn'])->find();
                $List[$k]['PayTime'] = $PayTime['pay_time'];
                $List[$k]['Total'] = $v['total'];
                $OrderConArray = Db::name('order_con')
                    ->alias('o')
                    ->where('o.OrderSn', $v['OrderSn'])
                    ->join('goods g', 'g.id=o.Goods_id')
                    ->field('o.Goods_id,g.title,g.image,g.speci,g.price,o.num')
                    ->select();
                if (empty($OrderConArray)) {
                    $this->error('订单拉取失败', 0);
                } else {
                    foreach ($OrderConArray as $key => $value) {
                        $List[$k]['GoodsArray'][$key]['Goods_id'] = $value['Goods_id'];
                        $List[$k]['GoodsArray'][$key]['Title'] = $value['title'];
                        $List[$k]['GoodsArray'][$key]['Image'] = cdnurl($value['image']);
                        $List[$k]['GoodsArray'][$key]['Speci'] = $value['speci'];
                        $List[$k]['GoodsArray'][$key]['Price'] = $value['price'];
                        $List[$k]['GoodsArray'][$key]['Num'] = $value['num'];
                    }
                }
王智 authored
233
            }
王智 authored
234 235 236 237
            $data = [
                'Count' => count($OrderArray),
                'List' => $this->page_array($params['rows'], $params['pages'], $List, 0)
            ];
王智 authored
238
        }
王智 authored
239
        $this->success('成功', $data);
王智 authored
240
    }
王智 authored
241 242 243 244


    /**
     * 订单板块接口
王智 authored
245 246
     * @ApiTitle    (操作订单)
     * @ApiSummary  (操作订单)
王智 authored
247
     * @ApiMethod   (POST)
王智 authored
248
     * @ApiRoute    (/api/Order/OrderCao)
王智 authored
249
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
王智 authored
250 251
     * @ApiParams   (name="OrderSn", type="integer", required=true, description="订单号")
     * @ApiParams   (name="status", type="integer", required=true, description="6=取消订单,3=确认收货")
王智 authored
252 253 254 255 256
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
王智 authored
257
    "data":
王智 authored
258 259
    })
     */
王智 authored
260
    public function OrderCao()
王智 authored
261 262 263
    {
        $UserId = $this->is_token($this->request->header());
        $params = $this->request->param();
王智 authored
264 265 266 267 268 269 270 271 272
        if ($params['status'] == 6) {
            $OrderInfo = Db::name('order')->where('user_id', $UserId)->where('OrderSn', $params['OrderSn'])->find();
            if ($OrderInfo['you'] > 0) {
                Db::name('user')->where('id', $UserId)->update(
                    [
                        'is_new' => 1
                    ]
                );
            }
王智 authored
273
            //判断 待支付订单 取消 不减少积分
王智 authored
274
            $UserNum = Db::name('user')->where('id', $UserId)->find();
王智 authored
275
            if ($OrderInfo['status'] != 0) {
王智 authored
276 277
                //减少积分
                Db::name('user')->where('id', $UserId)->update(['num' => $UserNum['num'] - floor($OrderInfo['total']) + $OrderInfo['water_num']]);
王智 authored
278 279 280
                //13=订单退款积分扣除
                $NumData = [
                    'user_id' => $UserId,
王智 authored
281 282
                    'status' => 0,
                    'type' => 13,
王智 authored
283 284 285 286 287
                    'num' =>floor($OrderInfo['total']),
                    'up_num'=>$UserNum['num'] - floor($OrderInfo['total']) + $OrderInfo['water_num'],
                    'createtime'=>time()
                ];
            } else {
王智 authored
288
                //消耗积分归还
王智 authored
289 290 291 292
                Db::name('user')->where('id', $UserId)->update(['num' => $UserNum['num'] + $OrderInfo['water_num']]);
                //12=订单退还积分
                $NumData = [
                    'user_id' => $UserId,
王智 authored
293 294
                    'status' => 1,
                    'type' => 12,
王智 authored
295 296 297 298
                    'num' =>$OrderInfo['water_num'],
                    'up_num'=>$UserNum['num'] + $OrderInfo['water_num'],
                    'createtime'=>time()
                ];
王智 authored
299
            }
王智 authored
300
            Db::name('num')->insert($NumData);
王智 authored
301 302 303 304
            //优惠券归还
            if ($OrderInfo['tick_id'] != 0) {
                Db::name('user_tick')->where('id', $OrderInfo['tick_id'])->update(['status' => 0]);
            }
王智 authored
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
        } else {
            //更改上级活动钱包减少待入账 增加待提现
            $FuserId = Db::name('group')->where('user_id', $UserId)->value('fuser_id');
            if (!empty($FuserId)) {
                $HuodongMoney = Db::name('huodong_money')->where('user_id', $FuserId)->find();
                $Total = Db::name('order')->where('OrderSn', $params['OrderSn'])->value('total');
                $res2 = Db::name('huodong_money')->where('user_id', $FuserId)->update(
                    [
                        'befor_money' => $HuodongMoney['befor_money'] - $Total,
                        'befor_tixian' => $HuodongMoney['befor_tixian'] + $Total
                    ]
                );
                if (!$res2) {
                    $this->error('更改上级钱包失败', 0);
                    die;
                }
王智 authored
321
            }
王智 authored
322 323
            //更改收益为已入账
            Db::name('money')->where('OrderSn', $params['OrderSn'])->update(['status' => 1]);
王智 authored
324
        }
王智 authored
325 326 327 328 329
        $res = Db::name('order')->where('user_id', $UserId)->where('OrderSn', $params['OrderSn'])->update(
            [
                'status' => $params['status']
            ]
        );
王智 authored
330
        $this->success('成功', 1);
王智 authored
331
    }
王智 authored
332 333 334 335


    /**
     * 订单板块接口
王智 authored
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
     * @ApiTitle    (申请售后)
     * @ApiSummary  (申请售后)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Order/Tuikuan)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="OrderSn", type="integer", required=true, description="订单号")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data":
    })
     */
    public function Tuikuan()
    {
        $UserId = $this->is_token($this->request->header());
        $params = $this->request->param();
王智 authored
354 355 356 357
        $data = [
            'user_id' => $UserId,
            'OrderSn' => $params['OrderSn'],
            'createtime' => time()
王智 authored
358
        ];
王智 authored
359 360 361 362 363 364 365 366 367 368 369 370
        $res = Db::name('tuikuan')->insert($data);
        if (!$res) {
            $this->error('申请失败', 0);
            die;
        }
        //修改订单状态
        $res2 = Db::name('order')->where('OrderSn', $params['OrderSn'])->update(
            [
                'status' => 4,
                'updatetime' => time()
            ]
        );
王智 authored
371 372
        $GuanYiCloud = new GuanYiCloud();
        $GuanYiCloud->GetOrderOid($params['OrderSn']);
王智 authored
373 374 375 376 377 378
        if (!$res2) {
            $this->error('订单状态更改失败', 0);
            die;
        } else {
            $this->success('成功', 1);
        }
王智 authored
379 380 381 382 383
    }


    /**
     * 订单板块接口
王智 authored
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
     * @ApiTitle    (物流查询)
     * @ApiSummary  (物流查询)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Order/LogisticsInquiry)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="OrderSn", type="integer", required=true, description="订单号")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data":
    })
     */
    public function LogisticsInquiry()
    {
        $this->is_token($this->request->header());
        $params = $this->request->param();
        $data = $this->kuaidi100($params['OrderSn']);
王智 authored
403
        Db::name('order')->where('OrderSn', $params['OrderSn'])->value('ems_company');
王智 authored
404 405
        $this->success('成功', $data);
    }
王智 authored
406
}