import request from './utils/request';

App({
    onLaunch() {
        // Login
        wx.login({
            success: res => {
                // Send res.code to backend to swap for openId, sessionKey, unionId
                if (res.code) {
                    request.get('/auth/miniLogin', { code: res.code }).then(data => {
                        // Assuming the token is in data.token or data itself
                        const token = data.token || data;
                        if (token && typeof token === 'string') {
                            wx.setStorageSync('token', token);
                            console.log('Login successful, token stored');
                        } else {
                            console.warn('Login response did not contain a valid token string', data);
                        }
                    }).catch(err => {
                        console.error('Login failed', err);
                    });
                } else {
                    console.error('wx.login failed: ' + res.errMsg);
                }
            }
        });
    },
    globalData: {
        userInfo: null
    }
})
