uni-app基于原生input增强选择picker插件 可用于地图定位选位置 页面跳转选数据

随着技术的发展,开发的复杂度也越来越高,传统开发方式将一个系统做成了整块应用,经常出现的情况就是一个小小的改动或者一个小功能的增加可能会引起整体逻辑的修改,造成牵一发而动全身。通过组件化开发,可以有效实现单独开发,单独维护,而且他们之间可以随意的进行组合。大大提升开发效率低,降低维护成本。

组件化对于任何一个业务场景复杂的前端应用以及经过多次迭代之后的产品来说都是必经之路。组件化要做的不仅仅是表面上看到的模块拆分解耦,其背后还有很多工作来支撑组件化的进行,例如结合业务特性的模块拆分策略、模块间的交互方式和构建系统等等 。

本文给大家介绍的一款组件是:

前端基于原生input增强选择picker插件 可用于地图定位选位置 页面跳转选数据

小程序体验地址(已上线):

效果图如下:

edfc80a121d895577c1018ee26c85741.png

dad6d804ce7e1cdc926d96ee8570b960.png

319f9dd81bbc3ad5a1f7af85fe62369b.png

# 基于原生input增强选择picker插件 可用于地图定位选位置 页面跳转选数据

#### 使用方法

```使用方法

<!-- leftTitle:左边标题 name:输入框名字 value:输入框选择值  placeholder:占位符-->

<ccInputSelView leftTitle="收获地点" name="location" :value="mapSelData.name" placeholder="请选择位置"

@click="chooseAddress"></ccInputSelView>

```

#### HTML代码部分

```html

<template>

<view class="content">

<form @submit="formSubmit" @reset="formReset">

<!-- leftTitle:左边标题 name:输入框名字 value:输入框选择值  placeholder:占位符-->

<ccInputSelView leftTitle="收获地点" name="location" :value="mapSelData.name" placeholder="请选择位置"

@click="chooseAddress"></ccInputSelView>

<!-- leftTitle:左边标题 name:输入框名字  value:输入框值  placeholder:占位符 -->

<ccInputView leftTitle="详细地址" name="address" :value="mapSelData.address" placeholder="请输入详细地址">

</ccInputView>

<view class="uni-btn-v">

<button class="botBtn" type="primary" form-type="submit">下一步</button>

<view class="tipText"> 注意事项: 请确保您填写的房屋信息真实无误 </view>

</view>

</form>

</view>

</template>

```

#### JS代码 (引入组件 填充数据)

```javascript

<script>

import ccInputSelView from '../../components/ccInputSelView.vue'

import ccInputView from '../../components/ccInputView.vue'

export default {

components: {

ccInputSelView,

ccInputView

},

data() {

return {

mapSelData: {},

}

},

methods: {

formSubmit: function(e) {

console.log('form发生了submit事件,携带数据为:' + JSON.stringify(e));

var formdata = e.detail.value;

uni.showModal({

title: '温馨提示',

content: 'formsubmit事件携带数据为:' + JSON.stringify(e.detail.value)

})

},

// 选择地址

chooseAddress(e) {

let myThis = this;

// 需要运行手机硬件才可以选

uni.chooseLocation({

success: function(res) {

myThis.mapSelData = res;

console.log('位置名称:' + res.name);

console.log('详细地址:' + res.address);

console.log('纬度:' + res.latitude);

console.log('经度:' + res.longitude);

}

});

// 模拟地图选位数据

myThis.mapSelData = {

'name': '桂花便利店',

'address': '广东省佛山市南海区桂城街道南新四路36号',

'longitude': '113.22',

'latitude': '23.89',

};

},

}

}

</script>

```

#### CSS

```CSS

<style>

.content {

display: flex;

flex-direction: column;

}

.uni-btn-v {

width: 100%;

height: auto;

}

.botBtn {

width: 90%;

margin-top: 36px;

}

.tipText {

width: 100%;

margin-left: 0px;

text-align: center;

color: #666666;

margin-top: 36px;

margin-bottom: 36px;

font-size: 28rpx;

}

</style>

```

阅读全文下载完整组件代码请关注微信公众号: 前端组件开发

d848d5658a07453c843277846948c608.png

<think>我们参考提供的引用内容,特别是引用[1]中关于uni-app的Button组件的open-type属性,以及引用[2]中关于uni-app实现收货地址功能的描述。用户的需求是实现收货地址页面功能,需要示例或组件。根据引用[1],uni-app的Button组件提供了open-type属性,可以用于调用微信小程序的开放能力,比如获取用户手机号、分享等。在收货地址功能中,我们可能需要一个按钮来触发选择收货地址的操作,这里可以使用open-type="chooseAddress"。引用[2]提到了实现收货地址功能,并提供了效果图和插件包的下载链接,但考虑到我们无法访问外部链接,我们可以根据描述自行实现基本功能。因此,我们可以设计一个收货地址页面,包含以下部分:1.显示用户已添加的收货地址列表(如果用户已经添加过)。2.一个按钮,用于触发选择收货地址(调用微信小程序的收货地址选择功能)。3.一个按钮,用于新增收货地址(跳转到新增地址页面)。注意:在微信小程序中,使用button组件的open-type="chooseAddress"可以快速调取微信的收货地址选择功能。在uni-app中,同样可以使用这个属性,因为它会编译成对应小程序的代码。但是需要注意,uni-app的跨平台特性,不同平台(如H5、App)可能不支持open-type,所以我们需要使用条件编译或检查平台。下面我们提供一个基本的页面示例,包括使用open-type获取微信收货地址的功能:1.创建选择收货地址按钮,使用open-type="chooseAddress",并绑定一个事件处理函数。2.在事件处理函数中,获取用户选择的地址信息。示例代码(uni-appVue单文件组件):```vue<template><viewclass="container"><!--地址列表,这里简单展示,实际应该有列表结构--><viewv-if="addressList.length>0"><viewv-for="(item,index)inaddressList":key="index"class="address-item"><text>{{item.userName}}</text><text>{{item.telNumber}}</text><text>{{item.provinceName}}{{item.cityName}}{{item.countyName}}{{item.detailInfo}}</text></view></view><viewv-else><text>暂无收货地址</text></view><!--选择地址按钮,仅微信小程序支持--><buttonv-if="platform==='weixin'"open-type="chooseAddress"@chooseaddress="handleChooseAddress">选择微信收货地址</button><!--其他平台,可能需要自行实现地址选择界面,或者使用地图选择--><buttonv-else@click="openAddressForm">新增收货地址</button></view></template><script>exportdefault{data(){return{addressList:[],platform:''}},onLoad(){//判断平台//#ifdefMP-WEIXINthis.platform='weixin';//#endif},methods:{handleChooseAddress(res){//在微信小程序中,这个事件会返回用户选择的地址信息//注意:res.detail中是地址对象if(res.detail.errMsg==='chooseAddress:ok'){//获取到地址信息,可以存到地址列表constaddress=res.detail;//将地址信息加入地址列表this.addressList.push({userName:address.userName,telNumber:address.telNumber,provinceName:address.provinceName,cityName:address.cityName,countyName:address.countyName,detailInfo:address.detailInfo});}},openAddressForm(){//跳转到新增地址页面uni.navigateTo({url:'/pages/address/add'});}}}</script><style>.container{padding:20px;}.address-item{margin-bottom:10px;padding:10px;border:1pxsolid#eee;}</style>```说明:1.这个示例页面中,我们首先展示地址列表,如果没有地址则显示提示。2.微信小程序平台下,显示一个使用open-type="chooseAddress"的按钮,点击后会调起微信的收货地址选择界面。用户选择后,我们可以在`handleChooseAddress`事件中获取地址信息。3.注意,选择地址后返回的信息中,`detail`对象包含地址详情。我们将其加入到地址列表中(实际项目中可能需要保存到服务器或本地存储)。4.对于非微信小程序平台,我们显示一个新增地址按钮,点击后跳转到新增地址页面(这里需要自行实现该页面)。注意事项:-`chooseAddress`在微信小程序中需要用户授权,且返回的地址信息可能因为微信的更新而发生变化,请参考微信小程序官方文档。-不同小程序(如支付宝小程序)可能有不同的地址选择方式,需要条件编译处理。-App或H5端,没有open-type,因此我们需要自己实现地址选择页面,或者使用地图组件(如uni-app的map组件)来选择位置。另外,引用[2]提到了使用插件包,可能是用于地址选择的三方组件,但这里我们没有使用,而是使用了微信小程序的开放能力。如果需要跨平台支持,可以考虑使用uni-app插件市场(如https://ext.dcloud.net.cn/)中的地址选择组件。希望这个示例能帮助用户开始实现收货地址功能。相关问题:1.如何在不同平台(微信小程序、H5、App)实现统一的地址选择功能?2.uni-app中如何使用条件编译处理平台差异?3.如何将选择的收货地址保存到服务器?</think> 以下是基于您需求的`uni-app`收货地址页面实现方案,结合引用内容和开发实践提供详细指导: ### 核心实现思路 1. **数据源**:使用`uni-app`表单组件绑定地址数据对象 2. **地址选择**:利用`<picker>`组件选择省市区(需配合地区数据) 3. **开放能力**:通过Button组件的`open-type="chooseAddress"`直接调用小程序原生地址选择功能(仅限微信小程序) ### 基础代码示例 ```vue <template> <view class="address-container"> <!-- 表单组件 --> <uni-forms> <uni-forms-item label="收货人" required> <uni-easyinput v-model="address.name" placeholder="姓名" /> </uni-forms-item> <uni-forms-item label="手机号" required> <uni-easyinput v-model="address.phone" type="number" placeholder="11位手机号" /> </uni-forms-item> <!-- 地区选择--> <uni-forms-item label="地区" required> <picker mode="region" @change="regionChange"> <view>{{ address.region || '选择省/市/区' }}</view> </picker> </uni-forms-item> <uni-forms-item label="详细地址" required> <uni-easyinput v-model="address.detail" placeholder="街道门牌号" /> </uni-forms-item> <!-- 微信小程序专用地址选择 --> <button v-if="isWeixin" open-type="chooseAddress" @chooseaddress="onChooseAddress"> 选择微信收货地址 </button> </uni-forms> <!-- 地址保存操作 --> <button class="save-btn" @click="saveAddress">保存地址</button> </view> </template> <script> export default { data() { return { isWeixin: false, // 平台判断 address: { name: '', phone: '', region: '', detail: '' } } }, onLoad() { // 检测微信环境 // #ifdef MP-WEIXIN this.isWeixin = true; // #endif }, methods: { regionChange(e) { this.address.region = e.detail.value.join('-') }, // 微信开放能力获取地址 onChooseAddress(res) { const addr = res.detail this.address = { name: addr.userName, phone: addr.telNumber, region: `${addr.provinceName}-${addr.cityName}-${addr.countyName}`, detail: addr.detailInfo } }, saveAddress() { // 保存逻辑示例 uni.setStorageSync('savedAddress', this.address) uni.showToast({ title: '保存成功' }) } } } </script> <style> .address-container { padding: 20rpx; } .save-btn { margin-top: 40rpx; background: #07c160; color: white; } </style> ``` ### 关键功能说明 1. **跨平台处理**: - 使用`#ifdef MP-WEIXIN`条件编译微信专用功能 - 普通表单兼容所有平台(H5/App/小程序) 2. **微信开放能力**: ```vue <button open-type="chooseAddress" @chooseaddress="onChooseAddress"> 选择微信收货地址 </button> ``` 调用后自动打开微信官方地址选择界面[^1] 3. **地区选择优化**: - 使用`picker`组件的`region`模式实现三级联动 - 需在项目中引入`region.json`地区数据文件(可从uni插件市场获取) ### 组件资源推荐 1. **uni插件市场**: - [地址选择组件](https://ext.dcloud.net.cn/search?q=地址) - 推荐插件:`uni-area`(省市区三级选择器) 2. **腾讯位置服务**: - 使用`<map>`组件配合地图点功能 - 需申请腾讯位置服务密钥(JS API) ### 注意事项 1. **微信隐私政策**:`chooseAddress`接口需用户主动触发按钮[^1] 2. **数据格式**:保存地址时建议标准化数据结构: ```json { "id": "20240315_001", "default": true, "name": "张三", "phone": "13800138000", "location": { "province": "广东省", "city": "深圳市", "district": "南山区" }, "detail": "科技园南区1栋" } ``` ### 扩展功能建议 1. 地址管理页面(增删改查) 2. 腾讯地图定位集成 3. 地址智能解析(通过正则表达式提取省市区)
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端组件开发

你的钟意将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值