| | |
| | | import * as _ from 'lodash'; |
| | | import { CoolCommException, CoolUrlTagData, TagTypes } from '@cool-midway/core'; |
| | | import { Utils } from '../../../comm/utils'; |
| | | import { UserInfoService } from '../service/info'; |
| | | |
| | | /** |
| | | * 用户 |
| | |
| | | let { url } = ctx; |
| | | url = url.replace(this.prefix, '').split('?')[0]; |
| | | if (_.startsWith(url, '/app/')) { |
| | | const token = ctx.get('Authorization'); |
| | | let token = ctx.get('Authorization') || ''; |
| | | token = token.replace(/^Bearer\s+/i, '').trim(); |
| | | try { |
| | | ctx.user = jwt.verify(token, this.jwtConfig.secret); |
| | | |
| | | if (ctx.user.isRefresh) { |
| | | throw new CoolCommException('登录失效~'); |
| | | } catch (error) { |
| | | ctx.user = undefined; |
| | | if (token) { |
| | | console.error('[user] token无效:', (error as Error).message); |
| | | } |
| | | } catch (error) {} |
| | | } |
| | | if (ctx.user?.isRefresh) { |
| | | ctx.user = undefined; |
| | | } else if (ctx.user?.id) { |
| | | try { |
| | | // 令牌里的 id 是 unionid(手机号登录时就是手机号),整型 userId 列要用用户主键 |
| | | const userInfoService = await ctx.requestContext.getAsync( |
| | | UserInfoService |
| | | ); |
| | | const info = await userInfoService.person(ctx.user.id); |
| | | if (info?.id) { |
| | | ctx.user.userId = info.id; |
| | | } |
| | | } catch (error) {} |
| | | } |
| | | // 使用matchUrl方法来检查URL是否应该被忽略 |
| | | const isIgnored = this.ignoreUrls.some(pattern => |
| | | this.utils.matchUrl(pattern, url) |