Files
anmo/server/app/coachapi/service/CoachUserTokenService.php
2025-08-19 14:16:51 +08:00

124 lines
4.3 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// +----------------------------------------------------------------------
// | likeshop100%开源免费商用商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | 商业版本务必购买商业授权,以免引起法律纠纷
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | gitee下载https://gitee.com/likeshop_gitee
// | github下载https://github.com/likeshop-github
// | 访问官网https://www.likeshop.cn
// | 访问社区https://home.likeshop.cn
// | 访问手册http://doc.likeshop.cn
// | 微信公众号likeshop技术社区
// | likeshop团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshopTeam
// +----------------------------------------------------------------------
namespace app\coachapi\service;
use app\common\cache\CoachUserTokenCache;
use app\common\cache\UserTokenCache;
use app\common\model\coach\CoachUserSession;
use app\common\model\coach\ShopUserSession;
use app\common\model\user\UserSession;
use think\facade\Config;
class CoachUserTokenService
{
/**
* @notes 设置或更新用户token
* @param $userId
* @param $terminal
* @param int $multipointLogin
* @return array|false|mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 令狐冲
* @date 2021/7/13 23:07
*/
public static function setToken($userId, $terminal)
{
$time = time();
$coachUserSession = CoachUserSession::where([['coach_user_id', '=', $userId], ['terminal', '=', $terminal]])->find();
//获取token延长过期的时间
$expireTime = $time + Config::get('project.coach_user_token.expire_duration');
$coachUserTokenCache = new CoachUserTokenCache();
//token处理
if ($coachUserSession) {
//清空缓存
$coachUserTokenCache->deleteCoachUserInfo($coachUserSession->token);
//重新获取token
$coachUserSession->token = create_token($userId);
$coachUserSession->expire_time = $expireTime;
$coachUserSession->update_time = $time;
$coachUserSession->save();
} else {
//找不到在该终端的token记录创建token记录
$coachUserSession = CoachUserSession::create([
'coach_user_id' => $userId,
'terminal' => $terminal,
'token' => create_token($userId),
'expire_time' => $expireTime
]);
}
return $coachUserTokenCache->setCoachUserInfo($coachUserSession->token);
}
/**
* @notes 延长token过期时间
* @param $token
* @return array|false|mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 令狐冲
* @date 2021/7/5 14:25
*/
public static function overtimeToken($token)
{
$time = time();
$adminSession = UserSession::where('token', '=', $token)->find();
//延长token过期时间
$adminSession->expire_time = $time + Config::get('project.admin_token.expire_duration');
$adminSession->update_time = $time;
$adminSession->save();
return (new UserTokenCache())->setUserInfo($adminSession->token);
}
/**
* @notes 设置token为过期
* @param $token
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 令狐冲
* @date 2021/7/5 14:31
*/
public static function expireToken($token)
{
$userSession = UserSession::where('token', '=', $token)
->find();
if (empty($userSession)) {
return false;
}
$time = time();
$userSession->expire_time = $time;
$userSession->update_time = $time;
$userSession->save();
return (new UserTokenCache())->deleteUserInfo($token);
}
}