审查视图

application/api/controller/Rider.php 13.8 KB
李忠强 authored
1 2 3 4 5 6
<?php


namespace app\api\controller;

李忠强 authored
7
use app\api\model\GoodsComment;
李忠强 authored
8
use app\api\model\RiderOrder;
李忠强 authored
9
use app\api\model\UserMoneyLog;
李忠强 authored
10
use app\common\controller\Api;
李忠强 authored
11 12
use think\Config;
use think\Db;
李忠强 authored
13 14 15 16 17 18 19

/**
 * 骑手
 */
class Rider extends Api
{
    protected $noNeedRight = ['*'];
李忠强 authored
20
    protected $noNeedLogin = ['service'];
李忠强 authored
21 22 23 24 25

    /**
     * @ApiTitle    (骑手订单页)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
李忠强 authored
26
     * @ApiParams   (name=type, type=integer, required=true, description="1配送中2=已完成")
李忠强 authored
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
     * @ApiParams   (name=page, type=integer, required=true, description="页数")
     * @ApiParams   (name=lat, type=integer, required=true, description="骑手纬度")
     * @ApiParams   (name=lng, type=integer, required=true, description="骑手经度")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    'data':
        "total": 2,
        "per_page": 5,
        "current_page": 1,
        "last_page": 1,
        "data": [
            {
            "id": 1,
            "distance": 111.263, 距离
            "address": {
                "name": "1", 收件人
                "phone": "13549059988", 电话号码
                "detail": "阿松大", 地址
                "address": "阿松大", 门牌号
            },
            "orderdetail": {
                "order_no": "LQ-16420622494781", 订单编号
                "createtime_text": ""
            }
            }
        ]
    })
     */
    public function index()
    {
李忠强 authored
58
        $type = $this->request->post('type');
李忠强 authored
59 60 61
        $page = $this->request->post('page',1);
        $lat = $this->request->post('lat');
        $lng = $this->request->post('lng');
李忠强 authored
62
        if (!in_array($type,[1,2])) $this->error('type参数不合法');
李忠强 authored
63 64 65 66
        if (!is_numeric($page)) $this->error('页数不合法');
        if (!is_numeric($lat)) $this->error('纬度不合法');
        if (!is_numeric($lng)) $this->error('经度不合法');
        $model = new RiderOrder();
李忠强 authored
67 68 69 70 71 72 73 74
        if ($type == 1){
            $EARTH=6378.137; //地球半径
            $PI=3.14; //PI值 圆周率
            $list = $model
                ->with(['address','orderdetail'])
                ->where('fa_rider_order.user_id',$this->auth->id)
                ->where('fa_rider_order.status','1')
                ->field('Round((2 * '.$EARTH.'* ASIN(SQRT(POW(SIN('.$PI.'*('.$lat.'-lat)/360),2)
李忠强 authored
75 76
            +COS('.$PI.'*'.$lat.'/180)* COS(lat * '.$PI.'/180)
            *POW(SIN('.$PI.'*('.$lng.'-lng)/360),2)))),3) as distance')
李忠强 authored
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
                ->order('distance')
                ->paginate(5,false,['page'=>$page])
                ->each(function ($item,$key){
                    $item->getRelation('orderdetail')->visible(['order_no']);
                    $item->getRelation('address')->visible(['name','phone','address','detail']);
                    $item->visible(['orderdetail','address','distance','id']);
                });
        }else{
            $list = $model
                ->with(['address','orderdetail'])
                ->where('fa_rider_order.user_id',$this->auth->id)
                ->where('fa_rider_order.status','2')
                ->order('fa_rider_order.id','desc')
                ->paginate(5,false,['page'=>$page])
                ->each(function ($item,$key){
                    $item->getRelation('orderdetail')->visible(['order_no']);
                    $item->getRelation('address')->visible(['name','address','detail']);
                    $item->visible(['orderdetail','address','distance','id']);
                });
        }
李忠强 authored
98 99
        $this->success('订单列表',$list);
    }
李忠强 authored
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141


    /**
     * @ApiTitle    (骑手个人页面)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    'data':
        "avatar": 头像,
        "nickname": 昵称,
        "money": 余额,
        "today_price": 今日所得,
        "seven_price": 七日所得,
        "all_price": 历史所得,
        "today_order": 今日订单数,
        "seven_order": 七日订单数,
        "all_order": 历史订单数,
    })
     */
    public function userIndex()
    {
        $data = [
            'avatar' => cdnurl($this->auth->avatar,true),
            'nickname' => $this->auth->nickname,
            'money' => $this->auth->money
        ];
        $model = new RiderOrder();
        $todaytime = strtotime(date('Y-m-d',time()));
        $seventime = $todaytime-86400*7;
        $today = $model
            ->where('user_id',$this->auth->id)
            ->where('status','2')
            ->where('sendtime','>',$todaytime)
            ->sum('price');
        $todaycount = $model
            ->where('user_id',$this->auth->id)
            ->where('status','2')
            ->where('sendtime','>',$todaytime)
            ->count();
        $seven = $model
何书鹏 authored
142
            ->where('user_id',$this->auth->id)
李忠强 authored
143
            ->where('status','2')
何书鹏 authored
144
            ->where('sendtime','>',$seventime)
李忠强 authored
145 146
            ->sum('price');
        $sevencount = $model
何书鹏 authored
147
            ->where('user_id',$this->auth->id)
李忠强 authored
148
            ->where('status','2')
何书鹏 authored
149
            ->where('sendtime','>',$seventime)
李忠强 authored
150 151
            ->count();
        $all = $model
何书鹏 authored
152 153
            ->where('user_id',$this->auth->id)
            ->where('status','2')
李忠强 authored
154 155
            ->sum('price');
        $allcount = $model
何书鹏 authored
156 157
            ->where('user_id',$this->auth->id)
            ->where('status','2')
李忠强 authored
158 159 160 161 162 163 164 165 166
            ->count();
        $data['today_price'] = $today;
        $data['seven_price'] = $seven;
        $data['all_price'] = $all;
        $data['today_order'] = $todaycount;
        $data['seven_order'] = $sevencount;
        $data['all_order'] = $allcount;
        $this->success('骑手个人页',$data);
    }
李忠强 authored
167 168 169


    /**
李忠强 authored
170
     * @ApiTitle    (骑手未送达订单详情)
李忠强 authored
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiHeaders  (name=id, type=integer, required=true, description="骑手订单id")
     * @ApiHeaders  (name=lat, type=float, required=true, description="纬度")
     * @ApiHeaders  (name=lng, type=float, required=true, description="经度")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    'data':
    "id": 7,
    "order_no": "LQ-16420622494781", 订单编号
    "distance": 111.3195, 距离
    "address": {
        "id": 1,
        "name": "1", 姓名
        "phone": "13549059988",
        "detail": "阿松大1", 门牌号
        "address": "阿松大", 地址
        "order_id": 7,
        "user_id": 1,
        "lat": "116.397128",
        "lng": "39.916527",
        "createtime": 0,
        "province_id": 1,
        "city_id": 2,
        "region_id": 3,
        "mobile_hide": "135****9988"  电话号码
    },
    "goods": [
        {
        "goods_name": "Mate 20 华为 HUAWEI ",
        "goods_attr": "极光色 8GB+128GB",
        "image_text": "http://temporaryfood.qiniu.broing.cn123132"
        },
        {
        "goods_name": "MacBook Pro 13寸",
        "goods_attr": "天空灰",
        "image_text": "http://temporaryfood.qiniu.broing.cn123132"
        }
    ],
    "createtime_text": ""
    })
     */
    public function orderDetail()
    {
        $id = $this->request->post('id');
        $lat = $this->request->post('lat');
        $lng = $this->request->post('lng');
        $rider_order = RiderOrder::get($id);
        $ordermodel = new \app\api\model\Order();
        $order = $ordermodel
            ->with(['address','goods'])
            ->where('fa_litestore_order.id',$rider_order['order_id'])
            ->find();
        $order->visible(['order_no','address','goods','id','distance']);
        foreach ($order->getRelation('goods') as $key => $value){
李忠强 authored
227
            $value->visible(['goods_name','goods_attr','total_num']);
李忠强 authored
228 229 230 231 232 233 234 235 236 237 238
        }
        $lat1 = $order['address']['lat'];
        $lng1 = $order['address']['lng'];
        $lat2 = $lat;
        $lng2 = $lng;
        $order['distance'] = getDistance($lat1,$lng1,$lat2,$lng2);
        $this->success('骑手个人页',$order);
    }


    /**
李忠强 authored
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
     * @ApiTitle    (骑手已送达订单详情)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiHeaders  (name=id, type=integer, required=true, description="骑手订单id")
     * @ApiReturn   ({
    'code':'1',
    'msg':'订单详情'
    'data':
    "id": 7,
    "order_no": "LQ-16420622494781", 订单编号
    "distance": 111.3195, 距离
    "address": {
        "id": 1,
        "name": "1", 姓名
        "phone": "13549059988",
        "detail": "阿松大1", 门牌号
        "address": "阿松大", 地址
        "order_id": 7,
        "user_id": 1,
        "lat": "116.397128",
        "lng": "39.916527",
        "createtime": 0,
        "province_id": 1,
        "city_id": 2,
        "region_id": 3,
        "mobile_hide": "135****9988"  电话号码
    },
    "goods": [
        {
        "goods_name": "Mate 20 华为 HUAWEI ",
        "goods_attr": "极光色 8GB+128GB",
李忠强 authored
270
        "total_num": "1", 购买数量
李忠强 authored
271 272 273 274 275
        "image_text": "http://temporaryfood.qiniu.broing.cn123132"
        },
        {
        "goods_name": "MacBook Pro 13寸",
        "goods_attr": "天空灰",
李忠强 authored
276
        "total_num": "1",
李忠强 authored
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
        "image_text": "http://temporaryfood.qiniu.broing.cn123132"
        }
    ],
    "createtime_text": ""
    })
     */
    public function orderOverDetail()
    {
        $id = $this->request->post('id');
        $rider_order = RiderOrder::get($id);
        $ordermodel = new \app\api\model\Order();
        $order = $ordermodel
            ->with(['address','goods'])
            ->where('fa_litestore_order.id',$rider_order['order_id'])
            ->find();
李忠强 authored
292
        $order->visible(['order_no','address','goods','id','distance','comment']);
李忠强 authored
293
        foreach ($order->getRelation('goods') as $key => $value){
李忠强 authored
294
            $value->visible(['goods_name','goods_attr','total_num']);
李忠强 authored
295 296 297 298 299 300 301 302 303
        }
        $comment = new GoodsComment();
        $content = $comment->where('order_id',$order->id)->select();
        $order['comment'] = $content;
        $this->success('订单详情',$order);
    }


    /**
李忠强 authored
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
     * @ApiTitle    (骑手已送达)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiHeaders  (name=id, type=integer, required=true, description="骑手订单id")
     * @ApiReturn   ({
    'code':'1',
    'msg':'送达成功'
    })
     */
    public function orderService()
    {
        $id = $this->request->post('id');
        $rider_order = RiderOrder::get($id);
        $ordermodel = new \app\api\model\Order();
        $order = $ordermodel
            ->where('id',$rider_order['order_id'])
            ->find();
        if ($order['rider_status'] == 20) $this->error('该订单已送达');
        $order->rider_status = '20';
何书鹏 authored
323
        $order->save();
李忠强 authored
324 325
        $rider_order->status = '2';
        $rider_order->sendtime = time();
何书鹏 authored
326
        $rider_order->save();
李忠强 authored
327
        $user = $this->auth->getUser();
李忠强 authored
328 329 330
        $data = [
            'user_id' => $this->auth->id,
            'money' => $order->express_price,
李忠强 authored
331 332
            'before' => $user->money,
            'after' => $order->express_price+$user->money,
李忠强 authored
333 334
            'memo' => '订单运费',
        ];
李忠强 authored
335 336 337
        $user->setInc('money',$order->express_price);
        $moneymodel = new UserMoneyLog();
        $moneymodel->isUpdate(false)->save($data);
李忠强 authored
338 339 340 341 342 343 344 345 346 347 348
        $this->success('送达成功',$order);
    }


    /**
     * @ApiTitle    (骑手提现)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiHeaders  (name=money, type=integer, required=true, description="提现金额")
     * @ApiReturn   ({
    'code':'1',
李忠强 authored
349
    'msg':'提现成功'
李忠强 authored
350 351 352 353 354 355 356 357 358
    })
     */
    public function withdraw()
    {
        $money = $this->request->post('money');
        $user = $this->auth->getUser();
        if ($user['money'] < $money) $this->error('可提现金额不足');
        $data = [
            'user_id' => $this->auth->id,
何书鹏 authored
359
            'money' => -$money,
何书鹏 authored
360 361 362
            'before' => $user->money,
            'after' => $user->money-$money,
            'memo' => '提现'
李忠强 authored
363
        ];
何书鹏 authored
364 365
        UserMoneyLog::create($data);
        $user->setDec('money',$money);
李忠强 authored
366 367 368
        $this->success('提现成功');
    }
李忠强 authored
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
    /**
     * @ApiTitle    (骑手提现页面)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn   ({
    'code':'1',
    'msg':'提现成功'
    })
     */
    public function withdrawDetail()
    {
        $data = [
            'money' => $this->auth->money,
            'content' => Config::get('site.withdraw_content')
        ];
        $this->success('提现成功',$data);
    }
李忠强 authored
387 388 389 390 391 392

    /**
     * @ApiTitle    (联系客服)
     * @ApiMethod   (POST)
     * @ApiReturn   ({
    'code':'1',
李忠强 authored
393
    'msg':'客服信息'
李忠强 authored
394 395 396 397 398 399 400 401 402 403 404
    })
     */
    public function service()
    {
        $data = [
            'qrcode' => cdnurl(Config::get('site.work_qrcode'),true),
            'time' => Config::get('site.work_time'),
            'mobile' => Config::get('site.work_mobile')
        ];
        $this->success('客服信息',$data);
    }
李忠强 authored
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425

    /**
     * @ApiTitle    (余额明细)
     * @ApiMethod   (POST)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name=page, type=integer, required=true, description="页数")
     * @ApiReturn   ({
    'code':'1',
    'msg':'余额明细'
    })
     */
    public function moneyList()
    {
        $page = $this->request->post('page',1);
        $model = new UserMoneyLog();
        $list = $model
            ->where('user_id',$this->auth->id)
            ->order('id','desc')
            ->paginate(10,false,['page'=>$page]);
        $this->success('余额明细',$list);
    }
李忠强 authored
426
}