// pages/user/index.js const { request, serverName } = require('../../utils/services'); const { Toast } = require('../../utils/util.js'); var app = getApp(); import { getWxUserInfo, wxUserLogin } from '../../utils/request' 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/saw.png", id: 3 }, { name: '课程', img: "../../imgs/icon/saw.png", id: 4 }, { 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: false }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { }, 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(); switch (idx) { case 0: wx.navigateTo({ url: './my_follow/index' }) break; case 1: let tabList = this.data.tabList; tabList[1].img = "../../imgs/icon/comment.png"; this.setData({ tabList }) wx.navigateTo({ url: './my_comment/index', }) break; case 2: wx.navigateTo({ url: './my_saw/index' }) break; case 3: console.log('userId', this.data.userInfo.id) wx.navigateTo({ url: `/pages/user/my_course/my_course?userId=${this.data.userInfo.id}` }) break; case 4: wx.navigateTo({ url: `/pages/user/my_host_course/index?userId=${this.data.userInfo.id}` }) break; } }, bindGetUserInfo: function (e) { console.log(e.detail.userInfo) }, _myLikes: function () { this._isLogin(); if (this.data.isLogin) { wx.navigateTo({ url: './my_follow/index' }) } else { Toast.showToast('tip', '请登录后查看', () => { }); } }, _myOrder: function () { this._isLogin(); if (this.data.isLogin) { wx.navigateTo({ url: './my_order/index' }) } 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; } }, // @deprecated getUserProfile: function (e) { Toast.showToast2('loading'); // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认 // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗 wx.getUserProfile({ desc: '获取您的身份信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写 success: (res) => { console.log("getUserProfile", 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); debugger 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, sessionKey } = res.data.data; wx.setStorageSync('token', loginSessionKey) wx.setStorageSync('sessionKey', sessionKey) this.updateUserinfo(); } 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(); } }) }, _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); wx.setStorageSync('sessionKey', 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}` }) } else { Toast.showToast('tip', '请登录后查看', () => { }); } // url="./my_order/index?status={{item.tag}}&&id={{item.id}}" }, clickCell: function () { wx.navigateTo({ url: './my_contact/index', }) }, toPersonal() { wx.navigateTo({ url: '/pages/user/personal/personal', }) }, // newLogin() { // wx.login({ // success: async res => { // let { // code // } = res; // if (code) { // const result = await wxUserLogin(code) // console.log('result', result) // if (result.code === 0) { // const { // loginSessionKey, // sessionKey, // token // } = result.data // wx.setStorageSync('token', loginSessionKey) // wx.setStorageSync('sessionKey', sessionKey) // this.setData({ // isLogin: true // }) // this.updateUserinfo(); // } // } // } // }) // }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: async function () { app.autoLogin((data) => { console.log('autoLogin', data) this.setData({ userInfo: data, isLogin: true }) }) }, async updateUserinfo() { const sessionKey = wx.getStorageSync('sessionKey') // console.log('sessionKey', sessionKey) if (sessionKey) { const res = await getWxUserInfo(sessionKey) const userInfo = wx.getStorageInfoSync('userInfo') if (res.data) { const mergeObj = { ...userInfo, ...res.data } console.log('userInfo-mergeObj', mergeObj) wx.setStorageSync('userInfo', mergeObj) this.setData({ userInfo: mergeObj }) } } }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })