| Conditions | 7 |
| Paths | 4 |
| Total Lines | 83 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | const Redis = require('./libraries/redis') |
||
| 10 | return async function (ctx, next) { |
||
| 11 | |||
| 12 | async function checkToken() { |
||
| 13 | let token = (typeof (ctx.request.headers.token) == 'undefined' || !ctx.request.headers.token) ? |
||
| 14 | ctx.cookies.get('token') : ctx.request.headers.token |
||
| 15 | let uid = (typeof (ctx.request.headers.uid) == 'undefined' || !ctx.request.headers.uid) ? |
||
| 16 | ctx.cookies.get('uid') : ctx.request.headers.uid |
||
| 17 | |||
| 18 | if (!token || !uid) { |
||
| 19 | console.log('token: ' + token) |
||
|
|
|||
| 20 | console.log('uid: ' + uid) |
||
| 21 | throw new ApiError('auth.error', 'token missing') |
||
| 22 | } |
||
| 23 | |||
| 24 | sessionKey = Constant.CATERING_SESSION + token |
||
| 25 | session = await Redis.get(sessionKey) |
||
| 26 | session = JSON.parse(session) |
||
| 27 | if (!session) { |
||
| 28 | throw new ApiError('auth.error', 'token error') |
||
| 29 | } |
||
| 30 | |||
| 31 | if (session.uid == uid) { |
||
| 32 | ctx.uid = uid |
||
| 33 | return true |
||
| 34 | } else { |
||
| 35 | throw new ApiError('auth.error', 'no permission') |
||
| 36 | } |
||
| 37 | |||
| 38 | } |
||
| 39 | |||
| 40 | async function checkUser() { |
||
| 41 | await checkToken() |
||
| 42 | await next() |
||
| 43 | } |
||
| 44 | |||
| 45 | async function checkAudit() { |
||
| 46 | await checkToken() |
||
| 47 | await isAudit() |
||
| 48 | await next() |
||
| 49 | } |
||
| 50 | |||
| 51 | async function checkStore() { |
||
| 52 | await checkToken() |
||
| 53 | await ownerStore() |
||
| 54 | await next() |
||
| 55 | } |
||
| 56 | |||
| 57 | async function isAudit() { |
||
| 58 | let check = await ServiceAudit.getAudit(ctx.uid) |
||
| 59 | if (_.isEmpty(check)) throw new ApiError('auth.error', 'no permission audit') |
||
| 60 | return true |
||
| 61 | } |
||
| 62 | |||
| 63 | async function ownerStore() { |
||
| 64 | let storeId = ctx.params.storeId |
||
| 65 | let check = await ServiceStore.getStore(storeId) |
||
| 66 | if (_.isEmpty(check)) throw new ApiError('auth.error', 'no permission store') |
||
| 67 | if (check.seller_id != ctx.uid) throw new ApiError('auth.notPermission') |
||
| 68 | return true |
||
| 69 | } |
||
| 70 | |||
| 71 | // 检查header |
||
| 72 | if (!_.has(ctx.request.headers, 'store-id')) { |
||
| 73 | throw new ApiError('validate.error', 'store-id') |
||
| 74 | } |
||
| 75 | if (!_.has(ctx.request.headers, 'mina-source')) { |
||
| 76 | throw new ApiError('validate.error', 'mina-source') |
||
| 77 | } |
||
| 78 | |||
| 79 | // guest |
||
| 80 | if (permission === 'guest') { |
||
| 81 | await next() |
||
| 82 | } else if (permission === 'user') { |
||
| 83 | return await checkUser() |
||
| 84 | } else if (permission === 'audit') { |
||
| 85 | return await checkAudit() |
||
| 86 | } else if (permission === 'store') { |
||
| 87 | return await checkStore() |
||
| 88 | } else { |
||
| 89 | throw new ApiError('role.notExist') |
||
| 90 | } |
||
| 91 | |||
| 92 | } |
||
| 93 | |||
| 95 |