路泰科技体检小程序UI设计新版本
1
wwl
5 天以前 a6cdbcfe28fcc40ebb4919f57d60fb20122e8e57
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
 * @fileoverview audio 组件
 */
const context = require('./context')
 
Component({
  data: {
    time: '00:00'
  },
  properties: {
    name: String, // 音乐名
    author: String, // 作者
    poster: String, // 海报图片地址
    autoplay: Boolean, // 是否自动播放
    controls: Boolean, // 是否显示控件
    loop: Boolean, // 是否循环播放
    src: { // 源地址
      type: String,
      observer (src) {
        this.setSrc(src)
      }
    }
  },
  created () {
    // 创建内部 context
    this._ctx = wx.createInnerAudioContext()
    this._ctx.onError(err => {
      this.setData({
        error: true
      })
      this.triggerEvent('error', err)
    })
    this._ctx.onTimeUpdate(() => {
      const time = this._ctx.currentTime
      const min = parseInt(time / 60)
      const sec = Math.ceil(time % 60)
      const data = {}
      data.time = (min > 9 ? min : '0' + min) + ':' + (sec > 9 ? sec : '0' + sec)
      // 不在拖动状态下需要更新进度条
      if (!this.lastTime) {
        data.value = time / this._ctx.duration * 100
      }
      this.setData(data)
    })
    this._ctx.onEnded(() => {
      if (!this.properties.loop) {
        this.setData({
          playing: false
        })
      }
    })
    // #ifndef ALIPAY
  },
  attached () {
    context.set(this.id, this)
    // #endif
    // #ifdef MP-ALIPAY
    context.set(this.properties.id, this)
    this.setSrc(this.properties.src)
    // #endif
  },
  // #ifdef MP-ALIPAY
  didUpdate (e) {
    if (e.src !== this.properties.src) {
      this.setSrc(this.properties.src)
    }
  },
  // #endif
  detached () {
    this._ctx.destroy()
    // #ifndef MP-ALIPAY
    context.remove(this.id)
    // #endif
    // #ifdef MP_ALIPAY
    context.remove(this.properties.id)
    // #endif
  },
  // #ifndef ALIPAY | TOUTIAO
  pageLifetimes: {
    show () {
      // 播放被后台打断时,页面显示后自动继续播放
      if (this.data.playing && this._ctx.paused) {
        this._ctx.play()
      }
    }
  },
  // #endif
  methods: {
    /**
     * @description 设置源
     * @param {string} src 源地址
     */
    setSrc (src) {
      this._ctx.autoplay = this.properties.autoplay
      this._ctx.loop = this.properties.loop
      this._ctx.src = src
      if (this.properties.autoplay && !this.data.playing) {
        this.setData({
          playing: true
        })
      }
    },
 
    /**
     * @description 播放音乐
     */
    play () {
      this._ctx.play()
      this.setData({
        playing: true
      })
      this.triggerEvent('play'
        // #ifdef MP-ALIPAY
        , {
          target: {
            id: this.props.id
          }
        }
        // #endif
      )
    },
 
    /**
     * @description 暂停音乐
     */
    pause () {
      this._ctx.pause()
      this.setData({
        playing: false
      })
      this.triggerEvent('pause')
    },
 
    /**
     * @description 设置播放速率
     * @param {Number} rate 播放速率
     */
    playbackRate (rate) {
      this._ctx.playbackRate = rate
    },
 
    /**
     * @description 停止音乐
     */
    stop () {
      this._ctx.stop()
      this.setData({
        playing: false,
        time: '00:00'
      })
      this.triggerEvent('stop')
    },
 
    /**
     * @description 控制进度
     * @param {number} sec 秒数
     */
    seek (sec) {
      this._ctx.seek(sec)
    },
 
    /**
     * @description 移动进度条
     * @param {event} e
     * @private
     */
    _seeking (e) {
      // 避免过于频繁 setData
      if (e.timeStamp - this.lastTime < 200) return
      const time = Math.round(e.detail.value / 100 * this._ctx.duration)
      const min = parseInt(time / 60)
      const sec = time % 60
      this.setData({
        time: (min > 9 ? min : '0' + min) + ':' + (sec > 9 ? sec : '0' + sec)
      })
      this.lastTime = e.timeStamp
    },
 
    /**
     * @description 进度条移动完毕
     * @param {event} e
     * @private
     */
    _seeked (e) {
      this._ctx.seek(e.detail.value / 100 * this._ctx.duration)
      this.lastTime = undefined
    }
  }
})