123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590 |
- // pages/user/index.js
- const {
- request,
- serverName
- } = require('../../utils/services');
- const {
- Toast
- } = require('../../utils/util.js');
- var app = getApp();
- import {
- getWxUserInfo
- } 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) {
- // this.isLongPolling()
- // this.newLogin();
- },
- 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;
- 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;
- }
- } 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("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);
- 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: res => {
- // let {
- // code
- // } = res;
- // console.log(789, res);
- // if (code) {
- // wx.request({
- // url: serverName + '/wx/api/user/getLoginSessionKey',
- // data: {
- // 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();
- // },
- // })
- // }
- // }
- // })
- // },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: async function () {
- let {
- avatar = ""
- } = this.data;
- const userInfo = wx.getStorageSync('userInfo')
- const sessionKey = wx.getStorageSync('sessionKey')
- let avatarUrl = userInfo ? userInfo.avatarUrl : "";
- let avatarName = userInfo ? userInfo.nickName : "";
- console.log('sessionKey', sessionKey)
- if (sessionKey) {
- this.updateUserinfo();
- }
- this.setData({
- avatarUrl: avatarUrl || avatar,
- avatarName: avatarName || '已登录'
- })
- this._isLogin()
- if (userInfo) {
- this.setData({
- userInfo: userInfo,
- })
- }
- },
- 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)
- }
- }
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
- }
- })
|