/*
 * @Descripttion: HJ
 * @Company: 思宏科技
 * @version: v1.0
 * @Author: HJ
 * @Date: 2021-06-16 11:10:10
 * @LastEditors: HJ
 * @LastEditTime: 2021-06-16 11:25:53
 */
import { fhGetPhone } from "../api/auth/index";
// 授权手机号
const authPhone = (e, resolve, reject) => {
  wx.login({
    success: async (res) => {
      const openId = wx.getStorageSync("openid");
      const { code } = res;
      const accountInfo = wx.getAccountInfoSync();
      const { appId } = accountInfo.miniProgram;
      try {
        const {
          results: { phoneNumber },
        } = await fhGetPhone({
          appId,
          code,
          openId,
          ...e.detail,
        });
        Tips("获取手机号成功");
        resolve(phoneNumber);
      } catch (e) {
        console.error("授权手机号 error:", e);
        reject(Error(e));
      }
    },
  });
};

// 自动获取手机号
export const automaticPhone = (e) =>
  new Promise((resolve, reject) => {
    const mobile = wx.getStorageSync("auth-phone");
    if (mobile) {
      resolve(mobile);
    } else {
      authPhone(e, resolve, reject);
    }
  });
