1 changed files with 130 additions and 55 deletions
@ -1,83 +1,158 @@ |
|||||
import * as w6s from 'szient-js-sdk' |
import * as w6s from 'szient-js-sdk' |
||||
|
import sha1 from 'crypto-js/sha1' |
||||
|
import { siqCode } from '@/api/enterprise' |
||||
import { baseUrl } from '@/config/env' |
import { baseUrl } from '@/config/env' |
||||
import { $globalStore } from '@/common/globalStore' |
import { $globalStore } from '@/common/globalStore' |
||||
|
|
||||
|
// ========== w6s 鉴权(参照 enterpriseHome.vue 的写法) ==========
|
||||
|
const generateRandomNumber = () => { |
||||
|
const min = 100000 |
||||
|
const max = 999999 |
||||
|
const randomNum = (Math.floor(Math.random() * (max - min + 1)) + min) |
||||
|
return randomNum.toString().padStart(6, '0') |
||||
|
} |
||||
|
|
||||
|
let w6sAuthPromise = null |
||||
|
|
||||
|
// 确保 w6s 鉴权完成,缓存 Promise 避免重复鉴权
|
||||
|
export const ensureW6sAuth = () => { |
||||
|
if (w6sAuthPromise) return w6sAuthPromise |
||||
|
w6sAuthPromise = new Promise((resolve, reject) => { |
||||
|
siqCode(null, success => { |
||||
|
let access_key = '' |
||||
|
let access_secret = '' |
||||
|
if (success.success) { |
||||
|
access_key = success.data.accessKey |
||||
|
access_secret = success.data.accessSecret |
||||
|
} |
||||
|
|
||||
|
if (!access_key || !access_secret) { |
||||
|
w6sAuthPromise = null |
||||
|
reject(new Error('获取深i企认证信息错误: access_key 或 access_secret 为空')) |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
const nonce = generateRandomNumber() |
||||
|
const timestamp = `${Date.now()}` |
||||
|
|
||||
|
const str = [access_secret, nonce, timestamp] |
||||
|
.sort() |
||||
|
.reduce((s, item) => s + item, '') |
||||
|
|
||||
|
const signature = sha1(str).toString().toLowerCase() |
||||
|
|
||||
|
w6s.config({ |
||||
|
access_key: access_key, |
||||
|
nonce: nonce, |
||||
|
timestamp: timestamp, |
||||
|
signature: signature, |
||||
|
}).then((res) => { |
||||
|
console.log('w6s 鉴权成功', res) |
||||
|
}).catch((err) => { |
||||
|
w6sAuthPromise = null |
||||
|
reject(err) |
||||
|
}) |
||||
|
|
||||
|
w6s.ready(() => { |
||||
|
console.log('w6s ready') |
||||
|
resolve() |
||||
|
}) |
||||
|
}, error => { |
||||
|
w6sAuthPromise = null |
||||
|
reject(error) |
||||
|
}) |
||||
|
}) |
||||
|
return w6sAuthPromise |
||||
|
} |
||||
|
|
||||
// 拍照并加水印(时间戳 + 经纬度自动叠加到照片)
|
// 拍照并加水印(时间戳 + 经纬度自动叠加到照片)
|
||||
export const takePhotoWithWaterMark = ({ |
export const takePhotoWithWaterMark = async ({ |
||||
content = '', |
content = '', |
||||
fontSize = 14, |
fontSize = 14, |
||||
color = '#FF5858', |
color = '#FF5858', |
||||
timeEnable = true, |
timeEnable = true, |
||||
locationEnable = true, |
locationEnable = true, |
||||
} = {}) => new Promise((resolve, reject) => { |
} = {}) => { |
||||
w6s.image.takePhotoAndAddWaterMark({ |
await ensureW6sAuth() |
||||
content, |
return new Promise((resolve, reject) => { |
||||
fontSize, |
w6s.image.takePhotoAndAddWaterMark({ |
||||
color, |
content, |
||||
markDisable: false, |
fontSize, |
||||
timeEnable, |
color, |
||||
locationEnable, |
markDisable: false, |
||||
success: resolve, |
timeEnable, |
||||
fail: reject, |
locationEnable, |
||||
|
success: resolve, |
||||
|
fail: reject, |
||||
|
}) |
||||
}) |
}) |
||||
}) |
} |
||||
|
|
||||
// 录制视频
|
// 录制视频
|
||||
export const startVideoRecorder = ({ |
export const startVideoRecorder = async ({ |
||||
duration = 60, |
duration = 60, |
||||
quality = 1, // 0 高清 / 1 一般 / 2 流畅
|
quality = 1, // 0 高清 / 1 一般 / 2 流畅
|
||||
front = false, |
front = false, |
||||
syncSystemAlbum = false, |
syncSystemAlbum = false, |
||||
} = {}) => new Promise((resolve, reject) => { |
} = {}) => { |
||||
w6s.video.startVideoRecoder({ |
await ensureW6sAuth() |
||||
duration, |
return new Promise((resolve, reject) => { |
||||
quality, |
w6s.video.startVideoRecoder({ |
||||
front, |
duration, |
||||
sync_system_album: syncSystemAlbum, |
quality, |
||||
success: resolve, |
front, |
||||
fail: reject, |
sync_system_album: syncSystemAlbum, |
||||
|
success: resolve, |
||||
|
fail: reject, |
||||
|
}) |
||||
}) |
}) |
||||
}) |
} |
||||
|
|
||||
// 获取定位(经纬度)
|
// 获取定位(经纬度)
|
||||
export const getLocation = () => new Promise((resolve, reject) => { |
export const getLocation = async () => { |
||||
w6s.location.getLocation({ success: resolve, fail: reject }) |
await ensureW6sAuth() |
||||
}) |
return new Promise((resolve, reject) => { |
||||
|
w6s.location.getLocation({ success: resolve, fail: reject }) |
||||
|
}) |
||||
|
} |
||||
|
|
||||
// 上传本地文件到后端 /app-enf/common/file/upload
|
// 上传本地文件到后端 /app-enf/common/file/upload
|
||||
// 使用 w6s.file.upload(Cordova FileTransfer),直接传本地路径,无需 base64 转换
|
// 使用 w6s.file.upload(Cordova FileTransfer),直接传本地路径,无需 base64 转换
|
||||
export const uploadLocalFileToBackend = ({ |
export const uploadLocalFileToBackend = async ({ |
||||
fileURL, |
fileURL, |
||||
fileName = 'upload', |
fileName = 'upload', |
||||
mimeType = 'image/jpeg', |
mimeType = 'image/jpeg', |
||||
}) => new Promise((resolve, reject) => { |
}) => { |
||||
const token = $globalStore.useCommon?.token || '' |
await ensureW6sAuth() |
||||
// w6s.file.upload 需要完整 URL
|
return new Promise((resolve, reject) => { |
||||
const api = `${baseUrl}/app-enf/common/file/upload` |
const token = $globalStore.useCommon?.token || '' |
||||
const server = /^https?:/.test(api) ? api : `${window.location.origin}${api}` |
// w6s.file.upload 需要完整 URL
|
||||
|
const api = `${baseUrl}/app-enf/common/file/upload` |
||||
|
const server = /^https?:/.test(api) ? api : `${window.location.origin}${api}` |
||||
|
|
||||
const opts = new window.FileUploadOptions() |
const opts = new window.FileUploadOptions() |
||||
opts.fileKey = 'file' |
opts.fileKey = 'file' |
||||
opts.fileName = fileName |
opts.fileName = fileName |
||||
opts.mimeType = mimeType |
opts.mimeType = mimeType |
||||
opts.headers = { |
opts.headers = { |
||||
Authorization: token ? `Bearer ${token}` : '', |
Authorization: token ? `Bearer ${token}` : '', |
||||
clientid: '9e6eb24cd4e169a98b0d2cd8034d877b', |
clientid: '9e6eb24cd4e169a98b0d2cd8034d877b', |
||||
} |
} |
||||
const fileIns = new w6s.file.upload(opts) |
const fileIns = new w6s.file.upload(opts) |
||||
fileIns.upload({ |
fileIns.upload({ |
||||
fileURL, |
fileURL, |
||||
server, |
server, |
||||
trustAllHosts: false, |
trustAllHosts: false, |
||||
success: (res) => { |
success: (res) => { |
||||
// Cordova FileTransfer 返回 res.response 为字符串
|
// Cordova FileTransfer 返回 res.response 为字符串
|
||||
try { |
try { |
||||
const parsed = typeof res.response === 'string' ? JSON.parse(res.response) : res.response |
const parsed = typeof res.response === 'string' ? JSON.parse(res.response) : res.response |
||||
resolve(parsed) |
resolve(parsed) |
||||
} catch (e) { |
} catch (e) { |
||||
resolve(res.response) |
resolve(res.response) |
||||
} |
} |
||||
}, |
}, |
||||
fail: reject, |
fail: reject, |
||||
|
}) |
||||
}) |
}) |
||||
}) |
} |
||||
|
|||||
Loading…
Reference in new issue