<?php

namespace app\index\controller\api;

use library\Controller;
use think\facade\Request;
use think\facade\Session;
use think\Db;

/**
 * 登录
 * Class Index
 * @package app\index\controller
 */
class Login extends Controller
{
    /**
     * @description：登录
     * @date: 2020/5/13 0013
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function login()
    {
        if($this->request->isPost()){
            $data = $this->request->param();
//            if(!isset($data['phone'])||!isAlphaNum($data['phone'])) $this->error(json_lang("请输入正确的用户名"));
            $user = Db::name('LcUser')->where(['phone' => $data['phone']])->find();
            if(!$user) $this->error(json_lang("用户不存在").'！');
            if (!isset($data['password']) || $user['password'] != md5($data['password'])) $this->error(json_lang("登录密码有误，请重试！"));
            if ($user['clock'] == 0) $this->error(json_lang("账号被锁定，请联系管理员！"));
            $loginip=$this->request->ip();
            $token = md5($user['id'] . $user['phone'] . time() . $loginip);
            Db::name('LcUser')->where(['id' => $user['id']])->update(['access_time'=>time(),'logintime'=>time(),'loginip'=>$loginip,'token'=>$token]);
            $user['token'] = $token;
            $this->success(json_lang("登录成功"),$user);
        }
    }
    /**
     * Describe: 客服链接
     * DateTime: 2020/5/14 0:31
     */
    public function service()
    {
        $msg = getInfo('service');
        if ($msg) {
            $this->success(json_lang("操作成功"), $msg);
        } else {
            $this->error('暂无数据');
        }
    }

    public function smsrand()
    {
        $rand = rand(1000, 9999);
        $this->app->session->set('smsRandCode',$rand);
        $this->success('获取成功',$rand);
    }

    public function smsSend(){
        $data = $this->request->param();
        if($this->app->session->get('smsRandCode') != $data['code']) $this->error('验证码错误！');
        $phone = $data['phone'];
        if (!$phone) $this->error("请输入手机号");
        if (Db::name('LcUser')->where(['phone' => $phone])->find()) $this->error(json_lang("该账号已注册！"));
        $sms_time = Db::name("LcSmsList")->where("phone = '$phone'")->order("id desc")->value('time');
        if ($sms_time && (strtotime($sms_time) + 300) > time()) $this->error("验证码五分钟内有效，请勿重复发送");
        $rand_code = rand(1000, 9999);
        Session::set('regSmsCode', $rand_code);
        $data = sendSms($phone, '18001', $rand_code);
        if ($data['code'] == '000') $this->success("操作成功");
        $this->error($data['msg']);
    }

    /**
     * @description：
     * @date: 2020/5/13 0013
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function reg(){
        if(Request::isPost()){
            $data = Request::param();
            if(Db::name('LcUser')->where(['phone' => $data['phone']])->find()) $this->error(json_lang("该账号已注册！"));
            if(strlen($data['password']) < 6 || 16 < strlen($data['password'])) $this->error(json_lang("请输入6-16位密码！"));

            if($data['password'] != $data['password2']){
            	$this->error(json_lang("两次密码不一致"));
            }

            if(strlen($data['password3']) < 6 || 16 < strlen($data['password3'])) $this->error('请输入6-16位支付密码！');
            if($data['password3'] != $data['password4']){
            	$this->error(json_lang("两次支付密码不一致"));
            }
            $top = Db::name('SystemConfig')->where(['value' => $data['top'],'name'=>'inviter_code'])->find();
            if (empty($top)) {
                $this->error(json_lang("邀请人不存在"));
            }
            $reward = Db::name('LcReward')->get(1);
            $add = array(
            	'zcly'=>$_SERVER['SERVER_NAME'],
                'phone'=>$data['phone'],
                'phones'=>$data['phones'],
                'password'=>md5($data['password']),
                'password2'=>md5($data['password3']),
                'mwpassword'=>$data['password'],
                'mwpassword2'=>$data['password3'],
                'top'=>0,
                'logintime'=>time(),
                'money'=>$reward['register'] ?: 0,
                'clock'=>1,
                'value'=>$reward['registerzzz'] ?: 0,
                'time'=>date('Y-m-d H:i:s'),
                'ip'=>$this->request->ip(),
				'loginip'=>$this->request->ip(),
                'member'=>8016,
            );
            $uid = Db::name('LcUser')->insertGetId($add);
            if (empty($uid)) $this->error(json_lang("系统繁忙，注册失败！"));
            if ($reward['register']>0){
                addFinance($uid, $reward['register'],1,'会员注册，系统赠送' . $reward['register'] . '元');
            }
            $this->app->session->set('uid', $uid);
            $this->success(json_lang("注册成功"),$uid);
        }
    }
}
