關(guān)于小程序獲取微信用戶信息:
1、open-type=“getUserInfo”在2021年4月13日停用
2、wx.getUserInfo 在 2021年4月28日停用
3、wx.getUserProfile 在2022年11月8日停用
以下是使用open-type=“chooseAvatar” 的uniapp寫法哦:
一、獲取微信頭像
效果圖:
html:文章來源:http://www.zghlxwxcb.cn/news/detail-503194.html
<button class="button" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
<image class="avatar" :src="infoObj.avatar == '' ? '@/static/images/xishi.jpg' : infoObj.avatar"></image>
</button>
js:
onChooseAvatar(e) {
const avatarUrl = e.detail.avatarUrl // 此處是頭像的臨時路徑
// 需要后端配合,獲得線上的地址
uploadFile(avatarUrl).then(res => {
console.log('上傳圖片成功')
const result = JSON.parse(res.data)
this.editInfoObj.avatar = 'http://' + result.data
// 調(diào)用修改頭像的方法
this.editUserInfo(this.editInfoObj)
}).catch(err => {
console.log('上傳圖片失敗', err)
})
},
因為上傳文件,項目有多個使用場景,所以簡單封裝了,
uploadFile的封裝:
import { baseURL } from '@/utils/request/env'
import { md5Libs } from '@/utils/request/md5'
/**
* 上傳文件
* @param {string} filePath // 臨時目錄
*/
export function uploadFile(filePath) {
// 添加token
let token = uni.getStorageSync('token')
// (是封裝MD5加密時的參數(shù)格式,可忽略~)
let options = {
data: {},
url: baseURL + 'public/uploadFile'
}
// 添加簽名
let sign = md5Libs(options)
let optionsData = Object.assign({}, options.data)
optionsData.sign = sign
return new Promise((resolved, rejected) => {
uni.uploadFile({
url: options.url,
filePath: filePath,
name: 'file',
header: {
'content-type' : 'multipart/form-data',
'token': token
},
formData: optionsData,
success: (res) => {
if (res.statusCode == 200) {
resolved(res)
} else {
rejected(res)
}
},
fail: (err) => {
rejected(err)
}
})
})
}
二、獲取微信昵稱
效果圖:
html:
<input type="nickname" class="input" :value="editInfoObj.nickName" @blur="bindblur"/>
js:文章來源地址http://www.zghlxwxcb.cn/news/detail-503194.html
bindblur(e) {
this.editInfoObj.nickName = e.detail.value // 獲取微信昵稱
}
到了這里,關(guān)于uniapp 微信小程序 open-type=“chooseAvatar“ 獲取微信用戶信息的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!