taro常用的一些功能

2024-03-23 19:52
文章标签 功能 常用 taro

本文主要是介绍taro常用的一些功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.打电话

Taro.makePhoneCall({phoneNumber:that.state.tell,success:function(){}})

2.弹窗获取位置-->获取本人地理位置

// 弹窗
Taro.showModal({content: '即将前往设置,允许小程序获取您的位置信息',showCancel:false,success: function (res) {if (res.confirm) {// 查询本机设置Taro.openSetting({success(reso) {console.log(reso)// 看看本机有没有授权if (reso.authSetting["scope.userLocation"]) {// 清缓存Taro.removeStorageSync("userLocation");}// 获取位置Taro.getLocation({type: 'wgs84',success: function (res) {console.log(res)putKey('longitude',res.longitude)putKey('latitude',res.latitude)// 成功就跳转Taro.navigateTo({url:url})},fail:function(err){console.log(err)showError('未获取位置信息,路线规划失败')}})},fail(err){console.log(err)}});} }})

 3.有位置信息,需要按照给的地址名,还有经纬度进行跳转

 

第一步需要有Map标签

 <Map id='myMap' markers={mar} longitude={this.state.longitude} latitude={this.state.latitude}  className='map' />

第二步进行跳转

open(item){const mapCtx = Taro.createMapContext('myMap');mapCtx.openMapApp({latitude: parseFloat(item.latitude),longitude: parseFloat(item.longitude),destination: item.stationName,success:function(e){console.log("openResult1",e)},fail:function(e){console.log("openResult2",e)},complete:function(e){console.log("openResult3",e)},})}

下边是一个地图展示站点,有相应的站点以及点击每个站点左边进入详情右边进行到第三方导航

import Taro, { Component, Config } from '@tarojs/taro'
import { View, Image,Map } from '@tarojs/components'
import { AtIcon, AtGrid, AtAvatar  } from 'taro-ui'
import { getKey,putKey } from '@/src/utils/storage-utils'
import { showError } from '@/src/utils/loading-utils'
import {wxLogin,listByDistance } from '@/src/api/api'
import LoginView from '@/src/components/loginView/index'
import './index.less'
let mar = []
class OtherServeSite extends Component {/*** 指定config的类型声明为: Taro.Config** 由于 typescript 对于 object 类型推导只能推出 Key 的基本类型* 对于像 navigationBarTextStyle: 'black' 这样的推导出的类型是 string* 提示和声明 navigationBarTextStyle: 'black' | 'white' 类型冲突, 需要显示声明类型*/config: Config = {navigationBarTitleText: '',navigationStyle: 'default',navigationBarBackgroundColor: '#FFFFFF',navigationBarTextStyle: 'black',backgroundColorTop: "#FFFFFF"}constructor (props) {super(props)this.state = {list:[],none:require('@/src/assets/images/none.png'),markers:[],latitude:'',longitude:'',title:''}}// componentWillReceiveProps () {}// componentWillUnmount () {}componentDidMount(){}// componentDidShow () {}// componentDidHide () {}// 下拉刷新onReachBottom() {var param = {latitude:getKey('latitude'),longitude: getKey('longitude'),types:7}console.log('param',param)listByDistance(param).then(res=>{console.log(res)if(res.code == 200){this.setState({list:res.rows,markers:res.rows,})}else{showError(res.msg)}})}componentDidShow() {var param = {latitude:getKey('latitude'),longitude: getKey('longitude'),types:7}console.log('param',param)listByDistance(param).then(res=>{console.log(res)if(res.code == 200){var list  = res.rowsfor(var j=0; j<list.length;j++){mar.push({'id': list[j].id,'latitude':list[j].latitude,'longitude':  list[j].longitude,'width': 30,  //图片显示宽度'height': 30,//图片显示高度'title': list[j].stationName,'label': {      //标记的提示文字的样式'color':'#e41111','content': list[j].stationName,//提示内容}})}console.log(mar,9999999)getKey('markers',mar)this.setState({latitude:mar[0].latitude,longitude:mar[0].longitude,title:mar[0].title})let arr = []for(var i=0; i<list.length; i++){arr.push(list[i])}this.setState({list:arr})// if(res.rows.length<=3){//   this.setState({//     list//   })// }else{//   let arr = []//   for(var i=0; i<3; i++){//     arr.push(list[i])//   }//   this.setState({//     list:arr//   })// }}else{showError(res.msg)}})}open(item){const mapCtx = Taro.createMapContext('myMap');mapCtx.openMapApp({latitude: parseFloat(item.latitude),longitude: parseFloat(item.longitude),destination: item.stationName,success:function(e){console.log("openResult1",e)},fail:function(e){console.log("openResult2",e)},complete:function(e){console.log("openResult3",e)},})}detail(item){Taro.navigateTo({url:'/pages/statusMapDetail/index?id='+item.id})}render () {const { otherServeGridList,list } = this.stateconsole.log(mar,909090)return (<View className='user-container'><View className='near'><View><Image className='nearImg' src={require('@/src/assets/user/address-icon.png')} /></View><View>其他服务站点</View></View><View className='scrollBox'>{list.length>0 ? <View>{list.map((item,index) => {return (<View  key={item.id}  className='site-item' ><View  onClick={this.open.bind(this,item)} className='daohang'><View><Image className='navigation' src={require('@/src/assets/user/navigation.png')} /></View><View>导航</View></View><View  className='left'  onClick={this.detail.bind(this, item)} ><View className='flex'><View  className='name' >{item.stationName}</View>{index == 0 &&<View className='jin'>最近</View>}</View>{/* <View className='tell' >{item.contactPhone}</View> */}<View className='address' >{item.stationLocation}</View></View></View>)})}</View>:<View className='none'><View  className='img' ><Image src={none} className='pic'></Image></View><View className='noText'>暂无数据</View></View>}</View><View className='map'><Map id='myMap' markers={mar} longitude={this.state.longitude} latitude={this.state.latitude}  className='map' /></View></View>)}
}export default OtherServeSite

 4.上传功能

背景:需求是最多三张图片,但是接口每次只能上传一张

 第一步:AtImagePicker组件上去

<AtImagePicker count={3} multiple={true} files={this.state.files} onChange={this.onImg.bind(this)} />

第二步 onImg方法

  onImg (files,method,index) {if(files.length>3){Taro.showToast({title: '最多上传3张图片',icon:'none',duration: 2000})return false}for(let i=0; i<files.length; i++){if( 2097152 -files[i].file.size <0 ){Taro.showToast({title: '图片不能大于2M',icon:'none',duration: 2000})return false}}this.setState({files,})let url =''if(files.length == 1){url = files[0].url}if(files.length == 2){url = files[1].url}if(files.length == 3){url = files[2].url}if(method == 'add'){Taro.showLoading({title: '上传中',mask:true})Taro.uploadFile({url: uploadUrl+'/wxapi/upload',  //仅为示例,非真实的接口地址filePath: url,name: 'file',header: {'content-type': 'application/json','openid': getKey('openId')},success (res){Taro.hideLoading()pics.push(JSON.parse(res.data).url)},fail(err){Taro.hideLoading()}})}if(method == 'remove'){pics.splice(index, 1)}console.log('获取的files',files)return files
}

今天周六,下午写一些感觉很好,后续继续添加,希望对用taro框架的童鞋有些帮助,继续加油中

这篇关于taro常用的一些功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/839369

相关文章

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

golang中reflect包的常用方法

《golang中reflect包的常用方法》Go反射reflect包提供类型和值方法,用于获取类型信息、访问字段、调用方法等,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录reflect包方法总结类型 (Type) 方法值 (Value) 方法reflect包方法总结

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

python常用的正则表达式及作用

《python常用的正则表达式及作用》正则表达式是处理字符串的强大工具,Python通过re模块提供正则表达式支持,本文给大家介绍python常用的正则表达式及作用详解,感兴趣的朋友跟随小编一起看看吧... 目录python常用正则表达式及作用基本匹配模式常用正则表达式示例常用量词边界匹配分组和捕获常用re

Golang如何用gorm实现分页的功能

《Golang如何用gorm实现分页的功能》:本文主要介绍Golang如何用gorm实现分页的功能方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录背景go库下载初始化数据【1】建表【2】插入数据【3】查看数据4、代码示例【1】gorm结构体定义【2】分页结构体

Java Web实现类似Excel表格锁定功能实战教程

《JavaWeb实现类似Excel表格锁定功能实战教程》本文将详细介绍通过创建特定div元素并利用CSS布局和JavaScript事件监听来实现类似Excel的锁定行和列效果的方法,感兴趣的朋友跟随... 目录1. 模拟Excel表格锁定功能2. 创建3个div元素实现表格锁定2.1 div元素布局设计2.

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置