first commit
This commit is contained in:
379
application/common/service/Member.php
Normal file
379
application/common/service/Member.php
Normal file
@@ -0,0 +1,379 @@
|
||||
<?php
|
||||
// +------------------------------------------------+
|
||||
// |http://www.cjango.com |
|
||||
// +------------------------------------------------+
|
||||
// | 修复BUG不是一朝一夕的事情,等我喝醉了再说吧! |
|
||||
// +------------------------------------------------+
|
||||
// | Author: 小陈叔叔 <Jason.Chen> |
|
||||
// +------------------------------------------------+
|
||||
namespace app\common\service;
|
||||
|
||||
use app\common\model\Member as MemberModel;
|
||||
use app\common\model\MemberInfo as MemberInfoModel;
|
||||
use app\common\model\Template as TemplateModel;
|
||||
use app\common\service\Score as ScoreService;
|
||||
use app\common\validate\Member as MemberValidate;
|
||||
use app\common\validate\MemberInfo as MemberInfoValidate;
|
||||
use think\Config;
|
||||
use think\Request;
|
||||
use think\Session;
|
||||
use tools\Crypt;
|
||||
|
||||
class Member extends _Init
|
||||
{
|
||||
/**
|
||||
* 获取用户信息
|
||||
* @param integer 用户id
|
||||
* @return object
|
||||
*/
|
||||
public static function info($uid)
|
||||
{
|
||||
return MemberInfoModel::get($uid);
|
||||
}
|
||||
|
||||
public static function isBind($uid)
|
||||
{
|
||||
$user = MemberModel::find($uid);
|
||||
if (\tools\Verify::isMobilePhone($user->username)) {
|
||||
return true;
|
||||
} else {
|
||||
return '用户尚未绑定手机号';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑用户资料
|
||||
* @param integer 用户id
|
||||
* @param array 更新的数据
|
||||
* @param string 数据验证的名称
|
||||
* @return boolean|string
|
||||
*/
|
||||
public static function editInfo($uid, $data, $scene = '')
|
||||
{
|
||||
$validate = new MemberInfoValidate();
|
||||
|
||||
if (!$validate->scene($scene)->check($data)) {
|
||||
return $validate->getError();
|
||||
}
|
||||
|
||||
$MemberInfo = self::info($uid);
|
||||
|
||||
if (MemberInfoModel::update($data, ['uid' => $uid])) {
|
||||
if (empty($MemberInfo->$scene)) {
|
||||
ScoreService::setting(UID, $scene);
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return '更新失败';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加 用户资料
|
||||
* @param [type] $user 添加的信息
|
||||
*/
|
||||
private static function createInfo($user)
|
||||
{
|
||||
$data = [
|
||||
'uid' => $user->id,
|
||||
'mobile' => $user->username,
|
||||
];
|
||||
MemberInfoModel::create($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册一个用户
|
||||
* @param string $username 用户名
|
||||
* @param string $password 密码
|
||||
* @param integer $status 初始状态
|
||||
* @return [type] 返回结果
|
||||
*/
|
||||
public static function register($username, $password, $status = null, $pid = 0, $extends = [])
|
||||
{
|
||||
$status = is_numeric($status) ?: (Config::get('new_member_status') ?: 0);
|
||||
$data = [
|
||||
'username' => $username,
|
||||
'password' => $password,
|
||||
'status' => $status,
|
||||
'pid' => $pid,
|
||||
];
|
||||
$validate = new MemberValidate();
|
||||
|
||||
if (!$validate->scene('register')->check($data)) {
|
||||
return $validate->getError();
|
||||
}
|
||||
|
||||
if (is_array($extends) && !empty($extends)) {
|
||||
$data = array_merge($data, $extends);
|
||||
}
|
||||
|
||||
$user = MemberModel::create($data);
|
||||
if ($user) {
|
||||
Logs::write('注册用户', $data);
|
||||
// 自动的注册一个用户资料
|
||||
self::createInfo($user);
|
||||
//增加积分
|
||||
Score::register($user->id);
|
||||
if (!empty($pid)) {
|
||||
Score::invite($pid);
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return '注册失败';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改密码
|
||||
* @param integer $uid 用户编号
|
||||
* @param string $old 旧密码
|
||||
* @param string $new 新密码
|
||||
* @return [type]
|
||||
*/
|
||||
public static function changePassword($uid, $old, $new, $repass)
|
||||
{
|
||||
$data = [
|
||||
'uid' => $uid,
|
||||
'oldpass' => $old,
|
||||
'newpass' => $new,
|
||||
'repass' => $repass,
|
||||
];
|
||||
|
||||
$validate = new MemberValidate();
|
||||
if (!$validate->scene('changepass')->check($data)) {
|
||||
return $validate->getError();
|
||||
}
|
||||
|
||||
return self::updatePassword($uid, $new);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新密码
|
||||
* @param [type] $uid 用户id
|
||||
* @param [type] $password 更新的密码
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public static function updatePassword($uid, $password)
|
||||
{
|
||||
$data = [
|
||||
'password' => $password,
|
||||
];
|
||||
|
||||
$validate = new MemberValidate();
|
||||
|
||||
if (!$validate->scene('updatepass')->check($data)) {
|
||||
return $validate->getError();
|
||||
}
|
||||
|
||||
$user = MemberModel::update($data, ['id' => $uid]);
|
||||
if ($user) {
|
||||
Logs::write('重置密码');
|
||||
return true;
|
||||
} else {
|
||||
return '重置密码失败';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
* @param [type] $username 用户民
|
||||
* @param [type] $password 密码
|
||||
* @return [type] 登录的结果
|
||||
*/
|
||||
public static function login($username, $password)
|
||||
{
|
||||
$data = [
|
||||
'username' => $username,
|
||||
'password' => $password,
|
||||
];
|
||||
|
||||
$validate = new MemberValidate();
|
||||
if (!$validate->scene('login')->check($data)) {
|
||||
return $validate->getError();
|
||||
}
|
||||
|
||||
$user = MemberModel::where('username', $username)->find();
|
||||
|
||||
if (!$user) {
|
||||
return '用户不存在';
|
||||
} elseif ($user->status != 1) {
|
||||
return '用户被禁用';
|
||||
} elseif ($user->password != Crypt::uMd5($password)) {
|
||||
return '密码错误';
|
||||
} else {
|
||||
// 设置session
|
||||
$auth = [
|
||||
'uid' => $user->id,
|
||||
'username' => $user->username,
|
||||
'last_time' => $user->last_time,
|
||||
'last_ip' => $user->last_ip,
|
||||
];
|
||||
|
||||
Session::set('user_auth', $auth);
|
||||
Session::set('user_auth_sign', Crypt::dataAuthSign($auth));
|
||||
// 保存登录记录
|
||||
$save = [
|
||||
'login' => $user->login + 1,
|
||||
'last_time' => time(),
|
||||
'last_ip' => Request::instance()->ip(),
|
||||
];
|
||||
$user->save($save);
|
||||
Logs::write('用户登录成功');
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据openid 登录
|
||||
* @param [type] $openid [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public static function openid_login($openid)
|
||||
{
|
||||
$user = MemberModel::where('openid', $openid)->find();
|
||||
if (!$user) {
|
||||
return '用户不存在';
|
||||
} elseif ($user->status != 1) {
|
||||
return '用户被禁用';
|
||||
} else {
|
||||
// 设置session
|
||||
$auth = [
|
||||
'uid' => $user->id,
|
||||
'username' => $user->username,
|
||||
'last_time' => $user->last_time,
|
||||
'last_ip' => $user->last_ip,
|
||||
];
|
||||
|
||||
Session::set('user_auth', $auth);
|
||||
Session::set('user_auth_sign', Crypt::dataAuthSign($auth));
|
||||
// 保存登录记录
|
||||
$save = [
|
||||
'login' => $user->login + 1,
|
||||
'last_time' => time(),
|
||||
'last_ip' => Request::instance()->ip(),
|
||||
];
|
||||
$user->save($save);
|
||||
Logs::write('用户登录成功');
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出当前账户
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public static function logout()
|
||||
{
|
||||
Logs::write('注销登录');
|
||||
|
||||
Session::delete('user_auth');
|
||||
Session::delete('user_auth_sign');
|
||||
Session::clear();
|
||||
Session::destroy();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改用户状态
|
||||
* @param [type] $uid 要修改的用户id
|
||||
* @param [type] $status 要修改的状态 正常 禁用
|
||||
*/
|
||||
public static function status($uid, $status)
|
||||
{
|
||||
$user = MemberModel::get($uid);
|
||||
|
||||
if (!$user) {
|
||||
return '用户不存在';
|
||||
} elseif ($user->save(['status' => $status])) {
|
||||
Logs::write('修改状态', ['status' => $status]);
|
||||
return true;
|
||||
} else {
|
||||
return '状态修改失败';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登录判断
|
||||
* @return boolean 返回用户id 失败返回0
|
||||
*/
|
||||
public static function isLogin()
|
||||
{
|
||||
$user = Session::get('user_auth');
|
||||
if (empty($user)) {
|
||||
return 0;
|
||||
} else {
|
||||
return Session::get('user_auth_sign') == Crypt::dataAuthSign($user) ? $user['uid'] : 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换模版
|
||||
* @param [type] $uid [description]
|
||||
* @param [type] $tpl [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public static function changeTpl($uid, $tpl)
|
||||
{
|
||||
if (!TemplateModel::get($tpl)) {
|
||||
return '模板不存在';
|
||||
}
|
||||
if (MemberInfoModel::update(['index_tpl' => $tpl], ['uid' => $uid])) {
|
||||
return true;
|
||||
} else {
|
||||
return '操作失败';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定微信
|
||||
* @param [type] $uid [description]
|
||||
* @param [type] $data [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public static function bindWechat($uid, $data)
|
||||
{
|
||||
$validate = new MemberValidate();
|
||||
|
||||
if (!$validate->scene('openid')->check($data)) {
|
||||
return $validate->getError();
|
||||
}
|
||||
$MemberInfo = self::info($uid);
|
||||
$scene = 'openid';
|
||||
if (MemberModel::update($data, ['id' => $uid])) {
|
||||
if (empty($MemberInfo->$scene)) {
|
||||
ScoreService::setting(UID, $scene);
|
||||
}
|
||||
return true;
|
||||
|
||||
} else {
|
||||
return '更新失败';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解除绑定
|
||||
* @param [type] $uid [description]
|
||||
* @param [type] $data [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public static function unBindWechat($uid, $openid)
|
||||
{
|
||||
$ids = MemberModel::where('openid', $openid)->column('id');
|
||||
$data = [
|
||||
'openid' => '',
|
||||
];
|
||||
|
||||
$map = [
|
||||
'id' => ['in', $ids],
|
||||
];
|
||||
|
||||
if (MemberModel::update($data, $map)) {
|
||||
return true;
|
||||
} else {
|
||||
return '解除绑定失败';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user