bourse stock
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1605 lines
60 KiB

<?php
namespace app\home\service;
use app\home\validate\UserValidate;
use app\model\CountryModel;
use app\model\CustomerRelationalModel;
use app\model\FileModel;
use app\model\UserChatLinkModel;
use app\model\UserContractModel;
use app\model\UserDigitalModel;
use app\model\UserForexModel;
use app\model\UserLevelModel;
use app\model\UserLoanModel;
use app\model\UserLoginLog;
use app\model\UserModel;
use app\model\UserMoneyModel;
use app\model\UserStockHkdModel;
use app\model\UserStockModel;
use app\utility\Jwt;
use app\utility\UnqId;
use Couchbase\User;
use think\exception\ValidateException;
use think\facade\Cache;
use think\facade\Log;
use think\facade\Queue;
class UserService extends BaseHomeService
{
/**
* 处理新用户注册初始化信息
* @param int $user_id
* @param int $parent_id
* @return void
*/
public function doRegInitUserInfo(int $user_id, int $parent_id)
{
$has_team = env('FEATURE.HAS_TEAM');
if ($has_team) {
$data = $this->createUserLevel($user_id, $parent_id);
(new TeamService())->regBrokerage($data['parent_id'], $data['grandpa_id'], $data['top_id']);
}
$this->initUserWallet($user_id);
}
/**
* 处理用户等级关系
* @param int $user_id
* @param int $parent_id
* @return bool
*/
private function createUserLevel(int $user_id, int $parent_id)
{
//插入用户等级关系
if ($parent_id > 0) {
$parent_info = UserLevelModel::getUserLevelById($parent_id);
$grandpa_id = $parent_info['parent_id'];
$top_id = $parent_info['grandpa_id'];
} else {
$grandpa_id = 0;
$top_id = 0;
}
$data['user_id'] = $user_id;
$data['parent_id'] = $parent_id;
$data['grandpa_id'] = $grandpa_id;
$data['top_id'] = $top_id;
UserLevelModel::InsertUserLevel($data);
return $data;
}
public function getUserLevel(int $user_id)
{
$level_info = UserLevelModel::getUserLevelById($user_id);
$this->setUserLevel($user_id, $level_info);
}
/**
* 初始化各钱包数据 U数据
* @param int $user_id
* @return void
*/
private function initUserWallet(int $user_id)
{
if(env('ACCOUT_TYPE.ALL_IN_ONE')==1){
UserMoneyModel::InsertUserMoneyk([
'user_id' => $user_id,
'stock_id' => 'USD',
'usable_num' => 0,
'frozen_num' => 0,
]);
}else{
if (env('FEATURE.HAS_STOCK_US')) {
UserStockModel::InsertUserStock([
'user_id' => $user_id,
'stock_id' => 'USD',
'usable_num' => 0,
'frozen_num' => 0,
]);
}
if (env('FEATURE.HAS_DIGITAL')) {
UserDigitalModel::InsertUserDigital([
'user_id' => $user_id,
'digital_id' => 'USDT',
'usable_num' => 0,
'frozen_num' => 0,
]);
}
if (env('FEATURE.HAS_CONTRACT')) {
UserContractModel::InsertUserContract([
'user_id' => $user_id,
'contract_id' => 'USDT',
'usable_num' => 0,
'frozen_num' => 0,
]);
}
}
}
/**
* @desc 获取用户信息
* @param $userId
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getUserInfo($userId)
{
try {
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first', []);
}
$info = UserModel::getFieldsByUserId('trade_password,lever_status,gender,last_name,first_name,real_status,country_id,user_no,nick_name,email,phone_number,country_code,is_real,head_img_id,invite_code,is_test_user', $userId);
if (empty($info)) {
return $this->toData('100400', 'The user does not exist.', []);
}
// 获取国家名称
$countryId = $info['country_id'];
$countryName = '';
if ($countryId > 0) {
$countryName = CountryModel::getNameById($countryId);
}
// 获取头像
$headPath = env('USER.DEFAULT_HEAD_IMG_PATH');
if ($info['head_img_id'] > 0) {
$headPath = FileModel::getFilePath($info['head_img_id']);
}
$key = 'LEVERAGE:' . $userId;
$leverageNum = Cache::store('redis')->get($key);
if (empty($leverageNum)) {
$key = 'LEVERAGE:0';
$leverageNum = Cache::store('redis')->get($key);
}
$leverageNum= empty($leverageNum) ? 1: $leverageNum;
// 查询用户的chat_uuid
$userChatInfo = UserChatLinkModel::where('user_id', $userId)->find();
$userChatUuid = "";
$userChatName = "";
if (!empty($userChatInfo)) {
$userChatUuid = $userChatInfo->chat_uuid;
$userChatName = $userChatInfo->chat_name;
}
// 查询用户绑定的客服chat_uuid
$customerChatUuid = "";
$customerChatName = "";
$customerRelationInfo = CustomerRelationalModel::where('user_id', $userId)->find(); //查询用户关联的客服账号信息
if ($customerRelationInfo) {
$customerChatInfo = UserChatLinkModel::where(['user_id'=>$customerRelationInfo->customer_id, 'user_type'=>2])->find(); //查询客服的chat账号
if ($customerChatInfo) {
$customerChatUuid = $customerChatInfo->chat_uuid;
$customerChatName = $customerChatInfo->chat_name;
}
}
// 返回数据
return $this->toData('0', 'Modification successful.', [
'logo' => $headPath,
'userId' => $userId,
'userNo' => $info['user_no'],
'nickName' => $info['nick_name'],
'nation' => $info['country_code'],
'phone' => $info['phone_number'],
'email' => $info['email'],
'isReal' => $info['is_real'],
'is_test_user' => $info['is_test_user'],
'real_status' => $info['real_status'],
'inviteCode' => $info['invite_code'],
'firstName' => $info['first_name'],
'lastName' => $info['last_name'],
'gender' => (string)$info['gender'],
'isSetPayPassword' => $info['trade_password'] ? '1' : '0',
'country' => $countryName,
'countryId' => $countryId,
'lever_status' => $info['lever_status'],
'leverage_num' =>$leverageNum,
'user_chat_uuid' => $userChatUuid,
'user_chat_name' => $userChatName,
'customer_chat_uuid' => $customerChatUuid,
'customer_chat_name' => $customerChatName,
'customer_chat_avatar' => '/bs/image/default.jpeg'
]);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]);
}
}
/**
* @desc 设置用户国家
* @param $userId
* @param $param
* @return array
*/
public function setCountry($userId, $param): array
{
try {
// 主键
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 邮件注册参数校验
validate(UserValidate::class)->scene('setCountry')->check($param);
$countryId = $param['country_id'];
// 国家是否存在
$country = CountryModel::getById($countryId);
if (empty($country)) {
return $this->toData('100400', 'Unsupported country or region.', []);
}
// 是否是黑名单国家
if ($this->checkForbidNation($country['code'])) {
return $this->toData('100400', 'Unsupported country or region.', []);
}
// 设置
UserModel::updateFieldsByUserId(['country_id' => $country['id']], $userId);
return $this->toData('0', 'Modification successful.');
} catch (ValidateException $validateException) {
$message = $validateException->getMessage();
return $this->toData('100400', $message, []);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage(), $exception->getTrace()]);
}
}
/**
* @desc 更新用户头像
* @param $userId
* @param $param
* @return array
*/
public function updateHeadImg($userId, $param): array
{
try {
// 主键
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 参数校验
validate(UserValidate::class)->scene('updateHeadImg')->check($param);
$fileId = $param['file_id'];
// 文件是否存在
$file = FileModel::getById($fileId);
if (empty($file)) {
return $this->toData('100400', 'The file does not exist.', []);
}
// 设置
UserModel::updateFieldsByUserId(['head_img_id' => $fileId], $userId);
return $this->toData('0', 'Modification successful.');
} catch (ValidateException $validateException) {
$message = $validateException->getMessage();
return $this->toData('100400', $message, []);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', []);
}
}
/**
* @desc 设置用户 昵称 性别 名字信息
* @param $userId
* @param $param
* @return array
*/
public function updateInfo($userId, $param): array
{
try {
// 主键
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 参数校验
validate(UserValidate::class)->scene('updateInfo')->check($param);
// 查找用户信息
$user = UserModel::getFieldsByUserId('user_id,nick_name,first_name,first_name,gender', $userId);
if (empty($user)) {
return $this->toData('100403', 'Please log in first.', []);
}
$nickName = $param['nick_name'];
$gender = $param['gender'];
$lastName = $param['last_name'] ?? $user['last_name'];
$first_name = $param['first_name'] ?? $user['first_name'];
UserModel::updateFieldsByUserId([
'nick_name' => $nickName,
'first_name' => $first_name,
'last_name' => $lastName,
'gender' => $gender,
], $userId);
// 返回
return $this->toData('0', 'Modification successful.', []);
} catch (ValidateException $validateException) {
$message = $validateException->getError();
return $this->toData('100400', $message);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage()]);
}
}
/**
* @desc 登陆状态下发送短信
* @param $userId
* @param $param
* @return array
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function sendEmail($userId, $param)
{
try {
$ip = $this->getClientRealIp();
// 主键
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 参数校验
validate(UserValidate::class)->scene('sendEmail')->check($param);
// 如果是非交易业务 需要判断验证码获取次数
$key = 'USER:SEND_CODE_NUM:' . $ip;
if ($param['business'] == '1') {
if ($this->checkGetNoTradeCodeNum($key)) {
return $this->toData('100300', 'No worries. Please feel free to reach out again tomorrow.', []);
}
}
// 提取参数
$emailType = $param['email_type']; // 1 给已经绑定的邮箱发 2 给新邮箱发
$email = $param['email'] ?? '';
if ($emailType == 1) {
$email = UserModel::getEmailById($userId);
if (empty($email)) {
return $this->toData('100400', '邮箱未绑定', []);
}
}
// 获取邮件内容
$content = $this->getEmailContent();
$content['email'] = $email;
$code = $content['code'];
Queue::push('app\home\job\SendEmail', $content, 'sendEmail');
// 设置缓存
$sendEmailCacheKey = 'USER:sendEmailLoginTrade:' . $email;
if ($param['business'] == '1') {
$sendEmailCacheKey = 'USER:sendEmailLoginNoTrade:' . $email;
// 累加ip获取的次数
$this->updateHadGetCodeNumCache($key);
}
$this->insertCodeToCache($sendEmailCacheKey, $code, 300);
// 返回结果
return $this->toData('0', 'Modification successful.');
} catch (ValidateException $validateException) {
// 参数校验失败 异常类
$message = $validateException->getError();
return $this->toData('100400', $message);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage()]);
}
}
/**
* @desc 登陆状态下发送短信
* @param $userId
* @param $param
* @return array
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function sendSms($userId, $param): array
{
try {
$ip = $this->getClientRealIp();
// 主键
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 参数校验
validate(UserValidate::class)->scene('sendSms')->check($param);
// 如果是非交易业务 需要判断验证码获取次数
$key = 'USER:SEND_CODE_NUM:' . $ip;
if ($param['business'] == '1') {
if ($this->checkGetNoTradeCodeNum($key)) {
return $this->toData('100300', 'No worries. Please feel free to reach out again tomorrow.', []);
}
}
// 提取参数
$phoneType = $param['phone_type']; // 1 给已经绑定的手机发 2 给新手机发
$phone = $param['phone'] ?? '';
$nation = $param['nation'] ?? '';
if ($phoneType == 1) {
$user = UserModel::getFieldsByUserId('country_code,phone_number', $userId);
if (empty($user)) {
return $this->toData('100400', 'The phone number is not linked/bound.', []);
}
$phone = $user['phone_number'];
$nation = $user['country_code'];
} else {
// 如果给新手机发 判断国家码是否存在
$nationCheck = CountryModel::checkCodeExists($nation);
if (!$nationCheck) {
return $this->toData('100400', 'Unsupported country or region.', []);
}
}
$mobile = $nation . $phone;
// 获取邮件内容
$content = $this->getSmsContent(1);
$content['mobile'] = $mobile;
$code = $content['code'];
Queue::push('app\home\job\SendSms', $content, 'sendSms');
// 设置缓存
$sendCodeKey = 'USER:sendSmsLoginTrade:' . $mobile;
if ($param['business'] == '1') {
$sendCodeKey = 'USER:sendSmsLoginNoTrade:' . $mobile;
// 累加已经获取的次数
$this->updateHadGetCodeNumCache($key);
}
$this->insertCodeToCache($sendCodeKey, $code, 300);
// 返回结果
return $this->toData('0', 'Modification successful.');
} catch (ValidateException $validateException) {
// 参数校验失败 异常类
$message = $validateException->getError();
return $this->toData('100400', $message);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage()]);
}
}
public function setPayPasswordByEmail($userId, $param)
{
try {
// 主键
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 参数校验 email_code pay_password
validate(UserValidate::class)->scene('setPayPasswordByEmail')->check($param);
// 获取用户
$user = UserModel::getFieldsByUserId('email,user_id,trade_password', $userId);
if (empty($user)) {
return $this->toData('100403', 'Please log in first.', []);
}
// // 校验验证码
// $key = 'USER:sendEmailLoginNoTrade:'.$user['email'];
// $emailCache = Cache::store('redis')->get($key);
// if(empty($param['email_code']) || $emailCache != $param['email_code']){
// return $this->toData('100400', 'The verification code is incorrect.', []);
// }
// 如果已经存在交易密码 则不能设置
if (!empty($user['trade_password'])) {
return $this->toData('100400', 'The payment password has already been set.', []);
}
// 加密密码
$payPassword = $param['pay_password'];
$salt = env('ENCRYPT.SALT');
$payPassword = (new UnqId())->encryptPassword($payPassword, $salt);
// 设置密码
UserModel::setPayPassword($payPassword, $salt, $userId);
return $this->toData('0', 'Modification successful.', []);
} catch (ValidateException $validateException) {
// 参数校验失败 异常类
$message = $validateException->getError();
return $this->toData('100400', $message);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', []);
}
}
/**
* @desc 通过短信设置取款密码
* @param $userId
* @param $param
* @return array
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function setPayPasswordBySms($userId, $param)
{
try {
// 主键
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 参数校验 nation phone pay_password
validate(UserValidate::class)->scene('setPayPasswordBySms')->check($param);
// 获取用户信息
$user = UserModel::getFieldsByUserId('country_code,phone_number,user_id,trade_password', $userId);
if (empty($user)) {
return $this->toData('100403', 'Please log in first.', []);
}
if (!empty($user['trade_password'])) {
return $this->toData('100400', 'The payment password has already been set.', []);
}
// // 校验验证码
// $mobile = $user['country_code'].$user['phone_number'];
// $key = 'USER:sendSmsLoginNoTrade:'.$mobile;
// if(Cache::store('redis')->get($key) != $param['sms_code']){
// return $this->toData('100400', 'The verification code is incorrect.', []);
// }
// 校验验证码
$payPassword = $param['pay_password'];
$salt = env('ENCRYPT.SALT');
$payPassword = (new UnqId())->encryptPassword($payPassword, $salt);
// 设置密码
UserModel::setPayPassword($payPassword, $salt, $userId);
return $this->toData('0', 'Modification successful.', []);
} catch (ValidateException $validateException) {
// 参数校验失败 异常类
$message = $validateException->getError();
return $this->toData('100400', $message);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage()]);
}
}
/**
* @desc 通过手机号修改交易密码
* @param $userId
* @param $param
* @return array
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function updatePayPasswordBySms($userId, $param)
{
try {
// user_id
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 参数校验 email_code email
validate(UserValidate::class)->scene('updatePayPasswordBySms')->check($param);
// 获取用户信息
$user = UserModel::getFieldsByUserId('user_id,trade_password,salt,phone_number,country_code', $userId);
if (empty($user)) {
return $this->toData('100403', 'Please log in first.', []);
}
// 提取参数
// $nation = $user['country_code'];
// $phone = $user['phone_number'];
// $smsCode = $param['sms_code'];
$payPassword = $param['pay_password'];
$payPasswordNew = $param['pay_password_new'];
// 校验验证码
// $mobile = $nation.$phone;
// $cacheSmsKey = 'USER:sendSmsLoginNoTrade:'.$mobile;
// $cacheSmsCode = Cache::store('redis')->get($cacheSmsKey);
// if(empty($smsCode) || $smsCode != $cacheSmsCode){
// return $this->toData('100400', 'The verification code is incorrect.', []);
// }
// 校验旧密码
$checkPasswordBool = (new UnqId())->checkPassword($payPassword, $user['trade_password'], $user['salt']);
if (!$checkPasswordBool) {
return $this->toData('100400', 'Incorrect password.');
}
// 设置密码
$newPassword = (new UnqId())->encryptPassword($payPasswordNew, $user['salt']);
UserModel::updateFieldsByUserId(['trade_password' => $newPassword], $userId);
// 返回结果
return $this->toData('0', 'Modification successful.', []);
} catch (ValidateException $validateException) {
// 参数校验失败 异常类
$message = $validateException->getError();
return $this->toData('100400', $message);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage()]);
}
}
/**
* @desc 根据邮箱修改交易密码
* @param $userId
* @param $param
* @return array
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function updatePayPasswordByEmail($userId, $param)
{
// 通过 邮箱 收款密码 重新设置收款密码
try {
// user_id
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 参数校验 email_code email
validate(UserValidate::class)->scene('updatePayPasswordByEmail')->check($param);
// 获取用户信息
$user = UserModel::getFieldsByUserId('user_id,trade_password,salt,email', $userId);
if (empty($user)) {
return $this->toData('100403', 'Please log in first.', []);
}
// 提取参数
// $email = $user['email'];
// $emailCode = $param['email_code'];
$payPassword = $param['pay_password'];
$payPasswordNew = $param['pay_password_new'];
// 校验验证码
// $cacheEmailKey = 'USER:sendEmailLoginNoTrade:'.$email;
// $cacheEmailCode = Cache::store('redis')->get($cacheEmailKey);
// if(empty($emailCode) || $emailCode != $cacheEmailCode){
// return $this->toData('100400', 'The verification code is incorrect.', []);
// }
// 校验旧密码
$checkPasswordBool = (new UnqId())->checkPassword($payPassword, $user['trade_password'], $user['salt']);
if (!$checkPasswordBool) {
return $this->toData('100400', 'Incorrect password.', []);
}
// 设置密码
$newPassword = (new UnqId())->encryptPassword($payPasswordNew, $user['salt']);
UserModel::updateFieldsByUserId(['trade_password' => $newPassword], $userId);
// 返回结果
return $this->toData('0', 'Modification successful.', []);
} catch (ValidateException $validateException) {
// 参数校验失败 异常类
$message = $validateException->getError();
return $this->toData('100400', $message);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage()]);
}
}
/**
* @desc 根据短信重新设置交易密码
* @param $userId
* @param $param
* @return array
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function resetPayPasswordBySms($userId, $param)
{
try {
// user_id
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 参数校验 email_code email
validate(UserValidate::class)->scene('resetPayPasswordBySms')->check($param);
// 获取用户信息
$user = UserModel::getFieldsByUserId('user_id,salt,phone_number,country_code', $userId);
if (empty($user)) {
return $this->toData('100403', 'Please log in first.', []);
}
// 提取参数
// $nation = $user['country_code'];
// $phone = $user['phone_number'];
// $smsCode = $param['sms_code'];
$payPasswordNew = $param['pay_password_new'];
// 校验验证码
// $mobile = $nation.$phone;
// $cacheSmsKey = 'USER:sendSmsLoginNoTrade:'.$mobile;
// $cacheSmsCode = Cache::store('redis')->get($cacheSmsKey);
// if(empty($smsCode) || $smsCode != $cacheSmsCode){
// return $this->toData('100400', 'The verification code is incorrect.', []);
// }
// 设置密码
$newPassword = (new UnqId())->encryptPassword($payPasswordNew, $user['salt']);
UserModel::updateFieldsByUserId(['trade_password' => $newPassword], $userId);
// 返回结果
return $this->toData('0', 'Modification successful.', []);
} catch (ValidateException $validateException) {
// 参数校验失败 异常类
$message = $validateException->getError();
return $this->toData('100400', $message);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage()]);
}
}
/**
* @desc 通过邮箱重新设置交易密码
* @param $userId
* @param $param
* @return array
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function resetPayPasswordByEmail($userId, $param)
{
// 通过邮箱验证码 新密码 设置交易密码
try {
// user_id
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 参数校验 email_code email
validate(UserValidate::class)->scene('resetPayPasswordByEmail')->check($param);
// 获取用户信息
$user = UserModel::getFieldsByUserId('user_id,salt,email', $userId);
if (empty($user)) {
return $this->toData('100403', 'Please log in first.', []);
}
// 提取参数
// $email = $user['email'];
// $emailCode = $param['email_code'];
$payPasswordNew = $param['pay_password_new'];
// 校验验证码
// $cacheEmailKey = 'USER:sendEmailLoginNoTrade:'.$email;
// $cacheEmailCode = Cache::store('redis')->get($cacheEmailKey);
// if(empty($emailCode) || $emailCode != $cacheEmailCode){
// return $this->toData('100400', 'The verification code is incorrect.', []);
// }
// 设置密码
$newPassword = (new UnqId())->encryptPassword($payPasswordNew, $user['salt']);
UserModel::updateFieldsByUserId(['trade_password' => $newPassword], $userId);
// 返回结果
return $this->toData('0', 'Modification successful.', []);
} catch (ValidateException $validateException) {
// 参数校验失败 异常类
$message = $validateException->getError();
return $this->toData('100400', $message);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage()]);
}
}
public function updatePasswordBySms($userId, $param)
{
try {
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 参数校验 email_code email
validate(UserValidate::class)->scene('updatePasswordBySms')->check($param);
// 获取用户信息
$user = UserModel::getFieldsByUserId('user_id,login_password,salt,phone_number,country_code', $userId);
if (empty($user)) {
return $this->toData('100403', 'Please log in first.', []);
}
// 提取参数
// $nation = $user['country_code'];
// $phone = $user['phone_number'];
// $smsCode = $param['sms_code'];
$password = $param['password'];
$passwordNew = $param['password_new'];
// 校验验证码
// $mobile = $nation.$phone;
// $cacheSmsKey = 'USER:sendSmsLoginNoTrade:'.$mobile;
// $cacheSmsCode = Cache::store('redis')->get($cacheSmsKey);
// if(empty($smsCode) || $smsCode != $cacheSmsCode){
// return $this->toData('100400', 'The verification code is incorrect.', []);
// }
// 校验旧密码
$checkPasswordBool = (new UnqId())->checkPassword($password, $user['login_password'], $user['salt']);
if (!$checkPasswordBool) {
return $this->toData('100400', 'Incorrect password.', []);
}
// 设置密码
$newPassword = (new UnqId())->encryptPassword($passwordNew, $user['salt']);
UserModel::updateFieldsByUserId(['login_password' => $newPassword], $userId);
// 返回结果
return $this->toData('0', 'Modification successful.', []);
} catch (ValidateException $validateException) {
// 参数校验失败 异常类
$message = $validateException->getError();
return $this->toData('100400', $message);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage()]);
}
}
/**
* @desc 邮箱修改登陆密码
* @param $userId
* @param $param
* @return array
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function updatePasswordByEmail($userId, $param)
{
try {
// user_id
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 参数校验
validate(UserValidate::class)->scene('updatePasswordByEmail')->check($param);
// 获取用户信息
$user = UserModel::getFieldsByUserId('user_id,login_password,salt,email', $userId);
if (empty($user)) {
return $this->toData('100403', 'Please log in first.', []);
}
// 提取参数
// $email = $user['email'];
// $emailCode = $param['email_code'];
$password = $param['password'];
$passwordNew = $param['password_new'];
// 校验验证码
// $cacheEmailKey = 'USER:sendEmailLoginNoTrade:'.$email;
// $cacheEmailCode = Cache::store('redis')->get($cacheEmailKey);
// if(empty($emailCode) || $emailCode != $cacheEmailCode){
// return $this->toData('100400', 'The verification code is incorrect.', []);
// }
// 校验旧密码
$checkPasswordBool = (new UnqId())->checkPassword($password, $user['login_password'], $user['salt']);
if (!$checkPasswordBool) {
return $this->toData('100400', 'Incorrect password.', []);
}
// 设置密码
$newPassword = (new UnqId())->encryptPassword($passwordNew, $user['salt']);
UserModel::updateFieldsByUserId(['login_password' => $newPassword], $userId);
// 返回结果
return $this->toData('0', 'Modification successful.', []);
} catch (ValidateException $validateException) {
// 参数校验失败 异常类
$message = $validateException->getError();
return $this->toData('100400', $message);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage()]);
}
}
/**
* @desc 短信重新设置登陆密码
* @param $userId
* @param $param
* @return array
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function resetPasswordBySms($userId, $param)
{
try {
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 参数校验 email_code email
validate(UserValidate::class)->scene('resetPasswordBySms')->check($param);
// 获取用户信息
$user = UserModel::getFieldsByUserId('user_id,salt,phone_number,country_code', $userId);
if (empty($user)) {
return $this->toData('100403', 'Please log in first.', []);
}
// 提取参数
// $nation = $user['country_code'];
// $phone = $user['phone_number'];
// $smsCode = $param['sms_code'];
$passwordNew = $param['password_new'];
// 校验验证码
// $mobile = $nation.$phone;
// $cacheSmsKey = 'USER:sendSmsLoginNoTrade:'.$mobile;
// $cacheSmsCode = Cache::store('redis')->get($cacheSmsKey);
// if(empty($smsCode) || $smsCode != $cacheSmsCode){
// return $this->toData('100400', 'The verification code is incorrect.', []);
// }
// 设置密码
$newPassword = (new UnqId())->encryptPassword($passwordNew, $user['salt']);
UserModel::updateFieldsByUserId(['login_password' => $newPassword], $userId);
// 返回结果
return $this->toData('0', 'Modification successful.', []);
} catch (ValidateException $validateException) {
// 参数校验失败 异常类
$message = $validateException->getError();
return $this->toData('100400', $message);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage()]);
}
}
/**
* @desc 邮箱重新设置登陆密码
* @param $userId
* @param $param
* @return array
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function resetPasswordByEmail($userId, $param)
{
try {
// user_id
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 参数校验 email_code email
validate(UserValidate::class)->scene('resetPasswordByEmail')->check($param);
// 获取用户信息
$user = UserModel::getFieldsByUserId('user_id,salt,email', $userId);
if (empty($user)) {
return $this->toData('100403', 'Please log in first.', []);
}
// 提取参数
// $email = $user['email'];
// $emailCode = $param['email_code'];
$passwordNew = $param['password_new'];
// 校验验证码
// $cacheEmailKey = 'USER:sendEmailLoginNoTrade:'.$email;
// $cacheEmailCode = Cache::store('redis')->get($cacheEmailKey);
// if(empty($emailCode) || $emailCode != $cacheEmailCode){
// return $this->toData('100400', 'The verification code is incorrect.', []);
// }
// 设置密码
$newPassword = (new UnqId())->encryptPassword($passwordNew, $user['salt']);
UserModel::updateFieldsByUserId(['login_password' => $newPassword], $userId);
// 返回结果
return $this->toData('0', 'Modification successful.', []);
} catch (ValidateException $validateException) {
// 参数校验失败 异常类
$message = $validateException->getError();
return $this->toData('100400', $message);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage()]);
}
}
public function bindEmail($userId, $param)
{
try {
// user_id
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 参数校验 email_code email
validate(UserValidate::class)->scene('bindEmail')->check($param);
// 判断验证码
// $emailCode = $param['email_code'];
$email = $param['email'];
// $cacheCodeKey = 'USER:sendEmailLoginNoTrade:'.$email;
// $cacheSmsCode = Cache::store('redis')->get($cacheCodeKey);
// if(empty($emailCode) || $emailCode != $cacheSmsCode){
// return $this->toData('100400', 'The verification code is incorrect.', []);
// }
// 获取用户信息
$user = UserModel::getFieldsByUserId('email,user_id', $userId);
if (empty($user)) {
return $this->toData('100403', 'Please log in first.', []);
}
// 判读是否已经绑定
if (!empty($user['email'])) {
return $this->toData('100400', 'You have already bound your email', []);
}
//判断手机号是否已经被别人绑定
$emailExits = UserModel::checkEmailExists($email);
if ($emailExits) {
return $this->toData('100400', 'The email has been bound', []);
}
// 执行入库操作
UserModel::updateFieldsByUserId(['email' => $email], $userId);
// 返回结果
return $this->toData('0', 'Binding successful', []);
} catch (ValidateException $validateException) {
// 参数校验失败 异常类
$message = $validateException->getError();
return $this->toData('100400', $message);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage()]);
}
}
/**
* @desc 修改已经绑定的手机号
* @param $userId
* @param $param
* @return array
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function updateEmail($userId, $param): array
{
try {
// user_id
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 参数校验 nation phone sms_code
validate(UserValidate::class)->scene('updateEmail')->check($param);
// 判断验证码
// $emailCode = $param['email_code'];
$email = $param['email'];
// $cacheCodeKey = 'USER:sendEmailLoginNoTrade:'.$email;
// $cacheSmsCode = Cache::store('redis')->get($cacheCodeKey);
// if(empty($emailCode) || $emailCode != $cacheSmsCode){
// return $this->toData('100400', 'The verification code is incorrect.', []);
// }
// 获取用户信息
$user = UserModel::getFieldsByUserId('login_password,email,user_id,salt', $userId);
if (empty($user)) {
return $this->toData('100403', 'Please log in first.', []);
}
// 判读是否已经绑定 需要已经绑定
if (empty($user['email'])) {
return $this->toData('100400', 'Email is not linked/bound.', []);
}
//校验登陆密码
$passwordCheck = (new UnqId())->checkPassword($param['password'], $user['login_password'], $user['salt']);
if (!$passwordCheck) {
return $this->toData('100400', 'Incorrect password.', []);
}
//判断手机号是否已经被别人绑定
$emailExits = UserModel::checkEmailExists($email);
if ($emailExits) {
return $this->toData('100400', 'The email has already been linked/bound.', []);
}
// 执行入库操作
UserModel::updateFieldsByUserId(['email' => $email], $userId);
// 返回结果
return $this->toData('0', 'Modification successful.', []);
} catch (ValidateException $validateException) {
// 参数校验失败 异常类
$message = $validateException->getError();
return $this->toData('100400', $message);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage()]);
}
}
public function bindPhone($userId, $param)
{
try {
// user_id
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 参数校验 nation phone sms_code
validate(UserValidate::class)->scene('bindPhone')->check($param);
// 判断验证码
// $smsCode = $param['sms_code'];
// $mobile = $param['nation'].$param['phone'];
// $cacheSmsKey = 'USER:sendSmsLoginNoTrade:'.$mobile;
// $cacheSmsCode = Cache::store('redis')->get($cacheSmsKey);
// if(empty($smsCode) || $smsCode != $cacheSmsCode){
// return $this->toData('100400', 'The verification code is incorrect.', []);
// }
// 获取用户信息
$user = UserModel::getFieldsByUserId('user_id,country_code,phone_number', $userId);
if (empty($user)) {
return $this->toData('100403', 'Please log in first.', []);
}
// 判读是否已经绑定
if (!empty($user['phone_number'])) {
return $this->toData('100400', 'The phone number has already been linked/bound.', []);
}
//判断手机号是否已经被别人绑定
$phoneExits = UserModel::checkPhoneExists($param['phone']);
if ($phoneExits) {
return $this->toData('100400', 'The phone number has already been linked/bound.', []);
}
// 判断国家码是否存在
$codeExists = CountryModel::checkCodeExists($param['nation']);
if (!$codeExists) {
return $this->toData('100400', 'Unsupported country or region.', []);
}
// 执行入库操作
UserModel::updateFieldsByUserId(['phone_number' => $param['phone'], 'country_code' => $param['nation']], $userId);
// 返回结果
return $this->toData('0', 'Binding successful.', []);
} catch (ValidateException $validateException) {
// 参数校验失败 异常类
$message = $validateException->getError();
return $this->toData('100400', $message);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage()]);
}
}
/**
* @desc 修改已经绑定的手机号
* @param $userId
* @param $param
* @return array
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function updatePhone($userId, $param): array
{
try {
// user_id
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 参数校验 nation phone sms_code
validate(UserValidate::class)->scene('updatePhone')->check($param);
// 判断验证码
// $smsCode = $param['sms_code'];
// $mobile = $param['nation'].$param['phone'];
// $cacheSmsKey = 'USER:sendSmsLoginNoTrade:'.$mobile;
// $cacheSmsCode = Cache::store('redis')->get($cacheSmsKey);
// if(empty($smsCode) || $smsCode != $cacheSmsCode){
// return $this->toData('100400', 'The verification code is incorrect.', []);
// }
// 判断国家码是否存在
$codeExists = CountryModel::checkCodeExists($param['nation']);
if (!$codeExists) {
return $this->toData('100400', 'Unsupported country or region', []);
}
//判断手机号是否已经被别人绑定
$phoneExits = UserModel::checkPhoneExists($param['phone']);
if ($phoneExits) {
return $this->toData('100400', 'The phone number has already been linked/bound.', []);
}
// 获取用户信息
$user = UserModel::getFieldsByUserId('salt,login_password,user_id,country_code,phone_number', $userId);
if (empty($user)) {
return $this->toData('100403', 'Please log in first.', []);
}
// 判读是否已经绑定 需要已经绑定手机
if (empty($user['phone_number'])) {
return $this->toData('100400', 'The phone number is not linked/bound.', []);
}
//校验登陆密码
$passwordCheck = (new UnqId())->checkPassword($param['password'], $user['login_password'], $user['salt']);
if (!$passwordCheck) {
return $this->toData('100400', 'Incorrect password.', []);
}
// 执行入库操作
UserModel::updateFieldsByUserId(['phone_number' => $param['phone'], 'country_code' => $param['nation']], $userId);
// 返回结果
return $this->toData('0', 'Modification successful.', []);
} catch (ValidateException $validateException) {
// 参数校验失败 异常类
$message = $validateException->getError();
return $this->toData('100400', $message);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage()]);
}
}
public function apply_loan($userId, $param)
{
try {
// user_id
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 参数校验 nation phone sms_code
validate(UserValidate::class)->scene('apply_loan')->check($param);
$count=UserLoanModel::where('status',0)->where('user_id',$userId)->count();
if($count>0){
return $this->toData('100402', 'You have applied, please be patient and wait.', []);
}
$userloan=new UserLoanModel();
$userloan->user_id=$userId;
$userloan->num= intval($param['num']);
$userloan->day= intval($param['day']);
$userloan->status= 0;
$userloan->is_delete= 0;
$userloan->create_time=date('Y-m-d H:i:s');
$userloan->save();
// 返回结果
return $this->toData('0', 'Binding successful.', []);
} catch (ValidateException $validateException) {
// 参数校验失败 异常类
$message = $validateException->getError();
return $this->toData('100400', $message);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage()]);
}
}
public function loan($userId, $param)
{
try {
// user_id
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
$data['where']['user_id']=$userId;
$data['page']=isset($param['page']) ? intval($param['page']) : 1;
$data['size']=isset($param['size']) ? intval($param['size']) : 10;
$list=UserLoanModel::getUserLoanList($data);
// 返回结果
return $this->toData('0', 'successful.', $list);
} catch (ValidateException $validateException) {
// 参数校验失败 异常类
$message = $validateException->getError();
return $this->toData('100400', $message);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage()]);
}
}
/**
* @desc 退出登陆
* @param $userId
* @return array
*/
public function logout($userId)
{
try {
// 删除缓存
$this->delUserTokenCache($userId);
return $this->toData('0', 'Modification successful.');
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', [$exception->getMessage()]);
}
}
/**
* @desc 获取用户登陆信息
* @param $userId
* @return array
*/
public function loginLog($userId): array
{
try {
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
$data = UserLoginLog::getLog($userId);
if (empty($data)) {
return $this->toData('0', 'Modification successful.', []);
}
$res = [];
foreach ($data as $item) {
$countryCn = '';
$countryEn = '';
$cityCn = '';
$cityEn = '';
if (!empty($item['country'])) {
$countryArr = json_decode($item['country'], true);
$countryCn = $countryArr['cn'];
$countryEn = $countryArr['en'];
}
if (!empty($item['city'])) {
$cityArr = json_decode($item['city'], true);
$cityCn = $cityArr['cn'];
$cityEn = $cityArr['en'];
}
$res[] = [
'device' => $item['device'],
'ip' => $item['ip'],
'countryCn' => $countryCn,
'countryEn' => $countryEn,
'cityCn' => $cityCn,
'cityEn' => $cityEn,
'loginDate' => $item['login_date'],
];
}
return $this->toData('0', 'Modification successful.', $res);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', []);
}
}
/**
* 用户申请使用杠杆
* @param $userId
* @return array
*/
public function leverApply($userId)
{
try {
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 获取用户信息
$user = UserModel::getFieldsByUserId('lever_status,user_id', $userId);
if (empty($user)) {
return $this->toData('100403', 'Please log in first.', []);
}
if (in_array($user['lever_status'], [UserModel::LEVER_STATUS_NO, UserModel::LEVER_STATUS_FAIL])) {
$level_status=env('USER_LEVEL.NO_APPLY')==1 ? UserModel::LEVER_STATUS_PASSED : UserModel::LEVER_STATUS_APPLY;
UserModel::update(['lever_status' => $level_status, 'update_time' => date('Y-m-d H:i:s')], ['user_id' => $userId]);
return $this->toData('0', 'success.', []);
}
return $this->toData('0', 'Already submitted.', []);
} catch (\Exception $exception) {
return $this->toData('100500', 'The system is busy.', []);
}
}
public function applyTestAccount($userId)
{
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 获取用户信息
$user = UserModel::getFieldsByUserId('test_account,user_id,user_no', $userId);
if (empty($user)) {
return $this->toData('100401', 'Please log in first.', []);
}
if(!empty($user['test_account'])){
return $this->toData('0', 'success.', []);
}
//添加模拟账号
$userNo=$this->getUniqUserNo();
$email="test_".$userNo."@test.com";
$salt = env('ENCRYPT.SALT');
$password = (new UnqId())->encryptPassword('test12345', $salt);
$userInviteCode=$this->getUniqInviteCode();
$parentUserId = $userId;
$ip = (new BaseHomeService())->getClientRealIp();
$test_userId = UserModel::emailRegister($email, $userNo, $userInviteCode, $parentUserId, $password, $ip, $salt, 2, 0);
if($test_userId){
$this->doRegInitUserInfo($test_userId,$parentUserId);
UserModel::where('user_id', $userId)->update([
'test_account' => $test_userId
]);
UserModel::where('user_id', $test_userId)->update([
'is_real' => 0,
'real_status'=>3
]);
if(env('ACCOUT_TYPE.ALL_IN_ONE')==1){
UserMoneyModel::where('user_id', $test_userId)->update([
'user_id' => $test_userId,
'stock_id' => 'USD',
'usable_num' => 100000,
'frozen_num' => 0,
]);
}else{
UserDigitalModel::where('user_id', $test_userId)->update([
'user_id' => $test_userId,
'digital_id' => 'USDT',
'usable_num' => 100000,
'frozen_num' => 0,
]);
UserContractModel::where('user_id', $test_userId)->update([
'user_id' => $test_userId,
'contract_id' => 'USDT',
'usable_num' => 100000,
'frozen_num' => 0,
]);
UserForexModel::InsertUserForex([
'user_id' => $test_userId,
'contract_id' => 'USD',
'usable_num' => 100000,
'frozen_num' => 0,
]);
UserStockHkdModel::InsertUserStock([
'user_id' => $test_userId,
'stock_id' => 'HKD',
'usable_num' => 100000,
'frozen_num' => 0,
]);
}
}
return $this->toData('0', 'success.', []);
}
public function applyTestMoney($userId,$param){
if (empty($userId) || $userId <= 0) {
return $this->toData('100403', 'Please log in first.', []);
}
// 获取用户信息
$user = UserModel::getFieldsByUserId('is_test_user,user_id,user_no', $userId);
if (empty($user)) {
return $this->toData('100401', 'Please log in first.', []);
}
if($user['is_test_user']!=2){
return $this->toData('100402', 'error.', []);
}
$num=isset($param['num']) ? intval($param['num']) : 0;
if($num<=0 || $num>100000){
return $this->toData('100401', 'error.', []);
}
$user_money=UserMoneyModel::getUserUSDByUserId(['user_id'=>$userId]);
$update_num=$user_money['usable_num']+$num;
UserMoneyModel::where('user_id', $userId)->update([
'stock_id' => 'USD',
'usable_num' => $update_num,
'update_time' => date('Y-m-d H:i:s'),
]);
return $this->toData('0', 'success.', []);
}
public function testLogin($userId)
{
// 获取用户信息
$user = UserModel::getFieldsByUserId('test_account,user_id,user_no', $userId);
if (empty($user)) {
return $this->toData('100401', 'Please log in first.', []);
}
if(empty($user['test_account'])){
return $this->toData('100402', 'Please log in first.', []);
}
//登录模拟账号
$test_user=UserModel::getFieldsByUserId('test_account,user_id,user_no,nick_name,invite_code,is_real,is_test_user', $user['test_account']);
// 生成token
$token = (new Jwt())->getToken($test_user['user_id'], env('ENCRYPT.SALT'));
if (empty($token)) {
return $this->toData('100300', 'The system is busy.', []);
}
// 将token存致缓存 覆盖新的缓存 实现单设备登陆
$this->setUserTokenCache($token, $test_user['user_id']);
// 删除缓存
$this->delUserTokenCache($userId);
// 返回结果以及用户信息
return $this->toData('0', 'Request successful.', [
'userId' => $test_user['user_id'],
'userNo' => $test_user['user_no'],
'nickName' => $test_user['nick_name'],
'inviteCode' => $test_user['invite_code'],
'isReal' => $test_user['is_real'],
'is_test_user' => $test_user['is_test_user'],
'logo' => env('USER.DEFAULT_HEAD_IMG_PATH'),
'token' => $token,
]);
}
public function formalLogin($userId)
{
// 获取用户信息
$user = UserModel::getFieldsByUserId('parent_id,user_id,user_no,is_test_user', $userId);
if (empty($user)) {
return $this->toData('100401', 'Please log in first.', []);
}
if(empty($user['parent_id'])){
return $this->toData('100402', 'The system is busy.', []);
}
if(empty($user['is_test_user'])){
return $this->toData('100403', 'The system is busy.', []);
}
//登录模拟账号
$test_user=UserModel::getFieldsByUserId('test_account,user_id,user_no,nick_name,invite_code,is_real,is_test_user', $user['parent_id']);
if($test_user['test_account']!=$userId){
return $this->toData('100404', 'The system is busy.', []);
}
// 生成token
$token = (new Jwt())->getToken($test_user['user_id'], env('ENCRYPT.SALT'));
if (empty($token)) {
return $this->toData('100300', 'The system is busy.', []);
}
// 将token存致缓存 覆盖新的缓存 实现单设备登陆
$this->setUserTokenCache($token, $test_user['user_id']);
// 删除缓存
$this->delUserTokenCache($userId);
// 返回结果以及用户信息
return $this->toData('0', 'Request successful.', [
'userId' => $test_user['user_id'],
'userNo' => $test_user['user_no'],
'nickName' => $test_user['nick_name'],
'inviteCode' => $test_user['invite_code'],
'isReal' => $test_user['is_real'],
'is_test_user' => $test_user['is_test_user'],
'logo' => env('USER.DEFAULT_HEAD_IMG_PATH'),
'token' => $token,
]);
}
}