大家好,开发遇到问题,登陆到会员系统后默认进入会员首页调用API接口数据中的员工姓名,然后给存储起来,供别的页面调用。但是别的页面打开无法调用,必须会员登陆后,再次点击会员首页刷新下,别的页面才可以获取。请问如何解决?
JS代码:
const App = getApp();
const func = require('../../utils/func');
var myzhenshi=wx.getStorageSync('zhenshi');
console.log(myzhenshi)
Page({
/**
* 页面的初始数据
*/
data: {
listARR:[],//获取SMT的员工信息
myzhenshi:myzhenshi,
myrealname:'',
isLogin: false, // 是否登录
userInfo: {}, // 用户信息
orderCount: {}, // 订单数量
globalConf: App.globalData,
// 底部导航菜单
tabbar: App.globalData.tabbar,
shop_open:'', //1是商场版 0企业版
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
this.ztabbar = this.selectComponent('#ztabbar');
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
let _this = this;
_this.setData({
isLogin: App.checkIsLogin()
});
// 获取当前用户信息
_this.getUserDetail();
},
/**
* 下拉刷新
*/
onPullDownRefresh: function () {
let _this = this;
// 获取当前用户信息
wx.stopPullDownRefresh()
},
/**
* 获取当前用户信息
*/
getUserDetail() {
let _this = this;
App._requestApi(_this, App.globalData.config.apiUsersdetailUrl, {}, function (result) {
let shop_open = result.data.usersConf.shop_open
if (!result.data.userInfo) {
result.data.isLogin = false;
// 移除token users_id
wx.removeStorageSync('token');
wx.removeStorageSync('users_id');
wx.removeStorageSync('username');
wx.removeStorageSync('zhenshi');
}
// 增加登陆后从SMT考勤系统调用员工信息的API接口 start
// 变量mynumber获取用户ID
var mynumber = wx.getStorageSync('users_id');
mynumber = mynumber ? mynumber : '0'
var myusername = wx.getStorageSync('username');
myusername = myusername ? myusername : '0'
// 微信头像
console.log(mynumber);
wx.request({
url: 'https://www.abc.com/get',
method:'POST',
data: {
pinlist:String(myusername),
},
success:function (res) {
// console.log(res.data);
console.log(res.data.data.items);
// console.log(mystaffcorporation);
_this.setData ({
listARR:res.data.data.items,
myusername:myusername
})
wx.setStorageSync('zhenshi', res.data.data.items[0].name)
}
})
// 增加登陆后从SMT考勤系统调用员工信息的API接口 end
wx.setStorageSync('username', result.data.userInfo.username);
_this.setData(result.data);
_this.setData({
shop_open
})
});
},
// 跳转到登录界面
navtoGoPage(e) {
let _this = this
if (!_this.onCheckLogin()) {
return false;
}
let Url = e.currentTarget.dataset.url
wx.navigateTo({
url: Url,
})
},
/**
* 订单导航跳转
*/
onTargetOrder(e) {
let _this = this;
if (!_this.onCheckLogin()) {
return false;
}
// 记录formid
App.saveFormId(e.detail.formId);
let urls = {
all: '/pages/order/index?type=all',
payment: '/pages/order/index?type=payment',
received: '/pages/order/index?type=received',
complete: '/pages/order/index?type=complete',
};
// 转跳指定的页面
wx.navigateTo({
url: urls[e.currentTarget.dataset.type]
})
},
navLink(e){
let Url = e.currentTarget.dataset.url
wx.showToast({
title: '敬请期待哦~',
duration: 2000,
success: (res) => {
// wx.navigateTo({
// url: Url,
// }) //跳转路径
},
fail: (res) => {},
complete: (res) => {},
})
// wx.navigateTo({
// url: Url,
// })
},
/**
* 菜单列表导航跳转
*/
onTargetMenus(e) {
let _this = this;
if (!_this.onCheckLogin()) {
return false;
}
// 记录formId
// App.saveFormId(e.detail.formId);
wx.navigateTo({
url: '/' + e.currentTarget.dataset.url
})
},
/**
* 跳转到登录页
*/
onLogin() {
App.doLogin();
},
/**
* 验证是否已登录
*/
onCheckLogin() {
let _this = this;
if (!_this.data.isLogin) {
App.showError('很抱歉,您还没有登录');
return false;
}
return true;
},
/**
* 跳转收货地址页面
*/
onAddressList(e) {
let _this = this;
if (!_this.onCheckLogin()) {
return false;
}
// 记录formId
App.saveFormId(e.detail.formId);
wx.navigateTo({
url: '/pages/address/index'
})
},
switchTab: function (e) {
func.switchTab(e);
},
onLogout: function (e) {
let _this = this
// 移除token users_id
wx.removeStorageSync('token');
wx.removeStorageSync('users_id');
wx.removeStorageSync('username');
wx.removeStorageSync('zhenshi');
wx.reLaunch({
url: e.currentTarget.dataset.url
})
_this.onPullDownRefresh()
},
jumpToView: function (e) {
func.jumpToView(e);
},
//推荐商品添加购物车
addCart: function (e) {
func.addCart(e);
},
edit:function (e) {
wx.navigateTo({
url: '/pages/user/edit/index'
})
},
// 判断是否登录 并 跳转到指定的URL页面
jumpSpecifyUrl(e) {
let _this = this;
// 判断是否登录
if (!_this.onCheckLogin()) {
return false;
}
// 转跳指定的页面
wx.navigateTo({url: e.currentTarget.dataset.url});
},
})
WXML文件为:
<view class="container">
<!-- 用户信息 -->
<view class="user">
<view class="user-header" style="background-color: #ff791c;">
<!-- 已登录 -->
<view wx:if="{{ isLogin }}" class="user-header-cont" catchtap='edit'>
<view class="user-header-avatar">
<!-- 微信头像 -->
<image style="width: 120rpx;height: 120rpx;border-radius: 50%;" src="{{userInfo.head_pic}}"></image>
<!-- <open-data class="" lang="zh_CN" type="userAvatarUrl"></open-data> -->
</view>
<view class="user-header-con">
<!-- 微信昵称 -->
<view class="dis-flex flex-dir-row flex-y-center">
<text class="f-32 c-f">{{userInfo.nickname}}</text>
<!-- <open-data class="user-header-cont-name" lang="zh_CN" type="userNickName"></open-data> -->
<text catchtap="navtoGoPage" data-url="/pages/user/edit/index" class="iconfont c-f sm-edit2 f-38 m-l-8"></text>
</view>
<view>
{{myzhenshi}}
</view>
<!-- 会员等级 -->
<view wx:if="{{ userInfo.level > 0 }}" class="user-header-grade dis-flex flex-y-center">
<view class="user-grade_name m-top20">
<text>{{ userInfo.level_name }}</text>
</view>
</view>
</view>
<view class="user-header-right">
<text class="iconfont sm-you"></text>
</view>
</view>
<!-- 未登录 -->
<view wx:else class="user-header-cont" catchtap="onLogin">
<view class="user-header-avatar">
<image src="/static/images/default-avatar.jpg"></image>
</view>
<view class="user-header-cont-name">点击登录</view>
<view class="user-header-right">
<text class="iconfont sm-you"></text>
</view>
</view>
</view>
</view>
<!-- 我的订单 -->
<view class="menus-list" >
<form catchtap="navtoGoPage" data-url="/pages/user/edit/index" report-submit="true">
<button formType="submit" class="btn-normal">
<view class="menus-item dis-flex b-f">
<view class="menus-item__left flex-box dis-flex flex-y-center">
<!-- <text class="iconfont sm-gerenxinxi f-38"></text>w; -->
<text class="menus-item__name col-0e0e0e f-33">个人信息</text>
</view>
<view class="menus-item__right flex-box dis-flex flex-x-end flex-y-center">
<text class="iconfont sm-you f-36 col-7"></text>
</view>
</view>
</button>
</form>
<!-- 我的企业 -->
<form catchtap="navtoGoPage" data-url="/pages/corporation/corporation" report-submit="true">
<button formType="submit" class="btn-normal">
<view class="menus-item dis-flex b-f">
<view class="menus-item__left flex-box dis-flex flex-y-center">
<!-- <text class="iconfont sm-gerenxinxi f-38"></text>w; -->
<text class="menus-item__name col-0e0e0e f-33">我的企业</text>
</view>
<view class="menus-item__right flex-box dis-flex flex-x-end flex-y-center">
<text class="iconfont sm-you f-36 col-7"></text>
</view>
</view>
</button>
</form>
<!-- 我的考勤 -->
<form catchtap="navtoGoPage" data-url="/pages/kaoqin/kaoqin" report-submit="true">
<button formType="submit" class="btn-normal">
<view class="menus-item dis-flex b-f">
<view class="menus-item__left flex-box dis-flex flex-y-center">
<!-- <text class="iconfont sm-gerenxinxi f-38"></text>w; -->
<text class="menus-item__name col-0e0e0e f-33">我的考勤</text>
</view>
<view class="menus-item__right flex-box dis-flex flex-x-end flex-y-center">
<text class="iconfont sm-you f-36 col-7"></text>
</view>
</view>
</button>
</form>
<!-- 列表 -->
<form bindsubmit="onTargetOrder" data-type="all" report-submit="true">
<button formType="submit" class="btn-normal">
<view class="menus-item dis-flex b-f">
<view class="menus-item__left flex-box dis-flex flex-y-center">
<text class="menus-item__name col-0e0e0e f-33">我的订单</text>
</view>
<view class="menus-item__right flex-box dis-flex flex-x-end flex-y-center">
<text class="iconfont sm-you f-36 col-7"></text>
</view>
</view>
</button>
</form>
<form catchtap="navtoGoPage" data-url="/pages/user/collect/index" report-submit="true">
<button formType="submit" class="btn-normal">
<view class="menus-item dis-flex b-f">
<view class="menus-item__left flex-box dis-flex flex-y-center">
<!-- <text class="iconfont sm-wodejifen f-38"></text> -->
<text class="menus-item__name col-0e0e0e f-33">我的收藏</text>
</view>
<view class="menus-item__right flex-box dis-flex flex-x-end flex-y-center">
<text class="iconfont sm-you f-36 col-7"></text>
</view>
</view>
</button>
</form>
<form bindsubmit="navtoGoPage" data-url="/pages/address/index" report-submit="true" wx:if="{{shop_open == 1}}">
<button formType="submit" class="btn-normal">
<view class="menus-item dis-flex b-f">
<view class="menus-item__left flex-box dis-flex flex-y-center">
<!-- <text class="iconfont sm-shouhuodizhi f-38"></text> -->
<text class="menus-item__name col-0e0e0e f-33">收货人地址</text>
</view>
<view class="menus-item__right flex-box dis-flex flex-x-end flex-y-center">
<text class="iconfont sm-you f-36 col-7"></text>
</view>
</view>
</button>
</form>
<form bindsubmit="onLogout" data-url="/pages/user/index" report-submit="true">
<button formType="submit" class="btn-normal">
<view class="menus-item dis-flex b-f">
<view class="menus-item__left flex-box dis-flex flex-y-center">
<!-- <text class="iconfont sm-tuichudenglu f-38"></text> -->
<text class="menus-item__name col-0e0e0e f-33">退出登录</text>
</view>
<view class="menus-item__right flex-box dis-flex flex-x-end flex-y-center">
<text class="iconfont sm-you f-36 col-7"></text>
</view>
</view>
</button>
</form>
</view>
</view>
<!--会员中心 end-->
<!--这里是底部 -->
<tabbar type='default'></tabbar>
onshow事件里面获取每次切换都会触发,