123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- const { Random } = require('mockjs')
- const tokens = {
- admin: `admin-token-${Random.guid()}-${new Date().getTime()}`,
- editor: `editor-token-${Random.guid()}-${new Date().getTime()}`,
- test: `test-token-${Random.guid()}-${new Date().getTime()}`,
- }
- const username2role = {
- admin: ['Admin'],
- editor: ['Editor'],
- test: ['Admin', 'Editor'],
- }
- const role2permission = {
- Admin: ['read:system', 'write:system', 'delete:system'],
- Editor: ['read:system', 'write:system'],
- Test: ['read:system'],
- }
- module.exports = [
- {
- url: '/publicKey',
- type: 'get',
- response() {
- return {
- code: 200,
- msg: 'success',
- data: {
- mockServer: true,
- publicKey:
- 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDBT2vr+dhZElF73FJ6xiP181txKWUSNLPQQlid6DUJhGAOZblluafIdLmnUyKE8mMHhT3R+Ib3ssZcJku6Hn72yHYj/qPkCGFv0eFo7G+GJfDIUeDyalBN0QsuiE/XzPHJBuJDfRArOiWvH0BXOv5kpeXSXM8yTt5Na1jAYSiQ/wIDAQAB',
- },
- }
- },
- },
- {
- url: '/login',
- type: 'post',
- response(config) {
- const { username } = config.body
- const token = tokens[username]
- if (!token)
- return {
- code: 500,
- msg: '帐户或密码不正确',
- }
- return {
- code: 200,
- msg: 'success',
- data: { token },
- }
- },
- },
- {
- url: '/socialLogin',
- type: 'post',
- response(config) {
- const { code } = config.body
- if (!code)
- return {
- code: 500,
- msg: '未成功获取Token',
- }
- return {
- code: 200,
- msg: 'success',
- data: { token: tokens['admin'] },
- }
- },
- },
- {
- url: '/register',
- type: 'post',
- response() {
- return {
- code: 200,
- msg: '模拟注册成功',
- data: { token: tokens['editor'] },
- }
- },
- },
- {
- url: '/userInfo',
- type: 'get',
- response(config) {
- const authorization =
- config.headers.authorization || config.headers.Authorization
- if (!authorization.startsWith('Bearer '))
- return {
- code: 401,
- msg: '令牌无效',
- }
- const _authorization = authorization.replace('Bearer ', '')
- const isTrue = _authorization.includes('-token-')
- const username = isTrue ? _authorization.split('-token-')[0] : 'admin'
- const roles = username2role[username] || []
- const permissions = [
- ...new Set(roles.flatMap((role) => role2permission[role])),
- ]
- return {
- code: 200,
- msg: 'success',
- data: {
- username,
- roles,
- permissions,
- avatar: 'https://i.gtimg.cn/club/item/face/img/2/16022_100.gif',
- },
- }
- },
- },
- {
- url: '/logout',
- type: 'get',
- response() {
- return {
- code: 200,
- msg: 'success',
- }
- },
- },
- ]
|