123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- 'use strict';
- const Service = require('egg').Service;
- const md5 = require('md5-node');
- class AppUserService extends Service {
-
- async login(name, password) {
-
- let { ctx , config, app} = this;
- let pwdmd5 = md5(password);
- //验证用户是否存在
- let data = await ctx.model.Admin.find({name, password: pwdmd5});
- if( data.length < 1 ) {
- throw '用户名或密码错误!';
- }
- let info = data[0];
- const beijingTimeOffset = 28800000;
- let ret = await ctx.model.Admin.update({_id: info._id}, {$set:{lastLogin: Date.now()+beijingTimeOffset}});
-
- let token = await ctx.jwtSign({name:info.name, id:info._id, role:info.role}, config.loginAdmin.secret, { expiresIn: 60 * 60 });
- return {token, role: info.name};
- }
- async getFamily( userid ){
- let {app} = this;
- 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"]});
- return user;
- }
- async addFamily( userid , data) {
- let { app } = this;
-
- data.user_id = userid;
- data.coverage_amt_total = 0;
- data.ins_num = 0;
- console.log("adding family", data);
- let ret = await app.mysql.insert("app_user_family", data);
- return ret;
- }
- async deleteFamily(id){
- let ret = await this.app.mysql.delete("app_user_family", {id});
- return ret;
- }
- async updateFamily( userid, data ) {
-
- let { app } = this;
- let ret = await app.mysql.update("app_user_family", data, {id: data.id});
- return ret;
- }
-
- async add ( data ) {
- }
- //个人中心返回用户基本信息
- async info(uid)
- {
- let mysql = this.app.mysql;
- let ret = await mysql.get("app_user", {id: uid}, {columns:["id","name", "tel", "sex", "avatar","wechat","address","birthday"]});
- return ret;
- }
-
- async updateInfo(uid, data) {
- let mysql = this.app.mysql;
- let ret = await mysql.update("app_user", data, {where: {id: uid}});
- return ret;
- }
- async send(uid, data) {
- let mysql = this.app.mysql;
- let phones = data.phones;
- let tels = phones.split(",");
- let n = tels.length;
- let inserted = 0;
- let i=0;
- for( ;i<n; i++) {
- let phone = tels[i];
- let ids = data.ids;
- let idsArr = ids.split(",");
- let k = idsArr.length;
- while( k-- ) {
- let ins_id = idsArr[k];
- let sql = `insert into ins_sync (sender, insId, phone, added_time, expired_time) values (${uid}, ${ins_id},${phone}, now(),now()+interval 3 day)`;
- await mysql.query( sql );
- inserted +=1;
- }
- }
- return {
- inserted: i
- };
- }
- async feedback(uid, data) {
- let mysql = this.app.mysql;
- await mysql.query("insert into app_user_feedback (uid, text, images, phone, time) values (?,?,?,?, now())", [uid, data.text, data.images, data.phone]);
- return "反馈成功";
- }
- async setting(uid, data) {
- let mysql = this.app.mysql;
-
- let ret = await mysql.update("app_user", data, {where: {id: uid}});
- return ret;
- }
- }
- module.exports = AppUserService;
|