/**
 * user service 
 */
const db = require('../bin/database/db')
var cryptUtil = require('../bin/util/cryptUtil')

function signUp(opts, done) {
    let result = {
        status: false
    }
    let {
        userName,
        passWord,
        nickName,
        orgId,
        sex,
        email,
        mobilePhone,
        telePhone
    } = opts
    let userId = cryptUtil.guid()
    let userTable = db.table('user')
    userTable.find({
        username: userName
    }).then(data => {
        if (data && data.length && data.length > 0) {
            return Promise.reject('此用户名已被占用！');
        }
    }).then(() => {
        return userTable.add({
            userid: userId,
            username: userName,
            password: passWord,
            nickname: nickName,
            orgid: orgId,
            sex: sex,
            email: email,
            mobilephone: mobilePhone,
            telephone: telePhone
        })
    }).then(data => {
        result.status = true
        done(result)
    }).catch(error => {
        result.message = error && typeof error === 'string' ? error : '系统错误！'
        done(result)
    })
}

function signIn(opts, done) {
    let result = {
        status: false
    }
    let {
        userName,
        passWord
    } = opts
    let userTable = db.table('user')
    userTable.findOne({
        username: userName
    }).then(data => {
        if (!data) {
            return Promise.reject('用户不存在！')
        }
        if (data.password !== passWord) {
            return Promise.reject('密码错误！')
        }
        result.status = true
        done(result)
    }).catch(error => {
        result.message = error && typeof error === 'string' ? error : '系统错误！'
        done(result)
    })
}
function updateUser(opts, done) {
    let result = {
        status: false
    }
    let { id } = opts
    let {
        userName,
        passWord,
        nickName,
        orgId,
        sex,
        email,
        mobilePhone,
        telePhone
    } = opts
    let userTable = db.table('user')
    userTable.update({ id }, {
        username: userName,
        password: passWord,
        nickname: nickName,
        orgid: orgId,
        sex: sex,
        email: email,
        mobilephone: mobilePhone,
        telephone: telePhone
    }).then((res) => {
        result.status = true
        done(result)
    }).catch((res) => {
        done(result)
    })
}
function deleteUser(opts, done) {
    let result = {
        status: false
    }
    let { id } = opts
    let userTable = db.table('user')
    userTable.remove({ id }).then((res) => {
        result.status = true
        done(result)
    }).catch((res) => {
        done(result)
    })
}
function userInfo(opts, done) {
    let result = {
        status: false
    }
    let {
        userId
    } = opts
    let userTable = db.table('user')
    userTable.findOne({
        userId
    }).then(data => {
        result.status = true
        result.data = data
        done(result)
    }).catch(err => {
        result.message = '系统错误！'
        done(result)
    })
}
function updatePassword(userId, opts, done) {
    let result = {
        status: false
    }
    let { newPwd } = opts
    let userTable = db.table('user')
    userTable.update({ userId }, { password: newPwd }).then(result => {
        result.status = true
        done(result)
    }).catch(err => {
        result.message = '系统错误！'
        done(result)
    })
}
/**
 * 根据条件获取用户
 * 
 * @param {any} opts 
 * @param {any} done 
 */
function getUserByOpts(opts, done) {
    let { nickname, orgid } = opts
    let query
    if (nickname) {
        query = {
            nickname
        }
    }
    if (orgid) {
        if (query) {
            query.orgid = orgid
        } else {
            query = {
                orgid
            }
        }
    }
    let userTable = db.table('user')
    let result = {
        status: false
    }
    userTable.like(query).then((res) => {
        result.status = true
        result.data = res
        done(result)
    }).catch(() => {
        result.message = '获取失败'
        done(result)
    })
}
module.exports = {
    signUp,
    signIn,
    userInfo,
    updatePassword,
    getUserByOpts,
    updateUser,
    deleteUser
}
@@ -1570,32 +1570,156 @@
 t.status = true%0A
+        let userId = data.userid%0A        let token = 'Bearer ' + cryptUtil.encodeToken(userId)%0A        result.token = token%0A
         done(res
@@ -1847,32 +1847,33 @@
 esult)%0A    %7D)%0A%7D%0A
+%0A
 function updateU
@@ -1938,35 +1938,47 @@
 %0A    %7D%0A    let %7B
- id
+%0A        id%0A   
  %7D = opts%0A    le
@@ -2189,19 +2189,31 @@
 update(%7B
- id
+%0A        id%0A   
  %7D, %7B%0A  
@@ -2538,32 +2538,33 @@
 esult)%0A    %7D)%0A%7D%0A
+%0A
 function deleteU
@@ -2637,19 +2637,31 @@
    let %7B
- id
+%0A        id%0A   
  %7D = opt
@@ -2721,19 +2721,31 @@
 remove(%7B
- id
+%0A        id%0A   
  %7D).then
@@ -2852,32 +2852,33 @@
 esult)%0A    %7D)%0A%7D%0A
+%0A
 function userInf
@@ -3238,16 +3238,17 @@
    %7D)%0A%7D%0A
+%0A
 function
@@ -3341,23 +3341,35 @@
    let %7B
+%0A       
  newPwd
+%0A   
  %7D = opt
@@ -3433,20 +3433,40 @@
 te(%7B
+%0A       
  userId
+%0A   
  %7D, %7B
+%0A       
  pas
@@ -3478,16 +3478,20 @@
 : newPwd
+%0A   
  %7D).then
@@ -3752,16 +3752,24 @@
    let %7B
+%0A       
  nicknam
@@ -3770,22 +3770,34 @@
 ickname,
+%0A       
  orgid
+%0A   
  %7D = opt

修改：登录接口返回token