初始版本

This commit is contained in:
贾祥聪
2025-08-19 14:16:51 +08:00
commit f937a1f9b9
4373 changed files with 359728 additions and 0 deletions

View File

@@ -0,0 +1,453 @@
<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台PHP版
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | gitee下载https://gitee.com/likeshop_gitee/likeadmin
// | github下载https://github.com/likeshop-github/likeadmin
// | 访问官网https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\shopapi\logic;
use app\common\enum\notice\NoticeEnum;
use app\common\enum\shop\ShopEnum;
use app\common\logic\BaseLogic;
use app\common\model\coach\Coach;
use app\common\model\shop\Shop;
use app\common\model\shop\ShopUser;
use app\shopapi\service\ShopUserTokenService;
use app\common\service\{ConfigService, FileService, sms\SmsDriver, WeChatService};
use think\facade\{Db, Config};
use think\Exception;
/**
* 登录逻辑
* Class LoginLogic
* @package app\coachapi\logic
*/
class LoginLogic extends BaseLogic
{
/**
* @notes 账号密码注册
* @param array $params
* @return bool
* @author 段誉
* @date 2022/9/7 15:37
*/
public static function register(array $params)
{
try {
$smsDriver = new SmsDriver();
$result = $smsDriver->verify($params['account'], $params['code'], NoticeEnum::REGISTER_CAPTCHA_SHOP);
if (!$result) {
throw new Exception('验证码错误');
}
$number = ShopUser::count()+1;
$sn = sprintf("%03d", $number);
$passwordSalt = Config::get('project.unique_identification');
$password = create_password($params['password'], $passwordSalt);
$avatar = ConfigService::get('default_image', 'user_avatar');
ShopUser::create([
'sn' => $sn,
'avatar' => $avatar,
'account' => $params['account'],
'password' => $password,
// 'channel' => $params['channel'],
]);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 账号/手机号登录,手机号验证码
* @param $params
* @return array|false
* @author 段誉
* @date 2022/9/6 19:26
*/
public static function login($params)
{
try {
$where = ['account' => $params['account']];
$shopUser = ShopUser::where($where)->findOrEmpty();
if ($shopUser->isEmpty()) {
throw new Exception('账号不存在');
}
$shop = Shop::where(['shop_user_id'=>$shopUser['id']])->order('id desc')->findOrEmpty();
// if($shop->isEmpty()){
// throw new Exception('您的账号未申请入驻,请先申请入驻',-10);
// }
// //待审核
// if(ShopEnum::AUDIT_STATUS_WAIT == $shop->audit_status){
// throw new Exception('感谢您对我们平台的支持我们将在13个工作日内审核');
// }
// //审核不通过
// if(ShopEnum::AUDIT_STATUS_REFUSE == $shop->audit_status){
// throw new Exception($shop->audit_remark,-20);
// }
//更新登录信息
$shopUser->login_time = time();
$shopUser->login_ip = request()->ip();
$shopUser->save();
//设置token
$shopUserInfo = ShopUserTokenService::setToken($shopUser->id, $params['terminal']);
//返回登录信息
$avatar = $shopUser->avatar ?: Config::get('project.default_image.user_avatar');
$avatar = FileService::getFileUrl($avatar);
return [
'sn' => $shopUserInfo['sn'],
'account' => $shopUserInfo['account'],
'avatar' => $avatar,
'token' => $shopUserInfo['token'],
'audit_status' => $shop->audit_status,
'audit_remark' => $shop->audit_remark,
];
} catch (Exception $e) {
self::setError($e->getMessage());
self::setReturnCode($e->getCode());
return false;
}
}
/**
* @notes 退出登录
* @param $shopInfo
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 段誉
* @date 2022/9/16 17:56
*/
public static function logout($shopInfo)
{
//token不存在不注销
if (!isset($shopInfo['token'])) {
return false;
}
//设置token过期
return ShopUserTokenService::expireToken($shopInfo['token']);
}
/**
* @notes 获取微信请求code的链接
* @param string $url
* @return string
* @author 段誉
* @date 2022/9/20 19:47
*/
public static function codeUrl(string $url)
{
return WeChatService::getCodeUrl($url);
}
/**
* @notes 公众号登录
* @param array $params
* @return array|false
* @throws \GuzzleHttp\Exception\GuzzleException
* @author 段誉
* @date 2022/9/20 19:47
*/
public static function oaLogin(array $params)
{
Db::startTrans();
try {
//通过code获取微信 openid
$response = WeChatService::getOaResByCode($params);
$userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_OA);
$userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
// 更新登录信息
self::updateLoginInfo($userInfo['id']);
Db::commit();
return $userInfo;
} catch (\Exception $e) {
Db::rollback();
self::$error = $e->getMessage();
return false;
}
}
/**
* @notes 小程序-静默登录
* @param array $params
* @return array|false
* @author 段誉
* @date 2022/9/20 19:47
*/
public static function silentLogin(array $params)
{
try {
//通过code获取微信 openid
$response = WeChatService::getMnpResByCode($params);
$userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_MMP);
$userInfo = $userServer->getResopnseByUserInfo('silent')->getUserInfo();
if (!empty($userInfo)) {
// 更新登录信息
self::updateLoginInfo($userInfo['id']);
}
return $userInfo;
} catch (\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
/**
* @notes 小程序-授权登录
* @param array $params
* @return array|false
* @author 段誉
* @date 2022/9/20 19:47
*/
public static function mnpLogin(array $params)
{
Db::startTrans();
try {
//通过code获取微信 openid
$response = WeChatService::getMnpResByCode($params);
$userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_MMP);
$userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
// 更新登录信息
self::updateLoginInfo($userInfo['id']);
Db::commit();
return $userInfo;
} catch (\Exception $e) {
Db::rollback();
self::$error = $e->getMessage();
return false;
}
}
/**
* @notes 更新登录信息
* @param $userId
* @throws \Exception
* @author 段誉
* @date 2022/9/20 19:46
*/
public static function updateLoginInfo($userId)
{
$user = User::findOrEmpty($userId);
if ($user->isEmpty()) {
throw new \Exception('用户不存在');
}
$time = time();
$user->login_time = $time;
$user->login_ip = request()->ip();
$user->update_time = $time;
$user->save();
}
/**
* @notes 小程序端绑定微信
* @param array $params
* @return bool
* @author 段誉
* @date 2022/9/20 19:46
*/
public static function mnpAuthLogin(array $params)
{
try {
//通过code获取微信openid
$response = WeChatService::getMnpResByCode($params);
$response['user_id'] = $params['user_id'];
$response['terminal'] = UserTerminalEnum::WECHAT_MMP;
return self::createAuth($response);
} catch (\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
/**
* @notes 公众号端绑定微信
* @param array $params
* @return bool
* @throws \GuzzleHttp\Exception\GuzzleException
* @author 段誉
* @date 2022/9/16 10:43
*/
public static function oaAuthLogin(array $params)
{
try {
//通过code获取微信openid
$response = WeChatService::getOaResByCode($params);
$response['user_id'] = $params['user_id'];
$response['terminal'] = UserTerminalEnum::WECHAT_OA;
return self::createAuth($response);
} catch (\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
/**
* @notes 生成授权记录
* @param $response
* @return bool
* @throws \Exception
* @author 段誉
* @date 2022/9/16 10:43
*/
public static function createAuth($response)
{
//先检查openid是否有记录
$isAuth = UserAuth::where('openid', '=', $response['openid'])->findOrEmpty();
if (!$isAuth->isEmpty()) {
throw new \Exception('该微信已经绑定其他账号');
}
if (isset($response['unionid']) && !empty($response['unionid'])) {
//在用unionid找记录防止生成两个账号同个unionid的问题
$userAuth = UserAuth::where(['unionid' => $response['unionid']])
->findOrEmpty();
if (!$userAuth->isEmpty() && $userAuth->user_id != $response['user_id']) {
throw new \Exception('该微信已经绑定其他账号');
}
}
//如果没有授权,直接生成一条微信授权记录
UserAuth::create([
'user_id' => $response['user_id'],
'openid' => $response['openid'],
'unionid' => $response['unionid'] ?? '',
'terminal' => $response['terminal'],
]);
return true;
}
/**
* @notes 解绑微信(测试用)
* @param $user_id
* @return bool
* @author ljj
* @date 2023/1/11 10:35 上午
*/
public static function unbinding($user_id)
{
$session = UserSession::where(['user_id'=>$user_id])->findOrEmpty()->toArray();
UserAuth::where(['user_id'=>$user_id,'terminal'=>$session['terminal']])->delete();
return true;
}
/**
* @notes 更新用户头像昵称
* @param $post
* @param $user_id
* @return bool
* @throws \think\db\exception\DbException
* @author ljj
* @date 2023/2/2 6:36 下午
*/
public static function updateUser($post,$user_id)
{
Db::name('user')->where(['id'=>$user_id])->update(['nickname'=>$post['nickname'],'avatar'=>FileService::setFileUrl($post['avatar']),'is_new_user'=>0]);
return true;
}
/**
* @notes 重置登录密码
* @param $params
* @return bool
* @author 段誉
* @date 2022/9/16 18:06
*/
public function resetPassword(array $params): bool
{
try {
// Db::startTrans();
// 校验验证码
$smsDriver = new SmsDriver();
if (!$smsDriver->verify($params['mobile'], $params['code'], NoticeEnum::RESET_PASSWORD_CAPTCHA_SHOP)) {
throw new \Exception('验证码错误');
}
// 重置密码
$passwordSalt = Config::get('project.unique_identification');
$password = create_password($params['password'], $passwordSalt);
// 更新
ShopUser::where('account', $params['mobile'])->update([
'password' => $password
]);
Db::commit();
// return true;
} catch (\Exception $e) {
// Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 重置登录密码
* @param $params
* @return bool
* @author 段誉
* @date 2022/9/16 18:06
*/
public function changePassword(array $params): bool
{
try {
// 重置密码
$passwordSalt = Config::get('project.unique_identification');
$password = create_password($params['password'], $passwordSalt);
$shopUser = ShopUser::where(['id'=>$params['shop_info']['shop_user_id']])->findOrEmpty();
// if($coachUser->isEmpty()){
// throw new \Exception('账号不存在');
// }
// 更新
$shopUser->password = $password;
$shopUser->save();
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
}