import mockjs from 'mockjs'; import { format, delay } from 'roadhog-api-doc'; import config from './src/utils/config'; import Logger from './src/utils/logger'; import { imageDatabase, videoDatabase } from './mock/resource'; import { tagList, wareList, lessonList, courseList, supportList } from './mock/product'; import { itemList } from './mock/item'; const logger = Logger.getLogger('RoadhogMock'); // 是否禁用代理 const noProxy = process.env.NO_PROXY === 'true'; // 测试API const OFFLINE_API_URL = 'http://192.168.199.119:8100'; // 代码中会兼容本地 service mock 以及部署站点的静态数据 const proxy = { [`GET ${config.api.resourceList}`]: (req, res) => { let params = req.query; if (!params || !params.type) { res.send({ code: '401', message: '请求参数不完整!' }); } else { let dataset = []; if (parseInt(params.type) === config.RESOURCE_TYPE_IMAGE) { dataset = imageDatabase; } else if (parseInt(params.type) === config.RESOURCE_TYPE_VIDEO) { dataset = videoDatabase; } let type = params.type; let pageSize = params.pageSize ? parseInt(params.pageSize) : 10; let pageNo = params.pageNo ? parseInt(params.pageNo) : 1; let totalSize = dataset.length; let totalNo = Math.floor(dataset.length / pageSize) + 1; let start = pageNo * pageSize; let list = dataset.slice(start, start + pageSize); res.send({ code: '200', message: 'Request success!', data: { pageNo, pageSize, totalSize, list } }); } }, [`GET ${config.api.wareList}`]: (req, res) => { let params = req.query; let dataset = wareList; logger.info('【WareList Request】: ', params); let pageSize = params.pageSize ? parseInt(params.pageSize) : 10; let pageNo = params.pageNo ? parseInt(params.pageNo) : 1; let totalSize = dataset.length; let start = pageNo * pageSize; let list = dataset.slice(start, start + pageSize); res.send({ code: '200', message: 'Request success!', data: { pageNo, pageSize, totalSize, list } }); }, [`GET ${config.api.lessonList}`]: (req, res) => { let params = req.query; let dataset = lessonList; logger.info('【LessonList Request】: ', params); let pageSize = params.pageSize ? parseInt(params.pageSize) : 10; let pageNo = params.pageNo ? parseInt(params.pageNo) : 1; let totalSize = dataset.length; let start = pageNo * pageSize; let list = dataset.slice(start, start + pageSize); res.send({ code: '200', message: 'Request success!', data: { pageNo, pageSize, totalSize, list } }); }, [`GET ${config.api.lessonItem}`]: (req, res) => { logger.info('【LessonItem】:', req.params); res.send({ "code": 200, "success": true, "message": null, "data": { "id": 1511504034666794, "code": "K-test-310", "name": "小学310", "digest": "霍冬冬是个大傻蛋,这是一句很中肯的话,不要狡辩!", "sort": null, "status": "NORMAL", "gmtCreated": null, "gmtModified": null, "wareList": [{ "id": 1511425213814978, "code": "J-test-310", "name": "小学310", "digest": null, "type": "SINGLE", "playUrl": null, "imgUrls": [ "http://www.xxx.c/img.jpg", ], "sort": null, "status": "NORMAL", "gmtCreated": 1511264969000, "gmtModified": 1511264969000 }] } }); }, [`POST ${config.api.lessonItemAdd}`]: (req, res) => { logger.info('【LessonItemAdd】:', req.body); res.send({ code: 200, message: '新建课成功', data: {}, }); }, [`PUT ${config.api.lessonItemUpdate}`]: (req, res) => { logger.info('【LessonItemUpdate】:', req.body); res.send({ code: 200, message: '更新课成功', data: {}, }); }, [`DELETE ${config.api.lessonItemDel}`]: (req, res) => { logger.info('【LessonItemDelete】:', req.body); res.send({ code: 200, message: '删除课成功', data: {}, }); }, [`GET ${config.api.courseList}`]: (req, res) => { let params = req.query; let dataset = courseList; logger.info('【CourseList Request】: ', params); let pageSize = params.pageSize ? parseInt(params.pageSize) : 10; let pageNo = params.pageNo ? parseInt(params.pageNo) : 1; let totalSize = dataset.length; let start = pageNo * pageSize; let list = dataset.slice(start, start + pageSize); res.send({ code: '200', message: 'Request success!', data: { pageNo, pageSize, totalSize, list } }); }, [`GET ${config.api.courseItem}`]: (req, res) => { logger.info('【CourseItem】:', req.params); res.send({ "code": 200, "success": true, "message": null, "data": { "id": 1511767193919357, "code": "C-test-311", "name": "小学311", "title": null, "digest": null, "detail": null, "keyword": null, "cvImgIds": null, "bgImgIds": null, "status": null, "gmtCreated": null, "gmtModified": null, "subItemList": [{ "id": 1511509291305739, "code": "K-test-311", "name": "小学310", "digest": null, "type": "LESSON", "status": null, "gmtCreated": null, "gmtModified": null }], "supportList": [{ "id": 1511835822190182, "code": "S-test-310", "name": "小学310", "title": null, "digest": null, "detail": null, "imgIds": null, "status": null, "gmtCreated": null, "gmtModified": null }] } }) }, [`POST ${config.api.courseItemAdd}`]: (req, res) => { logger.info('【CourseItemAdd】:', req.body); res.send({ code: 200, message: '新建课程成功', data: {}, }); }, [`PUT ${config.api.courseItemUpdate}`]: (req, res) => { logger.info('【CourseItemUpdate】:', req.body); res.send({ code: 200, message: '更新课程成功', data: {}, }); }, [`DELETE ${config.api.courseItemDel}`]: (req, res) => { logger.info('【CourseItemDelete】:', req.body); res.send({ code: 200, message: '删除课程成功', data: {}, }); }, [`GET ${config.api.supportList}`]: (req, res) => { let params = req.query; let dataset = supportList; logger.info('【SupportList Request】: ', params); let pageSize = params.pageSize ? parseInt(params.pageSize) : 10; let pageNo = params.pageNo ? parseInt(params.pageNo) : 1; let totalSize = dataset.length; let start = pageNo * pageSize; let list = dataset.slice(start, start + pageSize); res.send({ code: '200', message: 'Request success!', data: { pageNo, pageSize, totalSize, list } }); }, [`GET ${config.api.supportItem}`]: (req, res) => { logger.info('【SupportItem】:', req.params); res.send({ "code": 200, "success": true, "message": null, "data": { "id": 1, "code": "S-test-310", "name": "先学急用的汉字1", "digest": "周边描述周边描述周边描述周边描述周边描述周边描述周边描述周边描述周边描述", "tags": [{tagId: 15, tagName:'EQ情商教育-15'}, {tagId:17, tagName:'EQ情商教育-2'}], "status": "NORMAL", "cpId": 1, "cpName": "贝尔安亲", "gmtCreated": null, "gmtModified": null, "playUrl": 'http://www.baidu.com', "imgUrls": ['http://www.baidu.com'], "supportArr": [{ "id": 2, "code": "S-test-311", "name": "先学急用的汉字2", "digest": "周边描述周边描述周边描述周边描述周边描述周边描述周边描述周边描述周边描述", "tags": [{15: 'EQ情商教育-15'}, {17: 'EQ情商教育-17'}], "status": "NORMAL", "cpId": 1, "cpName": "贝尔安亲", "playUrl": 'http://www.baidu.com', "imgUrls": ['http://www.baidu.com'], "gmtCreated": null, "gmtModified": null, }] } }); }, [`POST ${config.api.supportItemAdd}`]: (req, res) => { logger.info('【SupportItemAdd】:', req.body); res.send({ code: 200, message: '新建周边成功', data: {}, }); }, [`PUT ${config.api.supportItemUpdate}`]: (req, res) => { logger.info('【SupportItemUpdate】:', req.body); res.send({ code: 200, message: '更新周边成功', data: {}, }); }, [`DELETE ${config.api.supportItemDel}`]: (req, res) => { logger.info('【SupportItemDelete】:', req.body); res.send({ code: 200, message: '删除周边成功', data: {}, }); }, [`GET ${config.api.itemList}`]: (req, res) => { let params = req.query; let dataset = itemList; logger.info('【ItemList Request】: ', params); let pageSize = params.pageSize ? parseInt(params.pageSize) : 10; let pageNo = params.pageNo ? parseInt(params.pageNo) : 1; let totalSize = dataset.length; let start = pageNo * pageSize; let list = dataset.slice(start, start + pageSize); res.send({ code: '200', message: 'Request success!', data: { pageNo, pageSize, totalSize, list } }); }, [`GET ${config.api.itemItem}`]: (req, res) => { logger.info('【ItemItem Request】: ', req.params); res.send({ code: 200, message: null, data: { ...itemList[0] }, }); }, [`POST ${config.api.itemItemAdd}`]: (req, res) => { logger.info('【ItemItemAdd】:', req.body); res.send({ code: 200, message: '新建商品成功', data: {}, }); }, [`PUT ${config.api.itemItemUpdate}`]: (req, res) => { logger.info('【ItemItemUpdate】:', req.body); res.send({ code: 200, message: '更新商品信息成功', data: {}, }); }, [`DELETE ${config.api.itemItemDel}`]: (req, res) => { logger.info('【ItemItemDelete】:', req.body); res.send({ code: 200, message: '删除商品成功', data: {}, }); }, [`POST ${config.api.wareItemAdd}`]: (req, res) => { logger.info('【WareItemAdd】:', req.body); res.send({ code: 200, message: '新建课件成功', data: {}, }); }, [`PUT ${config.api.wareItemUpdate}`]: (req, res) => { logger.info('【WareItemUpdate】:', req.body); res.send({ code: 200, message: '更新课件成功', data: {}, }); }, [`DELETE ${config.api.wareItemDel}`]: (req, res) => { logger.info('【WareItemDelete】:', req.body); res.send({ code: 200, message: '删除课件成功', data: {}, }); }, [`GET ${config.api.tagList}`]: (req, res) => { let params = req.query; let dataset = tagList; logger.info('【TagList Request】: ', params); let pageSize = params.pageSize ? parseInt(params.pageSize) : 10; let pageNo = params.pageNo ? parseInt(params.pageNo) : 1; let totalSize = dataset.length; let start = pageNo * pageSize; let list = dataset.slice(start, start + pageSize); res.send({ code: '200', message: 'Request success!', data: { pageNo, pageSize, totalSize, list } }); }, [`GET ${config.api.tagItem}`]: (req, res) => { logger.info('【TagItem】:', req.params); res.send({ "code": 200, "success": true, "message": null, "data": { "tagId": 1511767193919357, "tagName": "EQ情商教育", "status": null, "type": 0, "gmtCreated": null, "gmtModified": null, "merchantId": 0, "merchantName": "贝尔安亲", } }) }, [`POST ${config.api.tagItemAdd}`]: (req, res) => { logger.info('【tagItemAdd】:', req.body); res.send({ code: 200, message: '新建标签成功', data: {}, }); }, [`PUT ${config.api.tagItemUpdate}`]: (req, res) => { logger.info('【TagItemUpdate】:', req.body); res.send({ code: 200, message: '更新标签成功', data: {}, }); }, [`DELETE ${config.api.tagItemDel}`]: (req, res) => { logger.info('【TagItemDelete】:', req.body); res.send({ code: 200, message: '删除标签成功', data: {}, }); }, [`GET ${config.api.merchantList}`]: { code: 200, message: 'Request success', success: true, data: { list:[ { merchantId: 0, merchantName: '贝尔安亲', },{ merchantId: 1, merchantName: '昂乐教育', },{ merchantId: 2, merchantName: '好托管', },{ merchantId: 3, merchantName: '红黄蓝', },{ merchantId: 4, merchantName: '英乐教育', } ] } }, [`GET ${config.api.cpList}`]: { code: 200, message: 'Request success', success: true, data: { list:[ { cpId: 0, cpName: '贝尔安亲', },{ cpId: 1, cpName: '昂乐教育', },{ cpId: 2, cpName: '鲨鱼公园', } ] } }, [`GET /api/v1/oss/upload`]: { "code": 200, "success": true, "message": null, "data": { "accessid": "LTAIUFvd17IXLBQ4", "policy": "eyJleHBpcmF0aW9uIjoiMjAxNy0xMS0yMlQwMzoyNTo0Mi41NjFaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCJyZXNvdXJjZXMiXV19", "signature": "YxqziyjItL6F4F0OzfxaBOTg6Ok=", "expire": "1511321142", "dir": "resources", "path": "a/b/001.png", // "host": "http://efunimgs.oss-cn-beijing-internal.aliyuncs.com" "host": "http://localhost:8000/oss/upload" } }, [`GET /oss/upload`]: { "code": 200, "success": true, "message": null, "data": { "fileName": "a-b-c.png", "path": "user-dir", "size": 80, } } }; export default noProxy ? {} : delay(proxy, 1000); // export default { // [`GET ${config.apiPrefix}/(.*)`]: `${OFFLINE_API_URL}`, // }