import service from "@/util/oaRequest"
import { setToken } from "../../util/auth"

// 获取oa系统的验证码（走个形式）
export const getSMSCodeFromOa = async (phoneNumber) => {
    const option = {
        url: '/fadapp/auth/send-code',
        data: {
            phone: phoneNumber
        },
        headers: {
            isToken: false
        },
        method: 'post'
    }
    const result = await service(option)
	console.log('验证码获取结果')
    return result
}

// 通过手机号自动登录oa系统
export const loginOaByPhone = async (phoneNumber) => {
  try {
	// 准备登录oa
    const data = {
        url: '/fadapp/auth/login-by-code',
        data: {
            phone: phoneNumber,
            code: '666666'
        },
        headers: {
            isToken: false
        },
        method: 'post'
    }
    const response = await service(data)
    console.log(response)
    // 响应拦截器已经处理了响应，直接返回 res.data
    if (response && response.data.token) {
      setToken(response.data.token)
      uni.setStorageSync('oaId', response.data.userId)
    //   localStorage.setItem('oaToken', response.data.token)
      return {
        token: response.data.token,
        userInfo: response.data
      }
    }
    
    throw new Error('登录失败：未获取到token')
  } catch (error) {
    console.error('OA系统登录失败:', error)
    throw error
  }
}
