Commit e9cfdbc3c412bfe53f8ceb7fd97db31a1855b7e8

Authored by xwenliang
1 parent 906191f7

fix: action by id instead of index

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