Goods.php 31.5 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 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
<?php


namespace app\api\controller;

use addons\litestore\Litestore;
use addons\litestore\model\Litestoreorder;
use addons\litestore\model\Wxlitestoregoods;
use addons\third\model\Third;
use addons\wechat\library\Config as ConfigService;
use app\admin\model\Litestorenews;
use app\api\model\GoodsMsg;
use app\api\model\LitestoreBanner;
use app\api\model\LitestoreGoodsSpec;
use app\api\model\Message;
use app\api\model\StoreComment;
use app\api\model\UserSearch;
use app\api\validate\GoodsValidate;
use app\api\validate\StoreValidate;
use app\common\controller\Api;
use EasyWeChat\Foundation\Application as WXPAY_APP;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order as WXPAY_ORDER;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\Request;

/**
 * 好物抢购接口
 */
class Goods extends Api
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    protected $goods_model;
    protected $banner_model;
    protected $spec_model;
    protected $search_model;
    protected $favorite_model;
    protected $comment_model;
    protected $good_model;
    protected $follow_model;
    protected $user_id;
    protected $model;

    public function __construct(Request $request, \app\api\model\LitestoreNews $banner, \app\api\model\LitestoreGoods $goods,LitestoreGoodsSpec $spec,UserSearch $search,Litestoreorder $order)
    {
        parent::__construct($request);
        $this->banner_model = $banner;
        $this->goods_model = $goods;
        $this->spec_model = $spec;
        $this->search_model = $search;
        $this->user_id = $this->auth->id;
        $this->model = $order;
    }


    /**
     * 轮播图列表
     * @ApiWeigh    (35)
     *
     * @ApiTitle    (轮播图列表)
     * @ApiSummary  (轮播图列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/goods/banner)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
        "list": [
            {
            "id": 1,
            "type": 跳转类型:1=无跳转,2=外链,3=商品,
            "href": "链接",
            "goods_id": 商品id,
            "image": "/uploads/20200830/85a473e047c77e7501889d1b19a9f794.jpg",
            }
        ]
    }
    })
     */
    public function banner()
    {
        if($this->request->isPost()){
            $where = [
                'where' => []
            ];
            $banner = $this->banner_model->selectOrFail($where,false,'*');
            $return = [
                'list' => $banner,
            ];
            $this->success('请求成功',$return);
        }
    }

    /**
     * 热销商品列表
     * @ApiWeigh    (25)
     *
     * @ApiTitle    (热销商品列表)
     * @ApiSummary  (热销商品列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/goods/goods_list)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="store_id", type="integer", required=false, description="店铺id")
     * @ApiParams   (name="keyword", type="string", required=false, description="关键词")
     * @ApiParams   (name="page", 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": {
        "list": [
            {
            "goods_id": 21,
            "goods_name": "小米Mix3",
            "category_id": 5,
            "spec_type": "20",
            "deduct_stock_type": "20",
            "sales_initial": 20,
            "sales_actual": 实际销量,
            "goods_sort": 21,
            "delivery_id": 22,
            "stock_num": 剩余库存,
            "goods_price": "销售价",
            "line_price": "划线价",
            "image": "https://her-family.oss-cn-qingdao.aliyuncs.com/addons_store_uploads/20181105/ffc4440df18661948b9c2d4dd4ae419b.jpg"
            },
        ],
        "this_page": 当前页数,
        "total_page": 总页数
    }
    })
     */
    public function goods_list()
    {
        if($this->request->isPost()){
            $param = (new GoodsValidate())->goCheck('common');
            $page = $param['page'];
            $field = '*';
            $order = ['goods_sort'=>'DESC','createtime'=>'DESC'];
            $basic_where = [
                'goods_status' => '10',
                'is_delete' => '0'
            ];
            if(!empty($param['keyword'])) {
                $basic_where['goods_name'] = ['like','%'.$param['keyword'].'%'];
            }
            if(!empty($param['store_id'])) {
                $basic_where['store_id'] = $param['store_id'];
            }
            $where = [
                'where' => $basic_where,
            ];
            $goods = $this->goods_model->pageSelect($page,$where,$field,$order,config('option.num'));
            $list = $goods->items();
            foreach ($list as &$v) {
                $stock_where = [
                    'goods_id' => $v['goods_id']
                ];
                $v['stock_num'] = $this->spec_model->getSum($stock_where,'stock_num');
                $spec_where = [
                    'where' => [
                        'goods_id' => $v['goods_id']
                    ]
                ];
                $spec = $this->spec_model->findOrFail($spec_where,false,'*','goods_price');
                $v['goods_price'] = $spec['goods_price'];
                $v['line_price'] = $spec['line_price'];
                $v['image'] = cdnurl(explode(',',$v['images'])[0],true);
            }
            // 搜索记录
            if(!empty($param['keyword'])) {
                $search_where = [
                    'where' => [
                        'user_id' => $this->auth->id,
                        'keyword' => $param['keyword']
                    ]
                ];
                $search = $this->search_model->findOrFail($search_where,false);
                if($search) {
                    $this->search_model->where('id',$search['id'])->setInc('count');
                } else {
                    $insert = [
                        'user_id' => $this->auth->id,
                        'keyword' => $param['keyword']
                    ];
                    $this->search_model->add($insert);
                }
            }
            $return = [
                'list' => $list,
                'this_page' => $goods->currentPage(),
                'total_page' => $goods->lastPage()
            ];
            $this->success('请求成功',$return);
        }
    }

    /**
     * 历史搜索
     * @ApiWeigh    (35)
     *
     * @ApiTitle    (历史搜索)
     * @ApiSummary  (历史搜索)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/goods/search_log)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    "list": [
    {
    "id": 1,
    "image": "http://cloud.caiyunpan.brotop.cn/assets/img/qrcode.png",
    "type": 跳转分类1=无跳转2=外链3=商品,
    "href": "",
    "weigh": 0,
    }
    ]
    }
    })
     */
    public function search_log()
    {
        if($this->request->isPost()){
            $where = [
                'where' => [
                    'user_id' => $this->auth->id
                ]
            ];
            $banner = $this->search_model->selectOrFail($where,false,'*');
            $return = [
                'list' => $banner,
            ];
            $this->success('请求成功',$return);
        }
    }

    /**
     * 清空历史搜索
     * @ApiWeigh    (35)
     *
     * @ApiTitle    (清空历史搜索)
     * @ApiSummary  (清空历史搜索)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/goods/clear_search_log)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    }
    })
     */
    public function clear_search_log()
    {
        if($this->request->isPost()){
            $where = [
                'user_id' => $this->auth->id
            ];
            $res = $this->search_model->where($where)->delete();
            if(!$res) {
                $this->error('删除失败');
            }
            $this->success('删除成功');
        }
    }

    /**
     * 商品详情
     * @ApiWeigh    (25)
     *
     * @ApiTitle    (商品详情)
     * @ApiSummary  (商品详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/goods/goods_detail)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="goods_id", type="integer", required=false, description="商品id")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
        "notice": 5,
        "qrcode": "20",
        "goods":{
            "goods_id": 21,
            "goods_name": "小米Mix3",
            "category_id": 5,
            "spec_type": "20",
            "deduct_stock_type": "20",
            "sales_initial": 20,
            "sales_actual": 实际销量,
            "goods_sort": 21,
            "delivery_id": 22,
            "stock_num": 剩余库存,
            "goods_price": "销售价",
            "line_price": "划线价",
            "store": {
                "id": 1,
                "order_sn": "20200826142729172194",
                "user_id": 1,
                "store_name": "店铺名称",
                "industry_id": "行业标签",
                "name": "测试11",
                "mobile": "13911111111",
                "store_icon": "http://cloud.caiyunpan.brotop.cn/assets/img/qrcode.png",
                "province": "地址省",
                "city": "地址市",
                "region": "地址区",
                "image_arr": [
                    "http://cloud.caiyunpan.brotop.cn/assets/img/qrcode.png",
                    "http://cloud.caiyunpan.brotop.cn/assets/img/qrcode.png"
                ]
            },
            "image": "https://her-family.oss-cn-qingdao.aliyuncs.com/addons_store_uploads/20181105/ffc4440df18661948b9c2d4dd4ae419b.jpg"
        },
    }
    })
     */
    public function goods_detail()
    {
        if($this->request->isPost()){
            $param = (new GoodsValidate())->goCheck('goods_detail');
            $goods_id = $param['goods_id'];
            $openid = Db::name('third')->where('user_id',$this->auth->id)->value('openid');
            $app = new Application(ConfigService::load());
            $userService = $app->user;
            $user = $userService->get($openid);
            // 判断是否关注公众号
            $notice = $user->subscribe;
            // 商品详情
            $detail = Wxlitestoregoods::detail($goods_id);
            $imgs=[];
            foreach (explode(",",$detail['images']) as $index => $item) {
                $imgs[] = cdnurl($item, true);
            }
            $detail['imgs_url'] = $imgs;

            if (!$detail || $detail['goods_status'] !== '10') {
                $this->error('很抱歉,商品信息不存在或已下架');
            }
            // 规格信息
            $specData = $detail['spec_type'] === '20' ? $detail->getManySpecData($detail['spec_rel'], $detail['spec']) : null;

            // 这里对规格的img格式化
            if($specData!=null){
                foreach($specData['spec_list'] as $index => $item){
                    $specData['spec_list'][$index]["form"]['imgshow'] = $specData['spec_list'][$index]["form"]['spec_image']==='' ? null:cdnurl($specData['spec_list'][$index]["form"]['spec_image'], true);
                }
            }
            $detail['store'] = \app\api\model\Store::get($detail['store_id']);
//            $basic_where = [
//                'goods_id' => $param['goods_id'],
//                'goods_status' => '10',
//                'is_delete' => '0'
//            ];
//            $where = [
//                'where' => $basic_where
//            ];
//            $goods = $this->goods_model->findOrFail($where);
            $stock_where = [
                'goods_id' => $goods_id
            ];
            $detail['stock_num'] = $this->spec_model->getSum($stock_where,'stock_num');
            $spec_where = [
                'where' => [
                    'goods_id' => $goods_id
                ]
            ];
            $spec = $this->spec_model->findOrFail($spec_where,false,'*','goods_price');
            $detail['goods_price'] = $spec['goods_price'];
            $detail['line_price'] = $spec['line_price'];
            $return = [
                'notice' => $notice,
                'qrcode' => cdnurl('/assets/img/qrcode.jpg'),
                'goods' => $detail,
                'spec' => $specData
            ];
            $this->success('请求成功',$return);
        }
    }

    /**
     * 联系我们
     * @ApiWeigh    (35)
     *
     * @ApiTitle    (联系我们)
     * @ApiSummary  (联系我们)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/goods/contact)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
        "phone": "联系方式",
        "contact": "内容"
    }
    })
     */
    public function contact()
    {
        if($this->request->isPost()){
            $return = [
                'phone' => config('site.phone'),
                'contact' => config('site.contact')
            ];
            $this->success('成功',$return);
        }
    }

    /**
     * 客服留言提交
     * @ApiWeigh    (35)
     *
     * @ApiTitle    (客服留言提交)
     * @ApiSummary  (客服留言提交)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/goods/msg)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="goods_id", type="integer", required=true, description="商品id")
     * @ApiParams   (name="content", type="string", required=true, description="内容")
     * @ApiParams   (name="mobile", type="string", 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 msg()
    {
        $param = (new StoreValidate())->goCheck('msg');
        $where = [
            'where' => [
                'goods_id' => $param['goods_id']
            ]
        ];
        $goods = $this->goods_model->findOrFail($where);
        $msg_model = new GoodsMsg();
        Db::startTrans();
        $result = false;
        try{
            $add = [
                'user_id' => $this->auth->id,
                'goods_id' => $param['goods_id'],
                'store_id' => $goods['store_id'],
                'content' => $param['content'],
                'mobile' => $param['mobile'],
            ];
            $result = $msg_model->add($add);
            Db::commit();
        } catch (PDOException $e) {
            Db::rollback();
            $this->error($e->getMessage());
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if(!$result) {
            $this->error('留言失败');
        }
        $this->success('留言成功');
    }

    /**
     * 立即购买
     * @ApiWeigh    (25)
     *
     * @ApiTitle    (立即购买)
     * @ApiSummary  (立即购买)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/goods/buyNow)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="goods_id", type="integer", required=true, description="商品id")
     * @ApiParams   (name="goods_num", type="integer", required=true, description="数量")
     * @ApiParams   (name="goods_sku_id", 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 buyNow()
    {
        $goods_id = $this->request->param('goods_id');
        $goods_num = $this->request->param('goods_num');
        $goods_sku_id = $this->request->param('goods_sku_id');

        $order = $this->model->getBuyNow($this->user_id, $goods_id, $goods_num, $goods_sku_id);

        return $this->success('获取成功',$order);
    }

    /**
     * 立即支付
     * @ApiWeigh    (25)
     *
     * @ApiTitle    (立即支付)
     * @ApiSummary  (立即支付)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/goods/buyNow_pay)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="goods_id", type="integer", required=true, description="商品id")
     * @ApiParams   (name="goods_num", type="integer", required=true, description="数量")
     * @ApiParams   (name="goods_sku_id", type="integer", required=true, description="商品规格")
     * @ApiParams   (name="remark", type="integer", required=false, 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 buyNow_pay(){
        $goods_id = $this->request->param('goods_id');
        $goods_num = $this->request->param('goods_num');
        $goods_sku_id = $this->request->param('goods_sku_id');
        $remark = $this->request->param('remark','');

        $order = $this->model->getBuyNow($this->user_id, $goods_id, $goods_num, $goods_sku_id);

        if ($this->model->hasError()) {
            return $this->error($this->model->getError());
        }

        // 创建订单
        if ($this->model->order_add($this->user_id, $order, $remark)) {
            // 发起微信支付
//            if($this->request->request('type')&&$this->request->request('type')=='gzh'){
                $this->init_wx_pay_for_gzh(true);
                $this->make_wx_pay('wechat');
//            }else{
//                $this->init_wx_pay_for_wxapp();
//                $this->make_wx_pay('wxapp');
//            }
        }
        $error = $this->model->getError() ?: '订单创建失败';
        return $this->error($error);

    }

    /**
     * 获取订单数量
     * @ApiWeigh    (25)
     *
     * @ApiTitle    (获取订单数量)
     * @ApiSummary  (获取订单数量)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/goods/Get_order_num)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
        "NoPayNum": 待付款数量,
        "NoFreightNum": 待使用,
        "NoReceiptNum": 已完成
    }
    })
     */
    public function Get_order_num(){
        $NoPayNum = $this->model->where(['user_id' => $this->user_id, 'pay_status' => '10', 'freight_status' => '10', 'order_status' => '10', 'receipt_status' => '10'])->count();
        $NoFreightNum = $this->model->where(['user_id' => $this->user_id, 'pay_status' => '20', 'freight_status' => '10', 'order_status' => '10', 'receipt_status' => '10'])->count();
        $NoReceiptNum = $this->model->where(['user_id' => $this->user_id, 'pay_status' => '20', 'freight_status' => '10', 'order_status' => '30', 'receipt_status' => '10'])->count();
        return $this->success('',['NoPayNum'=>$NoPayNum,'NoFreightNum'=>$NoFreightNum,'NoReceiptNum'=>$NoReceiptNum]);
    }


    /**
     * 我的订单
     * @ApiWeigh    (25)
     *
     * @ApiTitle    (我的订单)
     * @ApiSummary  (我的订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/goods/my)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="status", type="integer", required=true, description="状态0=全部1=待付款2=待使用3=已完成")
     * @ApiParams   (name="page", 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": {
         [
        {
        "id": 25, //订单ID
        "order_no": "2020082248974854", //订单号
        "total_price": "3299.00", //商品总价
        "pay_price": "3299.00", //实际支付金额
        "pay_status": "10", //支付状态:10=未支付,20=已支付
        "status": "1", //订单状态:1=待付款,2=待使用,3=已完成
        "pay_time": 0, //支付事件
        "express_price": "0.00",
        "express_company": "",
        "express_no": "",
        "freight_status": "10",
        "freight_time": 0,
        "receipt_status": "10",
        "receipt_time": 0,
        "order_status": "10", //订单状态:10=进行中,20=取消,30=已完成
        "transaction_id": "",
        "user_id": 1,
        "goods": [ //商品信息
            {
            "id": 25, //订单商品ID
            "goods_id": 21, //商品ID
            "goods_name": "小米Mix3", //商品名称
            "images": "https://her-family.oss-cn-qingdao.aliyuncs.com/addons_store_uploads/20181105/ffc4440df18661948b9c2d4dd4ae419b.jpg,https://her-family.oss-cn-qingdao.aliyuncs.com/addons_store_uploads/20181105/83bf8f141969a9e3e607a768407fc7e0.jpg,https://her-family.oss-cn-qingdao.aliyuncs.com/addons_store_uploads/20181105/c5d85254fc17b1a1b0e2254470881e59.jpg", //商品图片
            "deduct_stock_type": "20",
            "spec_type": "20",
            "spec_sku_id": "40_42",
            "goods_spec_id": 81,
            "goods_attr": "颜色:黑色; 版本:6+128; ", //规格描述
            "content": "<p><img src=\"https://her-family.oss-cn-qingdao.aliyuncs.com/addons_store_uploads/20181105/f5b49f703245ef61bb3faa574f32076d.jpg\" data-filename=\"filename\" style=\"width: 699px;\"><img src=\"https://her-family.oss-cn-qingdao.aliyuncs.com/addons_store_uploads/20181105/7d0fe135394408d4332386117c928003.jpg\" data-filename=\"filename\" style=\"width: 699px;\"><img src=\"https://her-family.oss-cn-qingdao.aliyuncs.com/addons_store_uploads/20181105/6a87fa6af95e39d7dc227f666d7b8ff6.jpg\" data-filename=\"filename\" style=\"width: 699px;\"><img src=\"https://her-family.oss-cn-qingdao.aliyuncs.com/addons_store_uploads/20181105/32d58a08cf92282c8f28078137c970f2.jpg\" data-filename=\"filename\" style=\"width: 699px;\"><br></p>", //商品描述
            "goods_no": "SN001", 
            "goods_price": "3299.00", //商品单价
            "line_price": "0.00", //原价
            "goods_weight": 0.5, 
            "total_num": 1, //购买该商品数量
            "total_price": "3299.00", //购买该商品总金额
            "order_id": 25,
            "user_id": 1,
            "createtime": 1598075584
            }
        ]
        }
    ]
    }
    })
     */
    public function my(){
        $list = $this->model->getList($this->user_id,$this->request->param('status'));
        return $this->success('',$list);
    }

    /**
     * 订单详情
     * @ApiWeigh    (25)
     *
     * @ApiTitle    (订单详情)
     * @ApiSummary  (订单详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/goods/detail)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="订单id")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
        "order": {
            "id": 10, //订单ID
            "order_no": "订单编号", //订单编号
            "total_price": "订单金额", //订单金额
            "pay_price": "3299.00",
            "pay_status": "支付状态10=未支付20=已支付",
            "status": "1", //订单状态:1=待付款,2=待使用,3=已完成
            "pay_time": 0,
            "order_status": "订单状态10=进行中20=取消30=已完成",
            "transaction_id": "",
            "createtime": "下单时间",
            "user_id": 1,
            "goods": [
                {
                "id": 10,
                "goods_id": 21,
                "goods_name": "商品名称",
                "deduct_stock_type": "20",
                "spec_type": "20",
                "spec_sku_id": "40_42",
                "goods_spec_id": 81,
                "goods_attr": "商品属性",
                "goods_no": "SN001",
                "goods_price": "商品售价",
                "line_price": "划线价格",
                "total_num": 商品数量,
                "total_price": "3299.00",
                "order_id": 10,
                "user_id": 1,
                "image": "商品图片",
                "sku_image": "",
                "spec": {
                    "goods_spec_id": 81,
                    "goods_id": 21,
                    "goods_no": "SN001",
                    "goods_price": "3299.00",
                    "line_price": "0.00",
                    "stock_num": 997,
                    "goods_sales": 2,
                    "goods_weight": 0.5,
                    "spec_sku_id": "40_42",
                    "spec_image": "",
                    },
                "goods": {
                    "goods_id": 21,
                    "goods_name": "商品名称",
                    "must_know": "抢购须知"
                    }
                }
            ]
        }
    }
    })
     */
    public function detail(){
        $id = $this->request->post('id');
        $order = $this->model->getOrderDetail($id, $this->user_id);
        return $this->success('',['order' => $order]);
    }

    /**
     * 取消订单
     * @ApiWeigh    (25)
     *
     * @ApiTitle    (取消订单)
     * @ApiSummary  (取消订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/goods/cancel)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="订单id")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    }
    })
     */
    public function cancel(){
        $id = $this->request->post("id");
        $order = $this->model->getOrderDetail($id, $this->user_id);

        if ($order->cancel($this->user_id,$id)) {
            return $this->success('');
        }
        return $this->error($order->getError());
    }

    /**
     * 订单立即付款
     * @ApiWeigh    (25)
     *
     * @ApiTitle    (订单立即付款)
     * @ApiSummary  (订单立即付款)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/goods/order_pay)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="订单id")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    }
    })
     */
    public function order_pay(){
        $id = $this->request->post("id");
        $order = $this->model->getOrderDetail($id, $this->user_id);
        if (!$order->checkGoodsStatusFromOrder($order['goods'])) {
            return $this->error($order->getError());
        }
        $this->model = $order;
        // 发起微信支付
//        if($this->request->post('type')&&$this->request->post('type')=='gzh'){
            $this->init_wx_pay_for_gzh(true);
            $this->make_wx_pay('wechat');
//        }else{
//            $this->init_wx_pay_for_wxapp();
//            $this->make_wx_pay('wxapp');
//        }
    }

    private function init_wx_pay_for_gzh($Ischeck=false){
        //这里首先判断 此用户是否绑定了微信公众号
        if($Ischeck){
            $third = Third::where(['user_id' => $this->user_id, 'platform' => 'wechat'])->find();
            if(!$third){
                //从这里自动绑定微信公众号的账户
                $this->error('您未绑定微信号',null,1008);
            }
        }

        $config = get_addon_config('litestore');

        $third_config = get_addon_config('third');
        $third_options = array_intersect_key($third_config, array_flip(['wechat']));
        $third_options = $third_options['wechat'];

        $options = [
            'debug'  => true,
            'log' => [
                'level' => 'debug',
                'file'  => '/tmp/easywechat.log',
            ],
            'app_id'   => $third_options['app_id'],
            'secret'   => $third_options['app_secret'],
            'payment' => [
                'merchant_id'        =>  $config['MCHIDGZH'],
                'key'                =>  $config['APIKEYGZH'],
                'notify_url'         =>  \think\Request::instance()->domain().'/index/ajax/callback_for_wxgzh',
            ],

        ];
        $this->wxapp = new WXPAY_APP($options);
    }

    private function make_wx_pay($platform){
        $third = Third::where(['user_id' => $this->user_id, 'platform' => $platform])->find();

        $payment = $this->wxapp->payment;

        $attributes = [
            'trade_type'       => 'JSAPI',
            'body'             => $this->model['order_no'],
            'detail'           => 'OrderID:'.$this->model['id'],
            'out_trade_no'     => $this->model['order_no'],
            'total_fee'        => $this->model['pay_price'] * 100, // 单位:分
//            'total_fee'        => 1, // 单位:分
            'openid'           => $third['openid'],
        ];
        $order = new WXPAY_ORDER($attributes);

        $result = $payment->prepare($order);
        if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS'){
            $prepayId = $result->prepay_id;
            $config = $payment->configForJSSDKPayment($prepayId); // 返回数组
            return $this->success('预支付成功',$config);
        }
        return $this->error('微信支付调用失败',$result);
    }
}