diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..802ccbe Binary files /dev/null and b/.DS_Store differ diff --git a/api/request.js b/api/request.js index 6106bf7..44820f7 100644 --- a/api/request.js +++ b/api/request.js @@ -1,5 +1,5 @@ -// const baseUrl = 'https://mini-chat.1024tool.vip/api/'; -const baseUrl = 'https://dsjhd9s.tbmw.cn/api/' +const baseUrl = 'https://mini-chat.1024tool.vip/api/'; +// const baseUrl = 'https://dsjhd9s.tbmw.cn/api/' function request(url, method = 'GET', data = {}) { const header = { diff --git a/pages/.DS_Store b/pages/.DS_Store new file mode 100644 index 0000000..f2dbd2e Binary files /dev/null and b/pages/.DS_Store differ diff --git a/pages/contact/index.js b/pages/contact/index.js index a5304a3..d46993c 100644 --- a/pages/contact/index.js +++ b/pages/contact/index.js @@ -37,7 +37,7 @@ Page({ }); console.log('contact page onLoad, options:', options); - // this.getMessages() + // 检查是否已有用户信息 if (wx.getStorageSync('user_info')) { this.setData({ showGetUser: false, @@ -47,16 +47,9 @@ Page({ // start polling when page loads and user is present this.startPolling(); } else { - this.setData({ - showGetUser: true, - }); + // 直接进行静默登录,不显示授权弹窗 + this.silentLogin(); } - - // this.setData({ - // appid: accountInfo.miniProgram.appId, - // }); - - // console.log('appId',accountInfo.miniProgram); }, // onShow() { // this.getMessages() @@ -139,43 +132,43 @@ Page({ that.setData({ loadingMore: false }); }); }, - requestUserProfile(e) { - console.log('user', e.detail); - const user = e.detail; + // 静默登录方法,只需要appid和js_code + silentLogin() { const that = this; - // wx.setStorageSync('user_info', e.detail.userInfo); const accountInfo = wx.getAccountInfoSync(); wx.login({ success: function (res) { request('wechat/miniprogram/login', 'post', { "app_id": accountInfo.miniProgram.appId, - "js_code": res.code, - "encrypted_data": user.encryptedData, - "iv": user.iv, - "raw_data": user.rawData, - "signature": user.signature + "js_code": res.code }).then(resp => { wx.setStorageSync('user_info', resp); - // wx.setStorageSync('open_id', resp.openid); that.setData({ showGetUser: false, userInfo: resp }); request('app/user/create', 'post', { "app_id": accountInfo.miniProgram.appId, - "user_avatar": resp.user_avatar, + "user_avatar": resp.user_avatar || '/static/user.png', "user_id": resp.openid, - "user_name": resp.user_name + "user_name": resp.user_name || '微信用户' }).then(resp => { that.getMessages() // start polling once user info exists that.startPolling(); - + }); + }).catch(err => { + console.error('静默登录失败:', err); + // 如果静默登录失败,可以选择显示授权弹窗或其他处理 + that.setData({ + showGetUser: true }); }); }, + fail: function(err) { + console.error('wx.login失败:', err); + } }); - }, // Start polling messages every 1 second. Ensures only one interval exists. @@ -234,11 +227,7 @@ Page({ } }, - dismissGetUser() { - this.setData({ - showGetUser: false - }); - }, + onReady() { // 初始化欢迎消息,带时间分割线 diff --git a/pages/contact/index.wxml b/pages/contact/index.wxml index 738b28a..8a4d1d1 100644 --- a/pages/contact/index.wxml +++ b/pages/contact/index.wxml @@ -39,17 +39,12 @@ - + - 授权获取您的用户信息 - 授权后可用于显示头像、昵称等,提升聊天体验。 - - - - - + 正在登录... + 请稍候,正在为您自动登录。