赞
踩
这里的appid是测试号,正式部署需要在微信公众平台进行申请,在平台上可以对小程序的开发团队、运营进行管理。
新建的项目代码结构就是这样的
没有这些插件,咱们的代码也就没有高亮,代码提示这些友好功能哦~
|- app.js 工程入口
|- app.json 小程序原生配置
|- app.wxss 部分公共样式
|- static 资源目录 --add
|- components 公用组件存放目录 --add
|- pages 页面路径
|- api api存放
|- utils 工程工具类
// app.json
"usingComponents": {
"van-button": "@vant/weapp/button/index"
}
const tips = { 1: '抱歉,出现了一个错误,请联系管理员', 200: '服务器成功返回请求数据', 201: '新建或修改数据成功', 202: '一个请求已经进入后台排队(异步任务)', 204: '删除数据成功', 400: '发出信息有误', 401: '用户没有权限(令牌失效、用户名、密码错误、登录过期)', 402: '令牌过期', 403: '用户得到授权,但是访问是被禁止的', 404: '访问资源不存在', 406: '请求格式不可得', 410: '请求资源被永久删除,且不会被看到', 500: '服务器发生错误', 502: '网关错误', 503: '服务不可用,服务器暂时过载或维护', 504: '网关超时', } let axiosPromiseArr = [] const baseUrl = 'http://*******' //请求相关默认配置 const option = { loading: true, //是否显示加载框 json: false, // 是否开启 json 格式 space: true, // 是否过滤参数空格 error: true, // 是否开启错误提示 loadingText: '正在加载中',//加载中提示文字 } function request({ url, method = 'GET', data = {}, options = option }) { try { options = Object.assign({}, option, options); return service(url, data, method, options); } catch (e) { console.log(e) wx.showToast({ title: '抱歉,出现了一个错误', icon: 'none', duration: 2000 }); } } function service(url, data, method, option) { const { space, json, error, loadingText, loading } = option return new Promise((resolve, reject) => { // 过滤空格 if (data && space) { for (const item in data) { (space[item] !== false && !!data[item] && typeof data[item] == 'string') && (data[item] = data[item].replace(/\s| /g, '')) } } !axiosPromiseArr.length && loading && wx.showLoading({ title: loadingText, mask: true }) axiosPromiseArr.push(url) const token = wx.getStorageSync('token'); const openId = wx.getStorageSync('openId'); const header = { token, openId, 'content-type': `application/${json ? 'json' : 'x-www-form-urlencoded'}` } wx.request({ url: baseUrl + url, data, method, header, complete(res) { axiosPromiseArr.pop() !axiosPromiseArr.length && loading && wx.hideLoading() console.log(res) resolve(checkStatusCode(res, error)) } }) }) } function checkStatusCode(res, error) { const { status = res.statusCode, msg = tips[res.statusCode] || '系统错误', data = null } = res.data || {} if (status === 200) return res.data // 403:未登录,402 401:强制下线 if (status === 403 || status === 402 || status === 401) { wx.reLaunch({ url: '/pages/login/login' }); } error && wx.showToast({ title: msg || tips[status], icon: 'none', duration: 3000 }) return { status, message: msg, data } } module.exports = { request: request }
//调用
import {request} from '../utils/request'
/**
* @description demo
*/
export function fun1(data) {
return request({
url: `/***`,
method: 'get',
data,
options:{loadingText:'..',loading:false}//不单独配置可不设置值
})
}
以上就是搭建项目架构的主要流程了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。