• 微信小程序登录获取不到头像和昵称解决办法!


    微信小程序登录获取不到头像和昵称主要原因是:小程序wx.getUserProfile接口被收回!

    大家可以按照文档操作↓

    PS:

    针对小程序wx.getUserProfile接口将被收回后做出的授权调整

    小程序文档中提出的调整说明
    attachmentId-21993
    attachmentId-21994
    对于此次变化,现将小程序授权方式做以调整

    添加判断当前基础库是否支持头像昵称填写能力
    在根目录App.vue中加入判断基础库是否大于2.21.2版本(大于此版本支持头像/昵称填写能力)

    // #ifdef MP
    	const version = uni.getSystemInfoSync().SDKVersion
    	if (Routine.compareVersion(version, '2.21.2') >= 0) {
    		that.$Cache.set('MP_VERSION_ISNEW', true)
    	} else {
    		that.$Cache.set('MP_VERSION_ISNEW', false)
    	}
    // #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    Copy
    2.修改/pages/users/wechat_login.vue文件

    (1) 在data中加入基础库判断,决定授权逻辑

    mp_is_new: this.$Cache.get('MP_VERSION_ISNEW') || false
    
    • 1

    Copy
    (2)dom中新增逻辑判断
    4704f202211101621275814.png
    (3) methods中加入方法userLogin

    // 小程序 22.11.8日删除getUserProfile 接口获取用户昵称头像
    	userLogin() {
    		Routine.getCode()
    			.then(code => {
    				uni.showLoading({
    					title: '正在登录中'
    				});
    			    authLogin({
    					code,
    					spread_spid: app.globalData.spid,
    					spread_code: app.globalData.code
    				}).then(res => {
    					if (res.data.key !== undefined && res.data.key) {
    						uni.hideLoading();
    						this.authKey = res.data.key;
    						this.isPhoneBox = true;
    					} else {
    						uni.hideLoading();
    						let time = res.data.expires_time - this.$Cache.time();
    						this.$store.commit('LOGIN', {
    							token: res.data.token,
    							time: time
    						});
    						this.getUserInfo()
    					}
    	
    				})
    			})
    			.catch(err => {
    				console.log(err)
    			});
    	},
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    3.新增用户头像/昵称获取能力

    (1)调整pages/users/user_info.vue文件
    data中添加

    mp_is_new: this.$Cache.get('MP_VERSION_ISNEW') || false
    
    • 1

    (2)调整dom中
    7fe26202211101620461344.png

    (3)methods中加入方法

    onChooseAvatar(e) {
    	const { avatarUrl } = e.detail
    	this.$util.uploadImgs('upload/image', avatarUrl, (res) => {
    		this.userInfo.avatar = res.data.url
    	}, (err) => {
    		console.log(err)
    	})
    },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    这里有一个公共方法uploadImgs需要在/utils/util.js中添加

    uploadImgs(uploadUrl, filePath, successCallback, errorCallback) {
    	let that = this;
    	uni.uploadFile({
    		url: HTTP_REQUEST_URL + '/api/' + uploadUrl,
    		filePath: filePath,
    		fileType: 'image',
    		name: 'pics',
    		formData: {
    			'filename': 'pics'
    		},
    		header: {
    			// #ifdef MP
    			"Content-Type": "multipart/form-data",
    			// #endif
    			[TOKENNAME]: 'Bearer ' + store.state.app.token
    		},
    		success: (res) => {
    			uni.hideLoading();
    			if (res.statusCode == 403) {
    				that.Tips({
    					title: res.data
    				});
    			} else if (res.statusCode == 413) {
    				that.Tips({
    					title: '上传图片失败,请重新上传小尺寸图片'
    				});
    			} else {
    				let data = res.data ? JSON.parse(res.data) : {};
    				if (data.status == 200) {
    					successCallback && successCallback(data)
    					} else {
    					errorCallback && errorCallback(data);
    					that.Tips({
    						title: data.msg
    					});
    				}
    			}
    		},
    		fail: (err) => {
    			uni.hideLoading();
    				that.Tips({
    				title: '上传图片失败'
    			});
    		}
    	})
    },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
  • 相关阅读:
    PaddleSharp:跨越一年的版本更新与亮点
    Python面试必备一之迭代器、生成器、浅拷贝、深拷贝
    超级实用的程序员接单平台,看完少走几年弯路,强推第一个!
    【周末闲谈】谈谈数学转码这一年来的体会与反思
    编写高质量代码改善程序的157个建议:使用Dynamic来简化反射的实现
    项目自动化构建工具——make/Makefile
    现货白银交易时间笔记
    基于pytorch实现Resnet对本地数据集的训练
    JVM内存和垃圾回收-13.垃圾回收算法
    Docker NetWork (网络)
  • 原文地址:https://blog.csdn.net/withkai44/article/details/127878598