// 认证接口： 获取 open_id 后登录

//https://mt5t6sp5cd.feishu.cn/docx/SftAdyxGzoQwAWxCnDrc3F8en3g
import rest from '@/stores/rest'
import { OPEN_ID } from '@/lib/config';

//获取 JsCode
export const wxLogin = async () => {
	console.log("WxLogin")
	return new Promise((resolve, reject) => {
		wx.login({
			timeout:3000,
			success(res) {
				console.log("wx.login success", res)
				if (res.code) {
					resolve(res.code)
					return;
				}
				reject(res.errMsg)
			},
			fail(err){
				reject(err.errMsg)
			}
		})
	});
}

export const getOpenId = async () => {
	let openid = await uni.getStorageSync(OPEN_ID)
	if (openid) return
	try {
		let jsCode = await wxLogin()
		let url = '/member/user/jscode2session?jsCode=' + jsCode
		let result = await rest.post(url, null)
		if (result['openid']) {
			uni.setStorageSync(OPEN_ID, result['openid'])
		}
	} catch (e) {
		console.log(e)
	}
}


//通过获取 jsCode 自动登录
export const userLogin = async (jsCode:string) => {
	
}