manager.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. const that = this;
  17. const token = that.ctx.request.header.authorization;
  18. const 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. const that = this;
  28. const token = that.ctx.request.header.authorization;
  29. const 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. const that = this;
  43. // const seq = that.app.Sequelize;
  44. let idxs = [];
  45. let result = [];
  46. const 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. const access = await that.app.model.Accesss.findAll(options);
  52. if (role_id > 0) {
  53. const tmpOptions = { where: { role_id }, attributes: [ 'access_id' ], raw: true };
  54. const 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. const 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. const 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. const that = this;
  99. const 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. loginData: await that.userLoginDataCount(),
  120. };
  121. await that.service.redis.set(cacheKey, result, 10 * 60);
  122. }
  123. return result;
  124. }
  125. /**
  126. * [adminConfig 后台管理配置]
  127. * @author szjcomo
  128. * @date 2021-08-08
  129. * @return {[type]} [description]
  130. */
  131. async adminConfig() {
  132. const that = this;
  133. const configs = await that.app.model.Configs.findAll({
  134. where: { field_index: { [that.app.Sequelize.Op.in]: [ 'website_title', 'files_upload_uri', 'image_ocr_uri', 'editor_templates_uri' ] } },
  135. attributes: [ 'field_value', 'field_index' ], raw: true,
  136. });
  137. const result = {};
  138. configs.forEach(item => {
  139. result[item.field_index] = item.field_value;
  140. });
  141. return result;
  142. }
  143. /**
  144. * [userLoginDataCount 最近每日用户登录数量统计]
  145. * @return {[type]} [description]
  146. */
  147. async userLoginDataCount(day = 20) {
  148. const that = this;
  149. const curTime = that.app.szjcomo.time();
  150. const seq = that.app.Sequelize;
  151. const times = [];
  152. for (let i = 0; i < day; i++) {
  153. const tmpTime = curTime - i * 24 * 60 * 60;
  154. times.push([ `${that.app.szjcomo.date('Y-m-d', tmpTime)} 00:00:00`, `${that.app.szjcomo.date('Y-m-d', tmpTime)} 23:59:59` ]);
  155. }
  156. const users = await that.app.model.UserLoginLogs.findAll({
  157. where: {
  158. update_ttime: { [seq.Op.between]: [ times[day - 1][0], times[0][1] ] },
  159. }, raw: true,
  160. });
  161. const luckyMoney = await that.app.model.UsersMoneyLogs.findAll({
  162. where: {
  163. change_time: { [seq.Op.between]: [ times[day - 1][0], times[0][1] ] },
  164. type: 4,
  165. }, raw: true,
  166. });
  167. const result = [];
  168. times.forEach(item => {
  169. const startTime = that.app.szjcomo.strToTime(item[0]);
  170. const endTime = that.app.szjcomo.strToTime(item[1]);
  171. const tmpData = { date: that.app.szjcomo.date('m-d', startTime), count: 0, luckyCount: 0 };
  172. users.forEach(childItem => {
  173. const tmpTime = that.app.szjcomo.strToTime(childItem.update_ttime);
  174. if (tmpTime >= startTime && tmpTime <= endTime) {
  175. tmpData.count += 1;
  176. }
  177. });
  178. luckyMoney.forEach(childItem => {
  179. const tmpTime = that.app.szjcomo.strToTime(childItem.change_time);
  180. if (tmpTime >= startTime && tmpTime <= endTime) {
  181. tmpData.luckyCount += 1;
  182. }
  183. });
  184. result.push(tmpData);
  185. });
  186. return result;
  187. }
  188. };