manager.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. 'use strict';
  2. //基类服务
  3. const BaseService = require('./base.js');
  4. /**
  5. * [exports 后台管理经理基类服务]
  6. * @type {[type]}
  7. */
  8. module.exports = class ManagerService extends BaseService {
  9. /*==================================session相关=====================================*/
  10. /**
  11. * [ActionAdminUserId 当前后台操作的管理员ID]
  12. * @author szjcomo
  13. * @date 2020-11-02
  14. */
  15. ActionAdminUserId() {
  16. let that = this;
  17. let token = that.ctx.request.header.authorization;
  18. let user = that.app.jwt.verify(token,that.app.config.jwt.secret);
  19. return user.admin_id;
  20. }
  21. /**
  22. * [ActionAdminUser 获取当前管理员]
  23. * @author szjcomo
  24. * @date 2021-02-27
  25. */
  26. ActionAdminUser() {
  27. let that = this;
  28. let token = that.ctx.request.header.authorization;
  29. let user = that.app.jwt.verify(token,that.app.config.jwt.secret);
  30. return user;
  31. }
  32. /**
  33. * [getRoleAuth 获取角色权限]
  34. * @author szjcomo
  35. * @date 2020-11-02
  36. * @param {[type]} role_id [description]
  37. * @param {Boolean} is_checked [description]
  38. * @param {Number} mobile [是否手机端访问]
  39. * @return {[type]} [description]
  40. */
  41. async getRoleAuth(role_id,is_checked = false,mobile = 0) {
  42. let that = this;
  43. let seq = that.app.Sequelize;
  44. let idxs = [];
  45. let result = [];
  46. let options = {
  47. attributes:['access_id','access_name','access_icon','pid','is_nav','router_name','router_path','vuecomponent','mobile_show'],
  48. raw:true,where:{access_status:1},order:[['access_sort','asc'],['access_id','asc']]
  49. };
  50. if(mobile) options.where.mobile_show = 1;
  51. let access = await that.app.model.Accesss.findAll(options);
  52. if(role_id > 0) {
  53. let tmpOptions = {where:{role_id:role_id},attributes:['access_id'],raw:true};
  54. let tmpAccess = await that.app.model.AccessRole.findOne(tmpOptions);
  55. if(tmpAccess) idxs = (tmpAccess.access_id.split(','));
  56. } else {
  57. result = access;
  58. }
  59. if(result.length == 0) {
  60. access.forEach(item => {
  61. if(that.app.szjcomo.inArray(idxs,`${item.access_id}`)) {
  62. item.checked = true;
  63. } else {
  64. item.checked = false;
  65. }
  66. if(is_checked == false) {
  67. result.push(item);
  68. } else {
  69. if(item.checked) result.push(item);
  70. }
  71. })
  72. }
  73. let finalResult = that.app.szjcomo.arrayRecursion(result,0,'pid','access_id','child');
  74. return finalResult;
  75. }
  76. /**
  77. * [getLevel 获取分类节点等级]
  78. * @author szjcomo
  79. * @date 2020-10-26
  80. * @param {Number} pid [description]
  81. * @param {[type]} actionModel [description]
  82. * @param {Object} options [description]
  83. * @return {[type]} [description]
  84. */
  85. async getLevel(pid = 0,actionModel = null,options = {}) {
  86. if(pid == 0) return 1;
  87. let result = await actionModel.findOne(options);
  88. return (result.level + 1);
  89. }
  90. /*==================================临时方法=====================================*/
  91. /**
  92. * [console 控制台数据]
  93. * @author szjcomo
  94. * @date 2020-11-02
  95. * @return {[type]} [description]
  96. */
  97. async console() {
  98. let that = this;
  99. let cacheKey = `${process.env.APP_CUSTOME}_console_data`;
  100. let result = await that.service.redis.get(cacheKey);
  101. if(!result) {
  102. result = {
  103. /**
  104. * [quick_data 快捷处理]
  105. * @type {Array}
  106. */
  107. quick_data:[
  108. {icon:'',title:'控制台',router_name:'system_dashboard'},
  109. {icon:'',title:'管理员列表',router_name:'admin_user'},
  110. {icon:'',title:'订单列表',router_name:'select_orders'},
  111. {icon:'',title:'商品列表',router_name:'select_products'},
  112. {icon:'',title:'配置设置',router_name:'config_setting'},
  113. {icon:'',title:'用户列表',router_name:'select_users'},
  114. {icon:'',title:'支付配置',router_name:'select_pays_config'},
  115. {icon:'',title:'商品分类',router_name:'select_product_category'}
  116. ],
  117. baseData:await that.service.order.orderDataCount(),
  118. saleData:await that.service.order.saleDataCount()
  119. };
  120. await that.service.redis.set(cacheKey,result,10*60);
  121. }
  122. return result;
  123. }
  124. /**
  125. * [adminConfig 后台管理配置]
  126. * @author szjcomo
  127. * @date 2021-08-08
  128. * @return {[type]} [description]
  129. */
  130. async adminConfig(){
  131. let that = this;
  132. let configs = await that.app.model.Configs.findAll({
  133. where:{field_index:{[that.app.Sequelize.Op.in]:['website_title','files_upload_uri','image_ocr_uri','editor_templates_uri']}},
  134. attributes:['field_value','field_index'],raw:true
  135. });
  136. let result = {};
  137. configs.forEach(item => {
  138. result[item.field_index] = item.field_value;
  139. })
  140. return result;
  141. }
  142. }