From cc88bc636a886b4e166f2fdf84aee34a77577eaa Mon Sep 17 00:00:00 2001 From: Miao_Spring <539943419@qq.com> Date: Fri, 10 Nov 2023 14:43:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=99=E4=BA=86SessionKey=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- miniprogram/pages/index/index.ts | 96 ++++++++++++++++++++++---------- 1 file changed, 67 insertions(+), 29 deletions(-) diff --git a/miniprogram/pages/index/index.ts b/miniprogram/pages/index/index.ts index 9016c2b..3f3bb46 100644 --- a/miniprogram/pages/index/index.ts +++ b/miniprogram/pages/index/index.ts @@ -3,6 +3,10 @@ const app = getApp() +// wx.showLoading({ +// title: '数据加载中...', +// mask: true +// }) // class stack{ // data=[] @@ -190,20 +194,24 @@ Page({ }, - user_session: "", + + sessionKey: "", userInfo: {}, hasUserInfo: false, canIUse: wx.canIUse('button.open-type.getUserInfo'), canIUseGetUserProfile: false, canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false }, - onLoad() { - // @ts-ignore - // console.log(this.data.userInfo); - // wx.login(success = code => { - // console.log('SUCCESS') - // console.log(code) - // }) + + // 启动时获取session + async onLoad() { + // wx.authorize() + // await this.updateSession() + // setInterval(()=>{ + // this.updateSession() + // },60*1000*5) + console.log(this.data.sessionKey) + }, //开始界面的按钮相关动画 @@ -462,7 +470,6 @@ Page({ wx.request({ url: 'http://127.0.0.1:5000/getLib', //仅为示例,并非真实的接口地址 data: { - }, method:"GET", header: { @@ -490,27 +497,58 @@ Page({ }) }, + //更新历史记录 updateHistory(){ - this.getCode().then(code => { - wx.request({ - url: 'http://127.0.0.1:5000/getHistory', //仅为示例,并非真实的接口地址 - data: { - userCode:code - }, - method:"POST", - header: { - 'content-type': 'application/json' // 默认值 - }, - success:res=> { - console.log(res) - var data = res.data - this.setData( - {"data_history" : data} - ) - }, - fail(res){ - console.log(res) - } + wx.request({ + url: 'http://127.0.0.1:5000/getHistory', //仅为示例,并非真实的接口地址 + data: { + userCode:this.data.sessionKey + }, + method:"POST", + header: { + 'content-type': 'application/json' // 默认值 + }, + success:res=> { + console.log(res) + var data = res.data + this.setData( + {"data_history" : data} + ) + }, + fail(res){ + console.log(res) + } + }) + }, + + // 更新session + updateSession(){ + return new Promise((resolve, reject)=>{ + this.getCode().then(code => { + wx.request({ + url: 'http://127.0.0.1:5000/updateSession', //仅为示例,并非真实的接口地址 + data: { + userCode:code + }, + method:"POST", + header: { + 'content-type': 'application/json' // 默认值 + }, + success:res=> { + console.log(res) + var data = res.data + this.setData( + {"sessionKey" : data} + ) + resolve() + // setTimeout(this.updateSession,1000*60*5) + }, + fail:res=>{ + console.log(res) + setTimeout(this.updateSession,1000) + reject() + } + }) }) }) },