/********************************************************************* */ function 功能requestMsgByUni( requestMsgByUni(url, data, 数据header, 页眉callback) { 回归){uni.request({ 请求({url: url,method: 方法:"POST", “邮政”,data: 数据:data, 数据header: 页眉:header, 页眉success: ( 成功:(res) => {callback( 回调(res); res);},fail: ( 失败: (err) => {console. 安慰。log("请求服务器失败,err", err); 呃);},}); } function 功能requestMsgGetByUni( requestMsgGetByUni(url, data, 数据header, 页眉callback) { 回归){uni.request({ 请求({url: url,method: 方法:"GET", “GET”,data: 数据:data, 数据header: 页眉:header, 页眉success: ( 成功:(res) => {callback( 回调(res); res);},fail: ( 失败: (err) => {console. 安慰。log("请求服务器失败,err", err); 呃);},}); } export { requestMsgByUni, requestMsgGetByUni }; /*********************************************************************************/ /*** 获取当前屏幕的尺寸* 高度相关信息,要放在 onReady 里获取* */ function getSysInfo() {let screenWidth = 0;uni.getSystemInfo({success: (res) => {// console.log("getSysInfo res: ", res);screenWidth = res.windowWidth;},fail: (err) => {console.log(err);},}); return screenWidth; } export { getSysInfo }; /*********************************************************************************/ /*** int 转 rgb(),如 -16580608 -> rgb(153, 0, 51)* num 是是int类型数字* */ function converIntToRgb(num) {var rgb = [];rgb[0] = (num & 0xff0000) >> 16;rgb[1] = (num & 0xff00) >> 8;rgb[2] = num & 0xff;return "rgb(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + ")"; } /*** rgb() 转 int,如rgb(153, 0, 51) -> -16580608* */ function converRgbToInt(r, g, b) {var color = (0xff << 24) | (r << 16) | (g << 8) | b;return color; } /*** 获取当前年月日* */ function getDate(type) {const date = new Date();let year = date.getFullYear();let month = date.getMonth() + 1;let day = date.getDate(); if (type === "start") {year = year - 60;} else if (type === "end") {year = year + 2;}month = month > 9 ? month : "0" + month;day = day > 9 ? day : "0" + day;return `${year}-${month}-${day}`; } export { converIntToRgb, converRgbToInt, getDate };
main.js
import Vue from "vue"; import App from "./App"; //引入vuex import store from "./store"; // 引入自定义 utils.js 文件 import * as utils from "./utils/utils.js"; //把vuex定义成全局组件 Vue.prototype.$store = store; // 把 utils 挂载到vue原型上,方便全局调用 Vue.prototype.$utils = utils; Vue.config.productionTip = false; App.mpType = "app"; const app = new Vue({...App,//挂载 store, }); app.$mount();
使用
this.$utils.requestMsgByUni(url, data, header, (res) => {if (res.statusCode == 200) {console.log("成功", res.data);} else {console.log("失败", res);} });
注意事项
有无 export default 引入方法不同!
一定要注意 import * as utils from "/utils/utils.js" 因为这里没有 export default 所以 import utils from "/utils/utils"是