utils.js 5.14 KB
import { getStageObj } from '@/utils'

export function getPlayerParams(options, isTest = false) {
	if (isTest) {
		return {
			atoken: uni.getStorageSync('Authorization'),
      
      // 非小程序使用外链
      // #ifndef MP-WEIXIN
      serverIp: uni.getStorageSync('serverIp'),
      // #endif
			
			userId: 8859, // 用户的id
			type: '', // ?
			id: 632, // ?
			name: '会议室门口', // ?
		
			accountId: 337,
			mallId: 9217,
			mallName: 'Demo Store2',
			bookmark: false,
			channelNo: 4,
			deviceSerial: 'F16423875',
			gateUnid: 'cf21f4a8-65c6-11ee-837e-00163e143ecd',
			ptzEnable: 0,
			
			origin: 'record',
			// aiChannelId: '办公室',
			// mallOrgName: '',
		}
	} else {
		const safeOptions = options || {}
		// const storeInfo = getStageObj('store') || {}
		const userInfo = getStageObj('user') || {}
		return {
			atoken: uni.getStorageSync('Authorization'),
			
			userId: userInfo.id, // 用户的id
			type: '',
			id: safeOptions.id, // ?
			name: safeOptions.name, // ?
      
      // 非小程序使用外链
      serverIp: uni.getStorageSync('serverIp'),
			
			accountId: safeOptions.accountId,
			mallId: safeOptions.mallId,
			mallName: safeOptions.name, // storeInfo.name
			
			bookmark: false,
			channelNo: safeOptions.patrolDeviceChannel.channelNo || '',
			deviceSerial: safeOptions.patrolDeviceChannel.deviceSerial || '',
			gateUnid: safeOptions.unid,
			ptzEnable: safeOptions.patrolDeviceChannel.ptzEnable,
			
			origin: safeOptions.origin || 'record', // processing record
		}
	}
}

function canvasToBlob(canvas) {
  return new Promise(resolve => {
    canvas.toBlob(blob => {
		const file = new File([blob], 'image.jpeg', { type: 'image/jpeg' });
		resolve(file)
	}, 'image/jpeg');
  });
}
function dataURLtoFile(dataurl, filename, filetype) {
  var arr = dataurl.split(","),
	bstr = atob(arr[1]),
	n = bstr.length,
	u8arr = new Uint8Array(n);
  
  while (n--) {
	u8arr[n] = bstr.charCodeAt(n);
  }
  return new File([u8arr], filename, {
	type: filetype
  });
}
function fileToBinary(file) {
    return new Promise((resolve, reject) => {
      const reader = new FileReader();
      reader.onload = function () {
        const arrayBuffer = reader.result; // ArrayBuffer 类型
		resolve(arrayBuffer)
        // const binaryData = new Uint8Array(arrayBuffer); // 可选:转为字节数组
        // resolve(binaryData);
      };
      reader.onerror = reject;
      reader.readAsArrayBuffer(file);
    });
}

export function getImagePath(url) {
	return new Promise((resolve, reject) => {
		uni.getImageInfo({
		  src: url,
      // src:'https://store.keliuyun.com/images/patrol%2Fscreenshot%2F20250801%2F81675f21-76e4-48f3-86b5-3f772d1e7326.jpg',
		  // src: 'https://store.keliuyun.com/images/patrol%2Fscreenshot%2F20250603%2Fb02a6a65-9ef9-436d-b7c6-e90c6cff2fd2.jpg',
		  success: function (res) {
			console.log('success', res)
		    resolve(res)
		  },
		  fail: function (err) {
		    console.error('获取图片信息失败', err)
			reject(err)
		  }
		})
	})
}
export async function getImageBase64(imgElement) {
    // 创建一个 canvas 元素
    /* const canvas = document.createElement("canvas");
    const ctx = canvas.getContext("2d"); */
	const ctx = uni.createCanvasContext()

    // 设置 canvas 的尺寸为图片的尺寸
    /* canvas.width = imgElement.width;
    canvas.height = imgElement.height; */
	// TODO: 
	canvas.width = imgElement.naturalWidth
	canvas.height = imgElement.naturalHeight
	console.log('size', imgElement.naturalWidth, imgElement.naturalHeight)

    // 将图片绘制到 canvas 上
    ctx.drawImage(imgElement, 0, 0);

    // 获取 base64 数据
    const base64 = canvas.toDataURL("image/jpeg");
	// 去掉前缀
	const base64Data = base64.split(',')[1]
	// const base64 = dataURLtoFile(canvas.toDataURL("image/jpeg"), new Date().getTime(), 'image/jpeg')
	// const file = await canvasToBlob(canvas);
	// return file
	// const base64 = await fileToBinary(file)
    // return base64;
	return base64Data
  }
  export function getImageBinary(imgUrl) {
	  return fetch(imgUrl)
	  .then(response => response.arrayBuffer())  // 或 response.blob()
	  .then(arrayBuffer => {
		// 转为 Uint8Array(二进制数据)
		const uint8Array = new Uint8Array(arrayBuffer);
		console.log("Binary data:", uint8Array);
  
		// 你可以进一步处理,比如发送给后端,保存,等。
		return uint8Array
	  })
	  .catch(error => {
		console.error("获取图片失败:", error);
	  });
  }
  
  export function getFormParams(isTest = false, gateId, mallId) {
	  if (isTest) {
		  return {
			  accountId: 337,
			  mallId: 9217,
			  gateId: 2922,
		  }
	  } else {
		  const storeInfo = getStageObj('store') || {}
		  return {
			  accountId: storeInfo.accountId,
			  mallId, // storeInfo.id
			  gateId,
		  }
	  }
  }
  
  export function getSops(list) {
	  // [{"id":218,"status":0,"score":5},{"id":219,"status":0,"score":5}]
	  return list.map(item => {
		  return {
			  id: item.value,
			  status: 0,
			  score: item.score,
		  }
	  })
  }
  
  export function toQueryString(obj) {
    const query = Object.keys(obj)
      .map(key => encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]))
      .join('&');
    return query;
  }