mock平台

install.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. const fs = require('fs-extra');
  2. const yapi = require('./yapi.js');
  3. const commons = require('./utils/commons');
  4. const dbModule = require('./utils/db.js');
  5. const userModel = require('./models/user.js');
  6. const mongoose = require('mongoose');
  7. yapi.commons = commons;
  8. yapi.connect = dbModule.connect();
  9. function install() {
  10. let exist = yapi.commons.fileExist(yapi.path.join(yapi.WEBROOT_RUNTIME, 'init.lock'));
  11. if (exist) {
  12. throw new Error(
  13. 'init.lock文件已存在,请确认您是否已安装。如果需要重新安装,请删掉init.lock文件'
  14. );
  15. }
  16. setupSql();
  17. }
  18. function setupSql() {
  19. let userInst = yapi.getInst(userModel);
  20. let passsalt = yapi.commons.randStr();
  21. let result = userInst.save({
  22. username: yapi.WEBCONFIG.adminAccount.substr(0, yapi.WEBCONFIG.adminAccount.indexOf('@')),
  23. email: yapi.WEBCONFIG.adminAccount,
  24. password: yapi.commons.generatePassword('ymfe.org', passsalt),
  25. passsalt: passsalt,
  26. role: 'admin',
  27. add_time: yapi.commons.time(),
  28. up_time: yapi.commons.time()
  29. });
  30. yapi.connect
  31. .then(function() {
  32. let userCol = mongoose.connection.db.collection('user');
  33. userCol.createIndex({
  34. username: 1
  35. });
  36. userCol.createIndex(
  37. {
  38. email: 1
  39. },
  40. {
  41. unique: true
  42. }
  43. );
  44. let projectCol = mongoose.connection.db.collection('project');
  45. projectCol.createIndex({
  46. uid: 1
  47. });
  48. projectCol.createIndex({
  49. name: 1
  50. });
  51. projectCol.createIndex({
  52. group_id: 1
  53. });
  54. let logCol = mongoose.connection.db.collection('log');
  55. logCol.createIndex({
  56. uid: 1
  57. });
  58. logCol.createIndex({
  59. typeid: 1,
  60. type: 1
  61. });
  62. let interfaceColCol = mongoose.connection.db.collection('interface_col');
  63. interfaceColCol.createIndex({
  64. uid: 1
  65. });
  66. interfaceColCol.createIndex({
  67. project_id: 1
  68. });
  69. let interfaceCatCol = mongoose.connection.db.collection('interface_cat');
  70. interfaceCatCol.createIndex({
  71. uid: 1
  72. });
  73. interfaceCatCol.createIndex({
  74. project_id: 1
  75. });
  76. let interfaceCaseCol = mongoose.connection.db.collection('interface_case');
  77. interfaceCaseCol.createIndex({
  78. uid: 1
  79. });
  80. interfaceCaseCol.createIndex({
  81. col_id: 1
  82. });
  83. interfaceCaseCol.createIndex({
  84. project_id: 1
  85. });
  86. let interfaceCol = mongoose.connection.db.collection('interface');
  87. interfaceCol.createIndex({
  88. uid: 1
  89. });
  90. interfaceCol.createIndex({
  91. path: 1,
  92. method: 1
  93. });
  94. interfaceCol.createIndex({
  95. project_id: 1
  96. });
  97. let groupCol = mongoose.connection.db.collection('group');
  98. groupCol.createIndex({
  99. uid: 1
  100. });
  101. groupCol.createIndex({
  102. group_name: 1
  103. });
  104. let avatarCol = mongoose.connection.db.collection('avatar');
  105. avatarCol.createIndex({
  106. uid: 1
  107. });
  108. let tokenCol = mongoose.connection.db.collection('token');
  109. tokenCol.createIndex({
  110. project_id: 1
  111. });
  112. let followCol = mongoose.connection.db.collection('follow');
  113. followCol.createIndex({
  114. uid: 1
  115. });
  116. followCol.createIndex({
  117. project_id: 1
  118. });
  119. result.then(
  120. function() {
  121. fs.ensureFileSync(yapi.path.join(yapi.WEBROOT_RUNTIME, 'init.lock'));
  122. console.log(
  123. `初始化管理员账号成功,账号名:"${yapi.WEBCONFIG.adminAccount}",密码:"ymfe.org"`
  124. ); // eslint-disable-line
  125. process.exit(0);
  126. },
  127. function(err) {
  128. throw new Error(`初始化管理员账号 "${yapi.WEBCONFIG.adminAccount}" 失败, ${err.message}`); // eslint-disable-line
  129. }
  130. );
  131. })
  132. .catch(function(err) {
  133. throw new Error(err.message);
  134. });
  135. }
  136. install();