知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側(cè)重于功能的便捷,營銷的便利,運營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序為后期升級提供便捷的支持!
您當(dāng)前位置>首頁 » 新聞資訊 » 小程序相關(guān) >
微信小程序--關(guān)于加快小程序開發(fā)的幾個小建議
發(fā)表時間:2021-1-4
發(fā)布人:葵宇科技
瀏覽次數(shù):49
app.json
創(chuàng)建頁面
? 按照我們平常的開發(fā)習(xí)慣,創(chuàng)建一個新的頁面,一般都會先創(chuàng)建文件夾,再創(chuàng)建對應(yīng)page
的形式,創(chuàng)建完成后,app.json
中會自動注冊該頁面。實際上,我們還可以通過直接在app.json
中注冊頁面來生成對應(yīng)的page
。
"pages": [
"pages/index/index",
"pages/newpage/newpage"
],
? 如上所示,在配置文件中注冊該路徑,小程序就會自動創(chuàng)建該對應(yīng)路徑。
? 我們想要調(diào)試某個頁面時,相當(dāng)一部分開發(fā)者習(xí)慣于直接修改app.json
來調(diào)整首個入棧頁面,實際上完全可以使用編譯模式添加編譯頁面,來代替修改配置文件的行為。
? 這一點經(jīng)常被遺忘,因為新建component
的時候,小程序并不會展示該配置項。配置options
如下,組件可以使用全局樣式(app.wxss
)
Component({
//繼承colorui樣式
options: {
addGlobalClass: true,
multipleSlots: true
},
...
}
4.app.js
初始化內(nèi)容函數(shù)化
? 很多小程序onLaunch
中寫著大量的內(nèi)容,混亂不堪,后期調(diào)試尤為痛苦??梢詫⒉煌某跏蓟瘍?nèi)容寫為不同的函數(shù),函數(shù)化、模塊化。
onLaunch: function(options) {
//此處需要有對進(jìn)入小程序方式的處理
this.InitCloud(); //初始化云服務(wù) / ESC
this.InitCustom(); //初始化custom所需配置信息
this.InitEdu(); //初始化教務(wù)系統(tǒng)配置
},
5.善用template
? 對于內(nèi)容大量重復(fù)的wxml
內(nèi)容,可以將之抽離為template
模板文件,使用時直接導(dǎo)入使用即可。
<import src="template/NexuTemplate.wxml"/>
<view wx:for="{{dirlist}}" wx:key="item">
<template is="dirshow" data="{{item}}"></template>
</view>
6.云開發(fā)混合開發(fā)
? 內(nèi)容安全識別,openid獲取,圖片鑒黃,支付流程,內(nèi)容推送這些東西如果使用自己的后臺實現(xiàn),并不是這么簡單,但是如果使用了云開發(fā)的技術(shù)替換這一部分,自己專注于業(yè)務(wù)邏輯,你會打開一片新天地。
云開發(fā)部分功能(后面我會專門寫一篇文章介紹云開發(fā)混合開發(fā)的內(nèi)容):
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
// 云函數(shù)入口函數(shù)
exports.main = async (event, context) => {
// console.log(event)
switch (event.action) {
case 'getWXACode': {
return getWXACode(event)
}
case 'getOpenData': {
return getOpenData(event)
}
case 'msgSecCheck': {
return msgSecCheck(event)
}
case 'imgSecCheck': {
return imgSecCheck(event)
}
case 'submitPages': {
return submitPages(event)
}
default: {
return
}
}
}
//獲取小程序碼
async function getWXACode(event) {
console.log(event.url)
// 此處將獲取永久有效的小程序碼,并將其保存在云文件存儲中,最后返回云文件 ID 給前端使用
const wxacodeResult = await cloud.openapi.wxacode.get({
path: event.url || 'pages/index/index',
})
const fileExtensionMatches = wxacodeResult.contentType.match(/\/([^\/]+)/)
const fileExtension = (fileExtensionMatches && fileExtensionMatches[1]) || 'jpg'
const uploadResult = await cloud.uploadFile({
// 云文件路徑,此處為演示采用一個固定名稱
cloudPath: `wxCode/wxCode${Math.random() * 9999999}.${fileExtension}`,
// 要上傳的文件內(nèi)容可直接傳入圖片 Buffer
fileContent: wxacodeResult.buffer,
})
if (!uploadResult.fileID) {
throw new Error(`上傳失敗,文件為空,存儲服務(wù)器狀態(tài)代碼為空 ${uploadResult.statusCode}`)
}
return uploadResult.fileID
}
// 獲取openid
async function getOpenData(event) {
// 需 wx-server-sdk >= 0.5.0
const wxContext = cloud.getWXContext()
return {
event,
openid: wxContext.OPENID,
appid: wxContext.APPID,
unionid: wxContext.UNIONID,
}
}
// 檢測文本是否合規(guī)
async function msgSecCheck(event) {
// 需 wx-server-sdk >= 0.5.0
return cloud.openapi.security.msgSecCheck({
content: event.content,
})
}
// 檢測圖片是否合規(guī)
async function imgSecCheck(event) {
return cloud.openapi.security.imgSecCheck({
media: {
contentType: event.contentType,
value: Buffer.from(event.value)
}
})
}
// 收錄頁面
async function submitPages(event) {
return cloud.openapi.search.submitPages({
pages: [{
path: event.path,
query: event.query
}]
})
}
//獲取日期
function getDateTime(sj) {
var now = new Date(sj * 1000);
var year = now.getFullYear();
var month = now.getMonth() + 1;
var date = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
// var second = now.getSeconds();
return year + "年" + month + "月" + date + "日";
}
7.將個人配置數(shù)據(jù)集中到一個文件中
? 比如說服務(wù)器域名、接口令牌這些可能會變化,但經(jīng)常使用的數(shù)據(jù),集中到一個文件中。
module.exports={
UseCloud:true,
CloudId:'', //云開發(fā)環(huán)境id
TraceUser:true, //記錄用戶訪問日志
AdaptStorge:true, //允許緩存用戶數(shù)據(jù)
SevDomain:'http://localhost' //服務(wù)器的域名
}
8.善用開發(fā)者工具的版本管理工具
相關(guān)案例查看更多
相關(guān)閱讀
- 汽車報廢管理
- 網(wǎng)頁制作
- 搜索引擎優(yōu)化
- 昆明軟件定制
- 云南小程序開發(fā)哪家好
- 云南小程序哪家好
- 云南網(wǎng)站建設(shè)方法
- 昆明網(wǎng)站建設(shè)公司
- 保險網(wǎng)站建設(shè)公司
- 模版消息
- 網(wǎng)站建設(shè)首選
- 小程序的開發(fā)公司
- 貴州小程序開發(fā)
- 云南小程序開發(fā)公司
- 麗江小程序開發(fā)
- 制作一個小程序
- 云南網(wǎng)絡(luò)推廣
- 花農(nóng)小程序
- web開發(fā)技術(shù)
- 云南網(wǎng)站建設(shè)服務(wù)公司
- 云南小程序被騙
- 表單
- 小程序被攻擊
- 網(wǎng)站建設(shè)選
- 報廢車回收
- 網(wǎng)站建設(shè)專家
- 云南網(wǎng)站建設(shè)特性
- 小程序開發(fā)課程
- 云南百度小程序
- 怎么做網(wǎng)站