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

相关文章

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

Debian 13升级后网络转发等功能异常怎么办? 并非错误而是管理机制变更

《Debian13升级后网络转发等功能异常怎么办?并非错误而是管理机制变更》很多朋友反馈,更新到Debian13后网络转发等功能异常,这并非BUG而是Debian13Trixie调整... 日前 Debian 13 Trixie 发布后已经有众多网友升级到新版本,只不过升级后发现某些功能存在异常,例如网络转

基于Java和FFmpeg实现视频压缩和剪辑功能

《基于Java和FFmpeg实现视频压缩和剪辑功能》在视频处理开发中,压缩和剪辑是常见的需求,本文将介绍如何使用Java结合FFmpeg实现视频压缩和剪辑功能,同时去除数据库操作,仅专注于视频处理,需... 目录引言1. 环境准备1.1 项目依赖1.2 安装 FFmpeg2. 视频压缩功能实现2.1 主要功

使用Python实现无损放大图片功能

《使用Python实现无损放大图片功能》本文介绍了如何使用Python的Pillow库进行无损图片放大,区分了JPEG和PNG格式在放大过程中的特点,并给出了示例代码,JPEG格式可能受压缩影响,需先... 目录一、什么是无损放大?二、实现方法步骤1:读取图片步骤2:无损放大图片步骤3:保存图片三、示php

深度解析Python yfinance的核心功能和高级用法

《深度解析Pythonyfinance的核心功能和高级用法》yfinance是一个功能强大且易于使用的Python库,用于从YahooFinance获取金融数据,本教程将深入探讨yfinance的核... 目录yfinance 深度解析教程 (python)1. 简介与安装1.1 什么是 yfinance?

Python脚本轻松实现检测麦克风功能

《Python脚本轻松实现检测麦克风功能》在进行音频处理或开发需要使用麦克风的应用程序时,确保麦克风功能正常是非常重要的,本文将介绍一个简单的Python脚本,能够帮助我们检测本地麦克风的功能,需要的... 目录轻松检测麦克风功能脚本介绍一、python环境准备二、代码解析三、使用方法四、知识扩展轻松检测麦

Java实现TXT文件导入功能的详细步骤

《Java实现TXT文件导入功能的详细步骤》在实际开发中,很多应用场景需要将用户上传的TXT文件进行解析,并将文件中的数据导入到数据库或其他存储系统中,本文将演示如何用Java实现一个基本的TXT文件... 目录前言1. 项目需求分析2. 示例文件格式3. 实现步骤3.1. 准备数据库(假设使用 mysql

Java Stream流以及常用方法操作实例

《JavaStream流以及常用方法操作实例》Stream是对Java中集合的一种增强方式,使用它可以将集合的处理过程变得更加简洁、高效和易读,:本文主要介绍JavaStream流以及常用方法... 目录一、Stream流是什么?二、stream的操作2.1、stream流创建2.2、stream的使用2.