|
@@ -1,123 +1,212 @@
|
|
|
import { getUserInfo, setWxAuthUserInfo, getThirdInfo, createAccount, changePhone } from '@/api/user.js';
|
|
|
import store from '@/store';
|
|
|
import config from '@/config';
|
|
|
+import { login } from '@/api/user.js';
|
|
|
|
|
|
|
|
|
function getUserData() {
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- Promise.all([getUserInfo(), getThirdInfo()]).then(([userInfoRes, thirdInfores]) => {
|
|
|
- let userInfo = userInfoRes.data || {};
|
|
|
- let thirdInfo = thirdInfores.data || {};
|
|
|
- let cachedInfo = wx.getStorageSync('UserInfo');
|
|
|
- userInfo = { ...cachedInfo, ...userInfo, ...thirdInfo };
|
|
|
- userInfo.userName = userInfo.userName || '';
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- userInfo = addRolesInfoToUserInfo(userInfo);
|
|
|
- if (!userInfo.userName) {
|
|
|
- userInfo.defaultUserName = 'Hello';
|
|
|
- }
|
|
|
-
|
|
|
- if (config.projectName === 'yintai') {
|
|
|
- userInfo.buildingLogo = `${config.h5StaticPath}/page-yintai/project-logo.png`;
|
|
|
- }
|
|
|
-
|
|
|
- store.commit('setUserInfo', userInfo);
|
|
|
- resolve(userInfo);
|
|
|
- }).catch((res) => {
|
|
|
- reject(res);
|
|
|
- })
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ Promise.all([getUserInfo(), getThirdInfo()]).then(([userInfoRes, thirdInfores]) => {
|
|
|
+ let userInfo = userInfoRes.data || {};
|
|
|
+ let thirdInfo = thirdInfores.data || {};
|
|
|
+ let cachedInfo = wx.getStorageSync('UserInfo');
|
|
|
+ userInfo = { ...cachedInfo, ...userInfo, ...thirdInfo };
|
|
|
+ userInfo.userName = userInfo.userName || '';
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ userInfo = addRolesInfoToUserInfo(userInfo);
|
|
|
+ if (!userInfo.userName) {
|
|
|
+ userInfo.defaultUserName = 'Hello';
|
|
|
+ }
|
|
|
+
|
|
|
+ if (config.projectName === 'yintai') {
|
|
|
+ userInfo.buildingLogo = `${config.h5StaticPath}/page-yintai/project-logo.png`;
|
|
|
+ }
|
|
|
+
|
|
|
+ store.commit('setUserInfo', userInfo);
|
|
|
+ resolve(userInfo);
|
|
|
+ }).catch((res) => {
|
|
|
+ reject(res);
|
|
|
})
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
|
|
|
function checkLogin(needGetetUserInfo = true) {
|
|
|
- let token = store.state.user.token;
|
|
|
- if (!token) {
|
|
|
- return wxLogin(needGetetUserInfo);
|
|
|
- } else {
|
|
|
- return Promise.resolve();
|
|
|
- }
|
|
|
+ let token = store.state.user.token;
|
|
|
+ if (!token) {
|
|
|
+ return wxLogin(needGetetUserInfo);
|
|
|
+ } else {
|
|
|
+ return Promise.resolve();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
function checkHasUserInfo() {
|
|
|
- let userInfo = store.state.user.userInfo;
|
|
|
- if (userInfo && JSON.stringify(userInfo) !== '{}') {
|
|
|
- return Promise.resolve(userInfo);
|
|
|
- } else {
|
|
|
- return checkLogin(false).then(() => {
|
|
|
- return getUserData();
|
|
|
- })
|
|
|
- }
|
|
|
+ let userInfo = store.state.user.userInfo;
|
|
|
+ if (userInfo && JSON.stringify(userInfo) !== '{}') {
|
|
|
+ return Promise.resolve(userInfo);
|
|
|
+ } else {
|
|
|
+ return checkLogin(false).then(() => {
|
|
|
+ return getUserData();
|
|
|
+ })
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
function setUserInfoByAuth() {
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- wx.getUserProfile({
|
|
|
- desc: '用于完善用户信息',
|
|
|
- success: res => {
|
|
|
- let detail = res;
|
|
|
- wx.login({
|
|
|
- success: loginRes => {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- },
|
|
|
- fail: res => {
|
|
|
-
|
|
|
-
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ wx.getUserProfile({
|
|
|
+ desc: '用于完善用户信息',
|
|
|
+ success: res => {
|
|
|
+ let detail = res;
|
|
|
+ wx.login({
|
|
|
+ success: loginRes => {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ },
|
|
|
+ fail: res => {
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ fail: res => {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+function checkRegist() {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ wx.login({
|
|
|
+ success(wxLoginRes) {
|
|
|
+ const jsCode = wxLoginRes.code;
|
|
|
+ const appId = wx.getAccountInfoSync().miniProgram.appId;
|
|
|
+ if (jsCode) {
|
|
|
+ let params = {
|
|
|
+ "appId": appId,
|
|
|
+ "wxCode": jsCode
|
|
|
+ }
|
|
|
+ login(params).then(res => {
|
|
|
+ if (res.result == 'success') {
|
|
|
+ let data = res.data || []
|
|
|
+ resolve(data)
|
|
|
+ } else {
|
|
|
+ reject('login接口返回fail');
|
|
|
}
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ wx.showToast({
|
|
|
+ title: `网络异常, 请稍后重试`,
|
|
|
+ icon: 'none',
|
|
|
+ duration: 5e3
|
|
|
});
|
|
|
- },
|
|
|
- fail: res => {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+ reject('wx.login接口返回code为空');
|
|
|
}
|
|
|
- });
|
|
|
+ },
|
|
|
+ fail(res) {
|
|
|
+ reject(res);
|
|
|
+ wx.showToast({
|
|
|
+ title: `网络异常, 请稍后重试`,
|
|
|
+ icon: 'none',
|
|
|
+ duration: 5e3
|
|
|
+ });
|
|
|
+ }
|
|
|
});
|
|
|
- }
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+function wxLogin() {
|
|
|
+ store.commit('setTokenIsValid', false);
|
|
|
+ store.commit('setToken', '');
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ wx.login({
|
|
|
+ success(wxLoginRes) {
|
|
|
+ const jsCode = wxLoginRes.code;
|
|
|
+ const appId = wx.getAccountInfoSync().miniProgram.appId;
|
|
|
+ if (jsCode) {
|
|
|
+ let params = {
|
|
|
+ "appId": appId,
|
|
|
+ "wxCode": jsCode
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ wx.hideLoading();
|
|
|
+ wx.showToast({
|
|
|
+ title: `网络异常, 请稍后重试`,
|
|
|
+ icon: 'none',
|
|
|
+ duration: 5e3
|
|
|
+ });
|
|
|
+ reject('wx.login接口返回code为空');
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail(res) {
|
|
|
+ reject(res);
|
|
|
+ wx.showToast({
|
|
|
+ title: `网络异常, 请稍后重试`,
|
|
|
+ icon: 'none',
|
|
|
+ duration: 5e3
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+function setAvatar(avatar) {
|
|
|
+ wx.setStorageSync('avatarBase64', avatar);
|
|
|
+}
|
|
|
+function getAvatar() {
|
|
|
+ wx.getStorageSync('avatarBase64');
|
|
|
+}
|
|
|
|
|
|
export {
|
|
|
- checkHasUserInfo,
|
|
|
- setUserInfoByAuth
|
|
|
+ checkHasUserInfo,
|
|
|
+ checkRegist,
|
|
|
+ setAvatar,
|
|
|
+ getAvatar,
|
|
|
+ setUserInfoByAuth
|
|
|
}
|