index.js 1.59 KB
import { IMG_URL } from '~/utils/constantData'

Component({
  options: {
    addGlobalClass: true
  },
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    type: {
      type: String,
      value: 'success' // 'success' | 'fail'
    },
    title: {
      type: String,
      value: '提交成功'
    },
    description: {
      type: String,
      value: ''
    },
    okText: {
      type: String,
      value: '我知道了'
    },
    maskClosable: {
      type: Boolean,
      value: false
    },
    useSlot: {
      type: Boolean,
      value: false
    }
  },
  data: {
    IMG_URL,
    cardAnimation: ''
  },
  observers: {
    show(n) {
      if (n) {
        this.startAnim()
      }
    }
  },
  methods: {
    open(opts = {}) {
      const { type, title, description, okText, useSlot } = opts
      this.setData({
        type: type ?? this.data.type,
        title: title ?? this.data.title,
        description: description ?? this.data.description,
        okText: okText ?? this.data.okText,
        useSlot: useSlot ?? this.data.useSlot,
        show: true
      })
      this.startAnim()
    },
    close() {
      this.setData({ show: false })
      this.triggerEvent('close')
    },
    onOk() {
      this.close()
      this.triggerEvent('ok')
    },
    onMaskTap() {
      if (this.data.maskClosable) this.close()
    },
    startAnim() {
      const animation = wx.createAnimation({
        duration: 200,
        timingFunction: 'ease'
      })
      animation.scale(1).opacity(1).step()
      this.setData({ cardAnimation: animation.export() })
    },
    noop() {}
  }
})