lib.controller.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. const { v4 } = require('uuid');
  2. const libService = require('../service/lib.service');
  3. const util = require('../utils/util');
  4. const { md5Encry } = require('../utils/sign');
  5. const config = require('../config');
  6. const imgcloud = require('../service/imgcloud.service');
  7. module.exports = {
  8. async list(ctx) {
  9. const { userId } = util.decodeToken(ctx);
  10. const { pageNum = 1, pageSize = 10, keyword = '', type = 1 } = ctx.request.query;
  11. const { total } = await libService.listCount(keyword, type, userId);
  12. if (total == 0) {
  13. return util.success(ctx, {
  14. list: [],
  15. total: 0,
  16. pageSize: +pageSize || 10,
  17. pageNum: +pageNum || 1,
  18. });
  19. }
  20. const list = await libService.list(pageNum, pageSize, keyword, type, userId);
  21. util.success(ctx, {
  22. list,
  23. total,
  24. pageSize: +pageSize,
  25. pageNum: +pageNum,
  26. });
  27. },
  28. async installList(ctx) {
  29. const { userId } = util.decodeToken(ctx);
  30. const list = await libService.installList(userId);
  31. util.success(ctx, list);
  32. },
  33. async detail(ctx) {
  34. const { id } = ctx.request.params;
  35. if (!util.isNotEmpty(id)) {
  36. return ctx.throw(400, '组件id不能为空');
  37. }
  38. const { userId } = util.decodeToken(ctx);
  39. const [result = {}] = await libService.getDetailById(+id, userId);
  40. util.success(ctx, result);
  41. },
  42. async create(ctx) {
  43. const { tag, name, description = '' } = ctx.request.body;
  44. const { userId, userName } = util.decodeToken(ctx);
  45. if (!userId || !userName) {
  46. return ctx.throw(400, '账号信息异常,请重新登录');
  47. }
  48. if (!tag) {
  49. return ctx.throw(400, '组件标识不能为空');
  50. }
  51. if (/^[a-zA-Z]+$/g.test(tag) === false) {
  52. return ctx.throw(400, '组件标识只支持英文');
  53. }
  54. if (!name) {
  55. return ctx.throw(400, '组件名称不能为空');
  56. }
  57. await libService.createLib('MC' + tag, name, description, userId, userName);
  58. util.success(ctx);
  59. },
  60. async delete(ctx) {
  61. const { id } = ctx.request.params;
  62. if (!util.isNumber(id)) {
  63. return ctx.throw(400, '组件id不正确');
  64. }
  65. const { userId } = util.decodeToken(ctx);
  66. const res = await libService.deleteLibById(id, userId);
  67. if (res.affectedRows > 0) {
  68. await libService.deletePublishById(id, userId);
  69. util.success(ctx);
  70. } else {
  71. ctx.throw(400, '当前暂无权限');
  72. }
  73. },
  74. async update(ctx) {
  75. const { id, tag, name, description = '' } = ctx.request.body;
  76. if (!util.isNumber(id)) {
  77. return ctx.throw(400, '组件id不正确');
  78. }
  79. if (!tag || !name) {
  80. return ctx.throw(400, '参数异常');
  81. }
  82. const { userId } = util.decodeToken(ctx);
  83. const lib = libService.getOwnLibById(id, userId);
  84. if (!lib) return ctx.throw(400, '暂无权限修改');
  85. await libService.updateLib(tag, name, description, id);
  86. util.success(ctx);
  87. },
  88. async save(ctx) {
  89. const { id, reactCode, lessCode, configCode, mdCode, hash } = ctx.request.body;
  90. if (!util.isNumber(id)) {
  91. return ctx.throw(400, '组件id不正确');
  92. }
  93. if (!reactCode) {
  94. return ctx.throw(400, '源码不能为空');
  95. }
  96. if (!configCode) {
  97. return ctx.throw(400, '组件配置不能为空');
  98. }
  99. const { userId } = util.decodeToken(ctx);
  100. const lib = libService.getOwnLibById(id, userId);
  101. if (!lib) return ctx.throw(400, '暂无权限保存');
  102. await libService.saveLib({
  103. reactCode,
  104. lessCode,
  105. configCode,
  106. mdCode,
  107. hash,
  108. id,
  109. });
  110. util.success(ctx);
  111. },
  112. async publish(ctx) {
  113. const { libId, reactCompile, configCode, cssCompile, releaseHash } = ctx.request.body;
  114. if (!util.isNumber(libId)) {
  115. return ctx.throw(400, '组件id不正确');
  116. }
  117. if (!reactCompile) {
  118. return ctx.throw(400, 'react代码不能为空');
  119. }
  120. if (!configCode) {
  121. return ctx.throw(400, '组件配置不能为空');
  122. }
  123. if (!releaseHash) {
  124. return ctx.throw(400, '缺少hash参数');
  125. }
  126. const { userId, userName } = util.decodeToken(ctx);
  127. const detail = await libService.getPublishByLibId(libId);
  128. const jsName = md5Encry(userId + reactCompile + Date.now()) + '.js';
  129. const cssName = md5Encry(userId + cssCompile + Date.now()) + '.css';
  130. const configName = md5Encry(userId + configCode + Date.now()) + '.js';
  131. await util.uploadString(userId, jsName, reactCompile);
  132. await util.uploadString(userId, cssName, cssCompile);
  133. await util.uploadString(userId, configName, configCode);
  134. let prefix = config.OSS_CDNDOMAIN;
  135. if (config.OSS_TYPE === 'minio') {
  136. prefix = `${config.OSS_USESSL ? 'https://' : 'http://'}${config.OSS_ENDPOINT}:${config.OSS_PORT}/${config.OSS_BUCKET}`;
  137. } else if (config.OSS_TYPE === 'baidu') {
  138. if (config.OSS_CDNDOMAIN) {
  139. prefix = config.OSS_CDNDOMAIN;
  140. } else {
  141. prefix = config.OSS_ENDPOINT;
  142. }
  143. } else {
  144. if (config.OSS_CDNDOMAIN) {
  145. prefix = config.OSS_CDNDOMAIN;
  146. } else {
  147. prefix = `https://${config.OSS_BUCKET}.${config.OSS_REGION}.aliyuncs.com`;
  148. }
  149. }
  150. const reactUrl = `${prefix}/libs/${userId}/${jsName}`;
  151. const cssUrl = `${prefix}/libs/${userId}/${cssName}`;
  152. const configUrl = `${prefix}/libs/${userId}/${configName}`;
  153. if (detail) {
  154. if (detail && detail.releaseHash === releaseHash) {
  155. return ctx.throw(400, '当前已经是最新版本');
  156. }
  157. await libService.updateLibPublish({
  158. libId,
  159. reactUrl,
  160. cssUrl,
  161. configUrl,
  162. releaseHash,
  163. });
  164. } else {
  165. const id = v4();
  166. await libService.publish({
  167. libId,
  168. releaseId: id,
  169. reactUrl,
  170. cssUrl,
  171. configUrl,
  172. releaseHash,
  173. userId,
  174. userName,
  175. });
  176. }
  177. await imgcloud.create(userId, userName, jsName, jsName, 'application/javascript', Buffer.byteLength(reactCompile, 'utf8'), reactUrl);
  178. await imgcloud.create(userId, userName, cssName, cssName, 'text/css', Buffer.byteLength(cssCompile, 'utf8'), cssUrl);
  179. await imgcloud.create(userId, userName, configName, configName, 'application/javascript', Buffer.byteLength(configCode, 'utf8'), configUrl);
  180. util.success(ctx);
  181. },
  182. };