uni-app小程序项目实战获取手机号

642 阅读1分钟

获取手机号码

新手摸索微信小程序获取手机号全过程,如何获取手机号,直接上代码全篇无废话 用手就能会,简单实用

1.html 文件内容

<template>
    <view>
        <button class="btn" open-type="getPhoneNumber" type="primary" @getphonenumber="getPhoneNumber">微信账号快速授权</button>
    </view>
</template>

2.js

getPhoneNumber(e){
 console.log(e)
 wx.checkSession({
	success:function(){  //进行请求服务端解密手机号
            this.deciyption(e.detail.code);
	}
})
},
//解密方法
deciyption(code){
wx.request({
		      url: '解密接口地址',
		      method: 'GET',
		      data: {
				appid:'appid',
				code: code,
		 
		      },
		      header: {
		        'content-type': 'application/json;charset=UTF-8'
		      },
		      success: function(res) {
		        let data = res.data
		        if (data.resultCode == 'success') {
		            wx.setStorageSync('userTel', data.data.phoneNumber);//存储解密后的用户手机号
		        }else{
		            wx.showToast({
		                title: '获取信息失败请重新授权',
		                icon: 'none'
		            })
		           
		        }    
		      },
		      fail:function(res) {
		        wx.showToast({
		            title: '获取失败请重新授权',
		            icon: 'none'
		        })
		        
		      }
		   })
}