uniapp同时开发个人小程序与app遇到的坑

  1. 个人开发者不能注册微信开放平台,因为需要企业资质,故无法将自己的app与小程序关联起来
  2. uniapp的uni.login接口在小程序平台下与app平台下获取到的值不同,app平台可省去GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code获取openid这一步
 	uni.login({
		provider: 'weixin',
		success(loginRes) {
			console.log(loginRes)
		},
		fail(err) {
			console.log(err)
		}
	});
小程序下:
{
	errMsg: "login:ok", 
	code: "033ibAS71sRcjL1jkvT71VJnS71ibASG"
}
//app下:
{
	"authResult": {
		"access_token": "27_VqjGU4AKBNb-ypQ5t70uK_KJ5rWenB5yeLM5l7roeszJ6v_Fj-SRvveUXNrCf68GbjNPktTAd9Si8Wj2zbuE8hk0IXQiV6bSrgsVUoTpQ",
		"expires_in": 7200,
		"refresh_token": "27_Boa4BhhN16CR--ahHQvmemNmsg746xq64rh7LcMQTnGEmZKkrt5-7NxcEg9oMztgi9DKPCbto_bEdrL3uVtSb5iciGONnaybEeAF_u3C8",
		"openid": "oRrdQt7Z35cgnOaoNKXEz9I154",
		"scope": "snsapi_userinfo",
		"unionid": "oU5Yyt01ivWsHsTYgiE_aTUeQSG4"
	},
	"errMsg": "login:ok"
}

只能获取到code,需自己请求GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code来换取openid

  1. 不同应用的下,同一个用户的openid不相同。注册了微信开放者平台后,将应用关联,此时app与小程序获取的unionid相同,可用于标识同一用户。
  2. 小程序获取用户信息通过引导用户主动点击下方格式的button,即可在getUserInfo的$event中拿到用户相关信息,并结合通过code所换取的opedid,进行用户信息保存,具体看代码。官网地址https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserInfo.html
<button class="cu-btn bg-green margin-center" open-type="getUserInfo" @getuserinfo="userRegister">去探索</button>

userRegister(e) {
	console.log(e)
	this.userInfo = e.detail.userInfo
	this.$Req.post(this.$Url.users.add,[{
		avatarUrl: this.userInfo.avatarUrl, //头像
		nickName: this.userInfo.nickName, //昵称
		gender: this.userInfo.gender, // 性别
		openid: this.openid, //login接口获取code,code去微信服务器换取openid
		country:this.userInfo.country,//国家
		province: this.userInfo.province, //省份
		city: this.userInfo.city, // 城市
		language:this.userInfo.country,
	}]).then((res) => {
		console.log(res) 
		if(res.code == 0) {
		this.loginModel = false
		} else {
			this.$api.msg('服务器异常,请稍后再试')
		}
	})
}

  1. app平台通过请求接口来获取以上信息,且字段稍有不同,微信官网链接https://developers.weixin.qq.com/doc/oplatform/Mobile_App/WeChat_Login/Authorized_API_call_UnionID.html
GET https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID
{
  "openid": "OPENID",
  "nickname": "NICKNAME",
  "sex": 1,
  "province": "PROVINCE",
  "city": "CITY",
  "country": "COUNTRY",
  "headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
  "privilege": ["PRIVILEGE1", "PRIVILEGE2"],
  "unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL"
}
  1. 授权弹窗不用自己写,为微信自带组件,触发授权时弹窗会自动弹出,若已经授权过,弹窗不会弹出,但仍能获取到数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值