import settings from '../settings'
import { getAuthen } from '../assets/util'
import moment from 'moment'


const noncestr = Math.random().toString(36).substring(2);
const timestamp = +moment.utc();


// 带Origin后缀的通过getter获取
export const state = () => ({
  openIdOrigin: "",
  accessTokenOrigin: "",
  unionIdOrigin: "",
  userInfo: {},
  stationIdOrigin: "",
  appIdOrigin: "",
  noncestr,
  timestamp,
  signature:"",
})

export const getters = {
  openId: (state) => {
    if (!state.openIdOrigin) {
      getAuthen()
    }
    return state.openIdOrigin
  },
  accessToken: (state) => {
    if (!state.accessTokenOrigin) {
      getAuthen()
    }
    return state.accessToken
  },
  stationId: (state) => {
    if (!state.stationIdOrigin) {
      getAuthen()
    }
    return state.stationIdOrigin
  },
  appId: (state) => {
    if (!state.appIdOrigin) {
      getAuthen()
    }
    return state.appIdOrigin
  },
  unionId: (state) => {
    console.log(123)
    console.log(state)
    if (!state.unionIdOrigin) {
      console.log("进来了")
      getAuthen()
    }
    return state.unionIdOrigin
  },
  userInfo: (state) => state.userInfo,
}

export const mutations = {
  setOpenId(state, openId) {
    state.openIdOrigin = openId
  },
  setAccessToken(state, accessToken) {
    state.accessTokenOrigin = accessToken
  },
  setUnionId(state, unionId) {
    state.unionIdOrigin = unionId
  },
  setUserInfo(state, userInfo) {
    state.userInfo = userInfo
  },
  setSignature(state, signature) {
    state.signature = signature
  },
  setStationId(state,stationId){
    state.stationIdOrigin = stationId
  },
  setAppId(state, appId) {
    state.appIdOrigin = appId
  }
}

export const actions = {
  async login({ commit, state, getters }, code) {
    const gzhAuthenRes = await this.$axios.$get("/getGzhAuthorizeInfo", {
      params: {
        code,
        stationId: getters.stationId,
      },
    })
    if (gzhAuthenRes.retCode !== 0) { return Promise.reject("拉取基础id失败") }
    const openId = gzhAuthenRes.data.openid;
    const accessToken = gzhAuthenRes.data.access_token;
    const unionId = gzhAuthenRes.data.unionid;
    commit("setOpenId", openId)
    commit("setAccessToken", accessToken)
    commit("setUnionId", unionId)
    const getUserInfoRes = await this.$axios.$get("/getGzhUserInfo", {
      params: {
        openId: openId,
        stationId: getters.stationId
      }
    })
    if (getUserInfoRes.retCode !== 0) { return Promise.reject("拉取用户信息失败") }
    commit("setUserInfo", getUserInfoRes.data)
    const saveUserInfoRes = await this.$axios.$post("/addAppUserInfo", {
      userType: "1",
      openId: openId,
      unionId: unionId,
      stationId: getters.stationId, //油站Id
      blogNickName: getUserInfoRes.data.nickname,
      sexFlag: getUserInfoRes.data.sex === 1 ? 'M' : 'F',
      blogProfilePhoto: getUserInfoRes.data.headimgurl
    })
    if (saveUserInfoRes.retCode !== 0) { return Promise.reject("存取用户信息失败") }
    return Promise.resolve("用户登录成功")
  },
  async getSdkConfig({commit,state, getters},jsApiList){
    const sdkSignRes = await this.$axios.$get("/getSignature",{
      params:{
        noncestr:state.noncestr,
        timestamp:state.timestamp,
        url:window.location.href.replace(/#(\w|\W){0,}/g, ''),      
        stationId:getters.stationId
      }
    })
    if(sdkSignRes.retCode !== 0){Promise.reject("获取Sdk签名错误")}

    commit("setSignature", sdkSignRes.data)

    return Promise.resolve({
      debug: settings.debug, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
      appId: getters.appId, // 必填，公众号的唯一标识
      timestamp: state.timestamp, // 必填，生成签名的时间戳
      nonceStr: state.noncestr, // 必填，生成签名的随机串
      signature:sdkSignRes.data,
      jsApiList // 必填，需要使用的JS接口列表
    })
  },
  async getAppId({commit,state, getters},stationId){
    const appIdRes = await this.$axios.$get("/getStationGzhAppId",{
      params:{
        stationId:stationId
      }
    })
    if(appIdRes.retCode !== 0){return Promise.reject("获取appid失败")}
    commit("setStationId",stationId)
    commit("setAppId", appIdRes.data.appId)
    return Promise.resolve({
      stationId,
      appId:appIdRes.data.appId
    })
  }
}
