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>

你可能感兴趣的:(变成笔记,我的编程笔记)