import wxp from "../wxp" ; import { singleInstancePromise } from "../util" ; import { SESSION_KEY } from "../../config/global-config" ; import { API } from "../../config/cgi-config" ; import { request } from "./index" ; let hasSessionChecked = false ; let session ; /**
 * asyncLogin 逻辑的实现，不被单例保护，不能被直接调用
 */ async function asyncLogin ( ) { if ( ! hasSessionChecked ) { try { await wxp . checkSession ( ) ; hasSessionChecked = true ; } catch ( error ) { wx . removeStorageSync ( SESSION_KEY ) ; } } session = wx . getStorageSync ( SESSION_KEY ) ; if ( ! session ) { const res = await wxp . login ( ) ; const data = await request ( API . login , { code : res . code } ) ; try { wx . setStorageSync ( SESSION_KEY , data [ SESSION_KEY ] ) ; } catch ( error ) { throw error ; } } } export const login = singleInstancePromise ( asyncLogin ) ; export default login ;