| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- App({
- globalData: {
- userInfo: null,
- token: null,
- isLoggedIn: false,
- baseUrl: 'https://familial.yunzhixue.cn/manager'
- },
- onLaunch: function () {
- this.checkLoginStatus();
- },
- checkLoginStatus: function () {
- const token = wx.getStorageSync('token');
- const userInfo = wx.getStorageSync('userInfo');
-
- if (token && userInfo) {
- this.globalData.token = token;
- this.globalData.userInfo = userInfo;
- this.globalData.isLoggedIn = true;
- }
- },
- login: function (callback) {
- wx.login({
- success: (res) => {
- if (res.code) {
- wx.request({
- url: `${this.globalData.baseUrl}/api/wechat/login`,
- method: 'POST',
- data: {
- code: res.code
- },
- success: (response) => {
- if (response.data.success) {
- this.globalData.token = response.data.token;
- this.globalData.userInfo = response.data.user;
- this.globalData.isLoggedIn = true;
-
- wx.setStorageSync('token', response.data.token);
- wx.setStorageSync('userInfo', response.data.user);
-
- if (callback) callback(null, response.data);
- } else {
- if (callback) callback(new Error(response.data.message));
- }
- },
- fail: (err) => {
- if (callback) callback(err);
- }
- });
- } else {
- if (callback) callback(new Error('登录失败'));
- }
- },
- fail: (err) => {
- if (callback) callback(err);
- }
- });
- },
- logout: function () {
- this.globalData.token = null;
- this.globalData.userInfo = null;
- this.globalData.isLoggedIn = false;
-
- wx.removeStorageSync('token');
- wx.removeStorageSync('userInfo');
- }
- });
|