当前位置:网站首页>小程序:如何在插件中获取用户手机号
小程序:如何在插件中获取用户手机号
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)的效果
点击后也可以调起授权手机号的弹窗啦~
边栏推荐
- Decision tree classification and examples
- SCUACM22暑假集训前劝退赛部分题解
- Use of packet capturing tool Fiddler: simulating speed limit test process in weak network environment
- vim的安装以及常用命令
- [jvm learning] virtual machine stack
- IGMP message (tcp/ip details volume 1/ Volume 2)
- < 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(三)
- Getting started with JMeter
- [OWT server] customized script 3: server construction
- jupyter notebook新環境快捷方式
猜你喜欢
![[jvm learning] types of GC and allocation process of objects on JVM heap](/img/f3/8141be7bcbf79d9c874f9cc09683c8.jpg)
[jvm learning] types of GC and allocation process of objects on JVM heap

2021-06-20

How to use grafana to easily realize OVL data visualization

Interface.

IDEA中文棱形乱码错误解决方法--控制台中文输出棱形乱码

Multi thread knowledge induction

< 山东大学软件学院项目实训 > 渲染引擎系统——点云处理(十)

Jupyter notebook new environment shortcut

RARP summary (tcp/ip explanation volume 1/2)

< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(六)
随机推荐
< 山东大学软件学院项目实训 > 渲染引擎系统——辐射预计算(九)
< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(七)
Remote control of other computers -- detailed tutorial
vim的安装以及常用命令
Classic case of solidity - Smart games
How to analyze the running time and CPU utilization of Go programs?
Use of thread communication
< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(三)
Escape rules and examples of go
Decision tree classification and examples
国产CPLD中AG1280Q48进行开发的实践之一:思路分析
RARP总结(TCP/IP详解卷1/2)
华为设备配置CE双归属
Using the CSDN markdown editor
Why doesn't Alibaba recommend MySQL use the text type?
【周赛复盘】LeetCode第80场双周赛
Project training of Software College of Shandong University rendering engine system basic renderer (V)
Codeworks round 797 (Div. 3, cf1690)
UE4 常用类型转换
Multi thread knowledge induction