微信小程序获取手机号
详细代码
<t-button size="large" theme="primary" variant="outline" data-type="hasCancelBtn" bind:tap="showDialog" block style="display: none;">开放能力按钮
</t-button>
<t-dialog id="t-dialog" />
// 获取手机号showDialog() {let that = thisinterface PhoneNumberDetail {iv: string; // 解密向量encryptedData: string; // 加密数据errMsg: string; // 错误信息}const dialogConfig = {context: this,title: '小程序',content: '申请获取并验证你的手机号。',cancelBtn: '取消',confirmBtn: {openType: 'getPhoneNumber',content: '获取手机号',bindgetphonenumber({ detail }: { detail: PhoneNumberDetail }) {console.log(detail,'555')that.setData({encryptedData: detail.encryptedData,iv: detail.iv})if (detail.errMsg.includes('fail')) {return false; // 不关闭弹窗}return true; // 关闭弹窗},},};Dialog.confirm(dialogConfig).then(() => {console.log('点击确定');}).catch(() => {console.log('点击取消');});},
// 页面初始加载onLoad() {let iv = this.data.ivif (!iv) {this.showDialog()}},
// 页面的初始数据data: {encryptedData: '',//手机号参数iv: '',//手机号参数},