pages.controller.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. const pageService = require('../service/pages.service');
  2. const pagesRoleService = require('../service/pagesRole.service');
  3. const userService = require('../service/user.service');
  4. const templateService = require('../service/templates.service');
  5. const request = require('../utils/request');
  6. const util = require('../utils/util');
  7. const { MODEL_BASE_URL, ENABLE_MODEL_SERVICE } = require('../config');
  8. module.exports = {
  9. async list(ctx) {
  10. const { userId } = util.decodeToken(ctx);
  11. const { pageNum, pageSize, keyword, projectId } = ctx.request.query;
  12. const { total } = await pageService.listCount(keyword, userId, Number(projectId));
  13. if (total == 0) {
  14. return util.success(ctx, {
  15. list: [],
  16. total: 0,
  17. pageSize: +pageSize || 12,
  18. pageNum: +pageNum || 1,
  19. });
  20. }
  21. const list = await pageService.list(pageNum || 1, pageSize || 12, keyword, userId, Number(projectId));
  22. util.success(ctx, {
  23. list,
  24. total,
  25. pageSize: +pageSize,
  26. pageNum: +pageNum,
  27. });
  28. },
  29. // mars-admin 项目列表
  30. async getPageList(ctx) {
  31. const { pageNum = 1, pageSize = 10, isTemplate, userId } = ctx.request.query;
  32. const template = util.isNumber(isTemplate) ? Number(isTemplate) : '';
  33. const uid = util.isNumber(userId) ? Number(userId) : '';
  34. const { total } = await pageService.getPageCount(template, uid);
  35. if (total == 0) {
  36. return util.success(ctx, {
  37. list: [],
  38. total: 0,
  39. pageSize: +pageSize,
  40. pageNum: +pageNum,
  41. });
  42. }
  43. const list = await pageService.getPageList(pageNum, pageSize, template, uid);
  44. util.success(ctx, {
  45. list,
  46. total,
  47. pageSize: +pageSize,
  48. pageNum: +pageNum,
  49. });
  50. },
  51. async listPageTemplate(ctx) {
  52. const { pageNum, pageSize, keyword } = ctx.request.query;
  53. const { total } = await pageService.listPageTemplateCount(keyword);
  54. if (total == 0) {
  55. return util.success(ctx, {
  56. list: [],
  57. total: 0,
  58. pageSize: +pageSize || 12,
  59. pageNum: +pageNum || 1,
  60. });
  61. }
  62. const list = await pageService.listPageTemplate(pageNum || 1, pageSize || 12, keyword);
  63. util.success(ctx, {
  64. list,
  65. total,
  66. pageSize: +pageSize,
  67. pageNum: +pageNum,
  68. });
  69. },
  70. async detail(ctx) {
  71. const { id } = ctx.request.params;
  72. if (!util.isNumber(id)) {
  73. return ctx.throw(400, '页面ID不能为空');
  74. }
  75. const { userId } = util.decodeToken(ctx);
  76. // 查询页面信息
  77. const [pageInfo] = await pageService.getPageInfoById(+id);
  78. if (!pageInfo) {
  79. return util.fail(ctx, '当前页面不存在', 404);
  80. }
  81. // 查询页面开发者
  82. const list = await pagesRoleService.getPagesRoleList([id, pageInfo.projectId].join(','));
  83. // 如果不是创建者,又不是开发者,无法访问
  84. if (pageInfo.userId !== userId && list.filter((item) => item.userId === userId && item.role === 1).length === 0) {
  85. return util.fail(ctx, '您当前暂无查看权限', 403);
  86. }
  87. util.success(ctx, pageInfo || {});
  88. },
  89. async copy(ctx) {
  90. const { userId, userName } = util.decodeToken(ctx);
  91. const { id, name, remark, projectId } = ctx.request.body;
  92. if (!util.isNumber(id)) {
  93. return ctx.throw(400, '页面ID不能为空');
  94. }
  95. if (!name) {
  96. return ctx.throw(400, '页面名称不能为空');
  97. }
  98. if (!util.isNumber(projectId)) {
  99. return ctx.throw(400, '项目ID不能为空');
  100. }
  101. const [pageInfo] = await pageService.getPageInfoById(+id);
  102. if (!pageInfo) {
  103. return util.fail(ctx, '页面不存在', 404);
  104. }
  105. const { pageData, appType } = pageInfo;
  106. await pageService.createPage(name, userId, userName, remark, pageData, projectId, appType);
  107. util.success(ctx);
  108. },
  109. async delete(ctx) {
  110. const { id } = ctx.request.body;
  111. if (!util.isNumber(id)) {
  112. return ctx.throw(400, '页面ID不能为空');
  113. }
  114. const { userId } = util.decodeToken(ctx);
  115. const [pageInfo] = await pageService.getPageSimpleById(+id);
  116. if (!pageInfo || pageInfo.userId !== userId) {
  117. return util.fail(ctx, '您暂无权限删除该页面');
  118. }
  119. const res = await pageService.deletePage(id, userId);
  120. await pagesRoleService.deleteByPageId(id);
  121. if (res.affectedRows > 0) {
  122. util.success(ctx);
  123. } else {
  124. return ctx.throw(400, '当前暂无权限', 403);
  125. }
  126. },
  127. async create(ctx) {
  128. const { userId, userName } = util.decodeToken(ctx);
  129. const { name, remark, projectId, id, appType = 1 } = ctx.request.body;
  130. if (!name) {
  131. return ctx.throw(400, '页面名称不能为空');
  132. }
  133. if (!util.isNumber(projectId)) {
  134. return ctx.throw(400, '项目ID不能为空');
  135. }
  136. if (id) {
  137. if (!util.isNumber(id)) {
  138. return ctx.throw(400, '模板ID格式异常');
  139. }
  140. const page = await pageService.getPageByTemplateId(id);
  141. if (!page) {
  142. return ctx.throw(400, '模板不存在', 404);
  143. }
  144. // 开启模型服务后,需要复制模型
  145. if (ENABLE_MODEL_SERVICE) {
  146. // 获取项目模型列表
  147. const res = await request.post(
  148. `${MODEL_BASE_URL}/api/model/model/copy`,
  149. JSON.stringify({
  150. oldProjectId: String(page.projectId),
  151. newProjectId: String(projectId),
  152. }),
  153. {
  154. headers: {
  155. authorization: ctx.request.headers?.authorization,
  156. },
  157. },
  158. );
  159. if (res.status !== 200) {
  160. return ctx.throw(400, '模型创建失败');
  161. }
  162. const result = JSON.parse(res.data);
  163. const dictMap = result.oldNewDataDictionaryIdMap;
  164. page.pageData &&
  165. Object.keys(dictMap).forEach((key) => {
  166. page.pageData = page.pageData.replaceAll(`"dataDictionaryKey":"${key}"`, `"dataDictionaryKey":"${dictMap[key]}"`);
  167. });
  168. }
  169. await pageService.createPage(name, userId, userName, remark, page.pageData, projectId, appType);
  170. await templateService.updateInstallCount(id);
  171. util.success(ctx);
  172. } else {
  173. await pageService.createPage(name, userId, userName, remark, '', projectId, appType);
  174. util.success(ctx);
  175. }
  176. },
  177. async update(ctx) {
  178. const { id, name, remark = '', pageData, previewImg = '', projectId, isPublic } = ctx.request.body;
  179. if (!util.isNotEmpty(id)) {
  180. return ctx.throw(400, '页面ID不能为空');
  181. }
  182. const { userId } = util.decodeToken(ctx);
  183. const [pageInfo] = await pageService.getPageSimpleById(+id);
  184. if (!pageInfo) {
  185. return util.fail(ctx, '当前页面不存在', 404);
  186. }
  187. // 判断开发者权限
  188. const list = await pagesRoleService.getPagesRoleList([id, pageInfo.projectId].join(','));
  189. if (pageInfo.userId !== userId && list.filter((item) => item.userId === userId).length === 0) {
  190. return util.fail(ctx, '您当前暂无编辑权限', 403);
  191. }
  192. await pageService.updatePageInfo(id, name, remark, pageData, previewImg, projectId, isPublic);
  193. util.success(ctx);
  194. },
  195. // 页面角色 - 成员列表
  196. async roleList(ctx) {
  197. const { pageId } = ctx.request.body;
  198. if (!pageId) {
  199. return ctx.throw(400, '页面ID不能为空');
  200. }
  201. const list = await pagesRoleService.getPagesRoleList(pageId);
  202. util.success(ctx, { list });
  203. },
  204. /**
  205. * 页面或者项目 - 添加成员
  206. * page_id: 页面ID或者项目ID,共用同一张表
  207. */
  208. async roleAdd(ctx) {
  209. const { type, pageId, role, userName } = ctx.request.body;
  210. if (!type) {
  211. return ctx.throw(400, '成员类型不能为空');
  212. }
  213. if (!pageId || isNaN(+pageId)) {
  214. return ctx.throw(400, '页面ID或项目ID不能为空');
  215. }
  216. if (!role) {
  217. return ctx.throw(400, '角色不能为空');
  218. }
  219. if (!userName) {
  220. return ctx.throw(400, '开发者ID或名称不能为空');
  221. }
  222. const res = await userService.search(userName);
  223. if (!res) {
  224. return ctx.throw(400, '当前用户不存在');
  225. }
  226. const { userId: createdUId, userName: createdUName } = util.decodeToken(ctx);
  227. await pagesRoleService.create(type, pageId, role, res.id, userName, createdUId, createdUName);
  228. util.success(ctx);
  229. },
  230. // 删除页面成员
  231. async roleDelete(ctx) {
  232. const { id } = ctx.request.body;
  233. if (!util.isNumber(id)) {
  234. return ctx.throw(400, 'ID不能为空');
  235. }
  236. await pagesRoleService.delete(id);
  237. util.success(ctx);
  238. },
  239. // 页面回滚
  240. async rollback(ctx) {
  241. const { pageId, lastPublishId, env } = ctx.request.body;
  242. if (!util.isNotEmpty(pageId)) {
  243. return ctx.throw(400, '页面ID不能为空');
  244. }
  245. if (!util.isNotEmpty(lastPublishId)) {
  246. return ctx.throw(400, '回滚ID不能为空');
  247. }
  248. if (!util.checkEnv(env)) {
  249. return ctx.throw(400, '环境不能为空');
  250. }
  251. await pageService.updateLastPublishId(pageId, lastPublishId, env);
  252. util.success(ctx);
  253. },
  254. };