.roadhogrc.mock.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. import mockjs from 'mockjs';
  2. import { format, delay } from 'roadhog-api-doc';
  3. import config from './src/utils/config';
  4. import Logger from './src/utils/logger';
  5. import { imageDatabase, videoDatabase } from './mock/resource';
  6. import { tagList, wareList, lessonList, courseList, supportList } from './mock/product';
  7. import { itemList } from './mock/item';
  8. const logger = Logger.getLogger('RoadhogMock');
  9. // 是否禁用代理
  10. const noProxy = process.env.NO_PROXY === 'true';
  11. // 测试API
  12. const OFFLINE_API_URL = 'http://192.168.199.119:8100';
  13. // 代码中会兼容本地 service mock 以及部署站点的静态数据
  14. const proxy = {
  15. [`GET ${config.api.resourceList}`]: (req, res) => {
  16. let params = req.query;
  17. if (!params || !params.type) {
  18. res.send({
  19. code: '401',
  20. message: '请求参数不完整!'
  21. });
  22. }
  23. else {
  24. let dataset = [];
  25. if (parseInt(params.type) === config.RESOURCE_TYPE_IMAGE) {
  26. dataset = imageDatabase;
  27. }
  28. else if (parseInt(params.type) === config.RESOURCE_TYPE_VIDEO) {
  29. dataset = videoDatabase;
  30. }
  31. let type = params.type;
  32. let pageSize = params.pageSize ? parseInt(params.pageSize) : 10;
  33. let pageNo = params.pageNo ? parseInt(params.pageNo) : 1;
  34. let totalSize = dataset.length;
  35. let totalNo = Math.floor(dataset.length / pageSize) + 1;
  36. let start = pageNo * pageSize;
  37. let list = dataset.slice(start, start + pageSize);
  38. res.send({
  39. code: '200',
  40. message: 'Request success!',
  41. data: { pageNo, pageSize, totalSize, list }
  42. });
  43. }
  44. },
  45. [`GET ${config.api.wareList}`]: (req, res) => {
  46. let params = req.query;
  47. let dataset = wareList;
  48. logger.info('【WareList Request】: ', params);
  49. let pageSize = params.pageSize ? parseInt(params.pageSize) : 10;
  50. let pageNo = params.pageNo ? parseInt(params.pageNo) : 1;
  51. let totalSize = dataset.length;
  52. let start = pageNo * pageSize;
  53. let list = dataset.slice(start, start + pageSize);
  54. res.send({
  55. code: '200',
  56. message: 'Request success!',
  57. data: { pageNo, pageSize, totalSize, list }
  58. });
  59. },
  60. [`GET ${config.api.lessonList}`]: (req, res) => {
  61. let params = req.query;
  62. let dataset = lessonList;
  63. logger.info('【LessonList Request】: ', params);
  64. let pageSize = params.pageSize ? parseInt(params.pageSize) : 10;
  65. let pageNo = params.pageNo ? parseInt(params.pageNo) : 1;
  66. let totalSize = dataset.length;
  67. let start = pageNo * pageSize;
  68. let list = dataset.slice(start, start + pageSize);
  69. res.send({
  70. code: '200',
  71. message: 'Request success!',
  72. data: { pageNo, pageSize, totalSize, list }
  73. });
  74. },
  75. [`GET ${config.api.lessonItem}`]: (req, res) => {
  76. logger.info('【LessonItem】:', req.params);
  77. res.send({
  78. "code": 200,
  79. "success": true,
  80. "message": null,
  81. "data": {
  82. "id": 1511504034666794,
  83. "code": "K-test-310",
  84. "name": "小学310",
  85. "digest": "霍冬冬是个大傻蛋,这是一句很中肯的话,不要狡辩!",
  86. "sort": null,
  87. "status": "NORMAL",
  88. "gmtCreated": null,
  89. "gmtModified": null,
  90. "wareList": [{
  91. "id": 1511425213814978,
  92. "code": "J-test-310",
  93. "name": "小学310",
  94. "digest": null,
  95. "type": "SINGLE",
  96. "playUrl": null,
  97. "imgUrls": [
  98. "http://www.xxx.c/img.jpg",
  99. ],
  100. "sort": null,
  101. "status": "NORMAL",
  102. "gmtCreated": 1511264969000,
  103. "gmtModified": 1511264969000
  104. }]
  105. }
  106. });
  107. },
  108. [`POST ${config.api.lessonItemAdd}`]: (req, res) => {
  109. logger.info('【LessonItemAdd】:', req.body);
  110. res.send({
  111. code: 200,
  112. message: '新建课成功',
  113. data: {},
  114. });
  115. },
  116. [`PUT ${config.api.lessonItemUpdate}`]: (req, res) => {
  117. logger.info('【LessonItemUpdate】:', req.body);
  118. res.send({
  119. code: 200,
  120. message: '更新课成功',
  121. data: {},
  122. });
  123. },
  124. [`DELETE ${config.api.lessonItemDel}`]: (req, res) => {
  125. logger.info('【LessonItemDelete】:', req.body);
  126. res.send({
  127. code: 200,
  128. message: '删除课成功',
  129. data: {},
  130. });
  131. },
  132. [`GET ${config.api.courseList}`]: (req, res) => {
  133. let params = req.query;
  134. let dataset = courseList;
  135. logger.info('【CourseList Request】: ', params);
  136. let pageSize = params.pageSize ? parseInt(params.pageSize) : 10;
  137. let pageNo = params.pageNo ? parseInt(params.pageNo) : 1;
  138. let totalSize = dataset.length;
  139. let start = pageNo * pageSize;
  140. let list = dataset.slice(start, start + pageSize);
  141. res.send({
  142. code: '200',
  143. message: 'Request success!',
  144. data: { pageNo, pageSize, totalSize, list }
  145. });
  146. },
  147. [`GET ${config.api.courseItem}`]: (req, res) => {
  148. logger.info('【CourseItem】:', req.params);
  149. res.send({
  150. "code": 200,
  151. "success": true,
  152. "message": null,
  153. "data": {
  154. "id": 1511767193919357,
  155. "code": "C-test-311",
  156. "name": "小学311",
  157. "title": null,
  158. "digest": null,
  159. "detail": null,
  160. "keyword": null,
  161. "cvImgIds": null,
  162. "bgImgIds": null,
  163. "status": null,
  164. "gmtCreated": null,
  165. "gmtModified": null,
  166. "subItemList": [{
  167. "id": 1511509291305739,
  168. "code": "K-test-311",
  169. "name": "小学310",
  170. "digest": null,
  171. "type": "LESSON",
  172. "status": null,
  173. "gmtCreated": null,
  174. "gmtModified": null
  175. }],
  176. "supportList": [{
  177. "id": 1511835822190182,
  178. "code": "S-test-310",
  179. "name": "小学310",
  180. "title": null,
  181. "digest": null,
  182. "detail": null,
  183. "imgIds": null,
  184. "status": null,
  185. "gmtCreated": null,
  186. "gmtModified": null
  187. }]
  188. }
  189. })
  190. },
  191. [`POST ${config.api.courseItemAdd}`]: (req, res) => {
  192. logger.info('【CourseItemAdd】:', req.body);
  193. res.send({
  194. code: 200,
  195. message: '新建课程成功',
  196. data: {},
  197. });
  198. },
  199. [`PUT ${config.api.courseItemUpdate}`]: (req, res) => {
  200. logger.info('【CourseItemUpdate】:', req.body);
  201. res.send({
  202. code: 200,
  203. message: '更新课程成功',
  204. data: {},
  205. });
  206. },
  207. [`DELETE ${config.api.courseItemDel}`]: (req, res) => {
  208. logger.info('【CourseItemDelete】:', req.body);
  209. res.send({
  210. code: 200,
  211. message: '删除课程成功',
  212. data: {},
  213. });
  214. },
  215. [`GET ${config.api.supportList}`]: (req, res) => {
  216. let params = req.query;
  217. let dataset = supportList;
  218. logger.info('【SupportList Request】: ', params);
  219. let pageSize = params.pageSize ? parseInt(params.pageSize) : 10;
  220. let pageNo = params.pageNo ? parseInt(params.pageNo) : 1;
  221. let totalSize = dataset.length;
  222. let start = pageNo * pageSize;
  223. let list = dataset.slice(start, start + pageSize);
  224. res.send({
  225. code: '200',
  226. message: 'Request success!',
  227. data: { pageNo, pageSize, totalSize, list }
  228. });
  229. },
  230. [`GET ${config.api.supportItem}`]: (req, res) => {
  231. logger.info('【SupportItem】:', req.params);
  232. res.send({
  233. "code": 200,
  234. "success": true,
  235. "message": null,
  236. "data": {
  237. "id": 1,
  238. "code": "S-test-310",
  239. "name": "先学急用的汉字1",
  240. "digest": "周边描述周边描述周边描述周边描述周边描述周边描述周边描述周边描述周边描述",
  241. "tags": [{tagId: 15, tagName:'EQ情商教育-15'}, {tagId:17, tagName:'EQ情商教育-2'}],
  242. "status": "NORMAL",
  243. "cpId": 1,
  244. "cpName": "贝尔安亲",
  245. "gmtCreated": null,
  246. "gmtModified": null,
  247. "playUrl": 'http://www.baidu.com',
  248. "imgUrls": ['http://www.baidu.com'],
  249. "supportArr": [{
  250. "id": 2,
  251. "code": "S-test-311",
  252. "name": "先学急用的汉字2",
  253. "digest": "周边描述周边描述周边描述周边描述周边描述周边描述周边描述周边描述周边描述",
  254. "tags": [{15: 'EQ情商教育-15'}, {17: 'EQ情商教育-17'}],
  255. "status": "NORMAL",
  256. "cpId": 1,
  257. "cpName": "贝尔安亲",
  258. "playUrl": 'http://www.baidu.com',
  259. "imgUrls": ['http://www.baidu.com'],
  260. "gmtCreated": null,
  261. "gmtModified": null,
  262. }]
  263. }
  264. });
  265. },
  266. [`POST ${config.api.supportItemAdd}`]: (req, res) => {
  267. logger.info('【SupportItemAdd】:', req.body);
  268. res.send({
  269. code: 200,
  270. message: '新建周边成功',
  271. data: {},
  272. });
  273. },
  274. [`PUT ${config.api.supportItemUpdate}`]: (req, res) => {
  275. logger.info('【SupportItemUpdate】:', req.body);
  276. res.send({
  277. code: 200,
  278. message: '更新周边成功',
  279. data: {},
  280. });
  281. },
  282. [`DELETE ${config.api.supportItemDel}`]: (req, res) => {
  283. logger.info('【SupportItemDelete】:', req.body);
  284. res.send({
  285. code: 200,
  286. message: '删除周边成功',
  287. data: {},
  288. });
  289. },
  290. [`GET ${config.api.itemList}`]: (req, res) => {
  291. let params = req.query;
  292. let dataset = itemList;
  293. logger.info('【ItemList Request】: ', params);
  294. let pageSize = params.pageSize ? parseInt(params.pageSize) : 10;
  295. let pageNo = params.pageNo ? parseInt(params.pageNo) : 1;
  296. let totalSize = dataset.length;
  297. let start = pageNo * pageSize;
  298. let list = dataset.slice(start, start + pageSize);
  299. res.send({
  300. code: '200',
  301. message: 'Request success!',
  302. data: { pageNo, pageSize, totalSize, list }
  303. });
  304. },
  305. [`GET ${config.api.itemItem}`]: (req, res) => {
  306. logger.info('【ItemItem Request】: ', req.params);
  307. res.send({
  308. code: 200,
  309. message: null,
  310. data: { ...itemList[0] },
  311. });
  312. },
  313. [`POST ${config.api.itemItemAdd}`]: (req, res) => {
  314. logger.info('【ItemItemAdd】:', req.body);
  315. res.send({
  316. code: 200,
  317. message: '新建商品成功',
  318. data: {},
  319. });
  320. },
  321. [`PUT ${config.api.itemItemUpdate}`]: (req, res) => {
  322. logger.info('【ItemItemUpdate】:', req.body);
  323. res.send({
  324. code: 200,
  325. message: '更新商品信息成功',
  326. data: {},
  327. });
  328. },
  329. [`DELETE ${config.api.itemItemDel}`]: (req, res) => {
  330. logger.info('【ItemItemDelete】:', req.body);
  331. res.send({
  332. code: 200,
  333. message: '删除商品成功',
  334. data: {},
  335. });
  336. },
  337. [`POST ${config.api.wareItemAdd}`]: (req, res) => {
  338. logger.info('【WareItemAdd】:', req.body);
  339. res.send({
  340. code: 200,
  341. message: '新建课件成功',
  342. data: {},
  343. });
  344. },
  345. [`PUT ${config.api.wareItemUpdate}`]: (req, res) => {
  346. logger.info('【WareItemUpdate】:', req.body);
  347. res.send({
  348. code: 200,
  349. message: '更新课件成功',
  350. data: {},
  351. });
  352. },
  353. [`DELETE ${config.api.wareItemDel}`]: (req, res) => {
  354. logger.info('【WareItemDelete】:', req.body);
  355. res.send({
  356. code: 200,
  357. message: '删除课件成功',
  358. data: {},
  359. });
  360. },
  361. [`GET ${config.api.tagList}`]: (req, res) => {
  362. let params = req.query;
  363. let dataset = tagList;
  364. logger.info('【TagList Request】: ', params);
  365. let pageSize = params.pageSize ? parseInt(params.pageSize) : 10;
  366. let pageNo = params.pageNo ? parseInt(params.pageNo) : 1;
  367. let totalSize = dataset.length;
  368. let start = pageNo * pageSize;
  369. let list = dataset.slice(start, start + pageSize);
  370. res.send({
  371. code: '200',
  372. message: 'Request success!',
  373. data: { pageNo, pageSize, totalSize, list }
  374. });
  375. },
  376. [`GET ${config.api.tagItem}`]: (req, res) => {
  377. logger.info('【TagItem】:', req.params);
  378. res.send({
  379. "code": 200,
  380. "success": true,
  381. "message": null,
  382. "data": {
  383. "tagId": 1511767193919357,
  384. "tagName": "EQ情商教育",
  385. "status": null,
  386. "type": 0,
  387. "gmtCreated": null,
  388. "gmtModified": null,
  389. "merchantId": 0,
  390. "merchantName": "贝尔安亲",
  391. }
  392. })
  393. },
  394. [`POST ${config.api.tagItemAdd}`]: (req, res) => {
  395. logger.info('【tagItemAdd】:', req.body);
  396. res.send({
  397. code: 200,
  398. message: '新建标签成功',
  399. data: {},
  400. });
  401. },
  402. [`PUT ${config.api.tagItemUpdate}`]: (req, res) => {
  403. logger.info('【TagItemUpdate】:', req.body);
  404. res.send({
  405. code: 200,
  406. message: '更新标签成功',
  407. data: {},
  408. });
  409. },
  410. [`DELETE ${config.api.tagItemDel}`]: (req, res) => {
  411. logger.info('【TagItemDelete】:', req.body);
  412. res.send({
  413. code: 200,
  414. message: '删除标签成功',
  415. data: {},
  416. });
  417. },
  418. [`GET ${config.api.merchantList}`]: {
  419. code: 200,
  420. message: 'Request success',
  421. success: true,
  422. data: {
  423. list:[
  424. {
  425. merchantId: 0,
  426. merchantName: '贝尔安亲',
  427. },{
  428. merchantId: 1,
  429. merchantName: '昂乐教育',
  430. },{
  431. merchantId: 2,
  432. merchantName: '好托管',
  433. },{
  434. merchantId: 3,
  435. merchantName: '红黄蓝',
  436. },{
  437. merchantId: 4,
  438. merchantName: '英乐教育',
  439. }
  440. ]
  441. }
  442. },
  443. [`GET ${config.api.cpList}`]: {
  444. code: 200,
  445. message: 'Request success',
  446. success: true,
  447. data: {
  448. list:[
  449. {
  450. cpId: 0,
  451. cpName: '贝尔安亲',
  452. },{
  453. cpId: 1,
  454. cpName: '昂乐教育',
  455. },{
  456. cpId: 2,
  457. cpName: '鲨鱼公园',
  458. }
  459. ]
  460. }
  461. },
  462. [`GET /api/v1/oss/upload`]: {
  463. "code": 200,
  464. "success": true,
  465. "message": null,
  466. "data": {
  467. "accessid": "LTAIUFvd17IXLBQ4",
  468. "policy": "eyJleHBpcmF0aW9uIjoiMjAxNy0xMS0yMlQwMzoyNTo0Mi41NjFaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCJyZXNvdXJjZXMiXV19",
  469. "signature": "YxqziyjItL6F4F0OzfxaBOTg6Ok=",
  470. "expire": "1511321142",
  471. "dir": "resources",
  472. "path": "a/b/001.png",
  473. // "host": "http://efunimgs.oss-cn-beijing-internal.aliyuncs.com"
  474. "host": "http://localhost:8000/oss/upload"
  475. }
  476. },
  477. [`GET /oss/upload`]: {
  478. "code": 200,
  479. "success": true,
  480. "message": null,
  481. "data": {
  482. "fileName": "a-b-c.png",
  483. "path": "user-dir",
  484. "size": 80,
  485. }
  486. }
  487. };
  488. export default noProxy ? {} : delay(proxy, 1000);
  489. // export default {
  490. // [`GET ${config.apiPrefix}/(.*)`]: `${OFFLINE_API_URL}`,
  491. // }