index.uts 1.5 KB
Newer Older
VK1688's avatar
VK1688 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
import { OpenLocation, OpenLocationOptions, OpenLocationSuccessImpl, OpenLocationErrorCode } from "../interface.uts"
import { OpenLocationFailImpl } from "../unierror.uts"

export const openLocation : OpenLocation = function (options : OpenLocationOptions) {
  const uuid = `${Date.now()}${Math.floor(Math.random() * 1e7)}`
  const baseEventName = `uni_open_location_${uuid}`
  const readyEventName = `${baseEventName}_ready`
  const optionsEventName = `${baseEventName}_options`
  const successEventName = `${baseEventName}_success`
  const failEventName = `${baseEventName}_fail`
  
  const readyEventId = uni.$on(readyEventName, () => {
    uni.$emit(optionsEventName, JSON.parse(JSON.stringify(options)))
  })
  const successEventId = uni.$on(successEventName, () => {
    const res = new OpenLocationSuccessImpl()
    options.success?.(res)
    options.complete?.(res)
  })
  const failEventId = uni.$on(failEventName, (errCode : OpenLocationErrorCode) => {
    const res = new OpenLocationFailImpl(errCode)
    options.fail?.(res)
    options.complete?.(res)
  })
  uni.openDialogPage({
    url: `/uni_modules/uni-openLocation/pages/openLocation/openLocation?readyEventName=${readyEventName}&optionsEventName=${optionsEventName}&successEventName=${successEventName}&failEventName=${failEventName}`,
    fail(err) {
      const res = new OpenLocationFailImpl(4)
      options.fail?.(res)
      options.complete?.(res)
      uni.$off(readyEventName, readyEventId)
      uni.$off(successEventName, successEventId)
      uni.$off(failEventName, failEventId)
    }
  })
};