<?php
/**
 * @Statement: [ An Ordinary Person ]
 * @Author: 闻子 < QQ：270988107 >
 * @Copyright: 闻子 2016-2019 All rights reserved.
 *
 *    .--,       .--,
 *   ( (  \.---./  ) )
 *    '.__/o   o\__.'
 *       {=  ^  =}
 *        >  -  <
 *       /       \
 *      //       \\
 *     //|   .   |\\
 *     "'\       /'"_.-~^`'-.
 *        \  _  /--'         `
 *      ___)( )(___
 *     (((__) (__)))    高山仰止,景行行止.虽不能至,心向往之.
 *
 */

namespace app\common\logic;

use think\Db;

class User extends Basedata
{
	/**
	 * 定义模型
	 * @Author: 闻子 < QQ：270988107 >
	 */
    public static $UserModel = null;

	/**
	 * 实例化模型
	 * @Author: 闻子 < QQ：270988107 >
	 */
    public function __construct()
    {
        parent::__construct();
        self::$UserModel = model($this->name);
    }

	/**
	 * 获取用户数据
	 * @Author: 闻子 < QQ：270988107 >
	 */
    public function getUserInfo($where = [], $field = true)
    {
        return self::$UserModel->getInfo($where, $field);
    }

    /**
     * 会员统计数据逻辑
     * @Author 闻子 <270988107@qq.com>
     */
    public function getUserCount($where = [])
    {
        return self::$UserModel->stat($where);
    }

    /**
     * 会员编辑逻辑
     * @Author 闻子 <270988107@qq.com>
     */
    public function getedit($data)
    {
        $url = url('/ohbbs/config/user');
        return self::$UserModel->setInfo($data) ? ['code' => 1, 'msg' => '提交成功', 'data' => '', 'url' => $url] : ['code' => 0, 'msg' => '提交失败', 'data' => '', 'url' => $url];
    }

    /**
     * 会员删除逻辑
     * @Author 闻子 <270988107@qq.com>
     */
    public function getUserDel($id)
    {
        if (empty($id)) {
            return ["ERROR", '参数异常'];
        }
        $data['id'] = $id;
        $url = url('/ohbbs/config/user');
        return self::$UserModel->deleteyi($data) ? ['code' => 1, 'msg' => '删除成功', 'data' => '', 'url' => $url] : ['code' => 0, 'msg' => '删除失败', 'data' => '', 'url' => $url];
    }

    /**
     * 批量删除会员逻辑
     * @Author 闻子 <270988107@qq.com>
     */
    public function getUseralldel($data)
    {
        $str = rtrim($data, ","); 
        $row = self::$UserModel->deleteyi($str);
        $url = url('/ohbbs/config/user');
        return $row ? ['code' => 1, 'msg' => '删除成功', 'data' => '', 'url' => $url] : ['code' => 1, 'msg' => '删除失败', 'data' => '', 'url' => $url];
    }

	/**
	 * 会员登录处理逻辑
	 * @Author: 闻子 < QQ：270988107 >
	 */
    public function logindetection($data)
    {
        $UserData = self::getUserInfo(['tel' => $data['tel']]);
        if (empty($UserData)) {
            return [RESULT_ERROR,'手机号不存在'];
        }
        if ($UserData['juge'] == 1) {
            return [RESULT_ERROR,'您账号已被管理员禁用'];
        }
        if ($UserData['password'] == md5($data['password'])) {
            if ($UserData['auth_id'] != 0) {
                if (date('Y-m-d H:i:s')>$UserData['enddateline']) {
                    Db::name('user')->where('id',$UserData['id'])->update(['auth_id' => '0', 'enddateline' => '0000-00-00 00:00:00']);
                }
            }
            session('admin.id',$UserData['id']);
            session('admin.tel',$UserData['tel']);
            return [RESULT_SUCCESS,'登录成功','/member/index'];
        } else {
            return [RESULT_ERROR,'密码错误'];
        }
    }

	/**
	 * 后台登录处理逻辑
	 * @Author: 闻子 < QQ：270988107 >
	 */
    public function admindetection($data)
    {
        $UserData = self::getUserInfo(['tel' => $data['tel']]);
        if (empty($UserData)) {
            return [RESULT_ERROR,'手机号不存在'];
        }
        if ($UserData['manager'] != 1) {
            return [RESULT_ERROR,'您无权限登录此页面'];
        }
        $captcha = new \think\captcha\Captcha();
        $result = $captcha->check($data['verity']);
       	if($result === false){
            return [RESULT_ERROR,'验证码错误'];
        }
        if ($UserData['password'] == md5($data['password'])) {
            session('admin.id',$UserData['id']);
            session('admin.tel',$UserData['tel']);
            return [RESULT_SUCCESS,'登录成功','/ohbbs/index'];
        } else {
            return [RESULT_ERROR,'密码错误'];
        }
    }

    /**
     * 注册处理逻辑
     * @Author: 闻子 < QQ：270988107 >
     */
    public function regUserRes($data)
    {
        $validate = validate($this->name);
        $validate_result = $validate->check($data);
        if (!$validate_result) {
            return [RESULT_ERROR, $validate->getError()];
        }
        if (cookie('checkcode') == null) {
            return [RESULT_ERROR, '手机验证码超时，请重新发送'];
        }
        if (cookie('checkcode') != $data['code']) {
            return [RESULT_ERROR, '手机验证码超时，请重新发送'];
        }
        if ($data['password'] != $data['newpassword']) {
        	return [RESULT_ERROR, '两次密码不一样，请重新输入'];
        }
        $phone = self::getUserInfo(['tel' => $data['tel']]);
        if ($phone) {
            return [RESULT_ERROR, '该手机号码已被注册，请重新注册'];
        }
        $data['password'] = md5($data['password']);
        $data['avatar'] = "/public/static/skin/img/avatar_90.png";
        $data['create_time'] = date('Y-m-d H:i:s');
        $data['update_time'] = date('Y-m-d H:i:s');
        $url = url('/member/login');
        return self::$UserModel->setInfo($data) ? [RESULT_SUCCESS, '注册成功', $url] : [RESULT_ERROR, self::$UserModel->getError()];
    }

    /**
     * 修改密码逻辑
     */
    public function changePassword($data)
    {
        $validate = validate('changePassword');
        $validate_result = $validate->check($data);
        if (!$validate_result) {
            return [RESULT_ERROR, $validate->getError()];
        }
        $jugepass = self::getUserInfo(['password' => md5($data['passworda'])]);
        if (!$jugepass) {
            return [RESULT_ERROR, '当前密码不正确'];
        }
        if ($data['password'] != $data['passwordb']) {
            return [RESULT_ERROR, '两次输入密码不一致'];
        }
        if ($jugepass['password'] == md5($data['password'])) {
            return [RESULT_ERROR, '不可以使用近期的密码'];
        }
        session(null);
        $url = url('index/member/login');
        $data['password'] = md5($data['password']);
        $data['update_time'] = date('Y-m-d H:i:s');
        return self::$UserModel->setInfo($data) ? [RESULT_SUCCESS, '修改成功', $url] : [RESULT_ERROR, self::$UserModel->getError()];
    }

}