Files
yuyin-php/application/api/model/UserLogin.php
2025-11-10 14:25:26 +08:00

556 lines
21 KiB
PHP

<?php
namespace app\api\model;
use think\Db;
use think\Model;
use think\facade\Env;
class UserLogin extends Model
{
//注册
public function user_reg($user_name, $password, $reg_code)
{
$pid = 0;
$path = '0';
if (!empty($reg_code)) {
$map = [];
$map[] = ['reg_code', '=', $reg_code];
$p_user_info = Db::name('user')->where($map)->find();
if (empty($p_user_info)) {
return ['code' => 201, 'msg' => '邀请码不存在', 'data' => null];
}
$pid = $p_user_info['uid'];
$path = $p_user_info['path'];
}
$map = [];
$map[] = ['user_name', '=', $user_name];
$user_info = db::name('user')->where($map)->find();
if (!empty($user_info)) {
return ['code' => 201, 'msg' => '用户名已存在', 'data' => null];
}
$data = [];
$uid = $this->get_available_uid(); //获取自增用户id 过滤靓号
$data['uid'] = $uid;
$data['user_name'] = $user_name;
// $nick_name = model('admin/User')->get_rand_nick_name();
$nick_name = '游客'.$uid;
$data['nick_name'] = $nick_name;
$data['base64_nick_name'] = base64_encode($nick_name);
$data['password'] = $password;
$data['pid'] = $pid;
$reg_ip = request()->ip();
$data['reg_ip'] = $reg_ip;
$position = ip_to_position($reg_ip);
$data['country'] = $position['country'];
$data['province'] = $position['province'];
$data['city'] = $position['city'];
$birthday = date('Y-m-d');
$data['birthday'] = $birthday;
$constellation = model('user')->get_user_constellation($birthday);
$data['constellation'] = $constellation['data'];
$data['head_pic'] = 'head_pic/head_pic.png';
$data['autograph'] = '这个人很懒,什么都没写';
$data['hobby'] = '暂无';
$validate = validate('admin/user');
$reslut = $validate->scene('apiAdd')->check($data);
if ($reslut !== true) {
return ['code' => 201, 'msg' => $validate->getError(), 'data' => null];
}
Db::startTrans();
try {
$User = model('admin/user');
$reslut = $User->save($data);
if (!$reslut) {
Db::rollback();
return ['code' => 201, 'msg' => '注册失败', 'data' => null];
};
/*********** 腾讯云IM ********/
$user_sig = model('Tencent')->tencent_user_sig_info($uid);
if(empty($user_sig)){
Db::rollback();
return ['code' => 201, 'msg' => '登录失败', 'data' => null];
}
$data = [];
$data['uid'] = $uid;
$data['path'] = $path . ',' . $uid;
$data['user_sig'] = $user_sig;
$data['is_first_login'] = 1;
$data['update_time'] = time();
$reslut = db::name('user')->where(['uid' => $uid])->update($data);
if (!$reslut) {
Db::rollback();
return ['code' => 201, 'msg' => '注册失败', 'data' => null];
}
model('api/UserWallet')->create_data($uid);
Db::commit();
return ['code' => 200, 'msg' => '注册成功', 'data' => null];
} catch (\Exception $e) {
// 回滚事务
// dump($e);
Db::rollback();
return ['code' => 201, 'msg' => '注册失败', 'data' => null];
}
}
//账号登录
public function user_login($user_name, $password, $last_login_device, $system)
{
if (empty($user_name)) {
return ['code' => 201, 'msg' => '用户名不能为空', 'data' => null];
}
if (empty($password)) {
return ['code' => 201, 'msg' => '密码不能为空', 'data' => null];
}
$map = [];
$map[] = ['user_name', '=', $user_name];
$user_info = db::name('user')->where($map)->find();
if (empty($user_info)) {
return ['code' => 201, 'msg' => '用户名不存在', 'data' => null];
}
if (md5($password) != $user_info['password']) {
return ['code' => 201, 'msg' => '密码错误', 'data' => null];
}
if ($user_info['login_status'] != 1) {
return ['code' => 201, 'msg' => '用户已被封禁', 'data' => null];
}
$configs = get_uncache_system_config();
if($configs['open_verify_token'] == 1){
if(!empty($user_info['login_token'])){
return ['code' => 201, 'msg' => '用户已在线,无法重复登录', 'data' => null];
}
}
//该设备号是否已被封禁
$config = get_system_config();
// if(!empty($config['ban_device'])){
// $ban_device = trim($config['ban_device'],',');
// $ban_device = explode(',',$ban_device);
// if (in_array($last_login_device,$ban_device)) {
// return ['code' => 201, 'msg' => '该设备已被封禁', 'data' => null];
// }
// }
$map = [];
$map[] = ['type', '=', 1];
$map[] = ['type_text', '=', $last_login_device];
$map[] = ['is_delete', '=', 1];
$block_device = db::name('block')->where($map)->find();
if($block_device){
return ['code' => 201, 'msg' => '当前设备号已被封禁。', 'data' => null];
}
//该ip是否被封禁
$login_ip = request()->ip();
$map = [];
$map[] = ['type', '=', 3];
$map[] = ['type_text', '=', $login_ip];
$map[] = ['is_delete', '=', 1];
$block_ip = db::name('block')->where($map)->find();
if($block_ip){
return ['code' => 201, 'msg' => '当前IP地址已被封禁。', 'data' => null];
}
$data = [];
$data['uid'] = $user_info['uid'];
$login_token_string = md5($user_info['uid'] . date('YmdHis') . generateRandom(32));
// if (!empty($user_info['login_token'])) {
// $login_token_string = $user_info['login_token'];
// }
$rid = 0;
if($user_info['is_first_login'] == 1){
$data['is_first_login'] = 2;
$rid_array = db::name('room')->where('room_status', 1)->where('cate_id', 26)->order('today_hot_value desc')->limit(4)->column('rid');
if(!empty($rid_array)){
$rid = $rid_array[array_rand($rid_array,1)];
}
}
$user_sigs = $user_info['user_sig'];
if(empty($user_info['user_sig'])){
$user_sig = model('Tencent')->tencent_user_sig_info($user_info['uid']);
if(empty($user_sig)){
return ['code' => 201, 'msg' => '登录失败', 'data' => null];
}
$data['user_sig'] = $user_sig;
$user_sigs = $user_sig;
}
$data['system'] = $system;
$data['login_token'] = $login_token_string;
$data['last_login_time'] = time();
$data['last_login_device'] = $last_login_device;
$data['login_ip'] = request()->ip();
$data['update_time'] = time();
$reslut = db::name('user')->update($data);
$return_res = [];
$return_res['uid'] = $user_info['uid'];
$return_res['head_pic'] = localpath_to_netpath($user_info['head_pic']);
$return_res['user_name'] = $user_name;
$return_res['login_token'] = $login_token_string;
$return_res['user_sig'] = $user_sigs;
$return_res['rid'] = $rid;
$return_res['nick_name'] = mb_convert_encoding(base64_decode($user_info['base64_nick_name']), 'UTF-8', 'UTF-8');
if (!$reslut) {
return ['code' => 201, 'msg' => '登录失败', 'data' => null];
} else {
$this->add_user_login_log($user_info['uid'], request()->ip(), $last_login_device, 1);
return ['code' => 200, 'msg' => '登录成功', 'data' => $return_res];
}
}
//过滤靓号
private function get_available_uid($uid = 0)
{
if (empty($uid)) {
$uid = db::name('user')->order('uid desc')->value('uid');
if(empty($uid)){
$uid = 310001;
}
}
$uid = $uid + mt_rand(1, 10);
$config = get_system_config();
$filter_uid_arr = explode(",", $config['filter_uid']);
if (in_array($uid, $filter_uid_arr)) {
return $this->get_available_uid();
}
$user_info = db::name('user')->field('uid')->where('uid', $uid)->find();
if (!empty($user_info)) {
return $this->get_available_uid($user_info['uid']);
} else {
return $uid;
}
}
//验证码登录
public function phone_verification_code_log($user_name, $last_login_device, $system)
{
if (empty($user_name)) {
return ['code' => 201, 'msg' => '用户名不能为空', 'data' => null];
}
$map = [];
$map[] = ['user_name', '=', $user_name];
$user_info = db::name('user')->where($map)->find();
$configs = get_uncache_system_config();
//该设备号是否已被封禁
$config = get_system_config();
// if(!empty($config['ban_device'])){
// $ban_device = trim($config['ban_device'],',');
// $ban_device = explode(',',$ban_device);
// if (in_array($last_login_device,$ban_device)) {
// return ['code' => 201, 'msg' => '该设备已被封禁', 'data' => null];
// }
// }
$map = [];
$map[] = ['type', '=', 1];
$map[] = ['type_text', '=', $last_login_device];
$map[] = ['is_delete', '=', 1];
$block_device = db::name('block')->where($map)->find();
if($block_device){
return ['code' => 201, 'msg' => '当前设备号已被封禁。', 'data' => null];
}
//该ip是否被封禁
$login_ip = request()->ip();
$map = [];
$map[] = ['type', '=', 3];
$map[] = ['type_text', '=', $login_ip];
$map[] = ['is_delete', '=', 1];
$block_ip = db::name('block')->where($map)->find();
if($block_ip){
return ['code' => 201, 'msg' => '当前IP地址已被封禁。', 'data' => null];
}
Db::startTrans();
try {
if($user_info){//登录
if ($user_info['login_status'] != 1) {
return ['code' => 201, 'msg' => '用户已被封禁', 'data' => null];
}
if($configs['open_verify_token'] == 1){
if(!empty($user_info['login_token'])){
return ['code' => 201, 'msg' => '用户已在线,无法重复登录', 'data' => null];
}
}
$data = [];
$data['uid'] = $user_info['uid'];
$login_token_string = md5($user_info['uid'] . date('YmdHis') . generateRandom(32));
$user_sigs = $user_info['user_sig'];
if(empty($user_info['user_sig'])){
$user_sig = model('Tencent')->tencent_user_sig_info($user_info['uid']);
if(empty($user_sig)){
return ['code' => 201, 'msg' => '登录失败', 'data' => null];
}
$data['user_sig'] = $user_sig;
$user_sigs = $user_sig;
}
$data['system'] = $system;
$data['login_token'] = $login_token_string;
$data['last_login_time'] = time();
$data['last_login_device'] = $last_login_device;
$data['login_ip'] = request()->ip();
$data['update_time'] = time();
// $data['is_online'] = 1;
$reslut = db::name('user')->update($data);
$return_res = [];
$return_res['uid'] = $user_info['uid'];
$return_res['head_pic'] = localpath_to_netpath($user_info['head_pic']);
$return_res['user_name'] = $user_name;
$return_res['login_token'] = $login_token_string;
$return_res['user_sig'] = $user_sigs;
$return_res['nick_name'] = mb_convert_encoding(base64_decode($user_info['base64_nick_name']), 'UTF-8', 'UTF-8');
$return_res['is_reg'] = 2;
$return_res['rid'] = 0;
if (!$reslut) {
return ['code' => 201, 'msg' => '登录失败', 'data' => null];
}
$uid = $user_info['uid'];
}else{//注册
$path = '0';
$data = [];
$uid = $this->get_available_uid(); //获取自增用户id 过滤靓号
$data['uid'] = $uid;
$data['user_name'] = $user_name;
// $nick_name = model('admin/User')->get_rand_nick_name();
$nick_name = '游客'.$uid;
$data['nick_name'] = $nick_name;
$data['base64_nick_name'] = $base64_nick_name = base64_encode($nick_name);
// $data['password'] = $password;
// $data['pid'] = $pid;
$reg_ip = request()->ip();
$data['reg_ip'] = $reg_ip;
$position = ip_to_position($reg_ip);
$data['country'] = $position['country'];
$data['province'] = $position['province'];
$data['city'] = $position['city'];
$birthday = date('Y-m-d');
$data['birthday'] = $birthday;
$constellation = model('user')->get_user_constellation($birthday);
$data['constellation'] = $constellation['data'];
// $head_pic = 'head_pic/bc6053e1-0b93-4d9f-b130-cd336397f8e0.png';
$data['head_pic'] = 'head_pic/head_pic.png';
$data['autograph'] = '这个人很懒,什么都没写';
$data['hobby'] = '暂无';
$data['system'] = $system;
// $validate = validate('admin/user');
// $reslut = $validate->scene('apiAdd')->check($data);
// if ($reslut !== true) {
// return ['code' => 201, 'msg' => $validate->getError(), 'data' => null];
// }
$data['is_online'] = 1;
$User = model('admin/user');
$reslut = $User->save($data);
if (!$reslut) {
Db::rollback();
return ['code' => 201, 'msg' => '登录失败', 'data' => null];
};
$head_pic = localpath_to_netpath($data['head_pic']);
/*********** 腾讯云IM ********/
$user_sig = model('Tencent')->tencent_user_sig_info($uid);
if(empty($user_sig)){
Db::rollback();
return ['code' => 201, 'msg' => '登录失败', 'data' => null];
}
$data = [];
$data['uid'] = $uid;
$data['path'] = $path . ',' . $uid;
$data['user_sig'] = $user_sig;
$data['last_login_time'] = time();
$data['last_login_device'] = $last_login_device;
$data['is_first_login'] = 2;
$data['login_ip'] = request()->ip();
$data['login_token'] = $login_token_string = md5($uid . date('YmdHis') . generateRandom(32));
$data['update_time'] = time();
$reslut = db::name('user')->where(['uid' => $uid])->update($data);
if (!$reslut) {
Db::rollback();
return ['code' => 201, 'msg' => '登录失败', 'data' => null];
}
$rid = 0;
$rid_array = db::name('room')->where('room_status', 1)->where('cate_id', 26)->order('today_hot_value desc')->limit(4)->column('rid');
if(!empty($rid_array)){
$rid = $rid_array[array_rand($rid_array,1)];
}
$return_res = [];
$return_res['uid'] = $uid;
$return_res['head_pic'] = $head_pic;
$return_res['user_name'] = '';
$return_res['login_token'] = $login_token_string;
$return_res['user_sig'] = $user_sig;
$return_res['nick_name'] = mb_convert_encoding(base64_decode($base64_nick_name), 'UTF-8', 'UTF-8');
$return_res['is_reg'] = 1;
$return_res['rid'] = $rid;
model('api/UserWallet')->create_data($uid);
}
$this->add_user_login_log($uid, $login_ip, $last_login_device, 2);
Db::commit();
return ['code' => 200, 'msg' => '登录成功', 'data' => $return_res];
} catch (\Exception $e) {
// 回滚事务
dump($e);
Db::rollback();
return ['code' => 201, 'msg' => '登录失败', 'data' => null];
}
}
//修正
public function batch_create_test_account(){
$num = 20;
for($i=1;$i<=$num;$i++){
$data_result = $this->create_test_account();
}
return ['code' => 200, 'msg' => '操作成功', 'data' => null];
}
//单个生成账号
public function create_test_account(){
$pid = 0;
$path = '0';
$map = [];
//根据号段生成账号
$user_name = $this->hao_duan_create();
if($user_name === false){
return ['code' => 201, 'msg' => '注册失败0', 'data' => null];
}
//随机生成秘钥信息
$pwd = generateRandom(6);
$password = md5($pwd);
$data = [];
$uid = $this->get_available_uid(); //获取自增用户id 过滤靓号
$data['uid'] = $uid;
$data['user_name'] = $user_name;
$nick_name = model('admin/User')->get_rand_nick_name();
// $nick_name = '游客'.$uid;
$data['nick_name'] = $nick_name;
$data['base64_nick_name'] = base64_encode($nick_name);
$data['password'] = $password;
$data['pid'] = $pid;
// $reg_ip = request()->ip();
// $data['reg_ip'] = $reg_ip;
// $position = ip_to_position($reg_ip);
// $data['country'] = $position['country'];
// $data['province'] = $position['province'];
// $data['city'] = $position['city'];
$birthday = date('Y-m-d');
$data['birthday'] = $birthday;
$constellation = model('api/user')->get_user_constellation($birthday);
$data['constellation'] = $constellation['data'];
$sex = mt_rand(1,2);
$data['sex'] = $sex;
$data['is_tester'] = 2;
$rand_num = rand(1,16);
$head_pic = "r_head_pic/head_pic{$rand_num}.jpg";
// $head_pic = 'head_pic/head_pic.png';
$data['head_pic'] = $head_pic;
$data['autograph'] = '这个人很懒,什么都没写';
$data['hobby'] = '暂无';
$data['is_sys_tester'] = 1;
$data['login_status'] = 1;
$data['charm_level'] = 1;
$data['contribution_level'] = 1;
$data['is_real'] = 2;
$data['is_can_recharge'] = 2;
$data['real_name'] = '';
$data['is_sign'] = 1;
$data['path'] = $path . ',' . $uid;
$data['add_time'] = time();
$user_sig = model('api/Tencent')->tencent_user_sig_info($uid);
$data['user_sig'] = $user_sig;
try {
Db::startTrans();
db::name('user')->insert($data);
$data1 = [
'uid' => $uid,
'pwd' => $pwd,
'add_time' => time(),
'update_time' => time()
];
db::name('user_lu_pwd')->insert($data1);
model('api/UserWallet')->create_data($uid);
Db::commit();
}catch (\Exception $e) {
// 回滚事务
Db::rollback();
halt($e);
return ['code' => 201, 'msg' => '操作失败', 'data' => null];
}
}
//虚拟号段
public function hao_duan_create(){
$user_name = generateRandomPhoneNumber();
$user_name_exist = db::name('user')->where('user_name','=',$user_name)->field('uid')->find();
if(!empty($user_name_exist)){
// return $this->hao_duan_create();
return false;
}
return $user_name;
}
//登录记录表
public function add_user_login_log($uid, $login_ip, $login_device, $type = 1){
$insert = [];
$insert['uid'] = $uid;
$insert['type'] = $type;
$insert['login_ip'] = $login_ip;
$insert['login_device'] = $login_device;
$insert['add_time'] = time();
db::name('user_login_log')->insert($insert);
}
}