新代码如下,我异步异步改
<script>import { updateToken } from "./utils/token.js"import {apiGetUserInfo, uniGetSetting, uniGetSystemInfo, uniGetUserInfo} from "./js/api_base";export default {onLaunch: function () {this.showFavoriteGuide()this.reviewVersionUpdate()uniGetSetting().then(res=>{// #ifdef MP-BAIDUconst bdAppLogin = swan.isLoginSync().isLogin// #endifif(res['scope.userInfo'] && bdAppLogin){uniGetUserInfo().then(res=>{this.globalData.userInfo = res.userInfothis.globalData.encryptedData = res.encryptedData;this.globalData.iv = res.iv;}).catch(console.log)}})uniGetSystemInfo().then(res=>{this.globalData.screenWidth = res.screenWidth;this.globalData.screenHeight = res.screenHeight;this.globalData.statusBarHeight = res.statusBarHeight;})},onShow() {console.log("--appShow", this.globalData)if (this.globalData.tokenTime) {if (this.globalData.isPhone) { // 手机号登陆if (new Date().getTime() - this.globalData.tokenTime > 86400000) {// 手机号登陆的1天过期this.globalData.userInfo = null; // 过期就置空,方便在其他地方未登录判断this.globalData.tokenTime = null;this.globalData.token = null;}}else {// 正常登录后台默默刷新tokenif (new Date().getTime() - this.globalData.tokenTime > 7200000) {updateToken().then((res) => {if (res.data && res.data.code === 200) {this.globalData.tokenTime = new Date().getTime();this.globalData.token = res.data.data.token;}})}}}},globalData: {userInfo: null,firstOpenShowPreview: true,firstOpenShowNotice: true,},methods: {showFavoriteGuide(){// #ifdef MP-BAIDUswan.showFavoriteGuide({type: 'tip',content: '关注小程序,下次使用更便捷。'})// #endif},reviewVersionUpdate(){const updateManager = uni.getUpdateManager();updateManager.onCheckForUpdate(function (res) {// 请求完新版本信息的回调console.log(res.hasUpdate);});updateManager.onUpdateReady(function (res) {uni.showModal({title: '更新提示',content: '新版本已经准备好,是否重启应用?',success(res) {if (res.confirm) {// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启updateManager.applyUpdate();}}});});updateManager.onUpdateFailed(function (res) {// 新的版本下载失败});},customLogin(){return new Promise((resolve, reject) => {if (this.globalData.isPhone){resolve()}apiGetUserInfo().then(res=>{if (res.user_id === 0) {reject({err:401,msg:"用户未绑定,请用手机号登录!"});}this.globalData.tokenTime = new Date().getTime();this.globalData.token = res.token;this.updateGlobalUserinfo(res)}).catch(reject)})},phoneLogin(encryptedData, iv) {return new Promise((resolve, reject) => {// #ifdef MP-BAIDUswan.getLoginCode({success: function (res) {console.log("phoneLoginlogin", res);return resolve(res);},fail: function (res) {console.log("phoneLoginloginErr", res);reject("调用登录失败")}})// #endif}).then((res) => {let url = `/v2/login-code?code=${res.code}`;// #ifdef MP-BAIDUurl = `/v2/login/baidu-app?code=${res.code}`// #endifreturn this.fetchPure('GET', url, undefined, false);}).then((res) => {if (res.data && res.data.code === 200) {this.globalData.tokenTime = new Date().getTime();this.globalData.token = res.data.data.token;return this.fetchPure('POST', '/v2/baidu-app/login-phone', {encryptedData, iv}, true).then((res) => {if (res.data && res.data.code === 200) {this.updateGlobalUserinfo(res.data.data)}});} else {return Promise.reject(res.data.msg || "获取login-code接口失败")}})},updateGlobalUserinfo(res) {this.globalData.id = res.user_id;this.globalData.vip_id = res.vip_id;this.globalData.vip_icon = res.vip_icon;this.globalData.userInfo = {avatarUrl: res.avatar_url,nickName: res.nickname}},fetchPure(method, path, data, isFormData) {const host = 'https://api.tusij.com'// const host = 'https://api.isheji5.com'let url = "";if(path.indexOf("?") > -1) {url = host + path + `&token=${this && this.globalData && this.globalData.token}`;}else {url = host + path + `?token=${this && this.globalData && this.globalData.token}`;}url += "&source=baidu_app";return new Promise((resolve, reject) => {uni.request({method: method.toLowerCase(),url: url,header: {'content-type': isFormData ? "application/x-www-form-urlencoded" : 'application/json' // 默认值},data,success(res) {resolve(res)},fail(e) {reject(e);}})})},},}</script>
