<?php
namespace app\user\controller;
use think\Db;
/**
* 
*/
class Sendsms extends Base
{
	// 注册验证码
	public function Regverify()
	{
		if(request()->isPost())
		{
			$data = input('post.');
			$validate = new \think\Validate;
			$rule = ['mobile|手机号' => 'require|mobile'];
			$validate->rule($rule);
			if (!$validate->check($data)) {
				$this->error($validate->getError());
			}
			$user = model('user')->where('mobile',$data['mobile'])->find();
			if($user) {
				$this->error('用户已存在!');
			}
			$smscookie = cookie('smsreg_'.$data['mobile']);
			if($smscookie) {
				if((time() - $smscookie['time']) < 60) {
					$this->error('60秒内只能提交一次！');
				}
			} else {
				cookie('smsreg_'.$data['mobile'],['time' => time(), 'code' => random(4,'number')],300);
				$smscookie = cookie('smsreg_'.$data['mobile']);
			}
			$this->success('测试验证码为:'.$smscookie['code']);
		}
	}

	// 验证码登录
	public function Findverify()
	{
		if(request()->isPost())
		{
			$data = input('post.');
			$validate = new \think\Validate;
			$rule = ['mobile|手机号' => 'require|mobile'];
			$validate->rule($rule);
			if (!$validate->check($data)) {
				$this->error($validate->getError());
			}
			$user = model('user')->where('mobile',$data['mobile'])->find();
			if(!$user) {
				$this->error('用户不存在!');
			}
			$smscookie = cookie('smsfind_'.$data['mobile']);
			if($smscookie) {
				if((time() - $smscookie['time']) < 60) {
					$this->error('60秒内只能提交一次！');
				}
			} else {
				cookie('smsfind_'.$data['mobile'],['time' => time(), 'code' => random(4,'number')],300);
				$smscookie = cookie('smsfind_'.$data['mobile']);
			}
			$this->success('测试验证码为:'.$smscookie['code']);
		}
	}
}