User.php 27.7 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
<?php

namespace app\api\controller;

use app\common\model\User as UserModel;
use app\common\controller\Api;
use app\common\library\Sms;
use app\common\library\Token;
use fast\Random;
use nsms\sendAPI;
use think\Config;
use think\Db;
use think\Exception;
use think\Hook;


/**
 * 会员接口
 */
class User extends Api
{
    protected $noNeedLogin = ['miniLogin', 'miniLogin1','register','get_openid','sendVerifyCode'];
    protected $noNeedRight = '*';
    protected $config;

    public function _initialize()
    {
        parent::_initialize();

        $this->config = Config::get('weChat');
    }

    /**
     * 注册用户
     *
     * @param string $openid openid
     *
     * @param string $mobile
     *
     * @return boolean
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function register($openid,$mobile = '')
    {
        // 检测用户openid是否存在
        $user = new \app\admin\model\User;
        $user = $user->where(['openid'=>$openid])->find();
        if ($user) {
            $this->error('Openid already exist');
            return false;
        }

        $ip = request()->ip();
        $time = time();

        $data = [
            'username' => '',
            'password' => '',
            'email'    => '',
            'mobile'   => $mobile,
            'level'    => 1,
            'score'    => 0,
            'avatar'   => '',
            'openid'   => $openid,
            'is_default'=> true,
        ];
        $params = array_merge($data, [
            'nickname'  => '',
            'salt'      => Random::alnum(),
            'jointime'  => $time,
            'joinip'    => $ip,
            'logintime' => $time,
            'loginip'   => $ip,
            'prevtime'  => $time,
            'status'    => 'normal'
        ]);
        $params['password'] =  $this->auth->getEncryptPassword('', $params['salt']);

        //账号注册时需要开启事务,避免出现垃圾数据
        Db::startTrans();
        try {
            $user = UserModel::create($params, true);

            $this_user=UserModel::get($user->id);

            //设置Token
            $this_token = Random::uuid();
            Token::set($this_token, $user->id, 2592000);

            //注册成功的事件
            Hook::listen("user_register_successed", $this_user, $data);
            Db::commit();
        } catch (Exception $e) {
            $this->setError($e->getMessage());
            Db::rollback();
            return false;
        }
        $this->auth->direct($user->id);
        return true;
    }

    /**
     * 小程序登录
     *
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *
     *
     * @ApiTitle    (小程序登录)
     * @ApiSummary  (小程序登录)
     * @ApiMethod   (POST)
     * @ApiParams   (name="code", type="string", required=true, description="code")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{}", description="扩展数据返回")
     * @ApiReturn   ({'code':'1','msg':'返回成功'})
     *
     */
    public function miniLogin()
    {
        $code = $this->request->request('code');
        $iv = $this->request->request('iv');
        $encryptedData = $this->request->request('encryptedData');

        if (!$code) {
            $this->error(__('Invalid code'));
        }

        $mini = new \WeMini\Crypt($this->config);
        $session = $mini->session($code);

        if(!isset($session['openid']) || empty($session['openid'])){
            $this->error(__('Logged in failed'));
        }

        $openid = $session['openid'];
        $sessionKey = $session['session_key'];

        $mini = new \WeMini\Crypt($this->config);
        $getUserInfo = $mini->decode($iv,$sessionKey,$encryptedData);
        $mobile = isset($getUserInfo['purePhoneNumber'])?$getUserInfo['purePhoneNumber']:'';

        if(!empty($mobile)){
            $user = new \app\admin\model\User;
            $user->save(['openid'=>$openid],['mobile'=>$mobile,'openid'=>'']);
        }

        $user = new \app\admin\model\User;
        $user = $user->where(['openid'=>$openid,'is_default'=>true])->find();
        if ($user) {
            //如果已经有账号则直接登录
            $this->auth->direct($user->id);
            $return['user_info'] = $this->auth->getUserinfo();
        } else {
            if(!empty($mobile)){
                $this->register($openid,empty($mobile)?'':$mobile);
                $return['user_info'] = $this->auth->getUserinfo();
            }else{
                $return['user_info'] = [];
            }
        }


        $return['session'] = $session;

        $this->success('Logged in successful', $return);
    }

    /**
     * 获得微信手机号
     *
     *
     *
     * @ApiTitle    (小程序登录)
     * @ApiSummary  (小程序登录)
     * @ApiMethod   (POST)
     * @ApiParams   (name="iv", type="string", required=true, description="iv")
     * @ApiParams   (name="sessionKey", type="string", required=true, description="sessionKey")
     * @ApiParams   (name="encryptedData", type="string", required=true, description="encryptedData")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{}", description="扩展数据返回")
     * @ApiReturn   ({'code':'1','msg':'返回成功'})
     *
     */
    public function getUserInfo(){
        $iv = $this->request->request('iv');
        $sessionKey = $this->request->request('sessionKey');
        $encryptedData = $this->request->request('encryptedData');

        $mini = new \WeMini\Crypt($this->config);
        $return = $mini->decode($iv,$sessionKey,$encryptedData);

        $this->success('', $return);
    }


    /**
     * 小程序登录
     *
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *
     *
     * @ApiTitle    (小程序登录)
     * @ApiSummary  (小程序登录)
     * @ApiMethod   (POST)
     * @ApiParams   (name="code", type="string", required=true, description="code")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{}", description="扩展数据返回")
     * @ApiReturn   ({'code':'1','msg':'返回成功'})
     *
     */
    public function miniLogin1()
    {
        $code = $this->request->request('code');
        $openid = $code;

        $user = new \app\admin\model\User;
        $user = $user->where(['openid'=>$openid,'is_default'=>true])->find();
        if ($user) {
            //如果已经有账号则直接登录
            $ret = $this->auth->direct($user->id);
        } else {
            $ret = $this->register($openid);
        }
        if (!$ret) {
            $this->error($this->auth->getError());
        }

        $return['user_info'] = $this->auth->getUserinfo();
        $return['session'] = 123;

        $this->success('Logged in successful', $return);
    }

    /**
     * getId
     *
     * @ApiTitle    (查询用户byid)
     * @ApiSummary  (查询用户byid)
     * @ApiMethod   (POST)
     * @ApiParams   (name="id", type="string", required=true, description="id")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{}", description="扩展数据返回")
     * @ApiReturn   ({'code':'1','msg':'返回成功'})
     *
     *
     */
    public function get_id()
    {
        $id = $this->request->request('id');
        if (!$id) {
            $this->error(__('Invalid parameters'));
        }
        $user = new \app\admin\model\User;
        $user = $user->where(['id'=>$id])->field('password,salt',true)->find();
        if(empty($user)){
            $user = [];
        }else{
            $user = $user->toArray();
        }

        /*//查询订单
        $order = new \app\admin\model\Order();
        $order = $order->where(['user_id'=>$id,'status'=>'success','remain'=>['>',0]])->order(array('end' => 'asc','remain' => 'asc'))->find();
        if(empty($order)){
            $user['order'] = [];
        }else{
            $user['order'] = $order->toArray();
        }*/


        $this->success('ok',$user);
    }

    /**
     * 更换用户
     *
     * @ApiTitle    (更换用户)
     * @ApiSummary  (更换用户)
     * @ApiMethod   (POST)
     * @ApiParams   (name="old_id", type="string", required=true, description="老的id")
     * @ApiParams   (name="new_id", type="string", required=true, description="新的id")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{}", description="扩展数据返回")
     * @ApiReturn   ({'code':'1','msg':'返回成功'})
     *
     *
     */
    public function change_id()
    {
        $old_id = $this->request->request('old_id');
        $new_id= $this->request->request('new_id');
        if (!$old_id) {
            $this->error(__('Invalid parameters'));
        }
        if (!$new_id) {
            $this->error(__('Invalid parameters'));
        }
        $user = new \app\admin\model\User;
        $old_user = $user->where(['id'=>$old_id])->find();
        if(empty($old_user)){
            $this->error(__('Invalid parameters'));
        }
        $old_user = $old_user->toArray();

        $user = new \app\admin\model\User;
        $new_user = $user->where(['id'=>$new_id])->find();
        if(empty($new_user)){
            $this->error(__('Invalid parameters'));
        }
        $new_user = $new_user->toArray();

        if($old_user['openid'] != $new_user['openid']){
            $this->error(__('Invalid parameters'));
        }

        $ret = $this->auth->direct($new_id);
        $user = new \app\admin\model\User;
        $user->save(['is_default'=>true],['id'=>$new_id]);

        if (!$ret) {
            $this->error($this->auth->getError());
        }

        $return['user_info'] = $this->auth->getUserinfo();

        $this->success('Logged in successful', $return);
    }

    /**
     * getOpenId
     *
     * @ApiTitle    (查询用户byopenid)
     * @ApiSummary  (查询用户byopenid)
     * @ApiMethod   (POST)
     * @ApiParams   (name="openid", type="string", required=true, description="openid")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{}", description="扩展数据返回")
     * @ApiReturn   ({'code':'1','msg':'返回成功'})
     *
     *
     */
    public function get_openid()
    {
        $openid = $this->request->request('openid');
        if (!$openid) {
            $this->error(__('Invalid parameters'));
        }
        $user = new \app\admin\model\User;
        $user = $user->where(['openid'=>$openid])->field('password,salt',true)->select();
        if(empty($user)){
            $user = [];
        }else{
            foreach ($user as &$k){
                $k['avatar'] = cdnurl($k['avatar'],true);
            }
        }

        $this->success('ok',$user);
    }


    private function send_sms($data){

        $url      = "http://www.ztsms.cn/sendNSms.do";
        $username = 'qixiudaka';
        $password = 'Cxz307312';
        $sendAPI = new sendAPI($url, $username, $password);
        $sendAPI->data = $data;/*初始化数据包*/
        $return = $sendAPI->sendSMS('GET');
        return $return;
    }

    /**
     * 发送短信验证码
     *
     * @ApiTitle    (发送短信验证码)
     * @ApiSummary  (发送短信验证码)
     * @ApiMethod   (POST)
     * @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="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{}", description="扩展数据返回")
     * @ApiReturn   ({'code':'1','msg':'返回成功'})
     *
     */
    public function sendVerifyCode()
    {
        $mobile = $this->request->request('mobile');
        $code = mt_rand(1000, 9999);
        $time = time();
        $ip = request()->ip();
        $event = 'updateUserInfo';

        $sms = new \app\common\model\Sms;
        $sms = $sms->where(['mobile' => $mobile, 'event' => $event])
            ->order('createtime', 'DESC')
            ->find();
        if(!empty($sms)){
            if( time() - $sms['createtime'] <60 ){
                $this->error('请勿频繁发送');
            }
        }

        \app\common\model\Sms::create([ 'event' => $event,'mobile' => $mobile, 'code' => $code, 'ip' => $ip, 'createtime' => $time]);

        $text = '【维克猩球】您好,您的验证为' . $code . ',请于2分钟内使用,如非本人操作,可忽略此消息。';
        $data = array(
            'content'   => $text,
            'mobile'    => $mobile,
            'productid' => '676767',
            'xh'        => ''
        );
        $return = $this->send_sms($data);
        $ret = explode(',', $return);
        if ($ret[0] == '1') {
            $this->success('验证码已经发送成功');
        } else {
            $info = date('Y-m-d H:i:s') . " 短信发送失败 error:" . $return;
            $this->error($info,$return);
        }
    }

    /**
     * update
     *
     *
     *
     * @ApiTitle    (更新用户信息)
     * @ApiSummary  (更新用户信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/User/update)
     * @ApiParams   (name="user_id", type="string", required=true, description="用户名")
     * @ApiParams   (name="username", type="string", required=true, description="家长名")
     * @ApiParams   (name="nickname", type="string", required=true, description="学生名")
     * @ApiParams   (name="mobile", type="string", required=true, description="电话")
     * @ApiParams   (name="avatar", type="string", required=true, description="图片")
     * @ApiParams   (name="gender", type="string", required=true, description="性别")
     * @ApiParams   (name="birthday", type="string", required=true, description="生日")
     * @ApiParams   (name="area", type="string", required=true, description="地区")
     * @ApiParams   (name="address", type="string", required=true, description="地址")
     * @ApiParams   (name="Code", type="string", required=true, description="验证码")
     * @ApiParams   (name="is_default", type="string", required=true, description="默认")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{}", description="扩展数据返回")
     * @ApiReturn   ({'code':'1','msg':'返回成功'})
     *
     *
     */
    public function update()
    {
        $user_id = $this->request->request('user_id');
        $username = $this->request->request('username');
        $nickname = $this->request->request('nickname');
        $mobile = $this->request->request('mobile');
        $avatar = $this->request->request('avatar', '', 'trim,strip_tags,htmlspecialchars');
        $gender = $this->request->request('gender');
        $birthday = $this->request->request('birthday');
        $area = $this->request->request('area');
        $address = $this->request->request('address');
        $is_default = $this->request->request('is_default');
        $code = $this->request->request('code');
        $weChat_pic = $this->request->request('weChat_pic');

        if (!$user_id) {
            $this->error(__('Invalid parameters'));
        }

        $gender = ($gender === 1)?1:0;
        $birthday = date('Y-m-d',strtotime($birthday));

        if (!$is_default) {
            $is_default = false;
        }


        $user = new \app\admin\model\User();
        $userdate = $user->where(['id'=>$user_id])->find();
        if(empty($userdate)){
            $this->error(__('Invalid user'));
        }
        $userdate = $userdate->toArray();

        if (!Sms::check($mobile, $code, 'updateUserInfo')) {
            $this->error(__('Captcha is incorrect'));
        }

        $is_default = ($is_default)?true:false;
        if($is_default === true){
            $user = new \app\admin\model\User();
            $user->save(['is_default'=>false],['openid'=>$userdate['openid']]);
        }


        $user = new \app\admin\model\User();
        $data['username'] = $username;
        $data['nickname'] = $nickname;
        $data['mobile'] = $mobile;
        $data['avatar'] = $avatar;
        $data['gender'] = $gender;
        $data['birthday'] = $birthday;
        $data['area'] = $area;
        $data['address'] = $address;
        $data['weChat_pic'] = $weChat_pic;
        $data['is_default'] = $is_default;
        $user->save($data,['id'=>$user_id]);

        $user = new \app\admin\model\User();
        $user = $user->where(['id'=>$user_id])->field('password,salt',true)->find()->toArray();

        $this->success('ok',$user);
    }

    /**
     * add
     *
     *
     *
     * @ApiTitle    (添加用户信息)
     * @ApiSummary  (添加用户信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/User/add)
     * @ApiParams   (name="openid", type="string", required=true, description="openid")
     * @ApiParams   (name="username", type="string", required=true, description="家长名")
     * @ApiParams   (name="nickname", type="string", required=true, description="学生名")
     * @ApiParams   (name="mobile", type="string", required=true, description="电话")
     * @ApiParams   (name="avatar", type="string", required=true, description="图片")
     * @ApiParams   (name="gender", type="string", required=true, description="性别")
     * @ApiParams   (name="birthday", type="string", required=true, description="生日")
     * @ApiParams   (name="area", type="string", required=true, description="地区")
     * @ApiParams   (name="address", type="string", required=true, description="地址")
     * @ApiParams   (name="Code", type="string", required=true, description="验证码")
     * @ApiParams   (name="is_default", type="string", required=true, description="默认")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{}", description="扩展数据返回")
     * @ApiReturn   ({'code':'1','msg':'返回成功'})
     *
     *
     */
    public function add()
    {
        $openid = $this->request->request('openid');
        $username = $this->request->request('username');
        $nickname = $this->request->request('nickname');
        $mobile = $this->request->request('mobile');
        $avatar = $this->request->request('avatar', '', 'trim,strip_tags,htmlspecialchars');
        $gender = $this->request->request('gender');
        $birthday = $this->request->request('birthday');
        $area = $this->request->request('area');
        $address = $this->request->request('address');
        $code = $this->request->request('code');
        $is_default = $this->request->request('is_default');

        if (!$openid) {
            $this->error(__('Invalid parameters'));
        }
        if (!$username) {
            $this->error(__('Invalid parameters'));
        }
        if (!$nickname) {
            $this->error(__('Invalid parameters'));
        }
        if (!$mobile) {
            $this->error(__('Invalid parameters'));
        }
        if (!$avatar) {
            $this->error(__('Invalid parameters'));
        }
        $gender = ($gender === 1)?1:0;
        if (!$birthday) {
            $this->error(__('Invalid parameters'));
        }else{
            $birthday = date('Y-m-d',strtotime($birthday));
        }
        if (!$area) {
            $this->error(__('Invalid parameters'));
        }
        if (!$address) {
            $this->error(__('Invalid parameters'));
        }
        if (!$code) {
            $this->error(__('Invalid parameters'));
        }
        if (!$is_default) {
            $is_default = false;
        }

        if (!Sms::check($mobile, $code, 'updateUserInfo')) {
            $this->error(__('Captcha is incorrect'));
        }

        $is_default = ($is_default)?true:false;
        if($is_default === true){
            $user = new \app\admin\model\User();
            $user->save(['is_default'=>false],['openid'=>$openid]);
        }

        $ip = request()->ip();
        $time = time();

        $data = [
            'username'   => $username,
            'nickname'   => $nickname,
            'mobile'     => $mobile,
            'avatar'     => $avatar,
            'gender'     => $gender,
            'birthday'   => $birthday,
            'area'       => $area,
            'address'    => $address,
            'openid'     => $openid,
            'status'     => 'normal',
            'is_default' => $is_default,
        ];
        $params = array_merge($data, [
            'email'    => '',
            'level'    => 1,
            'score'    => 0,
            'salt'      => Random::alnum(),
            'jointime'  => $time,
            'joinip'    => $ip,
            'logintime' => $time,
            'loginip'   => $ip,
            'prevtime'  => $time,
            'pay_status'=> 'unpaid',
        ]);
        $params['password'] =  $this->auth->getEncryptPassword('', $params['salt']);

        $user = UserModel::create($params, true);

        $this->success('ok',$user);
    }

    /**
     * delete
     *
     *
     *
     * @ApiTitle    (删除用户信息)
     * @ApiSummary  (删除用户信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/User/delete)
     * @ApiParams   (name="user_id", type="string", required=true, description="用户名")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{}", description="扩展数据返回")
     * @ApiReturn   ({'code':'1','msg':'返回成功'})
     *
     *
     */
    public function delete()
    {
        $user_id = $this->request->request('user_id');

        if (!$user_id) {
            $this->error(__('Invalid parameters'));
        }


        $user = new \app\admin\model\User();
        $userdate = $user->where(['id'=>$user_id])->find();
        if(empty($userdate)){
            $this->error(__('Invalid user'));
        }
        $userdate = $userdate->toArray();
        if($userdate['is_default']){
            $this->error(__('Invalid user'));
        }

        $user = new \app\admin\model\User();
        $user = $user->where(['id'=>$user_id])->delete();

        $this->success('ok',$user);
    }

    /**
     * get_order
     *
     * @ApiTitle    (查询订单)
     * @ApiSummary  (查询订单)
     * @ApiMethod   (POST)
     * @ApiParams   (name="id", type="string", required=false, description="订单id")
     * @ApiParams   (name="user_id", type="string", required=false, description="用户id")
     * @ApiParams   (name="course_id", type="string", required=false, description="课程id")
     * @ApiParams   (name="status", type="string", required=false, description="状态")
     * @ApiParams   (name="enable", type="string", required=false, description="可用(true)")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{}", description="扩展数据返回")
     * @ApiReturn   ({'code':'1','msg':'返回成功'})
     *
     *
     */
    public function get_order()
    {
        $id = $this->request->request('id');
        $user_id = $this->request->request('user_id');
        $enable = $this->request->request('enable');
        $course_id = $this->request->request('course_id');
        $status = $this->request->request('status');

        $where = [];

        if (!empty($id)) {
            $where['id'] = $id;
        }
        if (!empty($user_id)) {
            $where['user_id'] = $user_id;
        }
        if (!empty($status)) {
            $where['status'] = $status;
        }
        if (isset($enable)) {
            $where['end'] = ['>',date('Y-m-d H:i:s')];
            $where['remain'] = ['>',0];
            $where['status'] = 'success';
        }
        $order = new \app\admin\model\Order();
        $order = $order->where($where)->select();

        $return = [];
        foreach ($order as $k){
            $product_doc = $k['product_doc'] = json_decode($k['product_doc'],true);
            if (!empty($course_id)) {
                if(!empty($product_doc)){
                    foreach ($product_doc['product_course'] as $v){
                        if($v['course_id'] == $course_id){
                            $return[] = $k;
                            break;
                        }
                    }
                }else{
                    $return[] = $k;
                }
            }else{
                $return[] = $k;
            }

        }

        $this->success('ok',$return);
    }

    /**
     * get_notice
     *
     * @ApiTitle    (查询通知)
     * @ApiSummary  (查询通知)
     * @ApiMethod   (POST)
     * @ApiParams   (name="user_id", type="string", required=true, description="用户id")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{}", description="扩展数据返回")
     * @ApiReturn   ({'code':'1','msg':'返回成功'})
     *
     *
     */
    public function get_notice()
    {
        $user_id = $this->request->request('user_id');
        if (!$user_id) {
            $this->error(__('Invalid parameters'));
        }
        $where = ['user_id'=>$user_id];
        $notice = new \app\admin\model\UserNotice();
        $notice = $notice->with(['user','courseSign','userOrder','notice'])->where($where)->order('time','desc')->select();
        foreach ($notice as &$k){
            if($k['type'] == 'course_sign' || $k['type'] == 'course_start'){
                $course = new \app\admin\model\Course();
                $course= $course->where(['id'=>$k['course_sign']['course_id']])->find();
                if(empty($course)){
                    $course = [];
                }else{
                    $course = $course->toArray();
                }

                $k['course'] = $course;

                $store = new \app\admin\model\Store();
                $store= $store->where(['id'=>$k['course_sign']['store_id']])->find();
                if(empty($store)){
                    $store = [];
                }else{
                    $store = $store->toArray();
                }

                $k['store'] = $store;
            }
        }
        $this->success('ok',$notice);
    }

}