app.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. 'use strict';
  2. const _ = require('lodash');
  3. const colors = require('colors');
  4. class AppBootHook {
  5. constructor(app) {
  6. this.app = app;
  7. }
  8. async willReady() {
  9. // await this.initAdmin();
  10. // await this.initMenu();
  11. }
  12. async serverDidReady() {
  13. // 应用已经启动完毕
  14. const ctx = await this.app.createAnonymousContext();
  15. ctx.service.util.install.init();
  16. // await this.initAdmin(ctx);
  17. // await this.initMenu(ctx);
  18. // await this.initRole(ctx);
  19. // 检查种子
  20. // await ctx.service.install.index();
  21. // await ctx.service.util.rabbitMq.mission();
  22. }
  23. async initRole(ctx) {
  24. console.log('开始=>初始化角色'.blue);
  25. const data = await ctx.model.Role.findOne();
  26. if (!data) {
  27. // 没有管理员,初始化一个
  28. const data = {
  29. default: true,
  30. _tenant: 'master',
  31. name: '管理员角色',
  32. };
  33. console.log('正在初始化角色'.blue);
  34. await ctx.model.Role.create(data);
  35. console.log('初始化角色=>结束'.green);
  36. } else console.log('无需再次初始化角色'.yellow);
  37. }
  38. async initMenu(ctx) {
  39. console.log('开始=>初始化总管理员菜单'.blue);
  40. const data = await ctx.model.Menu.count();
  41. if (data <= 0) {
  42. console.log('正在初始化总管理员菜单'.blue);
  43. console.log(colors.bgBlue.italic('菜单=>初始化 首页'));
  44. // 首页初始化
  45. const indexPage = {
  46. name: '首页',
  47. icon: 'icon-shouye',
  48. order_num: 1,
  49. path: '/admin/homeIndex',
  50. status: '0',
  51. type: '1',
  52. no_delete: true,
  53. _tenant: 'master',
  54. };
  55. await ctx.model.Menu.create(indexPage);
  56. console.log(colors.bgGreen.italic('菜单=>初始化 首页'));
  57. console.log(colors.bgBlue.italic('菜单=>初始化 系统菜单'));
  58. // 系统菜单初始化
  59. const sys = {
  60. name: '系统设置',
  61. icon: 'icon-shouye',
  62. order_num: 2,
  63. type: '0',
  64. status: '0',
  65. no_delete: true,
  66. _tenant: 'master',
  67. };
  68. const sysData = await ctx.model.Menu.create(sys);
  69. let sys_id = _.get(sysData, '_id');
  70. if (sys_id) sys_id = JSON.parse(JSON.stringify(sys_id));
  71. console.log(colors.bgBlue.italic('菜单=>系统菜单=>初始化 菜单管理'));
  72. // 系统-菜单管理
  73. const sys_menu = {
  74. name: '菜单设置',
  75. icon: 'icon-shouye',
  76. order_num: 1,
  77. parent_id: sys_id,
  78. path: '/admin/menu',
  79. status: '0',
  80. type: '1',
  81. no_delete: true,
  82. _tenant: 'master',
  83. };
  84. await ctx.model.Menu.create(sys_menu);
  85. console.log(colors.bgGreen.italic('菜单=>系统菜单=>初始化 菜单管理'));
  86. console.log(colors.bgBlue.italic('菜单=>系统菜单=>初始化 角色管理'));
  87. // 系统-角色
  88. const sys_role = {
  89. name: '角色管理',
  90. icon: 'icon-shouye',
  91. order_num: 2,
  92. parent_id: sys_id,
  93. path: '/admin/role',
  94. status: '0',
  95. type: '1',
  96. no_delete: true,
  97. _tenant: 'master',
  98. };
  99. const sr = await ctx.model.Menu.create(sys_role);
  100. let role_id = _.get(sr, '_id');
  101. if (role_id) role_id = JSON.parse(JSON.stringify(role_id));
  102. const sys_role_child = {
  103. status: '0',
  104. icon: 'icon-shouye',
  105. name: '角色信息',
  106. parent_id: role_id,
  107. path: '/admin/role/detail',
  108. type: '2',
  109. config: {
  110. api: [],
  111. table_btn: [],
  112. btn_area: [],
  113. },
  114. no_delete: true,
  115. _tenant: 'master',
  116. };
  117. await ctx.model.Menu.create(sys_role_child);
  118. console.log(colors.bgGreen.italic('菜单=>系统菜单=>初始化 角色管理'));
  119. console.log(colors.bgBlue.italic('菜单=>系统菜单=>初始化 分站管理'));
  120. // 系统-分站
  121. const sys_tenant = {
  122. status: '0',
  123. icon: 'icon-shouye',
  124. no_delete: true,
  125. _tenant: 'master',
  126. page_type: 'list',
  127. name: '分站管理',
  128. parent_id: sys_id,
  129. order_num: 3,
  130. path: '/list/v1/tenant',
  131. type: '1',
  132. config: {
  133. api: [
  134. {
  135. is_use: true,
  136. module: 'tenant',
  137. func: 'query',
  138. opera: 'search',
  139. desc: '查询列表',
  140. },
  141. {
  142. is_use: true,
  143. module: 'tenant',
  144. func: 'delete',
  145. opera: 'delete',
  146. desc: '删除',
  147. },
  148. ],
  149. table_btn: [
  150. {
  151. confirm: false,
  152. label: '修改',
  153. type: 'primary',
  154. api: 'writeBySelf',
  155. selfFunction: '(i)=>this.$router.push({name:"v1_detail",params:{service:this.service},query:{id:i._id}})',
  156. },
  157. {
  158. confirm: false,
  159. label: '删除',
  160. type: 'danger',
  161. api: 'delete',
  162. },
  163. ],
  164. btn_area: [
  165. {
  166. label: '添加',
  167. type: 'primary',
  168. api: 'writeBySelf',
  169. selfFunction: '(i)=>this.$router.push({name:"v1_detail",params:{service:this.service}})',
  170. confirm: false,
  171. },
  172. ],
  173. },
  174. };
  175. const st = await ctx.model.Menu.create(sys_tenant);
  176. let tenant_id = _.get(st, '_id');
  177. if (tenant_id) tenant_id = JSON.parse(JSON.stringify(tenant_id));
  178. const sys_tenant_child = {
  179. status: '0',
  180. icon: 'icon-shouye',
  181. no_delete: true,
  182. _tenant: 'master',
  183. page_type: 'detail',
  184. name: '分站编辑',
  185. parent_id: tenant_id,
  186. path: '/detail/v1/tenant',
  187. type: '2',
  188. config: {
  189. api: [
  190. {
  191. is_use: true,
  192. module: 'tenant',
  193. func: 'fetch',
  194. opera: 'search',
  195. desc: '查询',
  196. },
  197. {
  198. is_use: true,
  199. module: 'tenant',
  200. func: 'create',
  201. opera: 'create',
  202. desc: '创建',
  203. },
  204. {
  205. is_use: true,
  206. module: 'tenant',
  207. func: 'update',
  208. opera: 'update',
  209. desc: '修改',
  210. },
  211. ],
  212. table_btn: [],
  213. btn_area: [],
  214. },
  215. };
  216. await ctx.model.Menu.create(sys_tenant_child);
  217. console.log(colors.bgGreen.italic('菜单=>系统菜单=>初始化 分站管理'));
  218. console.log(colors.bgBlue.italic('菜单=>系统菜单=>初始化 管理员管理'));
  219. // 系统-管理员
  220. const sys_admin = {
  221. status: '0',
  222. icon: 'icon-shouye',
  223. no_delete: true,
  224. _tenant: 'master',
  225. name: '管理员用户',
  226. parent_id: sys_id,
  227. order_num: 4,
  228. path: '/list/v1/admin',
  229. type: '1',
  230. __v: 0,
  231. config: {
  232. api: [
  233. {
  234. is_use: true,
  235. module: 'admin',
  236. func: 'query',
  237. opera: 'search',
  238. desc: '查询列表',
  239. },
  240. {
  241. is_use: true,
  242. module: 'admin',
  243. func: 'create',
  244. opera: 'create',
  245. desc: '创建',
  246. },
  247. {
  248. is_use: true,
  249. module: 'admin',
  250. func: 'update',
  251. opera: 'update',
  252. desc: '修改',
  253. },
  254. {
  255. is_use: true,
  256. module: 'admin',
  257. func: 'delete',
  258. opera: 'toDelete',
  259. desc: '删除',
  260. },
  261. {
  262. is_use: true,
  263. module: 'admin',
  264. func: 'fetch',
  265. opera: 'toFetch',
  266. desc: '单项数据查询',
  267. },
  268. ],
  269. table_btn: [
  270. {
  271. confirm: false,
  272. label: '修改',
  273. type: 'primary',
  274. api: 'writeBySelf',
  275. methodZh: '',
  276. display: '',
  277. selfFunction: '(i)=>this.$router.push({name:"v1_detail", query:{id:i._id},params:{service:this.service}})',
  278. },
  279. {
  280. confirm: true,
  281. label: '删除',
  282. type: 'danger',
  283. api: 'toDelete',
  284. display: '(i)=>i.is_super !== true',
  285. methodZh: '确认是否删除该用户',
  286. },
  287. {
  288. confirm: false,
  289. label: '查看',
  290. type: 'primary',
  291. api: 'writeBySelf',
  292. selfFunction: '(i)=>this.$router.push({name:"v1_info" , query:{id:i._id},params:{service:this.service}})',
  293. },
  294. ],
  295. btn_area: [
  296. {
  297. label: '添加',
  298. type: 'primary',
  299. api: 'writeBySelf',
  300. selfFunction: '(i)=>this.$router.push({name:"v1_detail",params:{service:this.service}})',
  301. confirm: false,
  302. },
  303. ],
  304. },
  305. page_type: 'list',
  306. };
  307. const sa = await ctx.model.Menu.create(sys_admin);
  308. let admin_id = _.get(sa, '_id');
  309. if (admin_id) admin_id = JSON.parse(JSON.stringify(admin_id));
  310. const sys_admin_child = {
  311. status: '0',
  312. icon: 'icon-shouye',
  313. no_delete: true,
  314. _tenant: 'master',
  315. name: '管理员信息',
  316. parent_id: admin_id,
  317. path: '/detail/v1/admin',
  318. type: '2',
  319. config: {
  320. api: [
  321. {
  322. is_use: true,
  323. module: 'admin',
  324. func: 'fetch',
  325. opera: 'search',
  326. desc: '单数据查询',
  327. },
  328. {
  329. is_use: true,
  330. module: 'admin',
  331. func: 'create',
  332. opera: 'create',
  333. desc: '创建',
  334. },
  335. {
  336. is_use: true,
  337. module: 'admin',
  338. func: 'update',
  339. opera: 'update',
  340. desc: '修改',
  341. },
  342. ],
  343. table_btn: [],
  344. btn_area: [],
  345. },
  346. page_type: 'detail',
  347. };
  348. await ctx.model.Menu.create(sys_admin_child);
  349. console.log(colors.bgGreen.italic('菜单=>系统菜单=>初始化 管理员管理'));
  350. console.log(colors.bgGreen.italic('菜单=>初始化 系统菜单'));
  351. console.log('初始化总管理员菜单结束'.green);
  352. } else console.log('无需初始化总管理员菜单'.yellow);
  353. }
  354. }
  355. module.exports = AppBootHook;