// pages/LoginOrOutDetails/LoginOrOutDetails.js const MyApi = require('../../Api/api.js') Page({ /** * 页面的初始数据 */ data: { userInfo: { avatarUrl: '../../static/image/defaultAvatar.png', nickName: wx.getStorageSync('UserInfoCache').UserName, }, LoginTag: "一键登录", isadmin:wx.getStorageSync('UserInfoCache').IsAdministor, loginStatue:wx.getStorageSync('UserInfoCache').IsLogin }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { this.setData({ userInfo: { avatarUrl:'../../static/image/weixin.png', nickName: wx.getStorageSync('UserInfoCache').UserName, }, loginStatue: wx.getStorageSync('UserInfoCache').IsLogin }); var Storage = wx.getStorageSync('UserInfoCache'); if (Storage.IsLogin === "1") { this.setData({ userInfo: { //avatarUrl: getApp().globalData.BaseURL + "/Images/UsersHeadIcon/" + Storage.user_openid + ".png", avatarUrl:'../../static/image/weixin.png', nickName: Storage.UserName, }, }); }; }, onLoginBtnClick() { var isAdmin=this.data.isadmin; var tempusername=this.data.userInfo.nickName var random_username=""; if (!tempusername) { //生成随机用户名 var chars = 'abcdefghijklmnopqrstuvwxyz0123456789'; var random_username = ""; for (var i = 0; i < 8; i++) { var randomIndex = Math.floor(Math.random() * chars.length); random_username += chars[randomIndex]; } random_username = "用户"+random_username; } else {random_username = tempusername} console.log("用户名: "+random_username); // // 复制本地图片到用户数据路径,如果文件不存在,先复制 // const UpLoadfilePath = wx.env.USER_DATA_PATH + '/defaultAvatar.png'; // try {wx.getFileSystemManager().accessSync(UpLoadfilePath);} // catch (e) { // wx.getFileSystemManager().copyFileSync('static/image/defaultAvatar.png', UpLoadfilePath); // } // wx.uploadFile({ // filePath: UpLoadfilePath, // name: "HeadIconKey", // url: getApp().globalData.BaseURL+'/API/ApiChangeUserInfo.php', //这里会在后端设置为已登录 // formData:{ // username: random_username, // openid: wx.getStorageSync('UserInfoCache').user_openid // }, // success(res) { // //打印服务器返回数据 // wx.showToast({ // title: '成功!', // icon: 'success', // mask:true, // duration: 4000 // }); // //这里修改缓存 // var tempuesrinfo=wx.getStorageSync('UserInfoCache') // tempuesrinfo.UserName=random_username // tempuesrinfo.IsAdministor=isAdmin // tempuesrinfo.IsLogin="1" // wx.setStorageSync('UserInfoCache', tempuesrinfo) // setTimeout(function() { // wx.hideToast(); // wx.switchTab({ // url: '/pages/wode/wode', // }) // },3000); // }, // fail(res) { // //打印服务器返回数据 // console.log(res); // setTimeout(function() { // wx.hideToast(); // wx.switchTab({ // url: '/pages/wode/wode', // }) // },3000); // } // }); MyApi.ApiLogin(wx.getStorageSync('UserInfoCache').user_openid, random_username) .then(result1 => { //打印服务器返回数据 wx.showToast({ title: '成功!', icon: 'success', mask:true, duration: 4000 }); //这里修改缓存 var tempuesrinfo=wx.getStorageSync('UserInfoCache') tempuesrinfo.UserName=random_username tempuesrinfo.IsAdministor=isAdmin tempuesrinfo.IsLogin="1" wx.setStorageSync('UserInfoCache', tempuesrinfo) setTimeout(function() { wx.hideToast(); wx.switchTab({ url: '/pages/wode/wode', }) },3000); }) .catch(err => { }); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { this.onLoad(); }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, datachange(){ //退出登录操作! MyApi.ApiLogout(wx.getStorageSync('UserInfoCache').user_openid) .then(result1 => { //缓存状态修改 var tempuesrinfoislogin=wx.getStorageSync('UserInfoCache') tempuesrinfoislogin.IsLogin=null //tempuesrinfoislogin.UserName=null tempuesrinfoislogin.IsAdministor=null this.data.userInfo.avatarUrl= '../../static/image/defaultAvatar.png' this.data.LoginTag= "登录" wx.setStorageSync('UserInfoCache', tempuesrinfoislogin) //返回个人信息页面 wx.showToast({ title: '请稍等...', icon:'loading', mask:true, duration: 4000, }) setTimeout(function() { wx.hideToast(); wx.switchTab({ url: '/pages/wode/wode', }) },3000); }) .catch(err => { }); }, onlogout() { if (wx.getStorageSync('UserInfoCache').IsLogin){ wx.showModal({ title: '提示', content: '确定退出登录吗?', showCancel: true, // 是否显示取消按钮 cancelText: '取消', // 取消按钮的文本,默认是"取消" cancelColor: '#000000', // 取消按钮的文字颜色,默认是"#000000" confirmText: '确认', // 确认按钮的文本,默认是"确定" confirmColor: '#ff0000', // 确认按钮的文字颜色,默认是"#3CC51F" success:(res)=> { if (res.confirm) { // 用户点击了确认按钮 this.datachange() } else if (res.cancel) { // 用户点击了取消按钮 } } }); } } })