duidui_mini_program/pages/main/main.js
2026-03-27 10:41:46 +08:00

155 lines
4.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// index.js
const Apps = getApp();
Page({
data: {
userInfo: {},
currentuserinfo: {},
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
},
// 事件处理函数
bindViewTap() {
this.getUserProfile();
},
onLoad() {
if (wx.getUserProfile) {
this.setData({
canIUseGetUserProfile: true
})
}
},
getUserProfile(e) {
const that = this;
// 推荐使用wx.getUserProfile获取用户信息开发者每次通过该接口获取用户个人信息均需用户确认开发者妥善保管用户快速填写的头像昵称避免重复弹窗
wx.getUserProfile({
desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
success: (res) => {
Apps.globalData.usersaveinfo = res.userInfo;
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
// 登录
wx.login({
success(res) {
if (res.code) {
//发起网络请求
wx.request({
url: 'https://ndnd.duiduiedu.com/addons/unishop/user/authSession',
data: {
code: res.code,
platform: 'MP-WEIXIN'
},
header: {
'content-type': 'application/x-www-form-urlencoded'
},
method: "GET",
datatype: 'json',
success: function (res) {
console.log(res.data)
Apps.globalData.wxopenid = res.data.data.openid,
Apps.globalData.userInfo = res.data.data.userInfo
}
})
} else {
console.log('登录失败!' + res.errMsg)
}
}
})
},
fail: (err) => {
// 没有允许定位权限
wx.showToast({
title: '您拒绝了微信授权,将无法正常使用微信小程序',
icon: 'none'
});
}
})
},
getPhoneNumber(e) {
console.log(Apps.globalData)
if (e.detail.errMsg == 'getPhoneNumber:ok') {
wx.request({
url: Apps.globalData.baseUrl + '/user/loginForWechatMini',
data: {
'encryptedData': e.detail.encryptedData,
'iv': e.detail.iv,
'session_key': Apps.globalData.userInfo.session_key,
'openid': Apps.globalData.userInfo.openid,
'avatar': Apps.globalData.usersaveinfo.avatarUrl,
'nickname': Apps.globalData.usersaveinfo.nickName
},
header: {
'content-type': 'application/json' // 默认值
},
method: "POST",
success(res) {
Apps.globalData.currentuserinfo = res.data.data
}
})
} else {
// 没有允许定位权限
wx.showToast({
title: '您拒绝了微信授权,将无法正常使用微信小程序',
icon: 'none'
});
}
},
getUserInfo(e) {
// 不推荐使用getUserInfo获取用户信息预计自2021年4月13日起getUserInfo将不再弹出弹窗并直接返回匿名的用户个人信息
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
})
},
getcureentuserinfo() {
console.log(Apps.globalData.currentuserinfo);
this.setData({
// currentuserinfo: Apps.globalData.currentuserinfo
currentuserinfo: wx.getStorageSync('userinfo')
})
}, leftright() {
wx.navigateTo({
url: '/pages/leftright/leftright',
})
}, swipperpics() {
wx.navigateTo({
url: '/pages/mine/mine',
})
},
tarbar() {
wx.navigateTo({
url: '/pages/tarbar/tarbar',
})
},
clickgrid() {
wx.navigateTo({
url: '/pages/grid/grid',
})
},
clickpics() {
wx.navigateTo({
url: '/pages/picsdisplay/picsdisplay',
})
}, swipecell() {
wx.navigateTo({
url: '/pages/swipecell/swipecell',
})
}
})