<?php
namespace app\admin\controller;
use think\Controller;

/**
*	管理父类
*/
class Admin extends Controller{
	
	protected $user = null;
	protected function _initialize(){
		$site = cnf('site');
		$this->assign('site',$site);

		$this->init();
	}

	public function init(){
		if (!$this->user = is_login('admin')) {
			$isAjax = $this->request->isAjax();
			if($isAjax){
				return json(['data'=>null,'code'=>1001,'msg'=>'登录超时,请重新登录']);
			}else{
	            $login_url = url("Login/index");
	            $result =  redirect($login_url);
	            $result->send();exit();
			}
        }
        $this->assign('user',$this->user);
        oplog('admin',$this->user['id']);
	}

	public function output($code,$msg,$data = [],$count = 0){
	    try {
	      // 返回JSON数据格式到客户端 包含状态信息
	      $count = $count == 0?count($data):$count;
	      $data = json_encode(['code'=>$code,'msg'=>$msg,'data'=>$data,'count'=>$count], JSON_UNESCAPED_UNICODE);
	      if ($data === false) {
	        throw new \InvalidArgumentException(json_last_error_msg());
	      }

	      header('Content-type: application/json');
	      echo $data;exit();
	    } catch (\Exception $e) {
	      if ($e->getPrevious()) {
	        throw $e->getPrevious();
	      }
	      throw $e;
	    }
  	}

  	public function changepwd(){
        if ($this->request->isPost()) {
            $user_id = $this->user['id'];
            $User = model('User');
            $user = $User->where(['id'=>$user_id])->find();
            if (!$user) {
            	$this->output(1,'参数错误');
            }
            $password = input('post.password');
            if (!empty($password)) {
            	$oldpwd = input('post.oldpwd');
	            $salt = $user['salt'];
	            if ($user['password'] != md5(md5($oldpwd).$salt) ) {
	            	$this->output(1,'登录旧密码错误');
	            }
	            $user->password = md5(md5($password).$salt);;
            }
            $mobile = input('post.mobile');
            $user['mobile'] = $mobile;
            $result = $user->save();
            if (!$result) {
            	$this->output(1,'修改失败');
            }
            $this->user['mobile'] = $mobile;
	      	session('admin_auth', $this->user);

            $this->output(0,'修改成功');
        }else{
        	$this->assign('mobile',$this->user['mobile']);
            $this->assign('name',$this->user['name']);
            $this->assign('meta_title','修改密码');
            return $this->fetch();
        }
    }

    public function sendsms(){
	    $mobile = input('param.mobile');
	    if (empty($mobile)) {
	      $this->output(1,'手机不能为空');
	    }

	    $SmsCode = model('SmsCode');
	    $code = $SmsCode->where(['mobile'=>$mobile,'state'=>0])->find();
	    $time = $this->request->time();
	    $value = rand(1000,9999);
	    if ($code && $code['expire_time'] > $time) {
	      $value = $code['value'];
	      $create_time = strtotime($code['create_time']);
	      if ($create_time + 60 >= $time) {
	        $this->output(1,'请稍后重试');
	      }
	    }else{
	      $SmsCode->value = $value;
	      $SmsCode->expire_time = $time + 5 * 60;
	      $SmsCode->mobile = $mobile;
	      $SmsCode->state = 0;
	      $SmsCode->save();
	    }

	    vendor('aliyun.Dysms');
	    $result = \Dysms::send('帮帮福利网','SMS_224990133',$mobile,['code'=>$value]);
	    // var_dump($result);exit();
	    if ($result) {
	      $this->output(0,'发送成功');
	    }
	    $this->output(1,'发送失败');
	}

	public function checksms($mobile,$output = false){
	    $smscode = input('param.smscode');
	    if (empty($smscode)) {
	      $this->output(1,'验证码不能为空');
	    }

	    $SmsCode = model('SmsCode');
	    $code = $SmsCode->where(['mobile'=>$mobile,'state'=>0])->order('id desc')->find();
	    
	    $time = $this->request->time();
	    if (!$code || $code['expire_time'] < $time) {
	      $this->output(1,'验证码已过期,请重新获取');
	    }

	    if ($code['value'] != $smscode) {
	      $this->output(1,'验证码错误');
	    }
	    $code->state = 1;
	    $code->save();
	    if ($output) {
	      $this->output(0,'验证码正确');
	    }
	    return true;
  	}
}