Browse Source

图片水印修改

dev
wangmingyong@163.com 2 weeks ago
parent
commit
64dedb1dc8
  1. 55
      zdxt-web-client/zdxt-efcode-enterprise-app/src/util/w6sMedia.js

55
zdxt-web-client/zdxt-efcode-enterprise-app/src/util/w6sMedia.js

@ -72,24 +72,63 @@ export const ensureW6sAuth = () => {
return w6sAuthPromise
}
// 拍照并加水印(@w6s/sdk 2.x:timeEnable/locationEnable 时 SDK 自动在 content 后拼接当前时间与定位地址)
// 返回扁平结构 PhotoInfoAndMediaId: { imageURL, key, imageInfo, mediaId }
// 定位缓存(避免每次拍照都重新搜星,SDK 内部 locationEnable=true 时会阻塞等 GPS)
let _cachedLocation = null
let _locationCacheTime = 0
const LOCATION_CACHE_TTL = 2 * 60 * 1000 // 2 分钟内复用同一位置
const getCachedLocation = async () => {
if (_cachedLocation && Date.now() - _locationCacheTime < LOCATION_CACHE_TTL) {
console.log('[w6sMedia] 使用缓存定位:', _cachedLocation)
return _cachedLocation
}
await ensureW6sAuth()
_cachedLocation = await new Promise((resolve, reject) => {
w6s.location.getLocation({ success: resolve, fail: reject })
})
_locationCacheTime = Date.now()
console.log('[w6sMedia] 定位获取成功并已缓存:', _cachedLocation)
return _cachedLocation
}
// 格式化时间 yyyy-MM-dd HH:mm(与 SDK 内部 getTimeStr 格式一致)
const getTimeStr = () => {
const d = new Date()
const pad = (n) => String(n).padStart(2, '0')
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`
}
// 拍照并加水印
// 自己拼接时间+定位到 content,设 timeEnable/locationEnable 为 false 跳过 SDK 内部阻塞式 getLocation 调用
export const takePhotoWithWaterMark = async () => {
await ensureW6sAuth()
console.log('[w6sMedia] 鉴权完成, 准备调起拍照')
console.log('[w6sMedia] 鉴权完成, 准备获取定位')
// 预获取定位(带缓存),第二次拍照起直接用缓存不搜星
let locationStr = ''
try {
const loc = await getCachedLocation()
locationStr = (loc.city || '') + (loc.district || '') + (loc.street || '')
console.log('[w6sMedia] 定位地址:', locationStr)
} catch (e) {
console.warn('[w6sMedia] 获取定位失败, 水印不含地址:', e)
}
const content = `${getTimeStr()} ${locationStr}`.trim()
console.log('[w6sMedia] 水印 content:', content, '准备调起拍照')
return new Promise((resolve, reject) => {
w6s.image.takePhotoAndAddWaterMark({
content: '',
content,
fontSize: 14,
color: '#FF5858',
markDisable: false,
timeEnable: true,
locationEnable: true,
timeEnable: false, // 时间已自己拼入 content
locationEnable: false, // 定位已自己拼入 content,跳过 SDK 内部 getLocation 阻塞
success: (res) => {
console.log('[w6sMedia] takePhotoAndAddWaterMark success 原始返回:', res)
// 实际返回结构为 { result: { imageURL, key, imageInfo, mediaId } }(SDK jsonParser 包了一层 result)
const result = res?.result || res || {}
console.log('[w6sMedia] 返回字段 imageURL:', result?.imageURL, 'key:', result?.key, 'imageInfo:', result?.imageInfo)
console.log('[w6sMedia] 返回字段 imageURL:', result?.imageURL, 'key:', result?.key)
resolve(result)
},
fail: (err) => {

Loading…
Cancel
Save