当前位置:网站首页>小程序:如何在插件中获取用户手机号
小程序:如何在插件中获取用户手机号
2022-06-12 15:49:00 【Lvan的前端生活】
背景
我们知道,小程序插件中是无法使用 open-type 相关 API 的,官方文档 也有详细说明
但是如果我们想做一个登录相关的插件,就是要获取用户手机号,该怎么做呢,可以用 引用小程序的自定义组件功能
引用小程序的自定义组件功能介绍
插件中的某一部分(不管是组件还是页面)都可以通过小程序传入一个组件来进行渲染。
实践
小程序端
引入插件
"plugins": {
"auth-login": {
"version": "dev",
"provider": "wxxxxxxxx",
"genericsImplementation": {
"auth-login-default": {
// 将这个组件(get-phone-number-btn)传给插件页(auth-login-default)进行渲染
"get-phone-number-btn": "components/get-phone-number-btn/index"
}
}
}
},
在小程序中按照这个路径 components/get-phone-number-btn/index ,新建一个组件
index.js
Component({
properties: {
show: {
type: Boolean,
value: false
},
phoneStyle: {
type: String,
value: ''
}
},
methods: {
getPhoneNumber (e) {
console.log('e', e)
this.triggerEvent('getPhoneNumber', e.detail)
}
}
})
index.wxml
<button wx:if="{
{ show }}" class="btn-com ok" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber" style="{
{
phoneStyle }}">微信授权登录</button>
index.json
{
"component": true,
"usingComponents": {
}
}
插件端
比如插件端有个页面,叫 auth-login-default,可以在 plugin.json 中查看。
plugin.json
{
"publicComponents": {
},
"pages": {
"auth-login-default": "pages/auth-login/index"
},
"main": "index.js"
}
"auth-login-default": "pages/auth-login/index" 这个页面中的代码如下:
index.wxml
<text>我是插件原本的代码</text>
// 我是小程序传进来的组件
<get-phone-number-btn show="{
{ loginCode && !token && checked }}" bind:getPhoneNumber="getPhoneNumber" phoneStyle="width: 100%;height: 88rpx;line-height: 88rpx;text-align: center;border-radius: 44rpx;border: none;font-size: 32rpx;border: none;outline: none;background: #02C160;color: #fff;" />
index.json
"componentGenerics": {
"get-phone-number-btn": true
}
index.js
Page({
async getPhoneNumber (e) {
// 获取到手机号啦
console.log(e.detail)
}
})
效果
最终,我们看到这个插件页(auth-login-default)的效果
点击后也可以调起授权手机号的弹窗啦~
边栏推荐
- Remote control of other computers -- detailed tutorial
- Servlet知识详解(2)
- Why doesn't Alibaba recommend MySQL use the text type?
- Raccourci vers le nouvel environnement du carnet de notes Jupiter
- ssm中的文件上传和下载
- [jvm learning] types of GC and allocation process of objects on JVM heap
- Data analysis | kmeans data analysis
- What is JUC in high concurrency programming
- 【光源实用案例】 UV-LED固化创新,让产线变得更丝滑
- < 山东大学软件学院项目实训 > 渲染引擎系统——点云处理(十)
猜你喜欢

Apache kylin Adventure

Great God cracked the AMD k6-2+ processor 22 years ago and opened the hidden 128KB L2 cache

jupyter notebook新環境快捷方式

C language partition bin file program

Five models of software testing

Idea Encyclopedia (Reprinted)

< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(三)

What is reflection-- The soul of frame design

Task output: dense snow ice city theme song 0612

Design concept of ORM framework
随机推荐
< 山东大学软件学院项目实训 > 渲染引擎系统——点云处理(十)
[untitled]
< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(七)
Use of multithreading
Multi thread knowledge induction
第一章 线性表
Data analysis | kmeans data analysis
What is JUC in high concurrency programming
2022.02.28 - SX11-05. The largest rectangle in the histogram
如何使用Grafana轻松实现OVL数据可视化
[OWT server] customized script 3: server construction
一步步创建包含自定义 Screen 的 ABAP 程序的详细步骤试读版
当编程纳入到高考。。。
Village to village communication (and collective search)
Find the number of cells (connectivity map, wide search, deep search)
Web UI automation test
作业提交说明 上传作业到网盘中
Apache kylin Adventure
Use and understanding of generics
记一篇IT培训日记067-好人感恩,坏人无错