1
|
|
|
const Redis = require('./libraries/redis') |
|
|
|
|
2
|
|
|
const Constant = require('./libraries/constant') |
|
|
|
|
3
|
|
|
const ApiError = require('./util/api_error') |
|
|
|
|
4
|
|
|
const _ = require('underscore') |
|
|
|
|
5
|
|
|
const ServiceAudit = require('./services/catering/audit') |
|
|
|
|
6
|
|
|
const ServiceStore = require('./services/catering/store') |
|
|
|
|
7
|
|
|
|
8
|
|
|
module.exports = function (permission) { |
9
|
|
|
|
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
|
|
|
|
94
|
|
|
} |
95
|
|
|
|