const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

exports.main = async (event) => {
  const {
    username,
    password
  } = event.data
  const db = cloud.database();
  const _ = db.command
  let user = await db.collection('user').where({
    username: _.eq(username)
  }).get()
  user = user.data[0]
  if (!user || (user.password !== password)) {
    return {
      success: false,
      msg: "账号或密码错误",
    }
  } else {
    return {
      success: true,
      userInfo: {
        username: user.username,
        isAdmin: user.isAdmin
      }
    }
  }
}