Files
2026-06-11 12:15:29 +08:00

335 lines
11 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | likeadmin蹇€熷紑鍙戝墠鍚庣鍒嗙绠$悊鍚庡彴锛圥HP鐗堬級
// +----------------------------------------------------------------------
// | 娆㈣繋闃呰瀛︿範绯荤粺绋嬪簭浠g爜锛屽缓璁弽棣堟槸鎴戜滑鍓嶈繘鐨勫姩鍔?
// | 寮€婧愮増鏈彲鑷敱鍟嗙敤锛屽彲鍘婚櫎鐣岄潰鐗堟潈logo
// | gitee涓嬭浇锛歨ttps://gitee.com/likeshop_gitee/likeadmin
// | github涓嬭浇锛歨ttps://github.com/likeshop-github/likeadmin
// | 璁块棶瀹樼綉锛歨ttps://www.likeadmin.cn
// | likeadmin鍥㈤槦 鐗堟潈鎵€鏈?鎷ユ湁鏈€缁堣В閲婃潈
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\api\logic;
use app\common\{
enum\notice\NoticeEnum,
enum\user\UserTerminalEnum,
enum\YesNoEnum,
logic\BaseLogic,
model\SmsLog,
model\user\User,
model\user\UserAuth,
service\FileService,
service\sms\SmsDriver,
service\wechat\WeChatMnpService
};
use think\facade\Config;
/**
* 浼氬憳閫昏緫灞?
* Class UserLogic
* @package app\shopapi\logic
*/
class UserLogic extends BaseLogic
{
private static function findLatestUploadRecord(string $mobile)
{
return SmsLog::where(function ($query) use ($mobile) {
$query->where('phone', $mobile)
->whereOr('phone', '+86' . $mobile)
->whereOr('phone', '86' . $mobile);
})
->order('sms_time', 'desc')
->findOrEmpty();
}
/**
* @notes 涓汉涓績
* @param array $userInfo
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 娈佃獕
* @date 2022/9/16 18:04
*/
public static function center(array $userInfo): array
{
if (empty($userInfo['user_id'])) {
return [];
}
$user = User::where(['id' => $userInfo['user_id']])
->field('id,sn,sex,account,nickname,real_name,avatar,mobile,create_time,is_new_user,user_money,password,is_vip,vip_level,vip_expire_time,ai_free_count,ai_free_date,is_bind_mobile')
->findOrEmpty();
if (!$user->isEmpty() && !empty($user->mobile) && ((int) $user->is_bind_mobile !== 1 || (int) $user->is_new_user !== 1)) {
$uploadRecord = self::findLatestUploadRecord((string) $user->mobile);
if (!$uploadRecord->isEmpty()) {
$user->is_bind_mobile = 1;
$user->is_new_user = 1;
$user->save();
}
}
if (in_array($userInfo['terminal'], [UserTerminalEnum::WECHAT_MMP, UserTerminalEnum::WECHAT_OA])) {
$auth = UserAuth::where(['user_id' => $userInfo['user_id'], 'terminal' => $userInfo['terminal']])->find();
$user['is_auth'] = $auth ? YesNoEnum::YES : YesNoEnum::NO;
}
$user['has_password'] = !empty($user['password']);
$vipInfo = $user->getVipInfo();
$user->hidden(['password', 'is_vip', 'vip_level', 'vip_expire_time', 'ai_free_count', 'ai_free_date']);
$result = $user->toArray();
$result['vip_info'] = $vipInfo;
return $result;
}
/**
* @notes 涓汉淇℃伅
* @param $userId
* @return array
* @author 娈佃獕
* @date 2022/9/20 19:45
*/
public static function info(int $userId)
{
$user = User::where(['id' => $userId])
->field('id,sn,sex,account,password,nickname,real_name,avatar,mobile,create_time,user_money,is_vip,vip_level,vip_expire_time,ai_free_count,ai_free_date')
->findOrEmpty();
$user['has_password'] = !empty($user['password']);
$user['has_auth'] = self::hasWechatAuth($userId);
$user['version'] = config('project.version');
$vipInfo = $user->getVipInfo();
$user->hidden(['password', 'is_vip', 'vip_level', 'vip_expire_time', 'ai_free_count', 'ai_free_date']);
$result = $user->toArray();
$result['vip_info'] = $vipInfo;
return $result;
}
/**
* @notes 璁剧疆鐢ㄦ埛淇℃伅
* @param int $userId
* @param array $params
* @return User|false
* @author 娈佃獕
* @date 2022/9/21 16:53
*/
public static function setInfo(int $userId, array $params)
{
try {
if ($params['field'] == "avatar") {
$params['value'] = FileService::setFileUrl($params['value']);
}
return User::update(
[
'id' => $userId,
$params['field'] => $params['value']
]
);
} catch (\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
/**
* @notes 鏄惁鏈夊井淇℃巿鏉冧俊鎭?
* @param $userId
* @return bool
* @author 娈佃獕
* @date 2022/9/20 19:36
*/
public static function hasWechatAuth(int $userId)
{
//鏄惁鏈夊井淇℃巿鏉冪櫥褰?
$terminal = [UserTerminalEnum::WECHAT_MMP, UserTerminalEnum::WECHAT_OA, UserTerminalEnum::PC];
$auth = UserAuth::where(['user_id' => $userId])
->whereIn('terminal', $terminal)
->findOrEmpty();
return !$auth->isEmpty();
}
/**
* @notes 閲嶇疆鐧诲綍瀵嗙爜
* @param $params
* @return bool
* @author 娈佃獕
* @date 2022/9/16 18:06
*/
public static function resetPassword(array $params)
{
try {
// 鏍¢獙楠岃瘉鐮?
$smsDriver = new SmsDriver();
if (!$smsDriver->verify($params['mobile'], $params['code'], NoticeEnum::FIND_LOGIN_PASSWORD_CAPTCHA)) {
throw new \Exception('verification code error');
}
// 閲嶇疆瀵嗙爜
$passwordSalt = Config::get('project.unique_identification');
$password = create_password($params['password'], $passwordSalt);
// 鏇存柊
User::where('mobile', $params['mobile'])->update([
'password' => $password
]);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 淇瀵嗙爜
* @param $params
* @param $userId
* @return bool
* @author 娈佃獕
* @date 2022/9/20 19:13
*/
public static function changePassword(array $params, int $userId)
{
try {
$user = User::findOrEmpty($userId);
if ($user->isEmpty()) {
throw new \Exception('user not found');
}
// 瀵嗙爜鐩?
$passwordSalt = Config::get('project.unique_identification');
if (!empty($user['password'])) {
if (empty($params['old_password'])) {
throw new \Exception('璇峰~鍐欐棫瀵嗙爜');
}
$oldPassword = create_password($params['old_password'], $passwordSalt);
if ($oldPassword != $user['password']) {
throw new \Exception('鍘熷瘑鐮佷笉姝g‘');
}
}
// 淇濆瓨瀵嗙爜
$password = create_password($params['password'], $passwordSalt);
$user->password = $password;
$user->save();
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 鑾峰彇灏忕▼搴忔墜鏈哄彿
* @param array $params
* @return bool
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
* @author 娈佃獕
* @date 2023/2/27 11:49
*/
public static function getMobileByMnp(array $params)
{
try {
$response = (new WeChatMnpService())->getUserPhoneNumber($params['code']);
$phoneNumber = $response['phone_info']['purePhoneNumber'] ?? '';
if (empty($phoneNumber)) {
throw new \Exception('鑾峰彇鎵嬫満鍙风爜澶辫触');
}
$user = User::where([
['mobile', '=', $phoneNumber],
['id', '<>', $params['user_id']]
])->findOrEmpty();
if (!$user->isEmpty()) {
throw new \Exception('mobile number already bound to another account');
}
// 缁戝畾鎵嬫満鍙?
User::update([
'id' => $params['user_id'],
'mobile' => $phoneNumber
]);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 缁戝畾鎵嬫満鍙?
* @param $params
* @return bool
* @author 娈佃獕
* @date 2022/9/21 17:28
*/
public static function bindMobile(array $params)
{
try {
// 妫€鏌ユ墜鏈哄彿鏄惁宸茶鍏朵粬鐢ㄦ埛鍗犵敤
$existUser = User::where('mobile', $params['mobile'])
->where('id', '<>', $params['user_id'])
->findOrEmpty();
if (!$existUser->isEmpty()) {
throw new \Exception('璇ユ墜鏈哄彿宸茶鍏朵粬鐢ㄦ埛浣跨敤');
}
// 鏍¢獙鐭俊锛氫粠缂撳瓨鏍¢獙楠岃瘉鐮侊紝鐒跺悗鍙涓婁紶琛ㄥ瓨鍦ㄨ鎵嬫満鍙疯褰曞嵆瑙嗕负閫氳繃
$cacheKey = 'sms_verify_code:' . $params['mobile'];
$cached = \think\facade\Cache::get($cacheKey);
if (!$cached || $cached['code'] != $params['code']) {
throw new \Exception('verification code error');
}
// sms_upload.phone 鏄彂閫佹柟锛堢敤鎴锋墜鏈哄彿锛夛紝鐢ㄧ敤鎴锋墜鏈哄彿鏌ヨ
$userMobile = $params['mobile'];
$record = \app\common\model\SmsLog::where(function ($query) use ($userMobile) {
$query->where('phone', $userMobile)
->whereOr('phone', '+86' . $userMobile)
->whereOr('phone', '86' . $userMobile);
})
->order('sms_time', 'desc')
->findOrEmpty();
if ($record->isEmpty()) {
throw new \Exception('鐭俊鍔╂墜璁板綍涓湭鎵惧埌璇ユ墜鏈哄彿');
}
if ((int) $record->status === 0) {
$record->status = 1;
$record->save();
}
\think\facade\Cache::delete($cacheKey);
User::update([
'id' => $params['user_id'],
'mobile' => $params['mobile'],
'is_bind_mobile' => 1
]);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
}