本文主要是介绍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常用的一些功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!