Commit e9cfdbc3c412bfe53f8ceb7fd97db31a1855b7e8

Authored by xwenliang
1 parent 906191f7

fix: action by id instead of index

... ... @@ -65,7 +65,7 @@ App({
65 65 },
66 66 /**
67 67 * @param {(add|delete)} action - 增或删
68   - * @param {object|number} [param] - 增加时是 audio 描述,删除时是索引
  68 + * @param {object|number} [param] - 增加时是 audio 描述,删除时是唯一特征 path 值
69 69 */
70 70 setAudioList(action, param){
71 71 const storageKey = 'audioList';
... ... @@ -75,8 +75,11 @@ App({
75 75 wx.setStorageSync(storageKey, list);
76 76 }
77 77 if(action === 'delete' && String(param)){
78   - list.splice(param, 1);
79   - wx.setStorageSync(storageKey, list);
  78 + const index = list.findIndex(v => v.path === param);
  79 + if(index > -1){
  80 + list.splice(index, 1);
  81 + wx.setStorageSync(storageKey, list);
  82 + }
80 83 }
81 84 return list;
82 85 }
... ...
pages/result/result.js
... ... @@ -24,16 +24,16 @@ Page({
24 24 });
25 25 },
26 26 play(e){
27   - const { index } = e.target.dataset;
28   - const target = app.globalData.getAudioList()[index];
  27 + const { path } = e.target.dataset;
  28 + const target = app.globalData.getAudioList().find(v => v.path === path);
29 29 audio && audio.destroy();
30 30 audio = wx.createInnerAudioContext({useWebAudioImplement: true});
31 31 audio.autoplay = true;
32 32 audio.src = target.path;
33 33 },
34 34 download(e){
35   - const { index } = e.target.dataset;
36   - const target = app.globalData.getAudioList()[index];
  35 + const { path } = e.target.dataset;
  36 + const target = app.globalData.getAudioList().find(v => v.path === path);
37 37 const ext = target.path.split('.').pop();
38 38 wx.shareFileMessage({
39 39 filePath: target.path,
... ... @@ -41,7 +41,7 @@ Page({
41 41 });
42 42 },
43 43 delete(e){
44   - const { index } = e.target.dataset;
  44 + const { path } = e.target.dataset;
45 45 wx.showModal({
46 46 title: '提示',
47 47 content: '确认删除吗?',
... ... @@ -49,7 +49,7 @@ Page({
49 49 if(!res.confirm){
50 50 return;
51 51 }
52   - const newList = app.globalData.setAudioList('delete', index);
  52 + const newList = app.globalData.setAudioList('delete', path);
53 53 this.setData({
54 54 audioList: formateDate(newList)
55 55 });
... ...
pages/result/result.wxml
... ... @@ -10,8 +10,8 @@
10 10 </view>
11 11 <view class="btn-view">
12 12 <view class="text-time">{{item.date}}</view>
13   - <button class="button" type="warn" size="mini" data-index="{{index}}" bindtap="delete">删除</button>
14   - <button class="button" type="primary" size="mini" data-index="{{index}}" bindtap="play">播放</button>
15   - <button class="button" type="primary" size="mini" data-index="{{index}}" bindtap="download">下载</button>
  13 + <button class="button" type="warn" size="mini" data-path="{{item.path}}" bindtap="delete">删除</button>
  14 + <button class="button" type="primary" size="mini" data-path="{{item.path}}" bindtap="play">播放</button>
  15 + <button class="button" type="primary" size="mini" data-path="{{item.path}}" bindtap="download">下载</button>
16 16 </view>
17 17 </view>
18 18 \ No newline at end of file
... ...