item.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. import common from '../../utils/util';
  2. import main from '../../utils/main';
  3. const app = getApp();
  4. var TaskList = [];
  5. var isPlaying = false;
  6. var innerAudioContext1;
  7. var timeoutPlayAudio;
  8. var tempPlayUrl = "";
  9. var tempPlayUrlLocal = "";
  10. var timeoutEveryCard = 0;
  11. var isFinished=0;
  12. Page({
  13. data: {
  14. IsSelect: true,
  15. Color: { ID: 1 },
  16. },
  17. onReady: function () {
  18. var that = this;
  19. innerAudioContext1 = wx.createInnerAudioContext();
  20. innerAudioContext1.onPlay(() => {
  21. console.log('开始播放')
  22. });
  23. innerAudioContext1.onError((res) => {
  24. console.log("innerAudioContext1.errMsg:" + res.errMsg);
  25. console.log("innerAudioContext1.errCode:" + res.errCode);
  26. that.audioCtx.setSrc(tempPlayUrl);
  27. that.audioCtx.play();
  28. });
  29. this.audioCtx = wx.createAudioContext('myAudio');
  30. },
  31. onLoad: function (options) {
  32. var that = this;
  33. that.setData({
  34. IsShowAnswer: [0, 0],
  35. Color: main.getDetailColor(app.globalData.ColorIndex),
  36. Containnerheight: main.getWindowHeight(),
  37. });
  38. if (options.type)
  39. that.init2(options);
  40. else
  41. that.init(options);
  42. that.getColor();
  43. isFinished = 0;
  44. if (app.globalData.BaiduToken === "")
  45. main.getBaiduToken();
  46. },
  47. init: function (options) {
  48. var that = this;
  49. that.setData({
  50. TaskInfo: {},
  51. TestType: options.testtype,
  52. TestName1: options.name1,
  53. TestName2: options.name2,
  54. });
  55. main.getData('GetHanziUnitWords?UnitID=' + options.unitsid, function (data) {
  56. if (data) {
  57. TaskList = [];
  58. var words = data;
  59. for (var k = 0; k < words.length; k++) {
  60. var taskInfo = {
  61. FontSize: 144,
  62. Content: [
  63. { ContentType: 0, Content: [] },
  64. { ContentType: 1, Content: [] },
  65. { ContentType: 2, Content: [] },
  66. ]
  67. };
  68. taskInfo.Word=words[k].Name;
  69. var pinyinNormal = "(" + main.getPinyinNormal(words[k].Pinyin) + ")";
  70. taskInfo.ReadString = words[k].Name + pinyinNormal + "," + words[k].CombineWords + "的" + words[k].Name + pinyinNormal;
  71. if (that.data.TestType == "read") {
  72. taskInfo.Content[0].Content = ["怎么念"];
  73. var question = words[k].CombineWords;
  74. question = common.ReplaceAllString(question, words[k].Name, "(" + words[k].Name + ")");
  75. taskInfo.Content[1].Content = question;
  76. var answer = "[读 src='" + taskInfo.ReadString + "']" + words[k].Pinyin + "[/读]";
  77. taskInfo.Content[2].Content = answer;
  78. if (words[k].CombineWords.length > 6) {
  79. taskInfo.FontSize = 108;
  80. if (words[k].CombineWords.length > 10) {
  81. taskInfo.FontSize = 68;
  82. }
  83. }
  84. }
  85. else if (that.data.TestType == "write") {
  86. taskInfo.Content[0].Content = ["怎么写"];
  87. var question = words[k].CombineWords;
  88. var question2 = common.ReplaceAllString(question, words[k].Name, words[k].Pinyin);
  89. question = "[读 src='" + taskInfo.ReadString + "']" + question2 + "[/读]"
  90. taskInfo.Content[1].Content = question;
  91. var answer = words[k].Name;
  92. answer += "[图 w='650' h='650']" + words[k].BiShunUrl + "[/图]";
  93. taskInfo.Content[2].Content = answer;
  94. if (question2.length > 6) {
  95. taskInfo.FontSize = 108;
  96. if (question2.length > 10) {
  97. taskInfo.FontSize = 68;
  98. if (question2.length > 14) {
  99. taskInfo.FontSize = 48;
  100. }
  101. }
  102. }
  103. }
  104. taskInfo.ContentNew = main.changeStringToView(taskInfo.Content);
  105. TaskList.push(taskInfo);
  106. }
  107. TaskList = common.randomArray(TaskList);
  108. that.setData({
  109. TaskInfo: TaskList[0],
  110. TaskLength: TaskList.length,
  111. CurrentIndex: 0,
  112. });
  113. common.getStorageValue(that, "TestAutioPlay", 1, function () {
  114. if (that.data.TestAutioPlay == 1 && that.data.TestType == "write") {
  115. that.playSound({ currentTarget: { dataset: { content: TaskList[that.data.CurrentIndex].ReadString } } })
  116. }
  117. });
  118. }
  119. });
  120. },
  121. init2: function (options) {
  122. var that = this;
  123. var TestTask2=wx.getStorageSync("TestTask2");
  124. var name = TestTask2.Name.split("#");
  125. that.setData({
  126. TaskInfo: {},
  127. ID:TestTask2.ID,
  128. TestType: TestTask2.TestType,
  129. TestName1: name[0],
  130. TestName2: name[1],
  131. });
  132. TestTask2.List = common.randomArray(TestTask2.List);
  133. var list = [];
  134. var i = 0;
  135. for (var i = 0; i < TestTask2.List.length;i++){
  136. if (TestTask2.List[i].Result){
  137. list.push(TestTask2.List[i]);
  138. }
  139. }
  140. var currentIndex=list.length;
  141. for (var i = 0; i < TestTask2.List.length; i++) {
  142. if (!TestTask2.List[i].Result) {
  143. list.push(TestTask2.List[i]);
  144. }
  145. }
  146. TaskList = list;
  147. if (currentIndex >= TaskList.length || options.type=="restart"){
  148. currentIndex=0;
  149. for (var i = 0; i < TaskList.length; i++) {
  150. delete TaskList[i].Result;
  151. }
  152. }
  153. that.setData({
  154. TaskLength: TaskList.length,
  155. CurrentIndex: currentIndex,
  156. TaskInfo: TaskList[currentIndex],
  157. });
  158. common.getStorageValue(that, "TestAutioPlay", 1, function () {
  159. if (that.data.TestAutioPlay == 1 && that.data.TestType == "write") {
  160. that.playSound({ currentTarget: { dataset: { content: TaskList[that.data.CurrentIndex].ReadString } } })
  161. }
  162. });
  163. },
  164. //进入下一张题卡
  165. gotoNext: function (e) {
  166. var that = this;
  167. that.setData({
  168. IsShowAnswer: [0, 0],
  169. });
  170. var btnnumber = e.currentTarget.dataset.btnnumber;
  171. if (btnnumber>="0"){
  172. that.setSoundFile(0);
  173. if (btnnumber==="4")
  174. TaskList[that.data.CurrentIndex].Result = 1;
  175. else if (btnnumber === "0")
  176. TaskList[that.data.CurrentIndex].Result = -1;
  177. else if (btnnumber === "1")
  178. TaskList[that.data.CurrentIndex].Result = 0;
  179. that.data.CurrentIndex++;
  180. }
  181. else{
  182. that.setSoundFile(2);
  183. TaskList[that.data.CurrentIndex].Result=null;
  184. that.data.CurrentIndex--;
  185. }
  186. //结束
  187. if (that.data.CurrentIndex>=TaskList.length){
  188. isFinished = 1;
  189. that.onFinished();
  190. }
  191. else if (that.data.CurrentIndex <0) {
  192. wx.showToast({
  193. title: '到开始了',
  194. });
  195. that.setSoundFile(-1);
  196. }
  197. else{
  198. that.setData({
  199. TaskInfo: TaskList[that.data.CurrentIndex],
  200. CurrentIndex: that.data.CurrentIndex,
  201. });
  202. if (that.data.TestAutioPlay == 1 && that.data.TestType == "write") {
  203. that.playSound({ currentTarget: { dataset: { content: TaskList[that.data.CurrentIndex].ReadString } } })
  204. }
  205. }
  206. //console.log(that.data.TestType);
  207. //console.log(JSON.stringify(that.data.TaskInfo));
  208. },
  209. onPullDownRefresh: function () {
  210. var that = this;
  211. that.setData({
  212. IsShowAnswer: [0, 0],
  213. });
  214. wx.stopPullDownRefresh();
  215. },
  216. onShowAnswer: function (e) {
  217. var that = this;
  218. var clicktype = e.currentTarget.dataset.type;
  219. if (!(clicktype && clicktype == "all" && app.globalData.ClickType == 0)) {
  220. that.setData({
  221. IsShowAnswer: [1, 1],
  222. });
  223. if (that.data.TestAutioPlay == 1 && that.data.TestType == "read") {
  224. that.playSound({ currentTarget: { dataset: { content: TaskList[that.data.CurrentIndex].ReadString } } })
  225. }
  226. }
  227. },
  228. getColor: function () {
  229. var that = this;
  230. if (wx.setBackgroundColor) {
  231. wx.setBackgroundColor({
  232. backgroundColor: that.data.Color.BackColor,
  233. backgroundColorTop: that.data.Color.BackColor,
  234. backgroundColorBottom: that.data.Color.BackColor,
  235. })
  236. }
  237. if (wx.setNavigationBarColor) {
  238. wx.setNavigationBarColor({
  239. frontColor: "#ffffff",
  240. backgroundColor: that.data.Color.BackColor,
  241. })
  242. }
  243. },
  244. playSound: function (e) {
  245. clearTimeout(timeoutPlayAudio);
  246. var that = this;
  247. var str = e.currentTarget.dataset.content;
  248. var url;
  249. if (str == "recorder") {
  250. url = e.currentTarget.dataset.soundmark;
  251. }
  252. else if (str.indexOf("英 [") >= 0 || str.indexOf("美 [") >= 0) {
  253. str = str.replace("英 [", "[");
  254. str = str.replace("美 [", "[");
  255. url = e.currentTarget.dataset.soundmark;
  256. }
  257. else if (e.currentTarget.dataset.soundmark && e.currentTarget.dataset.soundmark != "undefined") {
  258. var soundmark = e.currentTarget.dataset.soundmark;
  259. if (soundmark && soundmark.indexOf("http") < 0) {
  260. url = app.globalData.audioUrlBaidu;
  261. url = url.replace("[token]", app.globalData.BaiduToken);
  262. url = url.replace("[word]", soundmark);
  263. }
  264. else {
  265. url = soundmark;
  266. }
  267. }
  268. else {
  269. url = app.globalData.audioUrlBaidu;
  270. url = url.replace("[token]", app.globalData.BaiduToken);
  271. url = url.replace("[word]", str);
  272. }
  273. if (url.indexOf("http") > 0)
  274. url = url.substr(url.indexOf("http"));
  275. if (url.indexOf("'") > 0)
  276. url = common.ReplaceAllString(url, "'", "");
  277. url = url.replace("http://", "https://");
  278. url = encodeURI(url);
  279. if (!isPlaying || tempPlayUrl == "" || url != tempPlayUrl) {
  280. isPlaying = true;
  281. timeoutPlayAudio = setTimeout(function () {
  282. isPlaying = false;
  283. }, 60000);
  284. wx.showLoading({
  285. title: '音频下载中',
  286. mask: true,
  287. });
  288. setTimeout(function () {
  289. wx.hideLoading();
  290. }, 30000);
  291. wx.downloadFile({
  292. url: url,
  293. success(res) {
  294. wx.hideLoading();
  295. if (res.statusCode === 200) {
  296. tempPlayUrlLocal = res.tempFilePath;
  297. that.audioCtx.setSrc(res.tempFilePath);
  298. that.audioCtx.play();
  299. tempPlayUrl = url;
  300. }
  301. },
  302. fail(err) {
  303. wx.hideLoading();
  304. }
  305. });
  306. }
  307. else {
  308. this.audioCtx.pause();
  309. if (innerAudioContext1)
  310. innerAudioContext1.stop();
  311. isPlaying = false;
  312. }
  313. },
  314. audioBindEnded: function () {
  315. isPlaying = false;
  316. },
  317. audioBindError: function (err) {
  318. console.log(err);
  319. innerAudioContext1.src = tempPlayUrlLocal;
  320. innerAudioContext1.play();
  321. main.getBaiduToken();
  322. },
  323. setSoundFile: function (isFinish) { //isFinish=3搁置 =2撤退 =1结束 =0正常 =-1开始
  324. var that = this;
  325. var url = "/pages/sounds/";
  326. if (isFinish == 1) {
  327. url += "end.mp3";
  328. } else if (isFinish == 2) {
  329. url += "short3.mp3";
  330. } else if (isFinish == 3) {
  331. url += "short1.mp3";
  332. } else {
  333. url += "short2.mp3";
  334. }
  335. if (isFinish >= 0) {
  336. innerAudioContext1.src = url;
  337. innerAudioContext1.play();
  338. }
  339. wx.pageScrollTo({
  340. scrollTop: 0,
  341. });
  342. },
  343. onAutoPlay:function(e){
  344. var id=e.currentTarget.dataset.id;
  345. this.setData({
  346. TestAutioPlay:id,
  347. });
  348. wx.setStorageSync("TestAutioPlay",id);
  349. },
  350. //获取导航栏高度
  351. getBarInfo(e) {
  352. this.setData({ topBarHeight: e.detail.topBarHeight })
  353. },
  354. onFinished:function(){
  355. if (TaskList.length > 0 && this.data.CurrentIndex > 0) {
  356. var TestTask = {};
  357. if (this.data.ID)
  358. TestTask.ID=this.data.ID;
  359. TestTask.TestType = this.data.TestType;
  360. TestTask.IsFinished = isFinished;
  361. TestTask.Name = this.data.TestName1 + "#" + this.data.TestName2;
  362. TestTask.List = TaskList;
  363. wx.setStorageSync("TestTask", TestTask);
  364. wx.redirectTo({
  365. url: './report?type=new',
  366. });
  367. }
  368. else{
  369. wx.navigateBack({
  370. delta: 1,
  371. });
  372. }
  373. },
  374. onUnload:function(){
  375. wx.removeStorageSync("TestTask2");
  376. if (innerAudioContext1) {
  377. innerAudioContext1.stop();
  378. }
  379. },
  380. onShareAppMessage: function () {
  381. return {
  382. title: app.globalData.ShareTitle,
  383. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  384. imageUrl: app.globalData.ShareImage,
  385. }
  386. },
  387. })