1
This commit is contained in:
@@ -72,4 +72,20 @@ export function uploadQiuFile(path, file, is_public = 1) {
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制文字内容
|
||||
* @param {文本} text
|
||||
*/
|
||||
export function copyText(text) {
|
||||
wx.setClipboardData({
|
||||
data: text,
|
||||
success() {
|
||||
wx.showToast({
|
||||
title: '已复制到剪切板',
|
||||
icon: 'none'
|
||||
})
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -2,11 +2,11 @@ import httpEnv from "./auth/env"
|
||||
import { getToken, removeToken } from "./auth/manageToken"
|
||||
const baseApi = {
|
||||
//开发版
|
||||
develop: "https://curainwxapp.test.tuzuu.com/api",
|
||||
develop: "https://kairos-wx.test.tuzuu.com/api",
|
||||
// 体验版
|
||||
trial: "https://curainwxapp.test.tuzuu.com/api",
|
||||
trial: "https://kairos-wx.test.tuzuu.com/api",
|
||||
// 正式版
|
||||
release: "https://yidaojia.cells.org.cn/api"
|
||||
release: "https://kairos-wx.test.tuzuu.com/api",
|
||||
}
|
||||
//获取当前环境的接口前缀
|
||||
export const baseUrl = baseApi[httpEnv]
|
||||
@@ -82,4 +82,65 @@ request.post = function (url, options) {
|
||||
})
|
||||
}
|
||||
|
||||
//流式
|
||||
let lastChunk = '' //流可能会被截断,需要补充
|
||||
export function streamRequest(url, data, onChunkReceived) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const token = getToken()
|
||||
let header = {}
|
||||
if (token) {
|
||||
header.Authorization = 'Bearer ' + token;
|
||||
}
|
||||
let requestTask = wx.request({
|
||||
url: baseUrl + url,
|
||||
data: data || {},
|
||||
method: 'POST',
|
||||
header,
|
||||
timeout: 100000,
|
||||
responseType: "arraybuffer",
|
||||
enableChunked: true, //关键!开启流式传输模式
|
||||
success: () => {
|
||||
resolve()
|
||||
},
|
||||
fail: (e) => {
|
||||
console.log(e);
|
||||
reject()
|
||||
}
|
||||
})
|
||||
//处理流
|
||||
if (onChunkReceived) {
|
||||
lastChunk = ''
|
||||
requestTask.onChunkReceived((response) => {
|
||||
let data = response.data;
|
||||
// console.log(decodeStream(data));
|
||||
onChunkReceived(decodeStream(data))
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
//流素具解码
|
||||
function decodeStream(data) {
|
||||
let uint8Array = new Uint8Array(data); // 将 ArrayBuffer 转换为 Uint8Array
|
||||
let responseText = decodeURIComponent(escape(String.fromCharCode.apply(null, uint8Array))); // 使用 apply 扩展字节
|
||||
// 处理数据,移除前缀等操作
|
||||
if (lastChunk) {
|
||||
responseText = "data: " + lastChunk + responseText
|
||||
lastChunk = ""
|
||||
}
|
||||
// 处理数据,移除前缀等操作
|
||||
responseText = responseText.replaceAll("data: ", '');
|
||||
let jsonStrings = responseText.trim().split(/\n+/);
|
||||
// 过滤掉解析失败的部分
|
||||
let jsonArray = jsonStrings.map(jsonStr => {
|
||||
try {
|
||||
return JSON.parse(jsonStr);
|
||||
} catch (e) {
|
||||
lastChunk = jsonStr
|
||||
console.error('JSON格式不正确(已做兼容处理):');
|
||||
return null; // 如果解析失败,返回null
|
||||
}
|
||||
}).filter(item => item !== null);
|
||||
return jsonArray
|
||||
}
|
||||
|
||||
export default request
|
||||
@@ -1,29 +1,63 @@
|
||||
module.exports = {
|
||||
/**
|
||||
* 日期格式化
|
||||
* @param {Date | String | Number} date
|
||||
* @param {String} format 格式
|
||||
*/
|
||||
formatDate: function (date, format) {
|
||||
if (!format) {
|
||||
format = 'YYYY/MM/DD hh:mm:ss';
|
||||
}
|
||||
if (!date) {
|
||||
return '';
|
||||
}
|
||||
date = getDate(date)
|
||||
var YYYY = date.getFullYear();
|
||||
var MM = ('0' + (date.getMonth() + 1)).slice(-2)
|
||||
var DD =('0' + date.getDate()).slice(-2);
|
||||
var hh = ("0" + date.getHours()).slice(-2);
|
||||
var mm = ("0" + date.getMinutes()).slice(-2);
|
||||
var ss = ("0" + date.getSeconds()).slice(-2);
|
||||
var result = format.replace(getRegExp('YYYY'), YYYY)
|
||||
.replace(getRegExp('MM'), MM)
|
||||
.replace(getRegExp('DD'), DD)
|
||||
.replace(getRegExp('hh'), hh)
|
||||
.replace(getRegExp('mm'), mm)
|
||||
.replace(getRegExp('ss'), ss)
|
||||
return result;
|
||||
},
|
||||
};
|
||||
/**
|
||||
* 日期格式化
|
||||
* @param {Date | String | Number} date
|
||||
* @param {String} format 格式
|
||||
*/
|
||||
formatDate: function (date, format) {
|
||||
if (!format) {
|
||||
format = 'YYYY/MM/DD hh:mm:ss';
|
||||
}
|
||||
if (!date) {
|
||||
return '';
|
||||
}
|
||||
date = getDate(date)
|
||||
var YYYY = date.getFullYear();
|
||||
var MM = ('0' + (date.getMonth() + 1)).slice(-2)
|
||||
var DD = ('0' + date.getDate()).slice(-2);
|
||||
var hh = ("0" + date.getHours()).slice(-2);
|
||||
var mm = ("0" + date.getMinutes()).slice(-2);
|
||||
var ss = ("0" + date.getSeconds()).slice(-2);
|
||||
var result = format.replace(getRegExp('YYYY'), YYYY)
|
||||
.replace(getRegExp('MM'), MM)
|
||||
.replace(getRegExp('DD'), DD)
|
||||
.replace(getRegExp('hh'), hh)
|
||||
.replace(getRegExp('mm'), mm)
|
||||
.replace(getRegExp('ss'), ss)
|
||||
return result;
|
||||
},
|
||||
/**
|
||||
* 获取浅色标签颜色
|
||||
* @param {string} hexColor 如 "#e3f9e9"
|
||||
*/
|
||||
getLightColor: function (color, level) {
|
||||
if (!color || color.indexOf('#') === -1) return color;
|
||||
|
||||
// 默认变浅程度
|
||||
var weight = level !== undefined ? level : 0.9;
|
||||
|
||||
// 处理 #fff 这种简写
|
||||
var hex = color.slice(1);
|
||||
if (hex.length === 3) {
|
||||
hex = hex.charAt(0) + hex.charAt(0) + hex.charAt(1) + hex.charAt(1) + hex.charAt(2) + hex.charAt(2);
|
||||
}
|
||||
|
||||
// 提取 RGB
|
||||
var r = parseInt(hex.substring(0, 2), 16);
|
||||
var g = parseInt(hex.substring(2, 4), 16);
|
||||
var b = parseInt(hex.substring(4, 6), 16);
|
||||
|
||||
// 与白色(255)混合逻辑:新颜色 = 原颜色 * (1 - weight) + 255 * weight
|
||||
r = Math.floor(r * (1 - weight) + 255 * weight);
|
||||
g = Math.floor(g * (1 - weight) + 255 * weight);
|
||||
b = Math.floor(b * (1 - weight) + 255 * weight);
|
||||
|
||||
// 转回 16 进制
|
||||
var toString16 = function (n) {
|
||||
var s = n.toString(16);
|
||||
return s.length === 1 ? '0' + s : s;
|
||||
};
|
||||
|
||||
return '#' + toString16(r) + toString16(g) + toString16(b);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user