test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import common from '../../utils/util';
  2. import server from '../../utils/main';
  3. import pinyin from '../../utils/pinyin';
  4. const app = getApp();
  5. var currentIndex = 0;
  6. var isRight = false;
  7. Page({
  8. data: {
  9. },
  10. onLoad: function (options) {
  11. this.setData({
  12. Height: common.getSystemHeight(),
  13. });
  14. this.getList();
  15. this.audioCtx = wx.createAudioContext('myAudio');
  16. },
  17. getList: function () {
  18. var that = this;
  19. server.getData('GetHanziTestList', function (data) {
  20. if (data) {
  21. var list = data;
  22. var item = list[currentIndex];
  23. var pinyinList = pinyin.getPinyinArray();
  24. for (var i = 0; i < list.length; i++) {
  25. for (var k = 0; k < list[i].PinyinTest.length; k++) {
  26. for (var j = 0; j < pinyinList.length; j++) {
  27. if (list[i].PinyinTest[k].options == pinyinList[j][1]) {
  28. list[i].PinyinTest[k].name = pinyinList[j][2][0];
  29. break;
  30. }
  31. }
  32. }
  33. }
  34. that.setData({
  35. List: data,
  36. CurrentItem: item,
  37. });
  38. }
  39. });
  40. },
  41. selectedHanzi: function (e) {
  42. var id = e.currentTarget.dataset.id;
  43. var list = this.data.CurrentItem.PinyinTest;
  44. for (var i = 0; i < list.length; i++) {
  45. list[i].IsSelected = false;
  46. }
  47. list[id].IsSelected = true;
  48. if (list[id].IsAnswer)
  49. isRight = true;
  50. this.data.CurrentItem.PinyinTest = list;
  51. this.playAudio(list[id].options, list[id].name);
  52. this.setData({
  53. CurrentItem: this.data.CurrentItem,
  54. });
  55. },
  56. playAudio: function (pinyin, name) {
  57. if (name != undefined){
  58. if (pinyin==undefined)
  59. pinyin="";
  60. var url = app.globalData.audioUrlBaidu;
  61. url = url.replace("[token]", app.globalData.BaiduToken);
  62. url = url.replace("[word]", name + "(" + pinyin + ")");
  63. url = encodeURI(url);
  64. this.audioCtx.setSrc(url);
  65. this.audioCtx.play();
  66. }
  67. },
  68. gotoOK: function () {
  69. var that = this;
  70. var title = "错误";
  71. var icon = "none";
  72. if (isRight) {
  73. title = "正确";
  74. icon = "success";
  75. }
  76. wx.showToast({
  77. title: title,
  78. icon: icon,
  79. duration: 1000,
  80. complete: function () {
  81. isRight=false;
  82. currentIndex++;
  83. if (currentIndex >= that.data.List.length)
  84. currentIndex = 0;
  85. that.setData({
  86. CurrentItem: that.data.List[currentIndex],
  87. });
  88. }
  89. })
  90. },
  91. onShareAppMessage: function () {
  92. return {
  93. title: app.globalData.ShareTitle,
  94. path: 'pages/index/index?UserID=' + app.globalData.userInfo.UserID,
  95. imageUrl: '../../images/07001.png',
  96. }
  97. },
  98. });