继去年毕设,使用小程序原生语言开发了一个英语学习小程序(smart英语学习》》)之后,一年没有写太小程序了。最近心血来潮,准备用很火的Taro(相似于react语法,多端)开发一个课堂签到小程序,踩踩坑,感觉一下。html
功能概述:基于地理位置(经纬度)的签到平台。另外包括校内新闻模块,word资料下载模块,我的信息模块等。扫码登陆: 学生体验帐号 123456,密码 123456。react
yarn global add @tarojs/cli
,使用 yarn 安装 CLItaro init myApp
建立模板项目(按照一系列默认命令,便可建立redux ,mobx等生成器模板)。而且以后会默认安装依赖。npm un dev:weap
,进入微信开发工具查看本身的小程序。componentDidShow、 componentDidMount
生命周期在小程序中的不一样表现。tab页componentDidMount
只走一次。备注:Taro 默认对小程序的异步 API 进行了包装,能够像使用 Promise 那样进行调用。ios
// WX
wx.request({
url: '', // 仅为示例,并不是真实的接口地址
data: {},
header: {
'content-type': 'application/json' // 默认值
},
success(res) {}
})
// Taro
Taro.request(url).then(function (res) {
console.log(res)
})
复制代码
sourcemap
不能用就很xxxxxkey value
传给子组件render
以外写jsx
this.props
传来的函数必须on
或者dispatch
开头props
,必须定义在static defaultProps
里,要否则获取不到componentDidMount
,在微信/百度/字节跳动/支付宝小程序中这一辈子命周期方法对应 app 的 onLaunch
componentDidShow
在微信/百度/字节跳动/支付宝小程序中这一辈子命周期方法对应 onShow
componentDidHide
在微信/百度/字节跳动/支付宝小程序中这一辈子命周期方法对应 onHide
process.env
的使用,不要以解构的方式来获取经过 env
配置的 process.env
环境变量,请直接以完整书写的方式 process.env.NODE_ENV
来进行使用this.$componentType
来判断当前 Taro.Component
是页面仍是组件,可能取值分别为 PAGE
和 COMPONENT
map
循环中使用 if
表达式Array#map
以外的方法操做 JSX 数组on
开头 以上是使用过程当中遇到的问题,更多注意事项请查阅官方文档// config/index.js
alias: {
'@actions': path.resolve(__dirname, '..', 'src/actions'),
'@assets': path.resolve(__dirname, '..', 'src/assets'),
'@components': path.resolve(__dirname, '..', 'src/components'),
'@constants': path.resolve(__dirname, '..', 'src/constants'),
'@reducers': path.resolve(__dirname, '..', 'src/reducers'),
'@style': path.resolve(__dirname, '..', 'src/style'),
'@util': path.resolve(__dirname, '..', 'src/util')
},
复制代码
// feth.js
import Taro from '@tarojs/taro'
const defaultMethod = 'POST'
const successStatus = 200
const successFlag = 1
const messageToast = title => {
Taro.showToast({
title,
icon: 'none',
duration: 1000
})
}
export default function fetch(options) {
const {
url,
method = defaultMethod,
params,
showErrorToast = true,
} = options
return Taro.request({
url,
method,
data: params,
}).then(response => {
const { statusCode, data } = response
// 不是200之外的
if (statusCode != successStatus) {
const { error } = data
// reject出去。showToast在catch里执行。
const errMessage = {errMsg: error || '请求接口失败'}
return Promise.reject(errMessage)
} else {
// flag是否是1的判断
const { flag, message, data: datas } = data
if (flag == successFlag) {
return Promise.resolve(datas)
} else {
const errMessage = {errMsg: message || '流程错误'}
return Promise.reject(errMessage)
}
}
}).catch(errors => {
const { errMsg } = errors
if (showErrorToast) {
messageToast(errMsg || '发起请求异常')
}
const errMessage = errMsg || '发起请求异常'
return Promise.reject(errMessage)
})
}
复制代码
toggleComments = () => {
Taro.createSelectorQuery().select('#comments-id').boundingClientRect(function(rect){
// 使页面滚动到响应位置
Taro.pageScrollTo({
scrollTop: rect.bottom
})
}).exec()
}
复制代码
小程序目前提供了wx.downloadFile的API,但目前只支持长传和下载图片,语音,视频 三种类型的文件。doc文件会下载为临时路径的 .myword文件,可供预览(安卓手机默认微信内置浏览器打开,可转发至电脑。ios tbd)。git
Taro.showLoading()
const params = {
url,
fileType: "doc",
success(res) {
// 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务须要自行判断是否下载到了想要的内容
if (res.statusCode === 200) {
const wxPath = res.tempFilePath
Taro.openDocument({
filePath: wxPath,
fileType: "doc",
success: function (ress) {
Taro.hideLoading()
console.log(ress)
},
fail: function (resfo) {
Taro.hideLoading()
util.showToast('打开文档失败')
}
})
}
},
fail: function (resfd) {
Taro.hideLoading()
util.showToast('文件下载失败')
},
}
Taro.downloadFile(params).then()
复制代码
wx.getLocation({
type: 'gcj02', // wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标
success(res) {
const { latitude, longitude } = res
}
})
复制代码
①、wx.getLocation,调用前须要 用户受权。scope.userLocation
②、发起签到与扫码签到时,应保证type
相同。github
// 用户受权,app.js中配置项
permission: {
"scope.userLocation": {
"desc": "获取用户的签到位置"
}
},
复制代码
预览二维码图片之后,不支持识别二维码。npm
getEventData(data, tag) {
return data.currentTarget.dataset[tag];
},
previewImage = (e) => {
const current = getEventData(e, 'src')
Taro.previewImage({
current: current, // 当前显示图片的http连接
urls: [current] // 须要预览的图片http连接列表
})
}
复制代码
wxacode.getUnlimited
)==菜的抠脚,大佬轻喷。内容较粗糙,有问题请指正。==
有必要夸一下,小程序的处理速度仍是很快的,两个小时审核就经过了。 json