import {
  userInfo
} from "@/api/user.js"

export const baseImgUrl = "https://guess-shop.oss-cn-beijing.aliyuncs.com"

export const storage = {
  set (key, value) {
    try {
      uni.setStorageSync(key, value)
    } catch (e) {
      // error
    }
  },
  get (key) {
    try {
      const value = uni.getStorageSync(key);
      return value
    } catch (e) {
      // error
    }
  },
  remove (key) {
    try {
      uni.removeStorageSync(key);
    } catch (e) {
      // error
    }
  },
  clear () {
    try {
      uni.clearStorageSync();
    } catch (e) {
      // error
    }
  }
}
// 100011
export const needLoginList = [100001, 100002, 100004, 100005, 100011]
// 返回 code
export const backCodeList = [9000001]
export const UserInfo = () => {
  return new Promise((resolve, reject) => {
    userInfo().then(res => {
      storage.set('USER_INFO', res.data)
      resolve(res.data)
    })
  })
}

export const handleBaseImgUrl = (img) => {
  return baseImgUrl + img
}
// 用户拒绝授权的展示
export const showRefuseLocationPermission = () => {
  return new Promise((resolve, reject) => {
    // 请求用户授权，会有位置授权的提示
    wx.showModal({
      title: "提示",
      content: "需要获取用户位置权限",
      confirmText: "前往设置",
      success (res) {
        if (res.confirm) {
          uni.openSetting({
            success: (res) => {
              if (res.authSetting['scope.userLocation']) {
                console.log('成功授权userLocation')
                resolve()
              } else {
                console.log('用户未授权userLocation')
                // 递归调用本函数，(函数套函数)
                showRefuseLocationPermission()
              }
            },
            fail: (err) => {
              console.log("打开设置失败", err)
            }
          })
        }
      }
    })
  })
}

