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

import store from '@/store'

// 获取登录凭证（code）
export function getWxCode() {
	return new Promise((resolve, reject) => {
		uni.login({
			success(res) {
				resolve(res.code);
			},
			fail(res) {
				reject(res);
			}
		});
	});
}

//小程序静默登录
export async function wxMnpLogin(invite_code){
  store.commit('user/LOGOUT')
  const code = await getWxCode()
  invite_code=invite_code?invite_code: uni.getStorageSync('INVITE_CODE')
 await silentLogin({code,invite_code})
}


async function silentLogin(data){
  // const provider = 'wechat-mini'
  // const resData = await http.post(`/v1/socialite/code-auth/${provider}`,data,{
  //   custom:{
  //     toast:false
  //   }
  // })
  const resData = await http.post(`/v1/wechat-mini/login`,data,{
    custom:{
      toast:false
    }
  })
  store.commit('user/LOGIN', resData.token)
 }
