<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Sms;
use think\Validate;
use think\Db;

/**
 * 会员接口
 */
class User extends Api
{
    protected $noNeedLogin = ['mobilelogin','enum_avatar','jssdkBuildConfig'];
    protected $noNeedRight = '*';

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



    /**
     * 手机验证码登录
     *
     * @ApiMethod (POST)
     * @param string $mobile  手机号
     * @param string $captcha 验证码
     */
    public function mobilelogin()
    {
        $mobile = $this->request->post('mobile','','trim,intval');
        $captcha = $this->request->post('captcha','','trim,intval');
        if (!$mobile || !$captcha) {
            $this->error(__('Invalid parameters'));
        }
        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }

        if(!$this->apiLimit(60,3,'60_'.$mobile)){
            $this->error('您的手机号登录频繁，请一分钟后再试');
        };
        if(!$this->apiLimit(3600,10,'3600_'.$mobile)){
            $this->error('您的手机号登录频繁，请一小时后再试!');
        };
        if(!$this->apiLimit(60,10,request()->ip())){
            $this->error('当前登录人数过多，请稍后再试');
        };


        if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
            $this->error(__('Captcha is incorrect'));
        }
        $user = \app\common\model\User::getByMobile($mobile);
        if ($user) {
            if ($user->status != 1) {
                $this->error('网络开小差了请稍后重试');
            }
            //如果已经有账号则直接登录
            $ret = $this->auth->direct($user->id);
        } else {
            //$this->error('注册已截止');
            $ret = $this->auth->register('', '', '', $mobile, []);
        }
        if ($ret) {
            Sms::flush($mobile, 'mobilelogin');
            $this->success(__('Logged in successful'), $this->auth->getUserinfo());
        } else {
            $this->error($this->auth->getError());
        }
    }


    /**
     * 退出登录
     * @ApiMethod (POST)
     */
    public function logout()
    {
        if (!$this->request->isPost()) {
            $this->error(__('Invalid parameters'));
        }
        $this->auth->logout();
        $this->success(__('Logout successful'));
    }

    //备选头像
    public function enum_avatar(){
        $list = Db::name('enum_avatar')->order('id desc')->select();
        $list = list_domain_image($list,['avatar']);
        $this->success(1,$list);
    }

    /**
     * 修改会员个人信息
     *
     * @ApiMethod (POST)
     * @param string $avatar   头像地址
     * @param string $nickname 昵称
     */
    public function profile()
    {
        $avatar   = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
        $nickname = input('nickname', '', 'trim,htmlspecialchars');

        //修改用户
        $data = [];

        if(!empty($avatar))
        {
           $data['avatar'] = $avatar;
        }

        if(!empty($nickname))
        {
            $data['nickname'] = $nickname;
        }

        if(!empty($data)){
            $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
        }

        $this->success();
    }

    //用户详细资料
    public function getuserinfo(){
        $info = $this->auth->getUserinfo();

        $this->success(__('success'),$info);
    }

    /**
     * 微信内H5-JSAPI支付
     */
    public function jssdkBuildConfig() {
        $url = $this->request->request("url");
        $wechat = new \app\common\library\Wechatshare;
        $sign = $wechat->getSignPackage(urldecode($url));
        $this->success("获取成功！",$sign);
    }
}
