Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/catering_permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Constant = require('./libraries/constant')
const ApiError = require('./util/api_error')
const _ = require('underscore')
const ServiceAudit = require('./services/catering/audit')
const ServiceStore = require('./services/catering/store')

module.exports = function (permission) {

Expand Down Expand Up @@ -47,12 +48,26 @@ module.exports = function (permission) {
await next()
}

async function checkStore() {
await checkToken()
await ownerStore()
await next()
}

async function isAudit() {
let check = await ServiceAudit.getAudit(ctx.uid)
if (_.isEmpty(check)) throw new ApiError('auth.error', 'no permission audit')
return true
}

async function ownerStore() {
let storeId = ctx.params.storeId
let check = await ServiceStore.getStore(storeId)
if (_.isEmpty(check)) throw new ApiError('auth.error', 'no permission store')
if (check.seller_id != ctx.uid) throw new ApiError('auth.notPermission')
return true
}

// 检查header
if (!_.has(ctx.request.headers, 'store-id')) {
throw new ApiError('validate.error', 'store-id')
Expand All @@ -68,6 +83,8 @@ module.exports = function (permission) {
return await checkUser()
} else if (permission === 'audit') {
return await checkAudit()
} else if (permission === 'store') {
return await checkStore()
} else {
throw new ApiError('role.notExist')
}
Expand Down
50 changes: 50 additions & 0 deletions app/controller/catering/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const Response = require('../../util/response')
const ServiceStore = require('../../services/catering/store')

module.exports = {
/**
* @api {get} /api/catering/v1/stores/:storeId 获取店铺信息
* @apiVersion 1.0.0
* @apiGroup store
* @apiPermission store
*
* @apiDescription API to edit the customer information.
*
* @apiExample Example usage:
curl --request PUT \
--url https://garylv.com/api/catering/v1/stores/1 \
--header 'mina-source: catering' \
--header 'store-id: 1'
*
*
* @apiSuccess {String} data response data
*
* @apiSuccessExample {json} Visual Preview:
{
"success": true,
"code": 200,
"data": {
"id": 1,
"seller_id": 1,
"name": "新视觉烧烤",
"thumb": "https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83eo13vc2RhqKyJI8f5qaGCcj2ZeRfic696O0a2PDZvJ2JrGL8ia8EJHA6KjR37ia2neD11IBcNJ4HianZg/132",
"store_type_id": 0,
"theme": "",
"tel": "234",
"wechat": "",
"address": "12",
"status": 1,
"created_at": "2018-07-03T06:59:01.000Z",
"updated_at": null
},
"message": "请求成功"
}
* @apiSampleRequest https://garylv.com/api/catering/v1/stores/:storeId
*/
detail: async function (ctx) {

let data = await ServiceStore.getStore(ctx.params.storeId)
return Response.output(ctx, data)
},

}
1 change: 1 addition & 0 deletions app/libraries/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var constant = {
"CATERING_SESSION": "NS:API:CATERING_SESSION:",

"AUDIT_INFO": "NS:API:AUDIT_INFO:",
"STORE_INFO": "NS:API:STORE_INFO:",

"EXPIRE_REFRESH": 86400,
}
Expand Down
5 changes: 5 additions & 0 deletions app/router/catering/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const CateringPermissions = require('../../catering_permissions')
const Session = require('../../controller/catering/session')
const Customer = require('../../controller/catering/customer')
const Audit = require('../../controller/catering/audit')
const Store = require('../../controller/catering/store')

module.exports = function () {
/**
Expand All @@ -25,7 +26,11 @@ module.exports = function () {
// 小程序码
Router.post('/api/catering/v1/customers/:uid/mina_qrcodes', CateringPermissions('user'), Customer.minaQRcode)

// 审核商家
Router.post('/api/catering/v1/audits/:uid/sellers/:sellerId', CateringPermissions('audit'), Audit.seller)

// 商店信息
Router.get('/api/catering/v1/stores/:storeId', CateringPermissions('store'), Store.detail)

return Router;
}
24 changes: 24 additions & 0 deletions app/services/catering/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const ModelStore = require('../../model/catering/store')
const Redis = require('../../libraries/redis')
const Constant = require('../../libraries/constant')
const _ = require('underscore')

module.exports = {
getStore: async function (id) {
let cacheKey = Constant.STORE_INFO + id
let result = await Redis.get(cacheKey)
if (_.isEmpty(result)) {
result = await ModelStore.first(id)
if (_.isEmpty(result)) return false
else await Redis.set(cacheKey, JSON.stringify(result))
} else {
result = JSON.parse(result)
}

// 每次拿都刷新时间
await Redis.expire(cacheKey, Constant.EXPIRE_REFRESH)

return result
},

}