最近在做小程序的登录,需要同时获取用户手机号和头像昵称等信息,但是小程序又不支持单个接口同时获取两种数据,因此想到自定义一个弹窗,通过弹窗按钮触发获取手机号事件。记录一下。

具体代码如下:

业务代码中:

  在业务代码中引入dialog组件即可

  <dialog visible="{{dialogVisible}}" showFooter="{{footerVisible}}" title="测试一下">
    <view class='dialog-body' slot="dialog-body">
      <view class='dialog-content'>申请获取你微信绑定的手机号</view>
      <view class='dialog-footer' slot="dialog-footer">
        <button class='cancel-btn' bindtap="close">取消</button>
        <button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber" class='confirm-btn'>授权</button>
      </view>
    </view>
  </dialog>

dialog组件:

component下面新建dialog。注意是 component 不是 page ,因为要作为组件引入到页面中

  dialog.wxml:

  需要传入四个属性

    visible:是否显示弹窗

    title :标题

    showClose:是否显示右上角关闭按钮

    showFooter:是否显示底部按钮

<!--components/dialog/dialog.wxml-->
<view class='dialog-custom' wx:if="{{visible}}">
  <view class='dialog-mask' bindtap="clickMask"></view>
    <view class="dialog-main">
      <view class="dialog-container">
        <view class='dialog-container__title' wx:if="{{title.length>0}}">
          <view class='title-label'>{{ title }}</view>
          <view class='title-icon'>
            <image wx:if="{{showClose}}" bindtap='close' src='/images/close-btn.png'></image>
          </view>
        </view>
      <view class='dialog-container__body'>
        <slot name="dialog-body"></slot>
      </view>
      <view class='dialog-container__footer' wx:if="{{showFooter}}">
        <view class='dialog-container__footer__cancel' bindtap="close">取消</view>
        <view class='dialog-container__footer__confirm' bindtap='confirm'>确定</view>
      </view>
    </view>
  </view>
</view>
 
dialog.js
  

Component({
/**
* 组件的属性列表
*/
properties: {
  visible: {
    type: Boolean,
    value: false
  },
  width: {
    type: Number,
    value: 85
  },
  position: {
    type: String,
    value: 'center'
  },
  title: {
    type: String,
    value: ''
  },
  showClose: {
    type: Boolean,
    value: true
  },
  showFooter: {
    type: Boolean,
    value: false
  },
},
/**
* 组件的初始数据
*/
data: {
 
},
options:{
  multipleSlots: true
},
/**
* 组件的方法列表
*/
methods: {
  clickMask() {
    this.setData({ visible: false });
  },
  close(){
    this.setData({ visible: false });
  },
  cancel() {
    this.setData({ visible: false });
    this.triggerEvent('cancel');
  },
  confirm() {
    this.setData({ visible: false });
    this.triggerEvent('confirm');
  }
}
})
 
dialog.json:声明是组件就行
 
{
  "component": true,
  "usingComponents": {}
}
 
 
dialog.wxss
  css可以根据自己喜好的样式调整,注意mask遮罩层的z-index高一点,确保在最上层
/* components/dialog/dialog.wxss */
.dialog-custom {
  width: 100vw;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  z-index: 9999;
}
.dialog-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10000;
  width: 100vw;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
}
.dialog-main {
  position: fixed;
  z-index: 10001;
  top: 50%;
  left: 0;
  right: 0;
  width: 85vw;
  height: auto;
  margin: auto;
  transform: translateY(-50%);
}
.dialog-container {
  margin: 0 auto;
  background: #fff;
  z-index: 10001;
  border-radius: 3px;
  box-sizing: border-box;
  padding: 40rpx;
}
.dialog-container__title {
  width: 100%;
  height: 50rpx;
  line-height: 50rpx;
  margin-bottom: 20rpx;
  position: relative;
}
.dialog-container__title .title-label{
  display: inline-block;
  width: 100%;
  height: 50rpx;
  line-height: 50rpx;
  font-size: 36rpx;
  color: #000;
  text-align: center;
}
.dialog-container__title .title-icon{
  width: 34rpx;
  height: 50rpx;
  position: absolute;
  top: 0;
  right: 0;
}
.dialog-container__title .title-icon image{
  width: 34rpx;
  height: 34rpx;
}
.dialog-container__body {
  padding-top: 10rpx;
  font-size: 32rpx;
  line-height: 50rpx;
}
.dialog-container__footer {
  height: 76rpx;
  line-height: 76rpx;
  font-size: 32rpx;
  text-align: center;
  border-top: 1px solid #f1f1f1;
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
}
.dialog-container__footer .dialog-container__footer__cancel {
  width: 50%;
  color: #999;
  display: inline-block;
}
.dialog-container__footer .dialog-container__footer__cancel::after{
  position: absolute;
  right: 50%;
  bottom: 0;
  content: '';
  width: 2rpx;
  height: 76rpx;
  background: #f1f1f1;
}
.dialog-container__footer .dialog-container__footer__confirm {
  color: #3B98F7;
  width: 50%;
  display: inline-block;
  text-align: center;
}
 
 
 
/* components/dialog/dialog.wxss */
.dialog-custom {
width: 100vw;
height: 100%;
position: absolute;
;
;
;
}
.dialog-mask {
position: fixed;
;
;
;
;
;
width: 100vw;
height: 100%;
, , , 0.3);
}
.dialog-main {
position: fixed;
;
top: 50%;
;
;
width: 85vw;
height: auto;
margin: auto;
transform: translateY(-50%);
}
.dialog-container {
auto;
background: #fff;
;
border-radius: 3px;
box-sizing: border-box;
rpx;
}
.dialog-container__title {
width: 100%;
rpx;
rpx;
rpx;
position: relative;
}
.dialog-container__title .title-label{
display: inline-block;
width: 100%;
rpx;
rpx;
rpx;
color: #000;
text-align: center;
}
.dialog-container__title .title-icon{
rpx;
rpx;
position: absolute;
;
;
}
.dialog-container__title .title-icon image{
rpx;
rpx;
}
.dialog-container__body {
rpx;
rpx;
rpx;
}
.dialog-container__footer {
rpx;
rpx;
rpx;
text-align: center;
border-top: 1px solid #f1f1f1;
position: absolute;
;
;
;
}
.dialog-container__footer .dialog-container__footer__cancel {
width: 50%;
color: #999;
display: inline-block;
}
.dialog-container__footer .dialog-container__footer__cancel::after{
position: absolute;
right: 50%;
;
content: '';
rpx;
rpx;
background: #f1f1f1;
}
.dialog-container__footer .dialog-container__footer__confirm {
color: #3B98F7;
width: 50%;
display: inline-block;
text-align: center;
}

微信小程序封装自定义弹窗的更多相关文章

  1. 微信小程序封装storage(含错误处理)

    这次给你们安利的是微信小程序封装storage,先说下微信官方的 wx.getStorage({ key:"", success: function (res) { }, fail ...

  2. 微信小程序 修改(自定义) 单选/复选按钮样式 checkbox/radio样式自定义

    参考文章: 微信小程序 修改(自定义) 单选/复选按钮样式 checkbox/radio样式自定义

  3. 微信小程序中自定义modal

    微信小程序中自定义modal .wxml <modal hidden="{{hidden}}" title="这里是title" confirm-text ...

  4. 微信小程序之自定义toast弹窗

    微信小程序里面的自带弹窗icon只有两种,success和loading.有时候用户输入错误的时候想加入一个提醒图标,也可以使用wx.showToast中的image来添加图片达到使用自定义图标的目的 ...

  5. 【微信小程序】自定义模态框实例

    原文链接:https://mp.weixin.qq.com/s/23wPVFUGY-lsTiQBtUdhXA 1 概述 由于官方API提供的显示模态弹窗,只能简单地显示文字内容,不能对对话框内容进行自 ...

  6. 微信小程序之自定义select下拉选项框组件

    知识点:组件,animation,获取当前点击元素的索引与内容 微信小程序中没有select下拉选项框,所以只有自定义.自定义的话,可以选择模板的方式,也可以选择组件的方式来创建. 这次我选择了组件, ...

  7. 微信小程序之自定义组件

    在微信小程序项目中 肯定会存在很多功能和样式上相似的部分 面对这种情况 只是单单的ctrl+c ctrl+v 就显得很low了,而且也不便于后期维护那么这时候 使用微信小程序中的自定义组件功能就很合适 ...

  8. 微信小程序:自定义组件

    为什么要学习自定义组件? 1.用上我自己的单词abc,我希望在页面中展示椭圆形的图片, 2.打开手机淘宝,假如现在要做一个企业级项目,里面有很多页面,首页存在导航模块,点击天猫,进入第二个页面,而第二 ...

  9. 微信小程序之自定义组件的应用

    小程序支持自定义组件,下面是一个简单的购物车组件,实现的效果如图: 效果图 创建组件 在根目录创建components目录,然后创建计数组件 count 如图: 组件内容 <!--compone ...

随机推荐

  1. Vue. 之 npm安装 axios

    Vue. 之 npm安装 axios 使用指令: cnpm install axios 

  2. day18 10.使用ThreadLocal来解决问题

    ThreadLocal是一个容器/集合,是一个Map集合.不管你跨多少层,只要你是同一个线程就可以取出来.Service和Dao是同一个线程.Service第一次调用JdbcUtils.getConn ...

  3. Nginx 函数解析之ngx_http_get_forwarded_addr_internal

    static ngx_int_t ngx_http_get_forwarded_addr_internal(ngx_http_request_t *r, ngx_addr_t *addr, u_cha ...

  4. UvaLive1347

    Programming contests became so popular in the year 2397 that the governor of New Earck — the largest ...

  5. 【CODEVS】2833 奇怪的梦境

    2833 奇怪的梦境 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description Aiden陷入了一个奇怪的梦境:他被困在一个小房子中,墙上有很 ...

  6. idea 创建properties配置文件

    [转载]原文链接:https://blog.csdn.net/caoPengFlying/article/details/78660379 我们在j2ee当中,连接数据库的时候经常会用到propert ...

  7. Django项目:CRM(客户关系管理系统)--08--03PerfectCRM创建基本数据03

    如果感觉本章博客对您有帮助,请尽情打赏吧!

  8. LUOGU 1278 单词游戏

    题目描述 Io和Ao在玩一个单词游戏. 他们轮流说出一个仅包含元音字母的单词,并且后一个单词的第一个字母必须与前一个单词的最后一个字母一致. 游戏可以从任何一个单词开始. 任何单词禁止说两遍,游戏中只 ...

  9. poj 2406 Power Strings(KMP入门,next函数理解)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 37685   Accepted: 15590 D ...

  10. LintCode刷题笔记-- Edit distance

    标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...