// pages/user/index.js const { request, serverName } = require('../../utils/services'); const { Toast } = require('../../utils/util.js'); var app = getApp(); Page({ /** * 页面的初始数据 */ data: { testImg: '../../imgs/testImg/loginBg.png', avatar: '../../imgs/testImg/userIcon.png', likeImg: '../../imgs/icon/like.png', commentImg: '../../imgs/icon/comment.png', sawImg: '../../imgs/icon/saw.png', tabItem: [{ name: '待付款', tag: "0", id: 1 }, { name: '待使用', tag: "-1", id: 2 }, { name: '待评论', tag: "-1", id: 3 }], tabList: [{ name: '收藏', img: "../../imgs/icon/like.png", id: 0 }, { name: '评论', img: "../../imgs/icon/comment.png", id: 1 }, { name: '足迹', img: "../../imgs/icon/saw.png", id: 2 }], active: false, isLogin: '' }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { // this.isLongPolling() }, isLongPolling: function() { let loginSessionKey = wx.getStorageSync("token"); let requestTask = wx.request({ url: serverName + '/wx/api/user/longPolling', data: { loginSessionKey }, header: { 'content-type': 'application/x-www-form-urlencoded' }, method: "post", success: (res) => { if (res.data.code == 0) { if (res.data.data) { let tabList = this.data.tabList; tabList[1].img = "../../imgs/icon/comment_active.png"; this.setData({ tabList }) } else { let tabList = this.data.tabList; tabList[1].img = "../../imgs/icon/comment.png"; this.setData({ tabList }) } } else { return } this.isLongPolling() } }) }, tapByIdx: function(e) { let { idx } = e.currentTarget.dataset; this._isLogin(); if (this.data.isLogin) { switch (idx) { case 0: wx.navigateTo({ url: './my_follow/index', success: function(res) {}, fail: function(res) {}, complete: function(res) {}, }) break; case 1: let tabList = this.data.tabList; tabList[1].img = "../../imgs/icon/comment.png"; this.setData({ tabList }) wx.navigateTo({ url: './my_comment/index', success: function(res) {}, fail: function(res) {}, complete: function(res) {}, }) break; case 2: wx.navigateTo({ url: './my_saw/index', success: function(res) {}, fail: function(res) {}, complete: function(res) {}, }) break; } } else { Toast.showToast('tip', '请登录后查看', () => { }); } }, bindGetUserInfo: function(e) { console.log(e.detail.userInfo) }, _myLikes: function() { this._isLogin(); if (this.data.isLogin) { wx.navigateTo({ url: './my_follow/index', success: function(res) {}, fail: function(res) {}, complete: function(res) {}, }) } else { Toast.showToast('tip', '请登录后查看', () => { }); } }, _myOrder: function() { this._isLogin(); if (this.data.isLogin) { wx.navigateTo({ url: './my_order/index', success: function(res) {}, fail: function(res) {}, complete: function(res) {}, }) } else { Toast.showToast('tip', '请登录后查看', () => { }); } }, _isLogin: function() { if (wx.getStorageSync('token')) { let loginSessionKey = wx.getStorageSync("token"); if (loginSessionKey != " ") { wx.checkSession({ success: res => { wx.request({ url: serverName + '/wx/api/user/checkLoginSessionKey', data: { loginSessionKey }, header: { 'content-type': 'application/x-www-form-urlencoded' }, method: "post", success: (res) => { if (res.data.code == 0) { this.setData({ isLogin: true }) app.globalData.isLogin = true; // app.globalData.cookieIDs = res.data.data.ids.split(','); } else { this.setData({ isLogin: false, avatarUrl: "" }) app.globalData.isLogin = false; } } }) // request['checkLoginSessionKey']({ // loginSessionKey // }, "post", res => { // if (res.data.code > -1) { // this.setData({ // isLogin: true // }) // } else { // this.setData({ // isLogin: false // }) // } // }) }, fail: res => { this.setData({ isLogin: false, avatarUrl: "" }) app.globalData.isLogin = false; } }) } else { this.setData({ isLogin: false, avatarUrl: "" }) app.globalData.isLogin = false; } } else { this.setData({ isLogin: false }) app.globalData.isLogin = false; } // console.log("isLogin",this.data.isLogin); }, getUserProfile:function(e){ Toast.showToast2('loading'); // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认 // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗 wx.getUserProfile({ desc: '获取您的身份信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写 success: (res) => { console.log(123456,res); let { encryptedData, iv, userInfo } = res; app.globalData.userInfo = userInfo; wx.setStorageSync("userInfo", userInfo) this.setData({ avatarUrl: wx.getStorageSync('userInfo').avatarUrl }) wx.login({ success: res => { let { code } = res; console.log(789,res); if (code) { wx.request({ url: serverName + '/wx/api/user/getLoginSessionKey', data: { encryptedData, iv, code, }, header: { 'content-type': 'application/x-www-form-urlencoded' }, method: 'post', // dataType: 'json', // responseType: 'text', success: res => { if (res.data.code > -1) { let { loginSessionKey } = res.data.data; wx.setStorageSync('token', loginSessionKey) } else{ Toast.showToast('warn', '登录失败,请重试'); } }, fail: res => { Toast.showToast('warn', '登录失败,请重试'); }, complete: res=> { this._isLogin(); Toast.hideLoading(); }, }) // request["getLoginSessionKey"]({ // encryptedData, // iv, // code, // loginSessionKey:'' // }, "post", res => { // if (res.data.code > -1) { // let { // loginSessionKey // } = res.data.data; // wx.setStorageSync('token', loginSessionKey) // this.isLongPolling(); // console.log(loginSessionKey) // } // }, err => { // }, complete => { // this._isLogin(); // Toast.hideLoading(); // }) } } }) // console.log(res.userInfo) // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 // 所以此处加入 callback 以防止这种情况 if (this.userInfoReadyCallback) { this.userInfoReadyCallback(res) } }, complete:(res)=>{ Toast.hideLoading(); } }) }, // 微信改版了 // _getUserInfoToLogin: function(e) { // Toast.showToast2('loading'); // if (e.detail.encryptedData) { // // 可以将 res 发送给后台解码出 unionId // // app.globalData.userInfo = res.userInfo; // // app.globalData.encryptedData = res.encryptedData; // // app.globalData.iv = res.iv; // let { // encryptedData, // iv, // userInfo // } = e.detail; // console.log('---------',e); // app.globalData.userInfo = userInfo; // wx.setStorageSync("userInfo", userInfo) // this.setData({ // avatarUrl: wx.getStorageSync('userInfo').avatarUrl // }) // wx.login({ // success: res => { // let { // code // } = res; // if (code) { // console.log(123456,code); // wx.request({ // url: serverName + '/wx/api/user/getLoginSessionKey', // data: { // encryptedData, // iv, // code, // }, // header: { // 'content-type': 'application/x-www-form-urlencoded' // }, // method: 'post', // // dataType: 'json', // // responseType: 'text', // success: res => { // if (res.data.code > -1) { // let { // loginSessionKey // } = res.data.data; // wx.setStorageSync('token', loginSessionKey) // } // else{ // Toast.showToast('warn', '登录失败,请重试'); // } // }, // fail: res => { // Toast.showToast('warn', '登录失败,请重试'); // }, // complete: res=> { // this._isLogin(); // Toast.hideLoading(); // }, // }) // request["getLoginSessionKey"]({ // encryptedData, // iv, // code, // loginSessionKey:'' // }, "post", res => { // if (res.data.code > -1) { // let { // loginSessionKey // } = res.data.data; // wx.setStorageSync('token', loginSessionKey) // this.isLongPolling(); // console.log(loginSessionKey) // } // }, err => { // }, complete => { // this._isLogin(); // Toast.hideLoading(); // }) // } // } // }) // // console.log(res.userInfo) // // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 // // 所以此处加入 callback 以防止这种情况 // if (this.userInfoReadyCallback) { // this.userInfoReadyCallback(res) // } // } // }, // _login:function(e){ // console.log(e) // // if(!this.data.isLogin){ // // Toast.showToast2('loading'); // // wx.getSetting({ // // success: res => { // // console.log(res.authSetting['scope.userInfo']) // // if (!res.authSetting['scope.userInfo'] == undefined || res.authSetting['scope.userInfo'] == false) { // // Toast.showToast('tip', '四维看展申请获取个人信息权限', () => { // // wx.openSetting({ // // success: res => { // // this._getUserInfoToLogin(); // // } // // }); // // }); // // } else { // // // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框 // // this._getUserInfoToLogin(); // // } // // } // // }) // // } // }, _logout: function() { if (this.data.isLogin) { Toast.showToast2('loading'); let loginSessionKey = wx.getStorageSync('token'); request["logout"]({ loginSessionKey }, "post", res => { if (res.data.code > -1) { wx.setStorageSync('userInfo', null); wx.setStorageSync('token', null); console.log(wx.getStorageSync('token')) this.setData({ isLogin: false, avatarUrl: "" }) app.globalData.isLogin = false; } }, err => { }, complete => { Toast.hideLoading(); }) } }, tapToOrderByidx: function(e) { let { status, id } = e.currentTarget.dataset; this._isLogin(); if (this.data.isLogin) { wx.navigateTo({ url: `./my_order/index?status=${status}&&id=${id}`, success: function(res) {}, fail: function(res) {}, complete: function(res) {}, }) } else { Toast.showToast('tip', '请登录后查看', () => { }); } // url="./my_order/index?status={{item.tag}}&&id={{item.id}}" }, clickCell: function() { wx.navigateTo({ url: './my_contact/index', }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function() { }, /** * 生命周期函数--监听页面显示 */ onShow: function() { let { avatar = "" } = this.data; console.log(wx.getStorageSync('userInfo')) let avatarUrl = wx.getStorageSync('userInfo') ? wx.getStorageSync('userInfo').avatarUrl : ""; let avatarName = wx.getStorageSync('userInfo') ? wx.getStorageSync('userInfo').nickName : ""; this.setData({ avatarUrl: avatarUrl || avatar, avatarName: avatarName || '已登录' }) this._isLogin() }, /** * 生命周期函数--监听页面隐藏 */ onHide: function() { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function() { }, /** * 用户点击右上角分享 */ onShareAppMessage: function() { } })