优化结构
This commit is contained in:
		
							parent
							
								
									04aebbe02d
								
							
						
					
					
						commit
						1e18d932f8
					
				@ -3,7 +3,7 @@ import hilog from '@ohos.hilog';
 | 
			
		||||
import UIAbility from '@ohos.app.ability.UIAbility';
 | 
			
		||||
import Want from '@ohos.app.ability.Want';
 | 
			
		||||
import window from '@ohos.window';
 | 
			
		||||
import { requestPermission } from '../utils/system';
 | 
			
		||||
import { requestPermission } from '../utils/SystemUtils';
 | 
			
		||||
 | 
			
		||||
export default class EntryAbility extends UIAbility {
 | 
			
		||||
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
 | 
			
		||||
 | 
			
		||||
@ -68,12 +68,12 @@ struct Home {
 | 
			
		||||
            // router.pushUrl({
 | 
			
		||||
            //   url: "pages/ProjectCheck"
 | 
			
		||||
            // }, router.RouterMode.Single)
 | 
			
		||||
            // router.pushUrl({
 | 
			
		||||
            //   url: "pages/Scan"
 | 
			
		||||
            // }, router.RouterMode.Single)
 | 
			
		||||
            router.pushUrl({
 | 
			
		||||
              url: "pages/Sign"
 | 
			
		||||
              url: "pages/Scan"
 | 
			
		||||
            }, router.RouterMode.Single)
 | 
			
		||||
            // router.pushUrl({
 | 
			
		||||
            //   url: "pages/Sign"
 | 
			
		||||
            // }, router.RouterMode.Single)
 | 
			
		||||
          })
 | 
			
		||||
        }.width("100%")
 | 
			
		||||
      }.layoutWeight(1).padding({ left: "36%", right: "36%", top: "10%" })
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,4 @@
 | 
			
		||||
import { CameraUtils } from '../utils/cameraUtils';
 | 
			
		||||
import { CameraUtils } from '../utils/CameraUtils';
 | 
			
		||||
import Header from '../components/Header';
 | 
			
		||||
import Footer from '../components/Footer';
 | 
			
		||||
import common from '@ohos.app.ability.common';
 | 
			
		||||
@ -6,17 +6,17 @@ import common from '@ohos.app.ability.common';
 | 
			
		||||
@Entry
 | 
			
		||||
@Component
 | 
			
		||||
struct Scan {
 | 
			
		||||
  @State showVideo: boolean = false
 | 
			
		||||
  @State showVideo: boolean = true
 | 
			
		||||
  private xComponentController: XComponentController = new XComponentController()
 | 
			
		||||
  private cameraUtils: CameraUtils = new CameraUtils(getContext(this) as common.UIAbilityContext)
 | 
			
		||||
  private cameraUtils?: CameraUtils
 | 
			
		||||
  private surfaceId: string = ""
 | 
			
		||||
 | 
			
		||||
  aboutToAppear(): void {
 | 
			
		||||
 | 
			
		||||
    this.cameraUtils = new CameraUtils(getContext(this) as common.UIAbilityContext)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  aboutToDisappear(): void {
 | 
			
		||||
    this.cameraUtils.destroy()
 | 
			
		||||
    this.cameraUtils?.destroy()
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  build() {
 | 
			
		||||
@ -31,8 +31,8 @@ struct Scan {
 | 
			
		||||
              controller: this.xComponentController
 | 
			
		||||
            }).onLoad(() => {
 | 
			
		||||
              this.surfaceId = this.xComponentController.getXComponentSurfaceId()
 | 
			
		||||
              this.cameraUtils.init(this.surfaceId).then(() => {
 | 
			
		||||
                this.cameraUtils.startPreview()
 | 
			
		||||
              this.cameraUtils?.init(this.surfaceId).then(() => {
 | 
			
		||||
                this.cameraUtils?.startPreview()
 | 
			
		||||
              })
 | 
			
		||||
            }).width("100%").height("100%").renderFit(RenderFit.RESIZE_CONTAIN)
 | 
			
		||||
          } else {
 | 
			
		||||
 | 
			
		||||
@ -52,33 +52,38 @@ struct Sign {
 | 
			
		||||
      Header()
 | 
			
		||||
      Column() {
 | 
			
		||||
        Text("车主签名").fontSize(32).fontColor(0x161B21).fontWeight(700).margin({ top: 36, bottom: 36 })
 | 
			
		||||
        Canvas(this.context)
 | 
			
		||||
          .backgroundColor(0xffffff)
 | 
			
		||||
          .layoutWeight(1)
 | 
			
		||||
          .width("100%")
 | 
			
		||||
          .onReady(() => {
 | 
			
		||||
            this.isReady = true
 | 
			
		||||
            this.context.fillStyle = "#ffffff"
 | 
			
		||||
            this.context.fillRect(0, 0, this.context.width, this.context.height)
 | 
			
		||||
          })
 | 
			
		||||
          .onTouch(event => {
 | 
			
		||||
            switch (event.type) {
 | 
			
		||||
              case TouchType.Down:
 | 
			
		||||
                this.onStart(event)
 | 
			
		||||
                break
 | 
			
		||||
              case TouchType.Up:
 | 
			
		||||
                this.onStop(event)
 | 
			
		||||
                break
 | 
			
		||||
              case TouchType.Move:
 | 
			
		||||
                this.onMove(event)
 | 
			
		||||
                break
 | 
			
		||||
              case TouchType.Cancel:
 | 
			
		||||
                this.onStop(event)
 | 
			
		||||
                break
 | 
			
		||||
              default:
 | 
			
		||||
                return
 | 
			
		||||
            }
 | 
			
		||||
          })
 | 
			
		||||
        if (this.base64) {
 | 
			
		||||
          Image(this.base64).layoutWeight(1).width("100%")
 | 
			
		||||
        } else {
 | 
			
		||||
          Canvas(this.context)
 | 
			
		||||
            .backgroundColor(0xffffff)
 | 
			
		||||
            .layoutWeight(1)
 | 
			
		||||
            .width("100%")
 | 
			
		||||
            .onReady(() => {
 | 
			
		||||
              this.isReady = true
 | 
			
		||||
              this.context.fillStyle = "#ffffff"
 | 
			
		||||
              this.context.fillRect(0, 0, this.context.width, this.context.height)
 | 
			
		||||
            })
 | 
			
		||||
            .onTouch(event => {
 | 
			
		||||
              switch (event.type) {
 | 
			
		||||
                case TouchType.Down:
 | 
			
		||||
                  this.onStart(event)
 | 
			
		||||
                  break
 | 
			
		||||
                case TouchType.Up:
 | 
			
		||||
                  this.onStop(event)
 | 
			
		||||
                  break
 | 
			
		||||
                case TouchType.Move:
 | 
			
		||||
                  this.onMove(event)
 | 
			
		||||
                  break
 | 
			
		||||
                case TouchType.Cancel:
 | 
			
		||||
                  this.onStop(event)
 | 
			
		||||
                  break
 | 
			
		||||
                default:
 | 
			
		||||
                  return
 | 
			
		||||
              }
 | 
			
		||||
            })
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Row() {
 | 
			
		||||
          CusButton({ label: "确认", buttonType: CusButtonType.Primary }).margin({ right: 12 }).onClick(() => {
 | 
			
		||||
            this.submit()
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,4 @@
 | 
			
		||||
import common from '@ohos.app.ability.common'
 | 
			
		||||
import common from '@ohos.app.ability.common';
 | 
			
		||||
import fs from '@ohos.file.fs';
 | 
			
		||||
import zlib from '@ohos.zlib';
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										101
									
								
								entry/src/main/ets/utils/TcpUtils.ets
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										101
									
								
								entry/src/main/ets/utils/TcpUtils.ets
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,101 @@
 | 
			
		||||
import socket from '@ohos.net.socket'
 | 
			
		||||
import util from '@ohos.util'
 | 
			
		||||
import { BusinessError } from '@ohos.base'
 | 
			
		||||
 | 
			
		||||
interface TcpClientOption {
 | 
			
		||||
  address: string
 | 
			
		||||
  port: number
 | 
			
		||||
  maxReconnectCount?: number
 | 
			
		||||
  timeout?: number
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Callback<T = string | BusinessError | void> = (message: T) => void
 | 
			
		||||
 | 
			
		||||
type EventType = "message" | "error"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TcpClient {
 | 
			
		||||
  private socket: socket.TCPSocket
 | 
			
		||||
  private address: string
 | 
			
		||||
  private port: number
 | 
			
		||||
  private maxReconnectCount: number
 | 
			
		||||
  private timeout: number
 | 
			
		||||
  private messageCallback: Map<EventType, Callback[]> = new Map()
 | 
			
		||||
 | 
			
		||||
  constructor(option?: TcpClientOption) {
 | 
			
		||||
    this.messageCallback.set("message", [])
 | 
			
		||||
    this.messageCallback.set("error", [])
 | 
			
		||||
    this.address = option.address
 | 
			
		||||
    this.port = option.port
 | 
			
		||||
    this.maxReconnectCount = option.maxReconnectCount || 5
 | 
			
		||||
    this.timeout = option.timeout || 1000 * 6
 | 
			
		||||
    this.socket = socket.constructTCPSocketInstance()
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  connect() {
 | 
			
		||||
    return this.socket.connect({
 | 
			
		||||
      address: {
 | 
			
		||||
        address: this.address,
 | 
			
		||||
        port: this.port
 | 
			
		||||
      },
 | 
			
		||||
      timeout: this.timeout
 | 
			
		||||
    }).then(() => {
 | 
			
		||||
      this.socket.on("message", (data) => {
 | 
			
		||||
        let string = util.TextDecoder.create().decodeWithStream(new Uint8Array(data.message))
 | 
			
		||||
        this.messageCallback.get("message").forEach(cb => {
 | 
			
		||||
          cb(string)
 | 
			
		||||
        })
 | 
			
		||||
      })
 | 
			
		||||
      this.socket.on("error", (error) => {
 | 
			
		||||
        this.messageCallback.get("error").forEach(cb => {
 | 
			
		||||
          cb(error)
 | 
			
		||||
        })
 | 
			
		||||
      })
 | 
			
		||||
    })
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  reconnect() {
 | 
			
		||||
    this.socket.close()
 | 
			
		||||
    this.socket = socket.constructTCPSocketInstance()
 | 
			
		||||
    this.connect()
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  on(event: EventType, cb: Callback) {
 | 
			
		||||
    switch (event) {
 | 
			
		||||
      case "message":
 | 
			
		||||
        this.messageCallback.get("message")!.push(cb)
 | 
			
		||||
        break;
 | 
			
		||||
      case "error":
 | 
			
		||||
        this.messageCallback.get("error")!.push(cb)
 | 
			
		||||
      default:
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  off(event: EventType, cb: Callback) {
 | 
			
		||||
    let index: number
 | 
			
		||||
    switch (event) {
 | 
			
		||||
      case "message":
 | 
			
		||||
        index = this.messageCallback.get("message")!.findIndex(item => item === cb)
 | 
			
		||||
        this.messageCallback.get("message")!.splice(index, 1)
 | 
			
		||||
        break;
 | 
			
		||||
      case "error":
 | 
			
		||||
        index = this.messageCallback.get("error")!.findIndex(item => item === cb)
 | 
			
		||||
        this.messageCallback.get("error")!.splice(index, 1)
 | 
			
		||||
      default:
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  send(data: string | ArrayBuffer) {
 | 
			
		||||
    return new Promise<void>((resolve, reject) => {
 | 
			
		||||
      this.socket.send({ data }).then(resolve).catch((err: BusinessError) => {
 | 
			
		||||
 | 
			
		||||
      })
 | 
			
		||||
    })
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  destroy() {
 | 
			
		||||
    this.socket.close()
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							| 
		 Before Width: | Height: | Size: 72 KiB After Width: | Height: | Size: 274 KiB  | 
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user