diff --git a/app/controller/catering/seller.js b/app/controller/catering/seller.js new file mode 100644 index 0000000..9e7810b --- /dev/null +++ b/app/controller/catering/seller.js @@ -0,0 +1,43 @@ +const Response = require('../../util/response') +const ServiceSeller = require('../../services/catering/seller') + +module.exports = { + /** + * @api {get} /api/catering/v1/sellers/:sellerId/stores 商家的店铺列表 + * @apiGroup seller + * @apiPermission user + * @apiVersion 1.0.0 + * @apiParam {String} param 参数 + * @apiSuccess {String} data 返回数据 + * @apiSuccessExample {json} Visual Preview: + * { + "success": true, + "code": 200, + "data": [ + { + "seller_id": 1, + "name": "吕松东🇧🇷🇫🇷", + "thumb": "https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83eo13vc2RhqKyJI8f5qaGCcj2ZeRfic696O0a2PDZvJ2JrGL8ia8EJHA6KjR37ia2neD11IBcNJ4HianZg/132", + "store_type_id": 0, + "theme": "", + "tel": "", + "wechat": "", + "license": "", + "address": "", + "approve_status": 0, + "status": 1, + "audit_id": 0, + "id": 1 + } + ], + "message": "请求成功" + } + */ + stores: async function (ctx) { + let data = await ServiceSeller.storeList(ctx.uid) + + return Response.output(ctx, data) + }, + + +} diff --git a/app/controller/catering/viewer.js b/app/controller/catering/viewer.js new file mode 100644 index 0000000..f029f56 --- /dev/null +++ b/app/controller/catering/viewer.js @@ -0,0 +1,89 @@ +const Response = require('../../util/response') +const ServiceViewer = require('../../services/catering/viewer') +const ServiceCategories = require('../../services/catering/category') +const ServiceProducts = require('../../services/catering/product') +const Validate = require('request-validate') + +module.exports = { + /** + * @api {get} /api/catering/v1/viewers viewer列表 + * @apiGroup viewer + * @apiPermission todo + * @apiVersion 1.0.0 + * @apiParam {String} param 参数 + * @apiSuccess {String} data 返回数据 + * @apiSuccessExample {json} Visual Preview: + * { + "success": true, + "code": 200, + "message": "请求成功", + "data": [ + { + "id": 1, + }, + .... + ] + } + */ + index: async function (ctx) { + let data = await ServiceViewer.list() + + return Response.output(ctx, data) + }, + + /** + * @api {get} /api/catering/v1/viewers/:viewerId/categories 访问店铺的分类 + * @apiGroup viewer + * @apiPermission guest + * @apiVersion 1.0.0 + * @apiParam {String} param 参数 + * @apiSuccess {String} data 返回数据 + * @apiSuccessExample {json} Visual Preview: + * { + "success": true, + "code": 200, + "message": "请求成功", + "data": [ + { + "id": 1, + }, + .... + ] + } + */ + categories: async function (ctx) { + let data = await ServiceCategories.list(ctx.params.viewerId, { status: 1 }) + + return Response.output(ctx, data) + }, + + /** + * @api {get} /api/catering/v1/viewers/:viewerId/products 访问店铺的商品 + * @apiGroup viewer + * @apiPermission guest + * @apiVersion 1.0.0 + * @apiParam {String} param 参数 + * @apiSuccess {String} data 返回数据 + * @apiSuccessExample {json} Visual Preview: + * { + "success": true, + "code": 200, + "message": "请求成功", + "data": [ + { + "id": 1, + }, + .... + ] + } + */ + products: async function (ctx) { + Validate(ctx.input, { + 'category_id': 'required|nunumericm' + }) + let data = await ServiceProducts.list(ctx.params.viewerId, { status: 1, categroy_id: ctx.input.category_id }) + + return Response.output(ctx, data) + }, + +} diff --git a/app/libraries/constant.js b/app/libraries/constant.js index a79d7c9..a76842d 100644 --- a/app/libraries/constant.js +++ b/app/libraries/constant.js @@ -3,6 +3,7 @@ var constant = { "CATERING_SESSION": "NS:API:CATERING_SESSION:", "AUDIT_INFO": "NS:API:AUDIT_INFO:", + "USER_INFO": "NS:API:USER_INFO:", "STORE_INFO": "NS:API:STORE_INFO:", "EXPIRE_REFRESH": 86400, diff --git a/app/model/catering/audit.js b/app/model/catering/audit.js index fc79334..a617dad 100644 --- a/app/model/catering/audit.js +++ b/app/model/catering/audit.js @@ -11,21 +11,21 @@ module.exports = { first: async function (id) { - let result = DB.readMysql.first( + let result = await DB.readMysql.first( '*' ) .from(table) .where('audit_id', id) .where('status', 1) - return await result + return result }, edit: async function (data, where, notWhere = {}) { let result = await ModelBase.execUpdate(table, data, where, notWhere) - return await result + return result } } \ No newline at end of file diff --git a/app/model/catering/category.js b/app/model/catering/category.js index 18854e1..2c983dc 100644 --- a/app/model/catering/category.js +++ b/app/model/catering/category.js @@ -1,5 +1,6 @@ const DB = require('../../libraries/db') const ModelBase = require('../../model/base') +const _ = require('underscore') const table = 'mist_category' @@ -9,14 +10,16 @@ module.exports = { return res; }, - list: async function (storeId) { + list: async function (storeId, filter = {}) { let result = DB.readMysql.select( '*' ) .from(table) .where('store_id', storeId) - .where('status', '!=', -1) + .whereNot('status', -1) + + if (_.has(filter, 'status')) result.where('status', filter.status) return await result @@ -24,20 +27,20 @@ module.exports = { first: async function (id) { - let result = DB.readMysql.first( + let result = await DB.readMysql.first( '*' ) .from(table) .where('id', id) - return await result + return result }, edit: async function (data, where, notWhere = {}) { let result = await ModelBase.execUpdate(table, data, where, notWhere) - return await result + return result }, getMaxSort: async function (storeId) { diff --git a/app/model/catering/customer.js b/app/model/catering/customer.js index c00dda6..09a6fc9 100644 --- a/app/model/catering/customer.js +++ b/app/model/catering/customer.js @@ -13,32 +13,32 @@ module.exports = { getCustomerByOpenId: async function (openid) { - let user = DB.readMysql.first( + let user = await DB.readMysql.first( '*' ) .from(table) .where('openid', openid) - return await user + return user }, first: async function (customerId) { - let user = DB.readMysql.first( + let user = await DB.readMysql.first( '*' ) .from(table) .where('id', customerId) - return await user + return user }, edit: async function (data, where, notWhere = {}) { let result = await ModelBase.execUpdate(table, data, where, notWhere) - return await result + return result } } \ No newline at end of file diff --git a/app/model/catering/desk.js b/app/model/catering/desk.js index 292aa43..8b7d521 100644 --- a/app/model/catering/desk.js +++ b/app/model/catering/desk.js @@ -11,33 +11,33 @@ module.exports = { list: async function (storeId) { - let result = DB.readMysql.select( + let result = await DB.readMysql.select( '*' ) .from(table) .where('store_id', storeId) .where('status', '!=', -1) - return await result + return result }, first: async function (id) { - let result = DB.readMysql.first( + let result = await DB.readMysql.first( '*' ) .from(table) .where('id', id) - return await result + return result }, edit: async function (data, where, notWhere = {}) { let result = await ModelBase.execUpdate(table, data, where, notWhere) - return await result + return result } } diff --git a/app/model/catering/product.js b/app/model/catering/product.js index 7d28ff4..65b5468 100644 --- a/app/model/catering/product.js +++ b/app/model/catering/product.js @@ -1,5 +1,6 @@ const DB = require('../../libraries/db') const ModelBase = require('../../model/base') +const _ = require('underscore') const table = 'mist_product' @@ -16,7 +17,13 @@ module.exports = { ) .from(table) .where('store_id', storeId) - .where('status', '!=', -1) + .whereNot('status', -1) + + if (_.has(filter, 'status')) result.where('status', filter.status) + + if (_.has(filter, 'category_id')) { + // todo + } return await result @@ -35,7 +42,7 @@ module.exports = { }, edit: async function (data, where, notWhere = {}) { - let result = await ModelBase.execUpdate(table, data, where, notWhere) + let result = ModelBase.execUpdate(table, data, where, notWhere) return await result } diff --git a/app/model/catering/seller.js b/app/model/catering/seller.js index 91b4bf1..b0c5c89 100644 --- a/app/model/catering/seller.js +++ b/app/model/catering/seller.js @@ -5,26 +5,39 @@ const table = 'mist_seller' module.exports = { add: async function (data) { - let res = await ModelBase.execInsert(table, data) - return res; + let res = ModelBase.execInsert(table, data) + return await res; }, + list: async function (kid) { + + let result = DB.readMysql.select( + '*' + ) + .from(table) + .where('kid', kid) + .where('status', '!=', -1) + + return await result + + }, + first: async function (id) { let result = DB.readMysql.first( '*' ) .from(table) - .where('seller_id', id) + .where('id', id) return await result }, edit: async function (data, where, notWhere = {}) { - let result = await ModelBase.execUpdate(table, data, where, notWhere) + let result = ModelBase.execUpdate(table, data, where, notWhere) return await result } -} \ No newline at end of file +} diff --git a/app/model/catering/store.js b/app/model/catering/store.js index ed0bf35..dcb933a 100644 --- a/app/model/catering/store.js +++ b/app/model/catering/store.js @@ -21,8 +21,20 @@ module.exports = { }, + getSellerStores: async function (sellerId) { + + let result = DB.readMysql.select( + '*' + ) + .from(table) + .where('seller_id', sellerId) + + return await result + + }, + edit: async function (data, where, notWhere = {}) { - let result = await ModelBase.execUpdate(table, data, where, notWhere) + let result = ModelBase.execUpdate(table, data, where, notWhere) return await result } diff --git a/app/router/catering/index.js b/app/router/catering/index.js index 9b87b52..7441527 100644 --- a/app/router/catering/index.js +++ b/app/router/catering/index.js @@ -8,6 +8,8 @@ const Store = require('../../controller/catering/store') const Category = require('../../controller/catering/category') const Desk = require('../../controller/catering/desk') const Product = require('../../controller/catering/product') +const Viewer = require('../../controller/catering/viewer') +const Seller = require('../../controller/catering/seller') module.exports = function () { /** @@ -35,6 +37,9 @@ module.exports = function () { // 商店信息 Router.get('/api/catering/v1/stores/:storeId', CateringPermissions('store'), Store.detail) + // 商店列表 + Router.get('/api/catering/v1/sellers/:sellerId/stores', CateringPermissions('user'), Seller.stores) + // 商店分类 Router.get('/api/catering/v1/stores/:storeId/categories', CateringPermissions('store'), Category.index) Router.post('/api/catering/v1/stores/:storeId/categories', CateringPermissions('store'), Category.add) @@ -56,5 +61,9 @@ module.exports = function () { Router.put('/api/catering/v1/stores/:storeId/products/:productId', CateringPermissions('store'), Product.edit) Router.delete('/api/catering/v1/stores/:storeId/products/:productId', CateringPermissions('store'), Product.delete) + // 店铺Viewer Category + Router.get('/api/catering/v1/viewers/:viewerId/categories', CateringPermissions('guest'), Viewer.categories) + Router.get('/api/catering/v1/viewers/:viewerId/products', CateringPermissions('guest'), Viewer.products) + return Router; } \ No newline at end of file diff --git a/app/services/catering/category.js b/app/services/catering/category.js index d434ec5..32e3b9a 100644 --- a/app/services/catering/category.js +++ b/app/services/catering/category.js @@ -5,7 +5,7 @@ const _ = require('underscore') module.exports = { list: async function (storeId, filter = {}) { - let result = await ModelCategory.list(storeId) + let result = await ModelCategory.list(storeId, filter) return result }, diff --git a/app/services/catering/customer.js b/app/services/catering/customer.js index c0e32cf..09506c3 100644 --- a/app/services/catering/customer.js +++ b/app/services/catering/customer.js @@ -1,3 +1,24 @@ +const ModelCustomer = require('../../model/catering/customer') +const Redis = require('../../libraries/redis') +const Constant = require('../../libraries/constant') +const _ = require('underscore') + module.exports = { + getCustomerInfo: async function (customerId) { + let cacheKey = Constant.USER_INFO + customerId + let result = await Redis.get(cacheKey) + if (_.isEmpty(result)) { + result = await ModelCustomer.first(customerId) + 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 + }, } \ No newline at end of file diff --git a/app/services/catering/product.js b/app/services/catering/product.js index b9e5789..84cee00 100644 --- a/app/services/catering/product.js +++ b/app/services/catering/product.js @@ -4,8 +4,8 @@ const Validate = require('request-validate') const _ = require('underscore') module.exports = { - list: async function (storeId) { - let result = await ModelProduct.list(storeId) + list: async function (storeId, filter = {}) { + let result = await ModelProduct.list(storeId, filter) return result }, diff --git a/app/services/catering/seller.js b/app/services/catering/seller.js new file mode 100644 index 0000000..8a71773 --- /dev/null +++ b/app/services/catering/seller.js @@ -0,0 +1,34 @@ +const ModelStore = require('../../model/catering/store') +const ServiceCustomer = require('../../services/catering/customer') +const ApiError = require('../../util/api_error') +const Validate = require('request-validate') +const _ = require('underscore') +const ConfigOss = require('../../../config/oss') + +module.exports = { + storeList: async function (sellerId) { + let result = await ModelStore.getSellerStores(sellerId) + + // 默认开店 + if (result.length <= 0) { + let customerInfo = await ServiceCustomer.getCustomerInfo(sellerId) + let storeInfo = { + seller_id: customerInfo.id, + name: customerInfo.nickname, + thumb: customerInfo.avatar, + store_type_id: 0, theme: '', tel: '', wechat: '', license: '', address: '', + approve_status: 0, status: 1, audit_id: 0 + } + let storeRes = await ModelStore.add(storeInfo) + storeInfo.id = storeRes.insertId + result = [storeInfo] + } + + result.forEach(element => { + if (element.thumb && element.thumb.indexOf('http') < 0) element.thumb = ConfigOss.catering.view_server + element.thumb + }) + + return result + }, + +} diff --git a/app/services/catering/viewer.js b/app/services/catering/viewer.js new file mode 100644 index 0000000..d80fe70 --- /dev/null +++ b/app/services/catering/viewer.js @@ -0,0 +1,11 @@ +const ApiError = require('../../util/api_error') +const Validate = require('request-validate') +const _ = require('underscore') + +module.exports = { + list: async function (kid) { + let result = [] + return result + }, + +} diff --git a/artisan.sh b/artisan.sh index bafda55..9523f22 100644 --- a/artisan.sh +++ b/artisan.sh @@ -223,8 +223,8 @@ const table = 'mist_${apiName}' module.exports = { add: async function (data) { - let res = await ModelBase.execInsert(table, data) - return res; + let res = ModelBase.execInsert(table, data) + return await res; }, list: async function (kid) { @@ -253,7 +253,7 @@ module.exports = { }, edit: async function (data, where, notWhere = {}) { - let result = await ModelBase.execUpdate(table, data, where, notWhere) + let result = ModelBase.execUpdate(table, data, where, notWhere) return await result }