chore: 重写初始提交(清空历史,整理后全量提交)
This commit is contained in:
@@ -0,0 +1,251 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace ywxapp\library;
|
||||
|
||||
use think\exception\HttpException;
|
||||
use think\facade\Db;
|
||||
use think\facade\Event;
|
||||
use ywxapp\library\Result;
|
||||
use ywxapp\model\Rule as MemberRule;
|
||||
use ywxapp\model\MemberUser as UsersModel;
|
||||
|
||||
/**
|
||||
* Member 类
|
||||
*
|
||||
* @author ywxapp <admin@ywxapp.cn>
|
||||
*/
|
||||
class Member
|
||||
{
|
||||
/***
|
||||
* 是否登录
|
||||
* @var bool
|
||||
*/
|
||||
protected $isLogin = false;
|
||||
|
||||
/**
|
||||
* Summary of config
|
||||
* @var array
|
||||
*/
|
||||
protected $config = [];
|
||||
|
||||
/**
|
||||
* Summary of options
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
* 登录标识
|
||||
* @var bool
|
||||
*/
|
||||
protected $token;
|
||||
|
||||
/**
|
||||
* 个人信息
|
||||
* @return \ywxapp\model\Member
|
||||
*/
|
||||
protected $info;
|
||||
|
||||
/**
|
||||
* 权限规则
|
||||
* @return array
|
||||
*/
|
||||
protected $powers;
|
||||
|
||||
/**
|
||||
* 角色
|
||||
* @var array
|
||||
*/
|
||||
protected $roles;
|
||||
|
||||
/**
|
||||
* 用户资料,用于临时存放一些用户数据,比如用户IP地址,需要使用后销毁
|
||||
* @var array
|
||||
*/
|
||||
protected $profile;
|
||||
|
||||
/**
|
||||
* 允许查询的字段
|
||||
* @var array
|
||||
*/
|
||||
protected $allowFields = ['id', 'account', 'nickname', 'roles', 'avatar', 'score'];
|
||||
|
||||
|
||||
public function __construct($options = [])
|
||||
{
|
||||
$this->info = new \stdClass();
|
||||
// if ($config = Config::get('member')) {
|
||||
// $this->config = array_merge($this->config, $config);
|
||||
// }
|
||||
// $this->options = array_merge($this->config, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化用户信息
|
||||
*/
|
||||
public function initUser($uid)
|
||||
{
|
||||
$this->direct($uid);
|
||||
return $this;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接登录账号.
|
||||
*
|
||||
* @param int $id
|
||||
* @return void
|
||||
*/
|
||||
public function direct($uid)
|
||||
{
|
||||
$info = UsersModel::findOrEmpty($uid);
|
||||
if ($info->isEmpty()) {
|
||||
Result::instance()->error("Uid:$uid is incorrect");
|
||||
}
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
if ($info->update_time < \ywxapp\utils\Date::unixtime('day')) {
|
||||
$info->successions = $info->logintime < \ywxapp\utils\Date::unixtime(
|
||||
'day',
|
||||
-1
|
||||
) ? 1 : $info->successions + 1;
|
||||
$info->maxsuccessions = max($info->successions, $info->maxsuccessions);
|
||||
}
|
||||
// $user->prevtime = $user->logintime;
|
||||
//记录本次登录的IP和时间
|
||||
//$user->loginip =request()->ip();
|
||||
//重置登录失败次数
|
||||
//$user->loginfailure = 0;
|
||||
$info->save();
|
||||
$this->info = $info;
|
||||
// $this->roles = $info->roles;
|
||||
// $this->powers = $info->roles;
|
||||
$this->isLogin = true;
|
||||
//登录成功的事件
|
||||
Event::trigger('user_login_successed', $this->info);
|
||||
Db::commit();
|
||||
} catch (\think\Exception $e) {
|
||||
Db::rollback();
|
||||
throw new HttpException(4001, $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员组别规则列表.
|
||||
* @return array
|
||||
*/
|
||||
public function getRuleList()
|
||||
{
|
||||
if ($this->powers) {
|
||||
return $this->powers;
|
||||
}
|
||||
|
||||
if (! $$this->roles) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$power = [];
|
||||
foreach ($this->roles as $role) {
|
||||
$powers = array_merge($powers, explode(',', $role->powers));
|
||||
}
|
||||
$powers = MemberRule::whereIn('id', implode(',', $power))
|
||||
->where('status', 0)
|
||||
->field('id,pid,name,title,ismenu')
|
||||
->select();
|
||||
$this->powers = $powers;
|
||||
return $this->powers;
|
||||
}
|
||||
|
||||
|
||||
private function getEncryptPassword($password, $salt = '')
|
||||
{
|
||||
return md5(md5($password) . $salt);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户权限验证
|
||||
*/
|
||||
public function verifyAuth($noNeedLogin = ["*"], $noNeedRight = ["*"])
|
||||
{
|
||||
$request = app()->request;
|
||||
$modulename = app()->http->getName();
|
||||
$controllername = strtolower($request->controller());
|
||||
$actionname = strtolower($request->action());
|
||||
|
||||
$path = str_replace('.', '/', $controllername) . '/' . $actionname;
|
||||
if (! $this->match($noNeedLogin)) {
|
||||
if (! $this->isLogin) {
|
||||
Result::instance()->error('Please login first');
|
||||
}
|
||||
|
||||
// 判断是否需要验证权限
|
||||
if (! $this->match($noNeedRight) && ! $this->check($path)) {
|
||||
Result::instance()->error('You have no permission');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测当前控制器和方法是否匹配传递的数组.
|
||||
* @param array $arr 需要验证权限的数组
|
||||
* @return bool
|
||||
*/
|
||||
private function match($arr = [])
|
||||
{
|
||||
if (in_array(strtolower(app()->request->action()), $arr) || in_array('*', $arr)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测是否是否有对应权限.
|
||||
* @param string $path 控制器/方法
|
||||
* @param string $module 模块 默认为当前模块
|
||||
* @return bool
|
||||
*/
|
||||
private function check($path = null, $module = null)
|
||||
{
|
||||
$ruleList = $this->getRuleList();
|
||||
$rules = [];
|
||||
foreach ($ruleList as $k => $v) {
|
||||
$rules[] = $v['name'];
|
||||
}
|
||||
$url = strtolower(str_replace('.', '/', $path));
|
||||
return in_array($url, $rules) ? true : false;
|
||||
}
|
||||
|
||||
|
||||
public static function instance($options = [])
|
||||
{
|
||||
return app()->auth;
|
||||
}
|
||||
|
||||
|
||||
public function __get($name)
|
||||
{
|
||||
if ($name == 'info' && $this->info != null) {
|
||||
return $this->info;
|
||||
//return array_intersect_key($this->info->toArray(), array_flip($this->allowFields));
|
||||
}
|
||||
|
||||
return $this->$name;
|
||||
}
|
||||
|
||||
|
||||
public function __set($name, $value)
|
||||
{
|
||||
$this->$name = $value;
|
||||
}
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user