_forum.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import { MockRequest, MockStatusError } from '@delon/mock';
  2. import { deepCopy } from '@delon/util';
  3. import { Random } from 'mockjs';
  4. import { genLabel, genMp } from './utils';
  5. let id = 1;
  6. const CATEGORY = [
  7. {
  8. id: 1,
  9. title: 'General',
  10. list: [
  11. {
  12. id: 1,
  13. title: 'Getting started',
  14. threads: Random.natural(1, 20),
  15. replies: Random.natural(6, 100),
  16. last: {
  17. id: id++,
  18. mp: genMp(),
  19. title: Random.title(5, 10),
  20. user_name: Random.name(),
  21. time: '1d ago',
  22. },
  23. },
  24. {
  25. id: 2,
  26. title: 'Announcements',
  27. threads: Random.natural(1, 20),
  28. replies: Random.natural(6, 100),
  29. last: {
  30. id: id++,
  31. mp: genMp(),
  32. title: Random.title(5, 10),
  33. user_name: Random.name(),
  34. time: '1d ago',
  35. },
  36. },
  37. {
  38. id: 3,
  39. title: 'Guides',
  40. threads: Random.natural(1, 20),
  41. replies: Random.natural(6, 100),
  42. last: {
  43. id: id++,
  44. mp: genMp(),
  45. title: Random.title(5, 10),
  46. user_name: Random.name(),
  47. time: '1d ago',
  48. },
  49. },
  50. ],
  51. },
  52. {
  53. id: 2,
  54. title: 'Shopping',
  55. list: [
  56. {
  57. id: 4,
  58. title: 'Guides',
  59. threads: Random.natural(1, 20),
  60. replies: Random.natural(6, 100),
  61. last: {
  62. id: id++,
  63. mp: genMp(),
  64. title: Random.title(5, 10),
  65. user_name: Random.name(),
  66. time: '1d ago',
  67. },
  68. },
  69. {
  70. id: 5,
  71. title: 'Payments',
  72. threads: Random.natural(1, 20),
  73. replies: Random.natural(6, 100),
  74. last: {
  75. id: id++,
  76. mp: genMp(),
  77. title: Random.title(5, 10),
  78. user_name: Random.name(),
  79. time: '1d ago',
  80. },
  81. },
  82. {
  83. id: 6,
  84. title: 'Products',
  85. threads: Random.natural(1, 20),
  86. replies: Random.natural(6, 100),
  87. last: {
  88. id: id++,
  89. mp: genMp(),
  90. title: Random.title(5, 10),
  91. user_name: Random.name(),
  92. time: '1d ago',
  93. },
  94. },
  95. {
  96. id: 7,
  97. title: 'Refund',
  98. threads: Random.natural(1, 20),
  99. replies: Random.natural(6, 100),
  100. last: {
  101. id: id++,
  102. mp: genMp(),
  103. title: Random.title(5, 10),
  104. user_name: Random.name(),
  105. time: '1d ago',
  106. },
  107. },
  108. ],
  109. },
  110. {
  111. id: 3,
  112. title: 'Support',
  113. list: [
  114. {
  115. id: 8,
  116. title: 'Common questions',
  117. threads: Random.natural(1, 20),
  118. replies: Random.natural(6, 100),
  119. },
  120. {
  121. id: 9,
  122. title: 'Site issues',
  123. threads: Random.natural(1, 20),
  124. replies: Random.natural(6, 100),
  125. last: {
  126. id: id++,
  127. mp: genMp(),
  128. title: Random.title(5, 10),
  129. user_name: Random.name(),
  130. time: '1d ago',
  131. },
  132. },
  133. ],
  134. },
  135. ];
  136. const THREAD: any[] = new Array(20).fill({}).map((v, idx) => ({
  137. id: id++,
  138. title: Random.title(5, 10),
  139. replies: Random.natural(1, 1000),
  140. label: idx % 2 === 0 && Random.boolean() ? genLabel() : null,
  141. category_id: Random.natural(1, 9),
  142. last: {
  143. id: id++,
  144. mp: genMp(),
  145. title: Random.title(5, 10),
  146. user_name: Random.name(),
  147. time: '1d ago',
  148. },
  149. }));
  150. const REPLIES: any[] = new Array(20).fill({}).map((v, idx) => ({
  151. id: id++,
  152. content: Random.paragraph(),
  153. user: {
  154. name: Random.name(),
  155. mp: genMp(),
  156. posts: Random.natural(0, 1000),
  157. },
  158. time: Random.time(),
  159. like: Random.natural(0, 100),
  160. dislike: Random.natural(0, 10),
  161. }));
  162. function get(params: any): any {
  163. let ret = deepCopy(THREAD);
  164. if (params.q) {
  165. ret = ret.filter((data: any) => data.name.indexOf(params.q) > -1);
  166. }
  167. return ret;
  168. }
  169. function getIdx(itemId: number): number {
  170. itemId = +itemId;
  171. const idx = THREAD.findIndex((w) => w.id === itemId);
  172. if (idx === -1) {
  173. throw new MockStatusError(404);
  174. }
  175. return idx;
  176. }
  177. function getCate(itemId: number): any {
  178. let item: any;
  179. const category: any = deepCopy(CATEGORY).find((w: any) => {
  180. item = w.list.find((l: any) => l.id === itemId);
  181. if (item) {
  182. return true;
  183. }
  184. return false;
  185. });
  186. delete category.list;
  187. return item;
  188. }
  189. export const FORUMS = {
  190. '/forum/category': CATEGORY,
  191. '/forum/thread/:id': (req: MockRequest) => {
  192. // list
  193. const pi = +(req.queryString.pi || 1);
  194. const ps = +(req.queryString.ps || 10);
  195. const data = get(req.queryString);
  196. return {
  197. category: getCate(+req.params.id),
  198. total: data.length,
  199. list: data.slice((pi - 1) * ps, pi * ps),
  200. };
  201. },
  202. '/forum/:id': (req: MockRequest) => {
  203. const idx = getIdx(req.params.id || 0);
  204. const item = {
  205. ...THREAD[idx],
  206. time: '3 days ago',
  207. like: Random.natural(0, 100),
  208. view: Random.natural(0, 10000),
  209. user: {
  210. name: Random.name(),
  211. mp: genMp(),
  212. posts: Random.natural(0, 1000),
  213. },
  214. desc:
  215. '<p>' +
  216. new Array(Random.natural(1, 3))
  217. .fill('')
  218. .map((v) => Random.paragraph())
  219. .join('</p><p>') +
  220. '</p>',
  221. };
  222. item.category = getCate(item.category_id);
  223. return item;
  224. },
  225. '/forum/:id/replies': (req: MockRequest) => {
  226. const pi = +(req.queryString.pi || 1);
  227. const ps = +(req.queryString.ps || 10);
  228. return {
  229. total: REPLIES.length,
  230. list: REPLIES.slice((pi - 1) * ps, pi * ps),
  231. };
  232. },
  233. 'POST /forum': (req: MockRequest) => {
  234. const itemId = req.body.id || 0;
  235. if (itemId > 0) {
  236. const idx = getIdx(itemId);
  237. THREAD[idx] = { ...THREAD[idx], ...req.body };
  238. return { msg: 'ok', item: THREAD[idx] };
  239. }
  240. const item = { ...req.body, id: THREAD.sort((a, b) => b.id - a.id)[0].id + 1 };
  241. THREAD.push(item);
  242. return { msg: 'ok', item };
  243. },
  244. 'DELETE /forum/:id': (req: MockRequest) => {
  245. const idx = getIdx(req.params.id || 0);
  246. THREAD.splice(idx, 1);
  247. return { msg: 'ok' };
  248. },
  249. };