309 lines
10 KiB
PHP
309 lines
10 KiB
PHP
<?php
|
|
|
|
namespace app\api\model;
|
|
|
|
use app\common\library\DatabaseRoute;
|
|
use app\common\model\BaseModel;
|
|
use app\common\model\Common;
|
|
use app\common\model\SysUser;
|
|
use ba\Exception;
|
|
use app\exception\SysException;
|
|
use extend\ba\Random;
|
|
use think\facade\Db;
|
|
use support\Log;
|
|
|
|
class TbUser extends BaseModel
|
|
{
|
|
// 查询username
|
|
public static function GetByusername($username, $field = 'phone')
|
|
{
|
|
// 全表扫描username
|
|
$dbmap = config('think-orm.db_map');
|
|
foreach ($dbmap as $dbname) {
|
|
if(!in_array($dbname, config('think-orm.unset_db_map'))) {
|
|
$connect = Db::connect($dbname);
|
|
$data = $connect->name('tb_user')->where([$field => $username])->find();
|
|
if($data) {
|
|
return $data;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
|
|
public static function GetByuserInvite($inviter_code)
|
|
{
|
|
// // 全表扫描username
|
|
// $dbmap = config('think-orm.db_map');
|
|
// $count = 0;
|
|
// foreach ($dbmap as $dbname) {
|
|
// if(!in_array($dbname, config('think-orm.unset_db_map'))) {
|
|
// $connect = Db::connect($dbname);
|
|
// $data = $connect->name('tb_user')->where(['inviter_code' => $inviter_code])->count();
|
|
// $count += $data;
|
|
// }
|
|
// }
|
|
//
|
|
$count = DatabaseRoute::getAllDbData('tb_user', function ($query) use ($inviter_code) {
|
|
return $query->where(['inviter_code' => $inviter_code]);
|
|
})->count();
|
|
return $count;
|
|
}
|
|
|
|
public static function GetByuserInviteselect($invitation_code, $page, $limit)
|
|
{
|
|
$data_list = [];
|
|
$data_arr = DatabaseRoute::paginateAllDb('tb_user', function ($query) use ($invitation_code) {
|
|
return $query->where(['inviter_code' => $invitation_code])->field('user_id,avatar,user_name,user_id');
|
|
}, $page, $limit);
|
|
if(!empty($data_arr['list'])) {
|
|
foreach ($data_arr['list'] as $k => $v) {
|
|
$data_list[] = [
|
|
'user_id' => (string) $v['user_id'],
|
|
'avatar' => $v['avatar'],
|
|
'user_name' => $v['user_name'],
|
|
];
|
|
}
|
|
}
|
|
return $data_list;
|
|
|
|
|
|
// $dbmap = config('think-orm.db_map');
|
|
// $data_list = [];
|
|
// foreach ($dbmap as $dbname) {
|
|
//
|
|
// if(!in_array($dbname, config('think-orm.unset_db_map'))) {
|
|
// $connect = Db::connect($dbname);
|
|
// $data_arr = $connect->name('tb_user')->where(['inviter_code' => $invitation_code])->field('user_id,avatar,user_name,user_id')->limit(page($page, $limit), $limit)->select()->toArray();
|
|
// if($data_arr) {
|
|
// foreach ($data_arr as $k => $v) {
|
|
// $data_list[] = [
|
|
// 'user_id' => $v['user_id'],
|
|
// 'avatar' => $v['avatar'],
|
|
// 'user_name' => $v['user_name'],
|
|
// ];
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
// Log::write('下级列表:' . json_encode($data_arr));
|
|
// return $data_list;
|
|
|
|
|
|
}
|
|
|
|
public static function getByUserIdOrInviterCode($userId, $inviterCode)
|
|
{
|
|
$user = $userId ? DatabaseRoute::getDb('tb_user', $userId)->where([
|
|
'user_id' => $userId
|
|
])->find() : null;
|
|
return $user ?? self::GetByinvitationCode($inviterCode);
|
|
}
|
|
|
|
public static function GetByinvitationCode($invitation_code)
|
|
{
|
|
// 全表扫描username
|
|
$dbmap = config('think-orm.db_map');
|
|
foreach ($dbmap as $dbname) {
|
|
if(!in_array($dbname, config('think-orm.unset_db_map'))) {
|
|
$connect = Db::connect($dbname);
|
|
$data = $connect->name('tb_user')->where(['invitation_code' => $invitation_code])->find();
|
|
if($data) {
|
|
return $data;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
|
|
public static function GetByinviterCodeCount($inviter_code)
|
|
{
|
|
// 全表扫描username
|
|
$dbmap = config('think-orm.db_map');
|
|
foreach ($dbmap as $dbname) {
|
|
if(!in_array($dbname, config('think-orm.unset_db_map'))) {
|
|
$connect = Db::connect($dbname);
|
|
$data = $connect->name('tb_user')->where(['inviter_code' => $inviter_code])->count();
|
|
if($data) {
|
|
return $data;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static function register($data):array
|
|
{
|
|
if(empty($data['password']) || empty($data['phone']) || empty($data['msg']) || empty($data['platform'])) {
|
|
return returnErrorData('参数不完整');
|
|
}
|
|
$toUser = self::GetByusername($data['phone']);
|
|
if($toUser) {
|
|
return returnErrorData('此号码已注册');
|
|
}
|
|
if(!Msg::checkCode($data['phone'], $data['msg'])) {
|
|
return returnErrorData('验证码错误');
|
|
}
|
|
if(!empty($data['inviterCode'])) {
|
|
$inviter = self::GetByinvitationCode($data['inviterCode']);
|
|
if(!$inviter) {
|
|
return returnErrorData('邀请码不正确');
|
|
}
|
|
}else {
|
|
$data['inviterCode'] = CommonInfo::where(['type' => 88])->find()->value;
|
|
$inviter = self::GetByinvitationCode($data['inviterCode']);
|
|
}
|
|
|
|
if(empty($data['qdCode'])) {
|
|
$data['qdCode'] = $inviter['qd_code'];
|
|
}else {
|
|
$sys_user = SysUser::GetByQrcode($data['qdCode']);
|
|
if(!$sys_user) {
|
|
return returnErrorData('渠道码错误');
|
|
}
|
|
}
|
|
$user_id = Random::generateRandomPrefixedId(19);
|
|
Db::startTrans();
|
|
try {
|
|
$insert = [
|
|
'user_id' => $user_id,
|
|
'user_name' => maskPhoneNumber($data['phone']),
|
|
'qd_code' => $data['qdCode'],
|
|
'phone' => $data['phone'],
|
|
'platform' => $data['platform'],
|
|
'create_time' => date('Y-m-d H:i:s'),
|
|
'sys_phone' => !empty($data['sys_phone'])?:'',
|
|
'password' => sha256Hex($data['password']),
|
|
'status' => 1,
|
|
'update_time' => date('Y-m-d H:i:s'),
|
|
'rate' => CommonInfo::where(['type' => 420])->find()->value,
|
|
'two_rate' => CommonInfo::where(['type' => 421])->find()->value,
|
|
'invitation_code' => toSerialCode($user_id),
|
|
'inviter_code' => $data['inviterCode'],
|
|
'inviter_user_id' => $inviter['user_id'],
|
|
];
|
|
$connect_name = DatabaseRoute::getConnection('tb_user', ['user_id' => $user_id], true);
|
|
$db = Db::connect($connect_name);
|
|
$db->name('tb_user')->insertGetId($insert);
|
|
$user = $db->name('tb_user')->where(['user_id' => $user_id])->find();
|
|
// 删除验证码
|
|
if(Msg::delCode($data['phone'], $data['msg'])) {
|
|
if($inviter) {
|
|
// 关于上级的处理
|
|
Invite::saveBody($user_id, $inviter);
|
|
}
|
|
Db::commit();
|
|
return returnSuccessData([], ['user' => apiconvertToCamelCase($user)]);
|
|
}
|
|
|
|
}catch (Exception $exception) {
|
|
Db::rollback();
|
|
return returnErrorData($exception->getMessage());
|
|
}
|
|
return returnErrorData('error');
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static function CheckPassword($userpasswoed, $formpassword):bool
|
|
{
|
|
$hash = hash('sha256', $formpassword);
|
|
return $hash === $userpasswoed;
|
|
}
|
|
|
|
public static function checkEnable($user)
|
|
{
|
|
// $user = DatabaseRoute::getDb('tb_user', $userId)->where([
|
|
// 'user_id' => $userId
|
|
// ])->find();
|
|
if (!$user) {
|
|
throw new SysException("用户不存在");
|
|
}
|
|
|
|
if ($user['status'] != 1) {
|
|
throw new SysException("用户已禁用");
|
|
}
|
|
|
|
return $user;
|
|
}
|
|
|
|
// 忘记密码
|
|
public static function forgetPwd($phone, $pwd, $msg):array
|
|
{
|
|
if(empty($phone) || empty($pwd) || empty($msg)) {
|
|
return returnErrorData('参数不完整');
|
|
}
|
|
$user = TbUser::GetByusername($phone);
|
|
if($user) {
|
|
if(Msg::checkCode($phone, $msg)) {
|
|
$pwd = sha256Hex($pwd);
|
|
$where = $sale = ['user_id' => $user['user_id']];
|
|
Db::startTrans();
|
|
try {
|
|
Common::saveDbData('tb_user', $sale, ['password' => $pwd], 'update', $where);
|
|
Msg::delCode($phone, $msg);
|
|
Db::commit();
|
|
return returnSuccessData();
|
|
}catch (Exception $e) {
|
|
Db::rollback();
|
|
return returnErrorData($e->getMessage());
|
|
}
|
|
|
|
}else {
|
|
return returnErrorData('验证码错误', 500);
|
|
}
|
|
}
|
|
return returnErrorData('error', 500);
|
|
}
|
|
|
|
public static function selectUserById($userId)
|
|
{
|
|
$user = DatabaseRoute::getDb('tb_user', $userId)->find();
|
|
if ($user) {
|
|
$userVip = Db::name('user_vip')->where([
|
|
'user_id' => $userId
|
|
])->find();
|
|
if ($userVip) {
|
|
$user['member'] = $userVip['is_vip'];
|
|
$user['end_time'] = $userVip['end_time'];
|
|
}
|
|
}
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
public static function selectUserByIdNew($user)
|
|
{
|
|
$db = Db::connect(config('think-orm.search_library'));
|
|
$userVip = $db->name('user_vip')->where(['user_id' => $user['user_id']])->find();
|
|
$db->close();
|
|
if ($userVip) {
|
|
$user['member'] = $userVip['is_vip'];
|
|
$user['end_time'] = $userVip['end_time'];
|
|
}
|
|
return $user;
|
|
|
|
}
|
|
|
|
/**
|
|
* 校验用户实名
|
|
*/
|
|
public static function checkReal($userId)
|
|
{
|
|
$count = DatabaseRoute::getDb('user_info', $userId)->whereNotNull('cert_name')->whereNotNull('cert_no')->count();
|
|
return $count > 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |