init
This commit is contained in:
1
src/libs/area.js
Normal file
1
src/libs/area.js
Normal file
File diff suppressed because one or more lines are too long
93
src/libs/haibao.js
Normal file
93
src/libs/haibao.js
Normal file
@@ -0,0 +1,93 @@
|
||||
export default class Haibao {
|
||||
constructor(width, height, color = '#fff') {
|
||||
this.canvas = document.createElement('canvas');
|
||||
this.ctx = this.canvas.getContext('2d', { alpha: false });
|
||||
this.canvas.width = width;
|
||||
this.canvas.height = height;
|
||||
this.ctx.fillStyle = color;
|
||||
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height)
|
||||
|
||||
this.zIndex = 0
|
||||
this.imagePromises = []
|
||||
this.images = []; // 存储待合成的图片信息
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加图片到合成队列(现在返回Promise)
|
||||
* @param {string|HTMLImageElement} image 图片URL或Image对象
|
||||
* @param {number} x 绘制X坐标
|
||||
* @param {number} y 绘制Y坐标
|
||||
* @returns {Promise} 图片加载完成的Promise
|
||||
*/
|
||||
add (image, x, y, index) {
|
||||
this.zIndex++
|
||||
const zIndex = index ? index : this.zIndex
|
||||
const loadPromise = this._createLoadPromise(image).then(img => {
|
||||
this.images.push({ img, x, y, zIndex });
|
||||
});
|
||||
|
||||
this.imagePromises.push(loadPromise);
|
||||
return loadPromise;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成合成后的图片(返回Promise)
|
||||
* @returns {Promise<string>} 合成后的Base64图片数据
|
||||
*/
|
||||
async generate (mimeType = 'image/jpeg', quality = .8) {
|
||||
const validTypes = ['image/png', 'image/jpeg', 'image/webp'];
|
||||
mimeType = validTypes.includes(mimeType) ? mimeType : 'image/png';
|
||||
quality = Math.min(1, Math.max(0, Number(quality))) || .8;
|
||||
|
||||
await Promise.all(this.imagePromises);
|
||||
// 清空画布
|
||||
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
||||
|
||||
// 绘制所有图片
|
||||
this.images.sort((a, b) => a.zIndex - b.zIndex)
|
||||
this.images.forEach(({ img, x, y, zIndex }) => {
|
||||
this.ctx.drawImage(img, x, y);
|
||||
});
|
||||
|
||||
const b64 = this.canvas.toDataURL(mimeType, quality)
|
||||
|
||||
this.images.length = 0;
|
||||
this.imagePromises.length = 0;
|
||||
|
||||
return b64
|
||||
|
||||
}
|
||||
|
||||
// 创建加载Promise(私有方法)
|
||||
_createLoadPromise (image) {
|
||||
if (typeof image === 'string') {
|
||||
return this._loadImageFromUrl(image);
|
||||
} else if (image instanceof HTMLImageElement) {
|
||||
return this._handleExistingImage(image);
|
||||
}
|
||||
return Promise.reject(new Error('Invalid image type'));
|
||||
}
|
||||
|
||||
// 从URL加载图片
|
||||
_loadImageFromUrl (url) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const img = new Image();
|
||||
img.crossOrigin = 'anonymous';
|
||||
img.src = url;
|
||||
img.onload = () => resolve(img);
|
||||
img.onerror = reject;
|
||||
});
|
||||
}
|
||||
|
||||
// 处理已存在的Image对象
|
||||
_handleExistingImage (img) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (img.complete && img.naturalHeight !== 0) {
|
||||
resolve(img);
|
||||
} else {
|
||||
img.onload = () => resolve(img);
|
||||
img.onerror = reject;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
305
src/libs/utils.js
Normal file
305
src/libs/utils.js
Normal file
@@ -0,0 +1,305 @@
|
||||
import QRCode from "qrcode"
|
||||
export const HOST = "https://huodong2.lzlj.com"
|
||||
export const DIR = "faceFamily"
|
||||
export const API = `${HOST}/api/${DIR}/`
|
||||
export const debuglog = (text) => {
|
||||
console.log(`%c -- ${text} `, "background:green;color:#fff")
|
||||
}
|
||||
export const Storage = {
|
||||
set (name, data) {
|
||||
localStorage.setItem(name, JSON.stringify(data))
|
||||
},
|
||||
get (name) {
|
||||
return JSON.parse(localStorage.getItem(name))
|
||||
},
|
||||
clear () {
|
||||
localStorage.clear()
|
||||
},
|
||||
remove (name) {
|
||||
localStorage.removeItem(name)
|
||||
}
|
||||
}
|
||||
export const Sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
||||
export const isLogin = () => {
|
||||
return !!(Storage.get("userinfos") && Storage.get("userinfos").phone)
|
||||
// return true
|
||||
}
|
||||
export const isBaseLogin = () => {
|
||||
return Storage.get("userinfos")
|
||||
// return true
|
||||
}
|
||||
export const generateQR = async (text) => {
|
||||
return await QRCode.toDataURL(`${HOST}/${DIR}/?${text}`, {
|
||||
errorCorrectionLevel: 'H',
|
||||
width: 160,
|
||||
height: 160,
|
||||
margin:1,
|
||||
colorDark: "#000000",
|
||||
colorLight: "#e8e2cc",
|
||||
})
|
||||
}
|
||||
|
||||
export const createPop = (id, classname) => {
|
||||
const wrapper = document.createElement("div")
|
||||
wrapper.id = id
|
||||
wrapper.classList.add(classname)
|
||||
document.querySelector("body").appendChild(wrapper)
|
||||
return wrapper
|
||||
}
|
||||
export const isIos = () => {
|
||||
return /(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)
|
||||
}
|
||||
export const isWeixin = () => {
|
||||
return /MicroMessenger/i.test(window.navigator.userAgent)
|
||||
}
|
||||
export const isWeixinPlatform = () => {
|
||||
return /MicroMessenger/i.test(window.navigator.userAgent) || window.__wxjs_environment === 'miniprogram'
|
||||
}
|
||||
export const isWeibo = () => {
|
||||
return /Weibo/i.test(window.navigator.userAgent)
|
||||
}
|
||||
export const isDouyin = () => {
|
||||
return /aweme/i.test(window.navigator.userAgent)
|
||||
}
|
||||
export const isWebp = () => {
|
||||
return !![].map && document.createElement('canvas').toDataURL('image/webp').indexOf('data:image/webp') == 0
|
||||
}
|
||||
export const isMiniPage = () => {
|
||||
return window.__wxjs_environment === 'miniprogram'
|
||||
// return true
|
||||
}
|
||||
export const webpAsPng = (url) => {
|
||||
const nowebp = document.querySelector("html").classList.value.indexOf("nowebp") > -1
|
||||
return nowebp ? url.replace(".webp", ".png").replace("images/", "images/png/") : url
|
||||
}
|
||||
export const getUserBrowersName = () => {
|
||||
const ua = navigator.userAgent
|
||||
if (isWeixin()) {
|
||||
return "weixin"
|
||||
} else if (isWeibo()) {
|
||||
return "weibo"
|
||||
} else if (isDouyin()) {
|
||||
return "douyin"
|
||||
} else if (isMiniPage()) {
|
||||
return "miniprogram"
|
||||
} else {
|
||||
return "other"
|
||||
}
|
||||
}
|
||||
export const getMiniPageBtnHack = (url) => {
|
||||
//TODO确认小程序appid!!!
|
||||
let html = ''
|
||||
html += `<wx-open-launch-weapp id="launch-btn" appid="wxc911dd6c6bc128de" path="${url}"><template>`
|
||||
html += '<style>.open-btn {position:absolute;width:100%;height:100%;opacity:0}</style>'
|
||||
html += '<button class="open-btn">打开小程序</button>'
|
||||
html += '</template></wx-open-launch-weapp>'
|
||||
return html
|
||||
}
|
||||
|
||||
export const miniJumpToScene = () => {
|
||||
wx.miniProgram.navigateTo({ url: '/pages/unify/unify?orgId=200282401019674482&targetUrl=%2Fpages%2Fretail%2Forder%2Forder-list%3Ftab%3DAll%26topTab%3D1' })
|
||||
}
|
||||
export const miniJumpToCouopon = () => {
|
||||
wx.miniProgram.navigateTo({
|
||||
url: '/pages/unify/unify?orgId=200282401019674482&targetUrl=%2Fpages%2Fcoupon%2Fcoupons-list'
|
||||
})
|
||||
}
|
||||
export const miniJumpToCenter = () => {
|
||||
wx.miniProgram.navigateTo({ url: '/pages/unify/unify?currentCode=my&orgId=200282401019674482' })
|
||||
}
|
||||
//[ ]
|
||||
// export const miniJumpToCouopon = () => {
|
||||
// wx.miniProgram.navigateTo({ url: 'pages/unify/unify?orgId=200282401019674482&targetUrl=%2Fpages%2Fcoupon%2Fcoupons-list' })
|
||||
// }
|
||||
export const getParam = (name) => {
|
||||
if ('URLSearchParams' in window) {
|
||||
var params = new URLSearchParams(window.location.search)
|
||||
return params.get(name) ? params.get(name) : null
|
||||
} else {
|
||||
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i'),
|
||||
r = window.location.search.substr(1).match(reg)
|
||||
|
||||
if (r != null) return unescape(r[2])
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export const Request = async (url, data, type, noloading, noerror) => {
|
||||
let headers = {
|
||||
'Content-Type': 'application/json',
|
||||
Accept: "application/json",
|
||||
"Source": "iceSip",
|
||||
'App-Channel': getUserBrowersName(),
|
||||
refer: document.referrer,
|
||||
blackbox: Storage.get("blackbox") ? Storage.get("blackbox") : false
|
||||
}
|
||||
|
||||
if (url != "sms/sendCode" && url != "sms/authPhone" && url != "wechat/login" && url != "wechat/jssdk") {
|
||||
if (isLogin()) {
|
||||
headers.Authorization = `Bearer ${Storage.get("userinfos").api_token}`
|
||||
} else {
|
||||
Storage.clear()
|
||||
weui.alert("错误的请求")
|
||||
window.location.reload()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if (isBaseLogin() && url == "sms/authPhone") {
|
||||
headers.Authorization = `Bearer ${Storage.get("userinfos").api_token}`
|
||||
}
|
||||
|
||||
let loading = false
|
||||
if (!noloading) {
|
||||
loading = weui.loading()
|
||||
}
|
||||
|
||||
let message = "请求失败,请重试"
|
||||
|
||||
let fetchData = {
|
||||
method: type || 'POST',
|
||||
headers: new Headers(headers),
|
||||
}
|
||||
|
||||
if (fetchData.method == "POST") {
|
||||
fetchData.body = JSON.stringify(data)
|
||||
}
|
||||
|
||||
let requrl = `${API}${url}`
|
||||
|
||||
if (type === "GET") {
|
||||
let paramArr = []
|
||||
Object.keys(data).forEach(v => {
|
||||
paramArr.push(`${v}=${data[v]}`)
|
||||
})
|
||||
requrl = paramArr.length === 0 ? `${requrl}` : `${requrl}?${paramArr.join("&")}`
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`${requrl}`, fetchData)
|
||||
const result = await response.json()
|
||||
message = result.message || message
|
||||
if (getParam("debug")) {
|
||||
console.log("url:", url)
|
||||
console.log("data:", data)
|
||||
console.log(response)
|
||||
console.log(result)
|
||||
}
|
||||
|
||||
loading && loading.hide()
|
||||
|
||||
if (response.status == 200 || response.status == 201) {
|
||||
return { res: response, json: result }
|
||||
} else if (response.status == 401) {
|
||||
Storage.clear()
|
||||
weui.alert("错误的请求")
|
||||
window.location.reload()
|
||||
return
|
||||
} else {
|
||||
if (!noerror) {
|
||||
weui.alert(message)
|
||||
}
|
||||
return { res: response, json: result }
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
if (!noerror) {
|
||||
weui.alert(message)
|
||||
}
|
||||
loading && loading.hide()
|
||||
}
|
||||
}
|
||||
|
||||
export const RequestImg = async (url, data, type, noloading, noerror) => {
|
||||
let headers = {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
Accept: "application/json",
|
||||
"Source": "faceFamily",
|
||||
'App-Channel': getUserBrowersName(),
|
||||
refer: document.referrer,
|
||||
blackbox: Storage.get("blackbox") ? Storage.get("blackbox") : false
|
||||
}
|
||||
|
||||
if (url != "sms/sendCode" && url != "sms/authPhone" && url != "wechat/login" && url != "wechat/jssdk") {
|
||||
if (isLogin()) {
|
||||
headers.Authorization = `Bearer ${Storage.get("userinfos").api_token}`
|
||||
} else {
|
||||
Storage.clear()
|
||||
weui.alert("错误的请求")
|
||||
window.location.reload()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if (isBaseLogin() && url == "sms/authPhone") {
|
||||
headers.Authorization = `Bearer ${Storage.get("userinfos").api_token}`
|
||||
}
|
||||
|
||||
let loading = false
|
||||
if (!noloading) {
|
||||
loading = weui.loading()
|
||||
}
|
||||
|
||||
let message = "请求失败,请重试"
|
||||
|
||||
let fetchData = {
|
||||
method: type || 'POST',
|
||||
headers: new Headers(headers),
|
||||
}
|
||||
|
||||
if (fetchData.method == "POST") {
|
||||
fetchData.body = JSON.stringify(data)
|
||||
}
|
||||
|
||||
let requrl = 'https://huodong2.lzlj.com/api/faceFamily/upload/image'
|
||||
|
||||
if (type === "GET") {
|
||||
let paramArr = []
|
||||
Object.keys(data).forEach(v => {
|
||||
paramArr.push(`${v}=${data[v]}`)
|
||||
})
|
||||
requrl = paramArr.length === 0 ? `${requrl}` : `${requrl}?${paramArr.join("&")}`
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`${requrl}`, fetchData)
|
||||
const result = await response.json()
|
||||
message = result.message || message
|
||||
if (getParam("debug")) {
|
||||
console.log("url:", url)
|
||||
console.log("data:", data)
|
||||
console.log(response)
|
||||
console.log(result)
|
||||
}
|
||||
|
||||
loading && loading.hide()
|
||||
|
||||
if (response.status == 200 || response.status == 201) {
|
||||
return { res: response, json: result }
|
||||
} else if (response.status == 401) {
|
||||
Storage.clear()
|
||||
weui.alert("错误的请求")
|
||||
window.location.reload()
|
||||
return
|
||||
} else {
|
||||
if (!noerror) {
|
||||
weui.alert(message)
|
||||
}
|
||||
return { res: response, json: result }
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
if (!noerror) {
|
||||
weui.alert(message)
|
||||
}
|
||||
loading && loading.hide()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// let str = ''
|
||||
// let s = ''
|
||||
// for (let index = 0; index < 60; index++) {
|
||||
// str += `import denglong2${index} from "./images/frame/denglong2/denglong2_${index}.webp";`
|
||||
// s+=`denglong2${index},`
|
||||
// }
|
||||
Reference in New Issue
Block a user