util.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const moment = require('moment');
  4. const _ = require('lodash');
  5. const fs = require('fs');
  6. // 工具
  7. class UtilService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx);
  10. this.model = this.ctx.model.Card; // 卡
  11. this.place = this.ctx.model.Place;// 行政区划(原生数据)
  12. this.xzqh = this.ctx.model.Xzqh;// 行政区划
  13. this.set = this.ctx.model.Set;// 套餐
  14. }
  15. async utilMethod(query, body) {
  16. console.log(await this.app.redis.get('5ff575b0a3f2771eb4d03114'));
  17. // this.initSetSeed();
  18. // console.log('in function:');
  19. // const file = await this.ctx.curl('https://vd3.bdstatic.com/mda-kar9n2ip1u508ewq/hd/mda-kar9n2ip1u508ewq.mp4');
  20. // console.log(file.data);
  21. // return file.data;
  22. // const ms = fs.create
  23. }
  24. async initSetSeed() {
  25. let xzqh = await this.xzqh.find({ pcode: { $exists: false } });
  26. xzqh = JSON.parse(JSON.stringify((xzqh)));
  27. const set169 = code => ({ title: '169套餐', has_group: true, contact: code });
  28. const set129 = code => ({ title: '129套餐', has_group: false, contact: code });
  29. const setList = [];
  30. const dirList = [ '110000', '120000', '310000', '500000', '150000', '450000', '640000', '650000', '540000', '810000', '820000' ];
  31. for (const i of xzqh) {
  32. const { code } = i;
  33. if (code !== '110000') {
  34. if (dirList.includes(code)) {
  35. setList.push(set169(code));
  36. setList.push(set129(code));
  37. } else {
  38. const citys = await this.xzqh.find({ pcode: code });
  39. for (const c of citys) {
  40. if (c.code !== '220100') {
  41. setList.push(set169(c.code));
  42. setList.push(set129(c.code));
  43. }
  44. }
  45. }
  46. }
  47. }
  48. this.set.insertMany(setList);
  49. }
  50. // 设置card表的初始数据
  51. async initCardSeed() {
  52. this.seed();
  53. }
  54. async seed() {
  55. // b梯队
  56. const b = [];
  57. for (let i = 1; i <= 5; i++) {
  58. const mobile = i < 10 ? `2222222220${i}` : `222222222${i}`;
  59. const data = {
  60. mobile,
  61. password: '111111',
  62. province: '220000',
  63. city: '220100',
  64. set: '169',
  65. name: `B梯队${i}`,
  66. id_card: '22010319950601161x',
  67. recommend: '刘睿峰',
  68. r_mobile: '13089419810',
  69. };
  70. b.push(data);
  71. await this.ctx.service.card.create(data);
  72. }
  73. // c梯队
  74. const c = [];
  75. let num = 1;
  76. for (const binfo of b) {
  77. const { name, mobile } = binfo;
  78. for (let i = num; i < num + 5; i++) {
  79. const m = i < 10 ? `3333333330${i}` : `333333333${i}`;
  80. const data = {
  81. mobile: m,
  82. password: '111111',
  83. province: '220000',
  84. city: '220100',
  85. set: '169',
  86. id_card: '22010319950601161x',
  87. name: `C梯队${i}`,
  88. recommend: name,
  89. r_mobile: mobile,
  90. };
  91. c.push(data);
  92. await this.ctx.service.card.create(data);
  93. }
  94. num = num + 5;
  95. }
  96. }
  97. // 整理查询条件,重写query时可以选择使用,框架方法改了下
  98. queryReset(filter, options = {}) {
  99. let { sort, desc } = options;
  100. if (sort && _.isString(sort)) {
  101. sort = { [sort]: desc ? -1 : 1 };
  102. } else if (sort && _.isArray(sort)) {
  103. sort = sort.map(f => ({ [f]: desc ? -1 : 1 }))
  104. .reduce((p, c) => ({ ...p, ...c }), {});
  105. }
  106. options.sort = sort;
  107. // 模糊查询
  108. filter = this.turnFilter(filter);
  109. // 日期范围查询
  110. filter = this.turnDateRangeQuery(filter);
  111. return { filter, options };
  112. }
  113. turnFilter(filter) {
  114. const str = /^%\w*%$/;
  115. const keys = Object.keys(filter);
  116. for (const key of keys) {
  117. const res = key.match(str);
  118. if (res) {
  119. const newKey = key.slice(1, key.length - 1);
  120. filter[newKey] = new RegExp(filter[key]);
  121. delete filter[key];
  122. }
  123. }
  124. return filter;
  125. }
  126. /**
  127. * 将时间转换成对应查询Object
  128. * @param {Object} filter 查询条件
  129. */
  130. turnDateRangeQuery(filter) {
  131. const keys = Object.keys(filter);
  132. for (const k of keys) {
  133. if (k.includes('@')) {
  134. const karr = k.split('@');
  135. // 因为是针对处理范围日期,所以必须只有,开始时间和结束时间
  136. if (karr.length === 2) {
  137. const type = karr[1];
  138. if (type === 'start') {
  139. filter[karr[0]] = {
  140. ..._.get(filter, karr[0], {}),
  141. $gte: filter[k],
  142. };
  143. } else {
  144. filter[karr[0]] = {
  145. ..._.get(filter, karr[0], {}),
  146. $lte: filter[k],
  147. };
  148. }
  149. delete filter[k];
  150. }
  151. }
  152. }
  153. return filter;
  154. }
  155. /**
  156. * 整理行政区划
  157. */
  158. resetXzqh() {
  159. // let res = await this.place.find();
  160. // res = JSON.parse(JSON.stringify(res));
  161. // const province = res.filter(f => f.code.endsWith('0000'));
  162. // let cList = [];
  163. // for (const p of province) {
  164. // const { code } = p;
  165. // const prefix = code.substr(0, 2);
  166. // let city = res.filter(f => f.code.startsWith(prefix) && !f.code.endsWith('0000') && f.code.endsWith('00'));
  167. // city = city.map(i => ({ ...i, pcode: code }));
  168. // cList = cList.concat(city);
  169. // }
  170. // await this.xzqh.insertMany([ ...province, ...cList ]);
  171. // return province;
  172. }
  173. }
  174. module.exports = UtilService;