uniapp获取微信小程序手机号码

<template>
	<view class="">
		<button type="default" open-type="getPhoneNumber" @getphonenumber="PhoneNumber">获取手机号</button>
		<button type="default" @click="getlogin">登录</button>

	</view>
</template>

<script>
	import WXBizDataCrypt from "../../static/commonjs/WXBizDataCrypt.js";
	let appId = "wx8a79f44b5c79a608";
	let sessionkey = "";
	let openid = "";
	let encry = "";
	let iv = "";
	export default {
		data() {
			return {
				title: 'Hello'
			}
		},
		onLoad() {

		},
		methods: {
			PhoneNumber(e){
				encry = e.detail.encryptedData;
				iv = e.detail.iv;
				uni.login({
				  provider: 'weixin',
				  success:res=>{
					 uniCloud.callFunction({
						 name:"getopenid",
						 data:{
							 code:res.code
						 }
				
					 }).then(res=>{						 
						 sessionkey = res.result.data.session_key;
						 openid = res.result.data.openid;
						 
						 var pc = new WXBizDataCrypt(appId, sessionkey)
						 
						 var data = pc.decryptData(encry , iv)
						 
						 console.log('解密后 data: ', data)
						 
					 })
					 
				  }
				
				})
				
			},
			getlogin(){
				uni.login({
				  provider: 'weixin',
				  success:res=>{
					 uniCloud.callFunction({
						 name:"getopenid",
						 data:{
							 code:res.code
						 }

					 }).then(res=>{
						 
						 sessionkey = res.result.data.session_key;
						 openid = res.result.data.openid;
						 this.PhoneNumber();
						 console.log(iv)
						 
					 })
					 
				  }

				})
				 

			}

		}
	}
</script>

<style>

</style>

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试
应支付0元
点击重新获取
扫码支付

支付成功即可阅读