原生微信小程序项目基础框架搭建
- 文件目录结构
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/499a78e48350f048f5ae1190752df335.png)
1. 环境变量(开发环境, 线上环境,测试环境)便于在不同环境的切换
- 接口的url
- webview的前缀url
- 埋点相关的环境参数
- 本地存储的环境变量配置
…
根目录中的 config
文件 中的 env.js
文件
const env = "dev"
const Env_CONFIG = {
dev: {
ENV: "dev",
API_URL: "http://pixiu.natapp1.cc",
UPLOAD_URL: "http://pixiu.natapp1.cc",
IMG_URL: "http://pixiu.natapp1.cc"
},
pre: {
ENV: "pre",
API_URL: "http://pixiu.natapp1.cc",
UPLOAD_URL: "http://pixiu.natapp1.cc",
IMG_URL: "http://pixiu.natapp1.cc"
},
pro: {
ENV: "pro",
API_URL: "http://pixiu.natapp1.cc",
UPLOAD_URL: "http://pixiu.natapp1.cc",
IMG_URL: "http://pixiu.natapp1.cc"
}
}
module.exports = Env_CONFIG[env]
2.配置本地存储
- 因为微信小程序的体验环境和线上环境的storage是统一的,经常会相互覆盖掉
- 存储的key和环境变量关联
- 添加上过期时间
在根目录utils
文件夹中的storage.js
文件
const { ENV } = require("../config/env")
class Storage {
constructor(env) {
this.ENV = env
}
get(key) {
const { value, exp, cTime } = wx.getStorageSync(`${this.ENV}_${key}`)
if (exp) {
const nowTime = (new Date()).getTime()
if (nowTime - cTime >= exp) {
this.move(key)
return undefined
}
}
return value
}
set(key, value, exp = null) {
let obj = { value, exp, cTime: (new Date()).getTime() }
wx.setStorageSync(`${this.ENV}_${key}`, obj)
}
remove(key) {
wx.removeStorageSync(`${this.ENV}_${key}`)
}
clear() {
wx.clearStorageSync()
}
}
module.exports = new Storage(ENV)
3.对请求进行封装
- 请求的loading, 根据参数配置 v
- 拦截的处理函数 v
- 调用方式优化 v
- 全局文件上传
- 后端允许最大的上传图片尺寸2m 图片上传前拦截
- a页面图片 最多500
- b页面最多300k
在根目录utils
文件夹中的axios.js
文件
const storage = require("./storage")
const { API_URL, UPLOAD_UR } = require("../config/env")
class axios {
constructor() {
this.token = storage.get('token')
}
require(method, url, data, loading) {
if (loading) {
wx.showLoading({
title: '数据加载ing',
})
}
return new Promise((resolve, reject) => {
wx.request({
url: API_URL + url,
data: data,
method: method,
header: {
'X-token': `${this.token}`
},
success: (result) => {
resolve(this.interseptorResponse(result))
},
fail: (res) => {
console.log("失败了");
reject(res)
},
complete: (res) => {
console.log("完成了");
wx.hideLoading()
}
})
})
}
get(url, data, loading) {
return this.require("get", url, data, loading)
}
post(url, data, loading) {
return this.require("post", url, data, loading)
}
uploadFile(path, size) {
wx.showLoading({
title: '图片上传中',
})
return new Promise((resolve, reject) => {
if (size >= 1024 * 1024 * 2) {
reject({
code: -1,
msg: "图片尺寸过大"
})
}
wx.uploadFile({
url: UPLOAD_URL,
filePath: path,
name: 'hehe',
header: {
authorization: `Bearer ${this.token}`
},
success(res) {
resolve(JSON.parse(res.data))
},
fail(res) {
reject(res)
},
complete() {
wx.hideLoading()
}
})
})
}
interseptorResponse(data) {
return data.data
}
}
module.exports = new axios()
4. 版本升级的处理
- 因为小程序的缓存问题,小程序包跟新之后,用户无法第一时间获取
- 控制是否强制更新用户版本
在根目录utils
文件夹中的versionMange.js
文件
class versionManage {
constructor(forceUpdate) {
const updateManager = wx.getUpdateManager()
updateManager.onUpdateReady(function () {
if (forceUpdate) {
updateManager.applyUpdate()
return false
}
wx.showModal({
title: '更新提示',
content: '新版本已经准备好了,是否重启应用?',
success(res) {
if (res.confirm) {
updateManager.applyUpdate()
}
}
})
})
}
}
module.exports = versionManage
5.处理全局共有的 api 或者 接口请求方法
在根目录下的service.js
文件下
const axios = require("./utils/axios")
function getVersionUpdate() {
return axios.get("/products/")
}
function wxlogin(data) {
return axios.post("/login", data)
}
function uploadFile(path, size) {
}
module.exports = {
getVersionUpdate,
wxlogin,
uploadFile
}