native.service.ts 5.0 KB
/**
 * @Description H5+插件
 * @Author ZPFly
 * @Date 2021/10/13
 */
import mediaService from "@/services/media.service";

declare global {
  interface Window {
    plus: any; //H5+插件
    $bridge: any;
  }
}
declare module "vue/types/vue" {
  interface Vue {
    $native: NativeService;
  }
}
import Vue from "vue";
import { MediaFile } from "@/model/entity/MediaFile";

class NativeService {
  init() {
    Vue.prototype.$native = this;
  }
  /**
   * @Description 拍照
   * @Author ZPFly
   * @Date 2021/10/13
   */
  capturePhoto(media: MediaFile) {
    const $bridge = window.$bridge;
    return new Promise((resolve) => {
      $bridge.takePhoto(media, (data: any) => {
        mediaService.batAdd(data).then(() => {
          alert("插入影像记录");
        });
        resolve(data);
      });
    });
  }
  /**
   * @Description 录音
   * @Author ZPFly
   * @Date 2021/10/13
   */
  captureRecord(media: MediaFile) {
    const $bridge = window.$bridge;
    return new Promise((resolve) => {
      const audio = window.plus.audio.getRecorder();
      const path = $bridge.getAudioPath();
      const startRecord = () => {
        const fileName = $bridge.generateAudioName();
        audio.record(
          { filename: `${path}/${fileName}` },
          (path: string) => {
            media.filePath = path;
            mediaService.add(media);
            startRecord();
          },
          () => {
            resolve({});
          }
        );
      };
      startRecord();
    });
  }
  /**
   * @Description 录像
   * @Author ZPFly
   * @Date 2021/10/13
   */
  captureVideo(media: MediaFile) {
    const $bridge = window.$bridge;
    return new Promise((resolve) => {
      const path = $bridge.getVideoPath();
      const startCapture = () => {
        const cmr = window.plus.camera.getCamera();
        const res = cmr.supportedVideoResolutions[0];
        const fmt = cmr.supportedVideoFormats[0];
        const fileName = $bridge.generateVideoName();
        cmr.startVideoCapture(
          (path: any) => {
            media.filePath = path;
            mediaService.add(media);
            startCapture();
            console.log("Capture video success: " + path);
          },
          (error: any) => {
            console.log("Capture video failed: " + error.message);
            resolve({});
          },
          { filename: `${path}/${fileName}`, resolution: res, format: fmt }
        );
      };
      startCapture();
    });
  }
  /**
   * @Description 二维码扫描
   * @Author ZPFly
   * @Date 2021/10/13
   */
  qrScan() {
    return new Promise((resolve) => {
      const plus = window.plus;
      // 创建Barcode扫码控件
      const barcode: any = plus.barcode.create("plusBarcode", [plus.barcode.QR], {
        top: "100px",
        left: "0px",
        width: "100%",
        height: "500px",
        position: "static",
      });
      barcode.onmarked = (type: any, result: string) => {
        console.log("扫描结果:" + type + "-" + result);
        resolve(result);
        plus.webview.currentWebview().remove(barcode);
      };
      plus.webview.currentWebview().append(barcode);
      // 开始识别
      barcode.start();
    });
  }
  /**
   * @Description 预览图片
   * @Author ZPFly
   * @Date 2021/10/13
   */
  previewImage(paths: Array<string>) {
    window.plus?.nativeUI.previewImage(paths);
  }
  /**
   * @Description 获取设备信息
   * @Author ZPFly
   * @Date 2021/10/13
   */
  getDeviceInfo() {
    const plus = window.plus;
    // getOAID 获取匿名设备标识符
    return new Promise((resolve, reject) => {
      plus.device.getInfo({
        success: (e: any) => {
          resolve(e);
          console.log("getDeviceInfo success: " + JSON.stringify(e));
        },
        fail: (e: any) => {
          reject(e);
          console.log("getDeviceInfo failed: " + JSON.stringify(e));
        },
      });
    });
  }
  /**
   * @Description 获取当前位置信息
   * @Author ZPFly
   * @Date 2021/10/13
   */
  getCurrentPosition() {
    // 使用百度地图地位模块获取位置信息
    return new Promise((resolve, reject) => {
      window.plus.geolocation.getCurrentPosition(
        function (p: any) {
          resolve(p.coords);
        },
        function (e: any) {
          reject(e);
        },
        { provider: "baidu", coordsType: "bd09ll" }
      );
    });
  }
  /**
  *@Desc 功能描述:云从人脸识别
  *@Author XWH
  *Date 2022/2/19 18:24
  */
  cloudWalkBiometrics() {
    const $bridge = window.$bridge;
    return new Promise((resolve) => {
      $bridge.cloudWalkBiometrics({},(data: any) => {
        resolve(data);
      });
    });
  }
  /**
  *@Desc 功能描述:新增日程
  *@Author XWH
  *Date 2022/2/19 18:27
  */
  addCalendar(params: any) {
    const $bridge = window.$bridge;
    return new Promise((resolve) => {
      $bridge.calendarOperate(params,(data: any) => {
        console.log('日程----'+data);
        resolve(data);
      });
    });
  }
}
const nativeService = new NativeService();
export default nativeService;