繼去年畢設,使用小程序原生語言開發了一個英語學習小程序(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