微信小程序自定义授权弹框

article/2025/9/21 8:39:09

前言

最近微信获取用户信息的接口有调整,就是这货:wx.getUserInfo(OBJECT),文档描述如下:

 此接口有调整,使用该接口将不再出现授权弹窗,请使用 <button open-type="getUserInfo"></button> 引导用户主动进行授权操作1.当用户未授权过,调用该接口将直接报错2.当用户授权过,可以使用该接口获取用户信息

这就很蛋疼了,以前只需要弹框授权就行了,现在还必须要先按一个按钮,这样的话多了一个操作,多一个按钮感觉很别扭,放在页面上哪里都不合适,
看评论区有个图亮了:
image

解决方案

无缘无故在页面上多了一个按钮,只是为了引导用户授权,加在哪里都会觉得和页面内容格格不入。
那就弹一个框提示吧,虽然连续两个弹框也很怪,但是个人觉得比页面多一个按钮好一点。

微信自己定义的弹框交互并不适合授权这个场景,那就想到自定义一个弹框组件来解决。

实现

新建 components 目录文件如下:
image

dialog.json 文件内容:

{"component": true,  // 自定义组件必须先声明"usingComponents": {}
}

dialog.js 文件内容:

Component({options: {multipleSlots: true // 在组件定义时的选项中启用多slot支持},/*** 组件的属性列表*/properties: {// 弹窗标题title: {          type: String,     value: '标题' // 默认值},// 弹窗内容content :{type : String ,value : '弹窗内容'},// 弹窗确认按钮文字confirmText :{type : String ,value : '确定'} },/*** 组件内私有数据*/data: {// 弹窗显示控制isShow:false},/*** 组件的公有方法列表*/methods: {//隐藏弹框hideDialog(){this.setData({isShow: !this.data.isShow})},//展示弹框showDialog(){this.setData({isShow: !this.data.isShow})},/*** triggerEvent 组件之间通信*/confirmEvent(){this.triggerEvent("confirmEvent");},bindGetUserInfo(){this.triggerEvent("bindGetUserInfo");}}
})

dialog.wxml 文件内容:

<view class='dialog-container' hidden="{{!isShow}}"><view class='dialog-mask'></view><view class='dialog-info'><view class='dialog-title'>{{ title }}</view><view class='dialog-content'>{{ content }}</view><view class='dialog-footer'><button class='dialog-btn' open-type="getUserInfo" bindgetuserinfo='bindGetUserInfo' catchtap='confirmEvent'>{{ confirmText }}</button></view></view>
</view>

dialog.wxss 文件内容:

.dialog-mask{position: fixed;z-index: 1000;top: 0;right: 0;left: 0;bottom: 0;background: rgba(0, 0, 0, 0.3);
}
.dialog-info{position: fixed;z-index: 5000;width: 80%;max-width: 600rpx;top: 50%;left: 50%;-webkit-transform: translate(-50%, -50%);transform: translate(-50%, -50%);background-color: #FFFFFF;text-align: center;border-radius: 3px;overflow: hidden;
}
.dialog-title{font-size: 36rpx;padding: 30rpx 30rpx 10rpx;
}
.dialog-content{padding: 10rpx 30rpx 20rpx;min-height: 80rpx;font-size: 32rpx;line-height: 1.3;word-wrap: break-word;word-break: break-all;color: #999999;
}
.dialog-footer{display: flex;align-items: center;position: relative;line-height: 90rpx;font-size: 34rpx;
}
.dialog-btn{display: block;-webkit-flex: 1;flex: 1;position: relative;color: #3CC51F;
}

在首页或者我的页面进行授权检测:
首先还是要在 json 文件进行声明
index.json:

{"usingComponents": {"dialog": "/components/dialog/dialog"}
}

index.wxml:

<dialog id='dialog' title='登录提示' content='小程序需要您的授权才能提供更好的服务哦' confirmText='知道了'bind:confirmEvent='confirmEvent'bind:bindGetUserInfo='bindGetUserInfo'>
</dialog>

index.js:

onReady: function () {//获得dialog组件this.dialog = this.selectComponent("#dialog");
},showDialog: function(){this.dialog.showDialog();
},confirmEvent: function(){this.dialog.hideDialog();
},bindGetUserInfo: function() {// 用户点击授权后,这里可以做一些登陆操作this.login();
},

效果

image

总结

参考:https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/


http://chatgpt.dhexx.cn/article/lqnpQ1oT.shtml

相关文章

微信小程序组件 - 中间底部弹出输入弹框

GitHub Demo 地址: jh-weapp-demo 实现一些常用效果、封装通用组件和工具类 小程序码 一、 jh-input-alert 中间输入弹框&#xff0c;可设置最大输入长度&#xff0c;单行多行显示 单行 <jh-input-alert title输入框标题 placeholder请输入 maxlength10 bind:cancelcancel …

微信小程序展示弹窗的几种方式

小程序中展示弹窗有四种方式&#xff1a;showToast、showModal、showLoading、showActionSheet 官方文档链接 效果图 wxml <!-- 1.消息提示框 --> <button size"mini" bindtap"handleShowToast">ShowToast</button><!-- 2.模态对话…

微信小程序——小程序自己的页面弹框

微信小程序——小程序自己的页面弹框 1. 页面样式: 2.代码块 在这里插入代码片 &#xff08;一&#xff09;.wxml文件中给一个触发事件 <image src"../../img/icon-delete.png" bindtapdeleteCar data-id{{car.platecard}} class"icon"></imag…

微信小程序底部弹出框

微信小程序的底部弹出框 wxml <!-- 弹出框 start --> <view class"dialog_screen" bindtap"hideModal" wx:if"{{showModalStatus}}"></view> <view animation"{{animationData}}" class"dialog_attr_bo…

【微信小程序】小程序中的各种弹窗API

前言&#xff1a;小程序中提供了很多种快捷方便的弹窗API供开发者使用&#xff0c;例如wx.showToast&#xff0c;wx.showModal&#xff0c;wx.showActionSheet&#xff0c;wx.showLoading还有wxml中的loading标签。 一、直接上代码 <!-- test.wxml --> <view class&q…

小程序带输入框的弹窗

老规矩&#xff0c;直接上代码 <block wx:if"{{isShowConfirm}}"><view classtoast-box><view classtoastbg></view><view classshowToast><view classtoast-title><text>确认支付</text></view><view cla…

微信小程序 自定义弹框组件

话不多说直接上代码 目录 1、wxml 2、js 3、wxss 4、json 5、效果展示&#xff08;具体内容可以自定义&#xff09; 1、wxml <!--pages/components/confirmBox/confirmBox.wxml--> <wxs src"../../../filter/urlFilter.wxs" module"filter"…

微信小程序——点击某个按钮实现下面弹窗的出现

效果图:(真的是图) wxml(代码里面有解释&#xff09; <button type"primary" bindtap"Popup">点我</button> <!--点击后灰色背景的设置--> <view wx:if"{{now_state}}" class"background" bindtap"hideM…

微信小程序实现底部向上弹框

本文转自 作者&#xff1a;smallzip 链接&#xff1a;https://www.jianshu.com/p/bd9a4f4b8e6a 著作权归作者所有。商业转载请联系作者获得授权&#xff0c;非商业转载请注明出处。 效果 小程序自定义底部弹出modal框组件&#xff0c;仿照小程序sheet-action的效果&#xff0…

uniapp微信小程序的各种弹框提示(轻提示)

您直接复制粘贴即可使用不需要做特殊的处理。 如您满意请给莫成尘点个Fabulous 1&#xff1a; 纯文字提示框 uni.showToast({title: 只有文字弹窗,icon: none, //如果要纯文本&#xff0c;不要icon&#xff0c;将值设为noneduration: 2000 //持续时间为 2秒 }) 2&…

小程序实现弹出输入框

1.微信自带组件 样式&#xff1a; wxml <view class"close" bindtap"close">拒绝</view>js Page({//拒绝close(e) {wx.showModal({editable:true,//显示输入框placeholderText:输入拒绝原因,//显示输入框提示信息success: res > {if (re…

微信小程序提示弹窗大全

wx.showToast(OBJECT) 显示消息提示框 OBJECT参数说明&#xff1a; 示例代码&#xff1a; ? 1 2 3 4 5 wx.showToast({ title: 成功 , icon: success , duration: 2000 }) wx.hideToast() 隐藏消息提示框 ? 1 2 3 4 5 6 7 8 9 wx.showToast({ title: 加载中 , icon: lo…

微信小程序 四种弹窗方式

微信小程序弹窗 一、wx.showToast(Object object)二 、wx.showModal(Object object)三、wx.showLoading(Object object)四、wx.showActionSheet(Object object)五、官方文档 一、wx.showToast(Object object) 显示消息提示框 wx.showToast({title: 内容, //提示的内容duration…

微信小程序弹出框详解

[html] view plain copy <span style"font-family:Comic Sans MS;font-size:18px;color:#333333;"><view class"container" class"zn-uploadimg"> <button type"primary"bindtap"showok">消息提示…

微信小程序几种常用弹窗提示

第一种&#xff1a;弹出提示框&#xff0c;可以选择确定或者取消。 代码&#xff1a;wx.showModal({title: 提示,content: 这是一个模态弹窗,success: function (res) {if (res.confirm) {//这里是点击了确定以后console.log(用户点击确定)} else {//这里是点击了取消以后conso…

微信小程序几种常用弹窗提示方法

1.提示文字 可以设置显示时间&#xff08;仅提示时使用&#xff09;duration设置时间 不显示icon&#xff0c;此时 title文字最多可显示两行 也可以显示icon&#xff0c;显示icon文字最多显示 7 个汉字长度 icon常用的有&#xff1a;success、error、loading wx.showToast(…

微信小程序展示弹窗的方式

微信小程序中展示弹窗有四种方式&#xff1a;wx.showToast、wx.showModal、wx.showLoading、wx.showActionSheet 具体参数可参照微信开发文档 1. wx.showToast <!-- index.wxml --> <!-- wx.showToast(消息提示框) --> <button bindtap"handleShowToast&…

微信小程序之弹出框

微信小程序中toast消息提示框只有两种显示的效果&#xff0c;就是成功和加载&#xff0c;使用wx.showToast(OBJECT)。 看下有关参数说明&#xff1a; 代码很简单&#xff1a; wx.showToast({title: 成功,icon: succes,duration: 1000,mask:true}) mask属性好像并没有起作用。有…

微信小程序弹窗

微信小程序自定义底部、顶部、中间、左边、右边弹窗 简单的微信小程序弹窗功能&#xff0c;具体实现过程&#xff0c;请浏览代码。 顶部弹出窗图例&#xff1a; 中间弹出窗图例&#xff1a; 底部弹出窗图例&#xff1a; 左边弹出窗图例&#xff1a; 右边弹出窗图例&#x…

微信小程序弹窗提示怎么写

第一种&#xff1a;弹出提示框&#xff0c;可以选择确定或者取消。 代码&#xff1a; wx.showModal({ title: 提示, content: 这是一个模态弹窗, success: function (res) { if (res.confirm) {//这里是点击了确定以后 console.log(用户点击…