/**
 * Created by JianJia.Zhou<jianjia.zhou> on 2022/8/14.
 */
import { getAuthorize, getToken } from '../../utils/auth'

const request = uni.$u.http

export async function authorize() {
  return new Promise(resolve => {
    uni.showLoading({
      title: '数据加载中...',
      mask: true
    })
    uni.login({
      provider: uni.$u.platform,
      success: loginRes => {
        uni.hideLoading()
        return request.post(
          '/auth/wechat/minni/code',
          { code: loginRes.code }
        ).then(res => {
          resolve(res)
        })
      }
    })
  })
}

export async function login(account, password, code) {
  return new Promise((resolve, reject) => {
    return request.post(
      '/auth/login',
      { account, password, code }
    ).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

export async function phoneQuickLogin(data) {
  return new Promise(resolve => {
    return request.post(
      '/auth/wechat/minni/phone',
      data
    ).then(res => {
      resolve(res)
    })
  })
}

export async function info() {
  return request.get(
    'user/info'
  )
}

export function isLogin() {
  return !!getToken()
}

export function isAuthorize() {
  return !!getAuthorize()
}

export default {
  authorize,
  login,
  info,
  isLogin,
  isAuthorize,
  phoneQuickLogin
}
