Shop.php 23.6 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
<?php
/**
 * Created by PhpStorm.
 * auther: sgj
 * Date: 2019/10/8
 * Time: 18:18
 */

namespace app\api\controller;


use addons\third\model\Third;
use app\admin\model\Goods;
use app\admin\model\Orders;
use app\admin\model\UserAddress;
use app\common\controller\Api;
use app\common\model\ScoreLog;
use EasyWeChat\Foundation\Application;
use think\Db;

/**
 * 积分商城接口
 */
class Shop extends Api
{

    protected $noNeedLogin = ['listMissionType'];
    protected $noNeedRight = ['*'];

    /**
     * @ApiTitle    (商品首页)
     * @ApiSummary  (商品首页)
     * @ApiMethod (POST)
     * @ApiSector   (三开相关接口)
     * @ApiReturnParams   (name="goods_avatar_text", type="string", required=true, description="商品图片")
     * @ApiReturnParams   (name="good_name", type="string", required=true, description="商品名称")
     * @ApiReturnParams   (name="price", type="string", required=true, description="商品积分价格")
     * @ApiReturnParams   (name="money", type="string", required=true, description="商品现金价格")
     * @ApiReturnParams   (name="good_data", type="string", required=true, description="1:普通商品2:现金商品(无法用现金支付)")
     * @ApiReturnParams   (name="id", type="string", required=true, description="商品id")
     * @ApiReturnParams   (name="msg", type="string", required=true, description="提示语")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1570532904",
    "data": {
    "total": 1,
    "per_page": "10",
    "current_page": 1,
    "last_page": 1,
    "data": [
    {
    "id": 1,
    "goods_avatar": "/uploads/20191008/FlacV2rtDNhzEZrV4bb60KH_9s9u.jpg",
    "good_name": "保湿面霜",
    "price": 11,
    "good_content": "保湿面霜一瓶&nbsp;<img src=\"http://pyffam7jw.bkt.clouddn.com/uploads/20191008/Fs6sUDk9_iZh5Zhe-xVX8aMeI4Rn.jpg\" alt=\"\" />",
    "show_switch": 1,
    "createtime": 1570532123,
    "weigh": 1,
    "deletetime": null,
    "goods_avatar_text": "http://pyffam7jw.bkt.clouddn.com/uploads/20191008/FlacV2rtDNhzEZrV4bb60KH_9s9u.jpg"
    }
    ]
    }
    })
     */
    public function index(){
        $Goods=new Goods();
        $goods=$Goods->getIndexGoods();
        $this->success('',$goods);
    }


    /**
     * @ApiTitle    (商品详情)
     * @ApiSummary  (商品详情)
     * @ApiMethod (POST)
     * @ApiReturnParams   (name="goods_avatar_text", type="string", required=true, description="商品图片")
     * @ApiReturnParams   (name="good_name", type="string", required=true, description="商品名称")
     * @ApiReturnParams   (name="price", type="string", required=true, description="商品价格")
     * @ApiReturnParams   (name="id", type="string", required=true, description="商品id")
     * @ApiReturnParams   (name="good_content", type="string", required=true, description="商品详情")
     * @ApiReturnParams   (name="msg", type="string", required=true, description="提示语")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1570533464",
    "data": {
    "id": 1,
    "goods_avatar": "/uploads/20191008/FlacV2rtDNhzEZrV4bb60KH_9s9u.jpg",
    "good_name": "保湿面霜",
    "price": 11,
    "good_content": "保湿面霜一瓶&nbsp;<img src=\"http://pyffam7jw.bkt.clouddn.com/uploads/20191008/Fs6sUDk9_iZh5Zhe-xVX8aMeI4Rn.jpg\" alt=\"\" />",
    "show_switch": 1,
    "createtime": 1570532123,
    "weigh": 1,
    "deletetime": null,
    "goods_avatar_text": "http://pyffam7jw.bkt.clouddn.com/uploads/20191008/FlacV2rtDNhzEZrV4bb60KH_9s9u.jpg"
    }
    })
     */
    public function goodInfo(){
        $id=input('id');
        if (empty($id)){
            $this->error('缺少必要参数!');
        }
        $Goods=new Goods();
        $info=$Goods::get($id)->toArray();
        $this->success('',$info);
    }
    /**
     * @ApiTitle    (获取地址列表)
     * @ApiSummary  (获取地址列表)
     * @ApiMethod (POST)
     * @ApiReturnParams   (name="id", type="string", required=true, description="地址id")
     * @ApiReturnParams   (name="user_name", type="string", required=true, description="姓名")
     * @ApiReturnParams   (name="tel", type="string", required=true, description="电话")
     * @ApiReturnParams   (name="address", type="string", required=true, description="地址")
     * @ApiReturnParams   (name="defaultswitch", type="string", required=true, description="是否为默认地址")
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1570533464",
    "data": {
    }
    })
     */
    public function getAddress(){
        $user_id=$this->auth->id;
        $where['user_id']=$user_id;
        $Address=new UserAddress();
        $list=$Address->getAddressList($where);
        $this->success('',$list);
    }
    /**
     * @ApiTitle    (获取默认地址)
     * @ApiSummary  (获取默认地址)
     * @ApiMethod (POST)
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1570533464",
    "data": {
    }
    })
     */
    public function getDefaultAddress(){
        $user_id=$this->auth->id;
        $where['user_id']=$user_id;
        $where['defaultswitch']=1;
        $Address=new UserAddress();
        $list=$Address->getAddress($where);
        $this->success('',$list);
    }
    /**
     * @ApiTitle    (创建地址)
     * @ApiSummary  (创建地址)
     * @ApiMethod (POST)
     * @ApiParams   (name="name", type="string", required=true, description="姓名")
     * @ApiParams   (name="tel", type="string", required=true, description="电话")
     * @ApiParams   (name="province", type="string", required=true, description="省市")
     * @ApiParams   (name="address", type="string", required=true, description="详细地址")
     * @ApiParams   (name="msg", type="string", required=true, description="提示语")
     * @ApiParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1570533464",
    "data": {
    }
    })
     */
    public function createAddress(){
        $user_id=$this->auth->id;
        $where['user_id']=$user_id;
        $Address=new UserAddress();
        $list=$Address->getAddress($where);

        $insert['user_id']=$user_id;
        $insert['user_name']=input('name');
        $insert['tel']=input('tel');
        $insert['province']=input('province');
        $insert['address']=input('address');
        $insert['createtime']=time();
        if (empty($list)){
            $insert['defaultswitch']='1';
        }else{
            $insert['defaultswitch']='0';
        }
        $result=$Address->insert($insert);
        if ($result>0){
            $this->success('编辑成功!');
        }else{
            $this->error('编辑失败!');
        }
    }
    /**
     * @ApiTitle    (获取地址信息)
     * @ApiSummary  (获取地址信息)
     * @ApiMethod (POST)
     * @ApiParams   (name="address_id", type="string", required=true, description="地址id")
     * @ApireturnParams   (name="user_name", type="string", required=true, description="用户名")
     * @ApireturnParams   (name="tel", type="string", required=true, description="电话")
     * @ApireturnParams   (name="address", type="string", required=true, description="地址")
     * @ApireturnParams   (name="defaultswitch", type="object", description="是否为默认地址1:是0:不是")
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1571030416",
    "data": {
    "id": 2,
    "user_id": 3,
    "user_name": "宋国杰",
    "tel": "17080088824",
    "address": "鑫茂科技园",
    "defaultswitch": 0,
    "createtime": 1571022797
    }
    })
     */
    public function getAddressInfo(){
        $map['id']=input('address_id');
        $map['user_id']=$this->auth->id;
        $address=UserAddress::get($map);
        if (empty($address)){
            $this->error('地址不存在');
        }
        $this->success('',$address);
    }


    /**
     * @ApiTitle    (编辑地址)
     * @ApiSummary  (编辑地址)
     * @ApiMethod (POST)
     * @ApiParams   (name="address_id", type="string", required=true, description="地址id")
     * @ApiParams   (name="user_name", type="string", required=true, description="用户名")
     * @ApiParams   (name="tel", type="string", required=true, description="电话")
     * @ApiParams   (name="address", type="string", required=true, description="地址")
     * @ApiParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1570533464",
    "data": {
    }
    })
     */
    public function editAddress(){
        $map['id']=input('address_id');
        $map['user_id']=$this->auth->id;
        $update['user_name']=input('user_name');
        $update['tel']=input('tel');
        $update['address']=input('address');
        $result=UserAddress::get($map)->save($update);
        if ($result>0){
            $this->success('编辑成功');
        }else{
            $this->error('编辑失败');
        }
    }
    /**
     * @ApiTitle    (设置默认地址)
     * @ApiSummary  (设置默认地址)
     * @ApiMethod (POST)
     * @ApiParams   (name="address_id", type="string", required=true, description="地址id")
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1570533464",
    "data": {
    }
    })
     */
    public function setDefaultAddress(){
        $id=input('address_id');
        $user_id=$this->auth->id;
        $map['id']=$id;
        $map['user_id']=$user_id;
        $update['defaultswitch']=1;
        $UserAddress=new UserAddress();
        $map1['user_id']=$user_id;
        $map1['defaultswitch']=1;
        $update1['defaultswitch']=0;
        $address=$UserAddress->get($map1);
        if(!empty($address)){
            $address->save($update1);
        }
        $UserAddress->get($map)->save($update);
        $this->success('设置成功!');
    }

    /**
     * @ApiTitle    (删除地址)
     * @ApiSummary  (删除地址)
     * @ApiMethod (POST)
     * @ApiParams   (name="address_id", type="string", required=true, description="地址id")
     * @ApiParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1570533464",
    "data": {
    }
    })
     */
    public function deleteAddress(){
        $map['user_id']=$this->auth->id;
        $map['id']=input('address_id');
        $one=UserAddress::get($map);
        if (empty($one)){
            $this->error('地址不存在');
        }
        $result=$one->delete();
        if ($result>0){
            $this->success('操作成功!');
        }else{
            $this->error('操作失败!');
        }
    }

    /**
     * @ApiTitle    (获取抵扣信息)
     * @ApiSummary  (获取抵扣信息)
     * @ApiSector   (三开相关接口)
     * @ApiMethod (POST)
     * @ApiParams   (name="good_id", type="string", required=true, description="商品id")
     * @ApiReturnParams   (name="remark", type="string", required=true, description="标注积分抵扣详情")
     * @ApiReturnParams   (name="good_price", type="string", required=true, description="商品价格")
     * @ApiReturnParams   (name="discount_money", type="string", required=true, description="抵扣金额")
     * @ApiReturnParams   (name="real_fee", type="string", required=true, description="实付金额")
     * @ApiReturnParams   (name="cost_score", type="string", required=true, description="消耗积分数")
     * @ApiReturnParams   (name="user_score", type="string", required=true, description="用户剩余积分数")
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1570533464",
    "data": {
    }
    })
     */
    public function getDiscount(){
        $user_id=$this->auth->id;
        /*查询当前商品积分数*/
        $good_id=input('good_id');
        $good=Goods::get($good_id);
        $user=\app\admin\model\User::get($user_id);
        $money=$good->money;
        $user_score=$user->score;
        $site=config('site');
        $rate=$site['good_ratio'];
        $return['remark']='每'.$rate.'分=1元';
        $return['good_price']=$money;
        $discount_money=$user_score/$rate;
        if ($discount_money>$money){
            $cost_score=$money*$rate;
            $real_fee=0;
            $discount_money=$money;
        }else{
            $cost_score=$user->score;
            $real_fee=ceil(($good['money']-$user_score/$site['good_ratio'])*100)/100;
            $discount_money=ceil($user_score/$site['good_ratio']*100)/100;
        }
        $return['discount_money']=$discount_money;
        $return['real_fee']=$real_fee;
        $return['cost_score']=$cost_score;
        $return['user_score']=$user_score;
        $this->success('',$return);
        /*查询当前商品积分数*/
    }



    /**
     * @ApiTitle    (提交订单)
     * @ApiSummary  (提交订单)
     * @ApiSector   (三开相关接口)
     * @ApiMethod (POST)
     * @ApiParams   (name="address_id", type="string", required=true, description="地址id")
     * @ApiParams   (name="good_id", type="string", required=true, description="商品id")
     * @ApiParams   (name="change_type", type="string", required=true, description="1:纯积分兑换2:现金组合兑换")
     * @ApiParams   (name="msg", type="string", required=true, description="提示语")
     * @ApiParams   (name="data", type="object", description="扩展数据返回(需要微信支付时会返回支付参数)")
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1570533464",
    "data": {
    }
    })
     */
    public function setOrder(){
        $address_id=input('address_id');
        $good_id=input('good_id');
        $change_type=input('change_type');
        if (empty($change_type)){
            $change_type=1;
        }
        if (empty($address_id)||empty($good_id)){
            $this->error('请选择地址');
        }
        /*查询是有对应地址*/
        $address=UserAddress::get($address_id);
        if (empty($address)){
            $this->error('地址不存在');
        }
        $address=$address->toArray();

        $good=Goods::get($good_id);
        
        if (empty($good)){
            $this->error('商品不存在');
        }
        if ($good->kucun<1){
            $this->error('商品库存不足');
        }
        $good=$good->toArray();

        $user_id=$this->auth->id;
        $user=\app\admin\model\User::get($user_id)->toArray();
        $site=config('site');
        $User=new \app\admin\model\User();
        switch($change_type){
            case 1: //积分支付 一期原有逻辑

                if ($user['score']<$good['price']){
                    $this->error('积分不足!');
                }

                $insert['order_sn']=cmf_get_order_sn();
                $insert['user_id']=$user_id;
                $insert['user_name']=$address['user_name'];
                $insert['tel']=$address['tel'];
                $insert['address']=$address['address'];
                $insert['good_id']=$good_id;
                $insert['status']=0;
                $insert['price']=$good['price'];
                Db::startTrans();
                try{
                    Orders::create($insert);
                    $User->where('id',$user_id)->setDec('score',$good['price']);
                    $insert_log['user_id']=$user_id;
                    $insert_log['score']=0-$good['score'];
                    $insert_log['memo']='积分兑换';
                    /*判断是否为现金商品*/

                    if ($good['good_data']==2){
                        /*添加用户现金金额*/
                        $User->where('id',$user_id)->setInc('money',$good['money']);
                    }
                    $Goods=new Goods();
                    $Goods->where('id',$good_id)->setDec('kucun');
                    ScoreLog::create($insert_log);
                    // 提交事务
                    Db::commit();
                } catch (\Exception $e) {
                    // 回滚事务
                    dump($e->getMessage());
                    Db::rollback();
                    $this->error('支付失败!');
                }
                $this->success('支付成功!');

                break;
            case 2: //现金支付
                //需要查看用户积分数量 判断出现金支付金额
                if ($good['good_data']==2){
                    $this->error('现金类型商品无法用现金支付');
                }

                if ($user['score']>=($good['price']*$site['good_ratio'])){
                    $real_fee=0;
                    $cost_score=$good['price']*$site['good_ratio'];
                    /*扣除相应积分*/
                    $insert['order_sn']=cmf_get_order_sn();
                    $insert['user_id']=$user_id;
                    $insert['user_name']=$address['user_name'];
                    $insert['tel']=$address['tel'];
                    $insert['address']=$address['address'];
                    $insert['good_id']=$good_id;
                    $insert['score']=$cost_score;
                    $insert['price']=$real_fee;
                    $insert['status']=1;
                    $insert['pay_type']=2;
                    Db::startTrans();
                    try{
                        Orders::create($insert);
                   //     $User->where('id',$user_id)->setDec('score',$good['price']);
                        $insert_log['user_id']=$user_id;
                        $insert_log['score']=0-$good['price'];
                        $insert_log['memo']='积分兑换';

                        $Goods=new Goods();
                     //   $Goods->where('id',$good_id)->setDec('kucun');
                       // ScoreLog::create($insert_log);
                        // 提交事务
                        Db::commit();
                    } catch (\Exception $e) {
                        // 回滚事务
                        dump($e->getMessage());
                        Db::rollback();
                        $this->error('支付失败!');
                    }
                    $this->success('支付成功!');

                }else{
                    $real_fee=ceil(($good['money']-$user['score']/$site['good_ratio'])*100)/100;
                    $cost_score=$user['score'];

                    $insert['order_sn']=cmf_get_order_sn();
                    $insert['user_id']=$user_id;
                    $insert['user_name']=$address['user_name'];
                    $insert['tel']=$address['tel'];
                    $insert['address']=$address['address'];
                    $insert['good_id']=$good_id;
                    $insert['score']=$cost_score;
                    $insert['price']=$real_fee;
                    $insert['status']=0;
                    $insert['pay_type']=2;
                    Db::startTrans();
                    try{
                        Orders::create($insert);
//                        $User->where('id',$user_id)->setDec('score',$cost_score);
//                        $insert_log['user_id']=$user_id;
//                        $insert_log['score']=0-$cost_score;
//                        $insert_log['memo']='积分兑换';
//                        $Goods=new Goods();
//                        $Goods->where('id',$good_id)->setDec('kucun');
//                        ScoreLog::create($insert_log);
                        // 提交事务
                        Db::commit();
                    } catch (\Exception $e) {
                        // 回滚事务
                        dump($e->getMessage());
                        Db::rollback();
                        $this->error('支付失败!');
                    }
                    $third=Third::get(['user_id'=>$user_id]);
                    /*调用微信支付*/
                    $notify_url=url('/api/common/notifyx1','','',true);
                    $params = [
                       'amount'=>$real_fee,
                        'orderid'=>"$insert[order_sn]",
                        'type'=>"wechat",
                        'title'=>"$good[good_name]",
                        'notifyurl'=>$notify_url,
                        'returnurl'=>$notify_url,
                        'method'=>"miniapp",
                        'openid'=>"$third->openid",
                        'auth_code'=>"1111"
                    ];
                    $payment=\addons\epay\library\Service::submitOrder($params);
                    $payment=json_decode($payment,true);
                    $this->success('支付信息',$payment);
                }
                break;
            default;
        }
    }

    /**
     * @ApiTitle    (我的订单)
     * @ApiSummary  (我的订单)
     * @ApiMethod (POST)
     * @ApiParams   (name="address_id", type="string", required=true, description="地址id")
     * @ApiParams   (name="good_id", type="string", required=true, description="商品id")
     * @ApiParams   (name="msg", type="string", required=true, description="提示语")
     * @ApiParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1570533464",
    "data": {
    }
    })
     */
    public function myOrderList(){
        $Order=new Orders();
        $where['user_id']=$this->auth->id;
        $where['status']='1';
        $orders['uncomplete']=$Order->orderList($where);
        $where['status']='2';
        $orders['complete']=$Order->orderList($where);
        $this->success('',$orders);
    }

    /**
     * @ApiTitle    (确认订单)
     * @ApiSummary  (确认订单)
     * @ApiMethod (POST)
     * @ApiParams   (name="order_id", type="string", required=true, description="订单id")
     * @ApiParams   (name="msg", type="string", required=true, description="提示语")
     * @ApiParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1570533464",
    "data": {
    }
    })
     */
    public function confirmReceipt(){
        $Order=new Orders();
        $order_id=input('order_id');
        if (empty($order_id)){
            $this->error('订单id不能为空');
        }
        $where['user_id']=$this->auth->id;
        $where['id']=$order_id;
        $result=$Order->completeOrder($where);
        if ($result>0){
            $this->success('确认收货成功!');
        }
        $this->error('操作失败!');
    }

    /**
     * @ApiTitle    (删除订单)
     * @ApiSummary  (删除订单)
     * @ApiMethod (POST)
     * @ApiParams   (name="order_id", type="string", required=true, description="订单id")
     * @ApiParams   (name="msg", type="string", required=true, description="提示语")
     * @ApiParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
    "code": 1,
    "msg": "",
    "time": "1570533464",
    "data": {
    }
    })
     */
    public function deleteOrder(){
        $id=input('order_id');
        $info=Orders::destroy($id);
        if(empty($info)){
            $this->error('操作失败!');
        }else{
            $this->success('操作成功!');
        }
    }

}