appUser.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. const md5 = require('md5-node');
  4. class AppUserService extends Service {
  5. async login(name, password) {
  6. let { ctx , config, app} = this;
  7. let pwdmd5 = md5(password);
  8. //验证用户是否存在
  9. let data = await ctx.model.Admin.find({name, password: pwdmd5});
  10. if( data.length < 1 ) {
  11. throw '用户名或密码错误!';
  12. }
  13. let info = data[0];
  14. const beijingTimeOffset = 28800000;
  15. let ret = await ctx.model.Admin.update({_id: info._id}, {$set:{lastLogin: Date.now()+beijingTimeOffset}});
  16. let token = await ctx.jwtSign({name:info.name, id:info._id, role:info.role}, config.loginAdmin.secret, { expiresIn: 60 * 60 });
  17. return {token, role: info.name};
  18. }
  19. async getFamily( userid ){
  20. let {app} = this;
  21. let user = await app.mysql.select("app_user_family", { where:{user_id: userid}, columns:["id","name","avatar","relation","ins_num","coverage_amt_total","birthday","career_type","income_num","loan_type"]});
  22. return user;
  23. }
  24. async addFamily( userid , data) {
  25. let { app } = this;
  26. data.user_id = userid;
  27. data.coverage_amt_total = 0;
  28. data.ins_num = 0;
  29. console.log("adding family", data);
  30. let ret = await app.mysql.insert("app_user_family", data);
  31. return ret;
  32. }
  33. async deleteFamily(id){
  34. let ret = await this.app.mysql.delete("app_user_family", {id});
  35. return ret;
  36. }
  37. async updateFamily( userid, data ) {
  38. let { app } = this;
  39. let ret = await app.mysql.update("app_user_family", data, {id: data.id});
  40. return ret;
  41. }
  42. async add ( data ) {
  43. }
  44. //个人中心返回用户基本信息
  45. async info(uid)
  46. {
  47. let mysql = this.app.mysql;
  48. let ret = await mysql.get("app_user", {id: uid}, {columns:["id","name", "tel", "sex", "avatar","wechat","address","birthday"]});
  49. return ret;
  50. }
  51. async updateInfo(uid, data) {
  52. let mysql = this.app.mysql;
  53. let ret = await mysql.update("app_user", data, {where: {id: uid}});
  54. return ret;
  55. }
  56. async send(uid, data) {
  57. let mysql = this.app.mysql;
  58. let phones = data.phones;
  59. let tels = phones.split(",");
  60. let n = tels.length;
  61. let inserted = 0;
  62. let i=0;
  63. for( ;i<n; i++) {
  64. let phone = tels[i];
  65. let ids = data.ids;
  66. let idsArr = ids.split(",");
  67. let k = idsArr.length;
  68. while( k-- ) {
  69. let ins_id = idsArr[k];
  70. let sql = `insert into ins_sync (sender, insId, phone, added_time, expired_time) values (${uid}, ${ins_id},${phone}, now(),now()+interval 3 day)`;
  71. await mysql.query( sql );
  72. inserted +=1;
  73. }
  74. }
  75. return {
  76. inserted: i
  77. };
  78. }
  79. async feedback(uid, data) {
  80. let mysql = this.app.mysql;
  81. await mysql.query("insert into app_user_feedback (uid, text, images, phone, time) values (?,?,?,?, now())", [uid, data.text, data.images, data.phone]);
  82. return "反馈成功";
  83. }
  84. async setting(uid, data) {
  85. let mysql = this.app.mysql;
  86. let ret = await mysql.update("app_user", data, {where: {id: uid}});
  87. return ret;
  88. }
  89. }
  90. module.exports = AppUserService;