iconDraftController.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * 字体图标CURD Controller
  3. *
  4. */
  5. let responseFormat = require('../util/responseFormat');
  6. let db = require('../database');
  7. let fileUtil = require('../util/fileUtil');
  8. let incUtil = require('../util/incUtil');
  9. let IconController = require('./iconController');
  10. let iconControllerIns = new IconController();
  11. let RepoController = require('./repoController');
  12. let repoControllerIns = new RepoController();
  13. let log = require('../util/log');
  14. let pinyin = require('pinyin');
  15. class IconDraftController {
  16. /**
  17. * 获取当前用户的草稿字体图标列表
  18. *
  19. * @param {Object} ctx 请求对象
  20. * @return {void}
  21. */
  22. async getIconDraftList (ctx) {
  23. let userInfo = ctx.userInfo;
  24. let query = ctx.request.query || {};
  25. let result = await db.iconDraft.find({
  26. ownerId: userInfo.userId
  27. }, global.globalConfig.iconDraftExportFields,
  28. {
  29. limit: parseInt(query.pageSize),
  30. skip: parseInt((query.pageIndex - 1) * query.pageSize)
  31. }
  32. );
  33. query.totalCount = await db.iconDraft.count({ownerId: userInfo.userId});
  34. ctx.body = responseFormat.responseFormatList(200, '', result, query);
  35. }
  36. /**
  37. * 上传字体图标
  38. *
  39. * @param {Object} ctx 请求对象
  40. * @return {void}
  41. */
  42. async saveDraftIcon (ctx) {
  43. let userInfo = ctx.userInfo;
  44. let fileParams = (ctx.request.body || {}).files;
  45. if (!/\.svg$/.exec(fileParams.file.name)) {
  46. ctx.body = responseFormat.responseFormat(200, '请上传svg格式图片!', false);
  47. return;
  48. } else {
  49. fileParams.file.name = fileParams.file.name.replace('.svg', '')
  50. }
  51. // 获取文件内容,再删掉
  52. let fileContent = await fileUtil.readFile(fileParams.file.path, {encoding: 'utf8'});
  53. await fileUtil.deleteFile(fileParams.file.path);
  54. // 对svg进行处理,重新绘制
  55. let iconInfo = await fileUtil.formatSvgFile(fileContent);
  56. await this.saveDraftIconToDB(fileParams.file.name, iconInfo, userInfo, ctx);
  57. }
  58. async collectIcon (ctx) {
  59. let userInfo = ctx.userInfo;
  60. let params = ctx.request.body;
  61. await this.saveDraftIconToDB(params.iconName, params, userInfo, ctx);
  62. }
  63. async saveDraftIconToDB (iconName, iconInfo, userInfo, ctx) {
  64. // 获取唯一自增Id
  65. let iconId = await incUtil.getIncId({model: 'iconDraft', field: 'iconId'});
  66. // 构建完整数据
  67. let params = {
  68. iconName: iconName,
  69. ownerId: userInfo.userId,
  70. iconContent: iconInfo.iconContent,
  71. iconOriginContent: iconInfo.iconOriginContent,
  72. svgPath: iconInfo.svgPath,
  73. createTime: global.globalConfig.nowTime,
  74. updateTime: global.globalConfig.nowTime,
  75. iconId: iconId
  76. };
  77. await db.iconDraft.add(params);
  78. ctx.body = responseFormat.responseFormat(200, '', {
  79. iconContent: iconInfo.iconContent,
  80. name: iconName
  81. });
  82. }
  83. /**
  84. * 删除草稿字体图标
  85. *
  86. * @param {Object} ctx 请求对象
  87. * @return {void}
  88. */
  89. async deleteDraftIcon (ctx) {
  90. let userInfo = ctx.userInfo;
  91. let params = ctx.request.body;
  92. await db.iconDraft.delete({
  93. iconId: params.iconId,
  94. ownerId: userInfo.userId
  95. });
  96. log.debug(`[%s.deleteDraftIcon] delete iconDraft success--${params.iconId}`, this.constructor.name)
  97. ctx.body = responseFormat.responseFormat(200, '删除成功!', true);
  98. }
  99. /**
  100. * 删除草稿字体图标
  101. *
  102. * @param {Object} ctx 请求对象
  103. * @return {void}
  104. */
  105. async updateDraftIcon (ctx) {
  106. let userInfo = ctx.userInfo;
  107. let params = ctx.request.body;
  108. await db.iconDraft.update({
  109. iconId: params.iconId,
  110. ownerId: userInfo.userId
  111. }, {
  112. iconName: params.iconName
  113. });
  114. ctx.body = responseFormat.responseFormat(200, '更新成功!', true);
  115. }
  116. /**
  117. * 下载字体图标
  118. *
  119. * @param {Object} ctx 请求对象
  120. * @return {void}
  121. */
  122. async downloadIcon (ctx) {
  123. let params = ctx.params || {};
  124. let iconItem = await db.iconDraft.findOne({
  125. iconId: params.iconId
  126. });
  127. if (!iconItem) {
  128. ctx.body = responseFormat.responseFormat(200, '无此图标!', false);
  129. return;
  130. }
  131. // 强制客户端直接下载svg headers
  132. ctx.set('Content-Type', 'application/force-download');
  133. ctx.set('Content-disposition', 'attachment; filename=' + iconItem.iconName + '.svg');
  134. ctx.body = iconItem.iconContent;
  135. }
  136. /**
  137. * 提交草稿转换成正式字体图标并删除
  138. *
  139. * @param {Object} ctx 请求对象
  140. * @return {void}
  141. */
  142. async changeDraft2Icon (ctx) {
  143. let userInfo = ctx.userInfo;
  144. let iconItems = await db.iconDraft.find({
  145. ownerId: userInfo.userId
  146. });
  147. let newIcons = [];
  148. // 允许名称重复,但名称不可修改
  149. // for (let i = 0; i < iconItems.length; i++) {
  150. // let iconItem = await db.icon.findOne({
  151. // iconName: iconItems[i].iconName,
  152. // ownerId: userInfo.userId
  153. // });
  154. // if (iconItem) {
  155. // throw new Error(`${iconItems[i].iconName}图标已经存在,请修改名称!`)
  156. // }
  157. // }
  158. // 验证完成之后再保存
  159. for (let i = 0; i < iconItems.length; i++) {
  160. let iconName = this.getName(iconItems[i].iconName);
  161. if (ctx.request.body.resetColor) {
  162. let iconInfo = await fileUtil.formatSvgFile(iconItems[i].iconContent);
  163. iconItems[i].iconContent = iconInfo.iconContent;
  164. }
  165. let iconItem = await iconControllerIns.saveIcon(iconName, iconItems[i].iconContent, userInfo);
  166. newIcons.push(iconItem);
  167. }
  168. // 添加到指定图标库中
  169. if (ctx.request.body.repoId) {
  170. ctx.request.body.icons = newIcons;
  171. await repoControllerIns.addIcon2Repo(ctx);
  172. }
  173. // 转化完之后删除
  174. await db.iconDraft.delete({
  175. ownerId: userInfo.userId
  176. });
  177. ctx.body = responseFormat.responseFormat(200, '', true);
  178. }
  179. /**
  180. * 转化中文为拼音
  181. *
  182. * @param {String} name 图标名称
  183. * @return {void}
  184. */
  185. getName (name) {
  186. return pinyin(name, {
  187. style: pinyin.STYLE_NORMAL,
  188. heteronym: true
  189. }).reduce(function (sum, val) {
  190. console.log(sum, val);
  191. return sum + val[0];
  192. }, '').replace(/\s+|、|&/g, '-');
  193. }
  194. };
  195. module.exports = IconDraftController;