import { request } from '@/utils/request';

/**
 * 通过wx.login()获取登录code令牌
 * @returns
 */
export const getWxLoginCode = () => {
  return new Promise((resolve, reject) => {
    wx.login({
      success(res) {
        if (res.code) {
          //发起网络请求log
          resolve(res.code);
        }
      },
    });
  });
};

/**
 * 微信一键注册/登录
 * @param {*} data
 * @returns
 */
export function loginByWxLoginCode(data) {
  return request({
    url: '/uc/oauth/token',
    method: 'post',
    header: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: data,
  });
}

/**
 *  通过手机号获取验证码
 * @param {string} phoneNumber 电话号码
 * @returns R
 */
export function getSmsCodeByPhone(phoneNumber) {
  return request({
    url: '/uc/login/captcha',
    method: 'post',
    data: {
      mobile: phoneNumber,
    },
  });
}

/**
 *  通过手机号验证码登录
 * @param {*} data
 * @returns
 */
export function loginByPhoneAndSmsCode(data) {
  return request({
    url: '/uc/oauth/token',
    method: 'post',
    data: data,
    header: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  });
}

/**
 * 刷新token
 * @param {*} data
 * @returns
 */
export function refreshToken(data) {
  return request({
    url: '/uc/oauth/token',
    method: 'post',
    data: data,
    header: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  });
}
