mock平台

controller.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. const baseController = require('controllers/base.js');
  2. const wikiModel = require('./wikiModel.js');
  3. const projectModel = require('models/project.js');
  4. const userModel = require('models/user.js');
  5. const jsondiffpatch = require('jsondiffpatch');
  6. const formattersHtml = jsondiffpatch.formatters.html;
  7. const yapi = require('yapi.js');
  8. // const util = require('./util.js');
  9. const fs = require('fs-extra');
  10. const path = require('path');
  11. const showDiffMsg = require('../../common/diff-view.js');
  12. class wikiController extends baseController {
  13. constructor(ctx) {
  14. super(ctx);
  15. this.Model = yapi.getInst(wikiModel);
  16. this.projectModel = yapi.getInst(projectModel);
  17. }
  18. /**
  19. * 获取wiki信息
  20. * @interface wiki_desc/get
  21. * @method get
  22. * @category statistics
  23. * @foldnumber 10
  24. * @returns {Object}
  25. */
  26. async getWikiDesc(ctx) {
  27. try {
  28. let project_id = ctx.request.query.project_id;
  29. if (!project_id) {
  30. return (ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空'));
  31. }
  32. let result = await this.Model.get(project_id);
  33. return (ctx.body = yapi.commons.resReturn(result));
  34. } catch (err) {
  35. ctx.body = yapi.commons.resReturn(null, 400, err.message);
  36. }
  37. }
  38. /**
  39. * 保存wiki信息
  40. * @interface wiki_desc/get
  41. * @method get
  42. * @category statistics
  43. * @foldnumber 10
  44. * @returns {Object}
  45. */
  46. async uplodaWikiDesc(ctx) {
  47. try {
  48. let params = ctx.request.body;
  49. params = yapi.commons.handleParams(params, {
  50. project_id: 'number',
  51. desc: 'string',
  52. markdown: 'string'
  53. });
  54. if (!params.project_id) {
  55. return (ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空'));
  56. }
  57. if (!this.$tokenAuth) {
  58. let auth = await this.checkAuth(params.project_id, 'project', 'edit');
  59. if (!auth) {
  60. return (ctx.body = yapi.commons.resReturn(null, 400, '没有权限'));
  61. }
  62. }
  63. let notice = params.email_notice;
  64. delete params.email_notice;
  65. const username = this.getUsername();
  66. const uid = this.getUid();
  67. // 如果当前数据库里面没有数据
  68. let result = await this.Model.get(params.project_id);
  69. if (!result) {
  70. let data = Object.assign(params, {
  71. username,
  72. uid,
  73. add_time: yapi.commons.time(),
  74. up_time: yapi.commons.time()
  75. });
  76. let res = await this.Model.save(data);
  77. ctx.body = yapi.commons.resReturn(res);
  78. } else {
  79. let data = Object.assign(params, {
  80. username,
  81. uid,
  82. up_time: yapi.commons.time()
  83. });
  84. let upRes = await this.Model.up(result._id, data);
  85. ctx.body = yapi.commons.resReturn(upRes);
  86. }
  87. let logData = {
  88. type: 'wiki',
  89. project_id: params.project_id,
  90. current: params.desc,
  91. old: result ? result.toObject().desc : ''
  92. };
  93. let wikiUrl = `${ctx.request.origin}/project/${params.project_id}/wiki`;
  94. if (notice) {
  95. let diffView = showDiffMsg(jsondiffpatch, formattersHtml, logData);
  96. let annotatedCss = fs.readFileSync(
  97. path.resolve(
  98. yapi.WEBROOT,
  99. 'node_modules/jsondiffpatch/dist/formatters-styles/annotated.css'
  100. ),
  101. 'utf8'
  102. );
  103. let htmlCss = fs.readFileSync(
  104. path.resolve(yapi.WEBROOT, 'node_modules/jsondiffpatch/dist/formatters-styles/html.css'),
  105. 'utf8'
  106. );
  107. let project = await this.projectModel.getBaseInfo(params.project_id);
  108. yapi.commons.sendNotice(params.project_id, {
  109. title: `${username} 更新了wiki说明`,
  110. content: `<html>
  111. <head>
  112. <meta charset="utf-8" />
  113. <style>
  114. ${annotatedCss}
  115. ${htmlCss}
  116. </style>
  117. </head>
  118. <body>
  119. <div><h3>${username}更新了wiki说明</h3>
  120. <p>修改用户: ${username}</p>
  121. <p>修改项目: <a href="${wikiUrl}">${project.name}</a></p>
  122. <p>详细改动日志: ${this.diffHTML(diffView)}</p></div>
  123. </body>
  124. </html>`
  125. });
  126. }
  127. // 保存修改日志信息
  128. yapi.commons.saveLog({
  129. content: `<a href="/user/profile/${uid}">${username}</a> 更新了 <a href="${wikiUrl}">wiki</a> 的信息`,
  130. type: 'project',
  131. uid,
  132. username: username,
  133. typeid: params.project_id,
  134. data: logData
  135. });
  136. return 1;
  137. } catch (err) {
  138. ctx.body = yapi.commons.resReturn(null, 400, err.message);
  139. }
  140. }
  141. diffHTML(html) {
  142. if (html.length === 0) {
  143. return `<span style="color: #555">没有改动,该操作未改动wiki数据</span>`;
  144. }
  145. return html.map(item => {
  146. return `<div>
  147. <h4 class="title">${item.title}</h4>
  148. <div>${item.content}</div>
  149. </div>`;
  150. });
  151. }
  152. // 处理编辑冲突
  153. async wikiConflict(ctx) {
  154. try {
  155. let result;
  156. ctx.websocket.on('message', async message => {
  157. let id = parseInt(ctx.query.id, 10);
  158. if (!id) {
  159. return ctx.websocket.send('id 参数有误');
  160. }
  161. result = await this.Model.get(id);
  162. let data = await this.websocketMsgMap(message, result);
  163. if (data) {
  164. ctx.websocket.send(JSON.stringify(data));
  165. }
  166. });
  167. ctx.websocket.on('close', async () => {});
  168. } catch (err) {
  169. yapi.commons.log(err, 'error');
  170. }
  171. }
  172. websocketMsgMap(msg, result) {
  173. const map = {
  174. start: this.startFunc.bind(this),
  175. end: this.endFunc.bind(this),
  176. editor: this.editorFunc.bind(this)
  177. };
  178. return map[msg](result);
  179. }
  180. // socket 开始链接
  181. async startFunc(result) {
  182. if (result && result.edit_uid === this.getUid()) {
  183. await this.Model.upEditUid(result._id, 0);
  184. }
  185. }
  186. // socket 结束链接
  187. async endFunc(result) {
  188. if (result) {
  189. await this.Model.upEditUid(result._id, 0);
  190. }
  191. }
  192. // 正在编辑
  193. async editorFunc(result) {
  194. let userInst, userinfo, data;
  195. if (result && result.edit_uid !== 0 && result.edit_uid !== this.getUid()) {
  196. userInst = yapi.getInst(userModel);
  197. userinfo = await userInst.findById(result.edit_uid);
  198. data = {
  199. errno: result.edit_uid,
  200. data: { uid: result.edit_uid, username: userinfo.username }
  201. };
  202. } else {
  203. if (result) {
  204. await this.Model.upEditUid(result._id, this.getUid());
  205. }
  206. data = {
  207. errno: 0,
  208. data: result
  209. };
  210. }
  211. return data;
  212. }
  213. }
  214. module.exports = wikiController;