item.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. },
  30. onLoad: function (options) {
  31. var that = this;
  32. that.setData({
  33. BookID:options.bookid,
  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. if (options.bookid<100){
  42. if ((options.bookid>=25 && options.bookid<=42) || (options.bookid>=74 && options.bookid<=85))
  43. that.initAncientPoetry(options);
  44. else if (options.bookid>=43 && options.bookid<=44)
  45. that.initPinyin(options);
  46. else if (options.bookid>=45 && options.bookid<=60)
  47. that.initCi(options);
  48. else
  49. that.init(options);
  50. }
  51. else
  52. that.initEng(options);
  53. }
  54. that.getColor();
  55. this.audioCtx = wx.createAudioContext('myAudio');
  56. isFinished = 0;
  57. if (app.globalData.BaiduToken === "")
  58. main.getBaiduToken();
  59. },
  60. init: function (options) {
  61. var that = this;
  62. that.setData({
  63. TaskInfo: {},
  64. TestType: options.testtype,
  65. TestName1: options.name1,
  66. TestName2: options.name2,
  67. });
  68. main.GetHanziUnitWords(options.unitsid,that.data.TestType, function (data) {
  69. //debugger;
  70. if (data) {
  71. TaskList=data;
  72. if (that.data.BookID==73){
  73. for (var k = 0; k < TaskList.length; k++) {
  74. TaskList[k].TagWidth=270;
  75. }
  76. }
  77. TaskList = common.randomArray(TaskList);
  78. that.setData({
  79. TaskInfo: TaskList[0],
  80. TaskLength: TaskList.length,
  81. CurrentIndex: 0,
  82. });
  83. common.getStorageValue(that, "TestAutioPlay", 1, function () {
  84. if (that.data.TestAutioPlay == 1 && that.data.TestType == "write") {
  85. that.playSound({ currentTarget: { dataset: { content: TaskList[that.data.CurrentIndex].ReadString } } })
  86. }
  87. });
  88. }
  89. });
  90. },
  91. initPinyin: function (options) {
  92. var that = this;
  93. that.setData({
  94. TaskInfo: {},
  95. TestType: options.testtype,
  96. TestName1: options.name1,
  97. TestName2: options.name2,
  98. });
  99. main.getData('GetPinyinUnitWords?TestType='+options.testtype+'&UserID='+app.globalData.userInfo.UserID+'&BookID='+options.bookid+'&UnitID='+options.unitsid+'&Word='+options.name2, function (data) {
  100. if (data) {
  101. TaskList = [];
  102. var list = data;
  103. for (var k = 0; k < list.length; k++) {
  104. var taskInfo = list[k];
  105. taskInfo.TagWidth=230;
  106. taskInfo.FontSize=108;
  107. taskInfo.ContentNew = main.changeStringToView(taskInfo.Content);
  108. TaskList.push(taskInfo);
  109. }
  110. TaskList = common.randomArray(TaskList);
  111. that.setData({
  112. TaskInfo: TaskList[0],
  113. TaskLength: TaskList.length,
  114. CurrentIndex: 0,
  115. });
  116. common.getStorageValue(that, "TestAutioPlay", 1, function () {
  117. if (that.data.TestAutioPlay == 1 && that.data.TestType == "write") {
  118. that.playSound({ currentTarget: { dataset: { content: TaskList[that.data.CurrentIndex].ReadString } } })
  119. }
  120. });
  121. }
  122. });
  123. },
  124. initCi: function (options) {
  125. var that = this;
  126. that.setData({
  127. TaskInfo: {},
  128. TestType: options.testtype,
  129. TestName1: options.name1,
  130. TestName2: options.name2,
  131. });
  132. main.getData('GetHanziUnitWords?UserID='+app.globalData.userInfo.UserID+'&UnitID='+options.unitsid+'&TestType='+options.testtype, function (data) {
  133. if (data) {
  134. TaskList = [];
  135. var list = data;
  136. for (var k = 0; k < list.length; k++) {
  137. var taskInfo = list[k];
  138. taskInfo.TagWidth=140;
  139. taskInfo.FontSize=64;
  140. taskInfo.ContentNew = main.changeStringToView(taskInfo.Content);
  141. TaskList.push(taskInfo);
  142. }
  143. //TaskList = common.randomArray(TaskList);
  144. that.setData({
  145. TaskInfo: TaskList[0],
  146. TaskLength: TaskList.length,
  147. CurrentIndex: 0,
  148. });
  149. common.getStorageValue(that, "TestAutioPlay", 1, function () {
  150. if (that.data.TestAutioPlay == 1 && that.data.TestType == "write") {
  151. that.playSound({ currentTarget: { dataset: { content: TaskList[that.data.CurrentIndex].ReadString } } })
  152. }
  153. });
  154. }
  155. });
  156. },
  157. init2: function (options) {
  158. var that = this;
  159. var TestTask2=wx.getStorageSync("TestTask2");
  160. var name = TestTask2.Name.split("#");
  161. that.setData({
  162. TaskInfo: {},
  163. ID:TestTask2.ID,
  164. TestType: TestTask2.TestType,
  165. TestName1: name[0],
  166. TestName2: name[1],
  167. BookID:TestTask2.BookID,
  168. });
  169. if (that.data.BookID<=12)
  170. TestTask2.List = common.randomArray(TestTask2.List);
  171. var list = [];
  172. var i = 0;
  173. for (var i = 0; i < TestTask2.List.length;i++){
  174. if (TestTask2.List[i].Result){
  175. list.push(TestTask2.List[i]);
  176. }
  177. }
  178. var currentIndex=list.length;
  179. for (var i = 0; i < TestTask2.List.length; i++) {
  180. if (!TestTask2.List[i].Result) {
  181. list.push(TestTask2.List[i]);
  182. }
  183. }
  184. TaskList = list;
  185. if (that.data.BookID==73){
  186. for (var k = 0; k < TaskList.length; k++) {
  187. TaskList[k].TagWidth=270;
  188. }
  189. }
  190. if (currentIndex >= TaskList.length || options.type=="restart"){
  191. currentIndex=0;
  192. for (var i = 0; i < TaskList.length; i++) {
  193. delete TaskList[i].Result;
  194. }
  195. }
  196. that.setData({
  197. TaskLength: TaskList.length,
  198. CurrentIndex: currentIndex,
  199. TaskInfo: TaskList[currentIndex],
  200. });
  201. common.getStorageValue(that, "TestAutioPlay", 1, function () {
  202. if (that.data.TestAutioPlay == 1 && that.data.TestType == "write") {
  203. that.playSound({ currentTarget: { dataset: { content: TaskList[that.data.CurrentIndex].ReadString } } })
  204. }
  205. });
  206. },
  207. initAncientPoetry:function(options){
  208. var that=this;
  209. that.setData({
  210. TaskInfo: {},
  211. TestType: options.testtype,
  212. TestName1: options.name1,
  213. TestName2: "古诗文",
  214. });
  215. main.getData('GetTestAncientPoetryList?UserID='+app.globalData.userInfo.UserID+'&BookID='+options.bookid, function (data) {
  216. if (data) {
  217. TaskList = [];
  218. var list = data;
  219. for (var k = 0; k < list.length; k++) {
  220. var taskInfo = list[k];
  221. taskInfo.TagWidth=306;
  222. if (options.bookid>="74")
  223. taskInfo.TagWidth=520;
  224. if (taskInfo.Word.length>8)
  225. taskInfo.FontSize=48;
  226. else
  227. taskInfo.FontSize=64;
  228. taskInfo.ContentNew = main.changeStringToView(taskInfo.Content);
  229. TaskList.push(taskInfo);
  230. }
  231. //TaskList = common.randomArray(TaskList);
  232. that.setData({
  233. TaskInfo: TaskList[0],
  234. TaskLength: TaskList.length,
  235. CurrentIndex: 0,
  236. });
  237. }
  238. });
  239. },
  240. initEng: function (options) {
  241. var that = this;
  242. that.setData({
  243. TaskInfo: {},
  244. TestType: options.testtype,
  245. TestName1: options.name1,
  246. TestName2: options.name2,
  247. UnitID:options.unitsid,
  248. });
  249. var wordStr="";
  250. if (options.bookid==100){
  251. wordStr="A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
  252. }
  253. else{
  254. var arrEnglist=wx.getStorageSync("EnglishAll");
  255. if (arrEnglist)
  256. arrEnglist=JSON.parse(arrEnglist);
  257. else
  258. arrEnglist=[];
  259. for(var i=0;i<arrEnglist.length;i++){
  260. if (arrEnglist[i].ID==Number(options.bookid)){
  261. for(var j=0;j<arrEnglist[i].Units.length;j++){
  262. if (arrEnglist[i].Units[j].ID==options.unitsid){
  263. wordStr=arrEnglist[i].Units[j].Words.join(",");
  264. break;
  265. }
  266. }
  267. }
  268. }
  269. }
  270. main.getData('GetTestEnglishWords?UserID='+app.globalData.userInfo.UserID+'&BookID='+options.bookid+'&LessonID='+options.unitsid+'&TestType='+options.testtype+'&Words=' + wordStr, function (data) {
  271. if (data) {
  272. TaskList = [];
  273. var words = data;
  274. for (var k = 0; k < words.length; k++) {
  275. var taskInfo = words[k];
  276. if (options.testtype=="read"){
  277. taskInfo.TagWidth=212;
  278. taskInfo.FontSize=108;
  279. if (taskInfo.Content[1].Content.length>=16)
  280. taskInfo.FontSize=48;
  281. else if (taskInfo.Content[1].Content.length>=13)
  282. taskInfo.FontSize=52;
  283. else if (taskInfo.Content[1].Content.length>=12)
  284. taskInfo.FontSize=64;
  285. }
  286. else{
  287. taskInfo.TagWidth=186;
  288. taskInfo.FontSize=36;
  289. }
  290. taskInfo.ContentNew = main.changeStringToView(taskInfo.Content);
  291. TaskList.push(taskInfo);
  292. }
  293. //TaskList = common.randomArray(TaskList);
  294. that.setData({
  295. TaskInfo: TaskList[0],
  296. TaskLength: TaskList.length,
  297. CurrentIndex: 0,
  298. });
  299. common.getStorageValue(that, "TestAutioPlay", 1, function () {
  300. if (that.data.TestAutioPlay == 1 && that.data.TestType == "write") {
  301. that.playSound({ currentTarget: { dataset: { content: TaskList[that.data.CurrentIndex].ReadString } } })
  302. }
  303. });
  304. }
  305. });
  306. },
  307. //进入下一张题卡
  308. gotoNext: function (e) {
  309. var that = this;
  310. that.setData({
  311. IsShowAnswer: [0, 0],
  312. });
  313. var btnnumber = e.currentTarget.dataset.btnnumber;
  314. if (btnnumber>="0"){
  315. that.setSoundFile(0);
  316. if (TaskList[that.data.CurrentIndex]){
  317. if (btnnumber==="4")
  318. TaskList[that.data.CurrentIndex].Result = 1;
  319. else if (btnnumber === "0")
  320. TaskList[that.data.CurrentIndex].Result = -1;
  321. else if (btnnumber === "1")
  322. TaskList[that.data.CurrentIndex].Result = 0;
  323. }
  324. that.data.CurrentIndex++;
  325. }
  326. else{
  327. that.setSoundFile(2);
  328. if (TaskList[that.data.CurrentIndex] && TaskList[that.data.CurrentIndex].Result)
  329. TaskList[that.data.CurrentIndex].Result=null;
  330. that.data.CurrentIndex--;
  331. }
  332. //结束
  333. if (that.data.CurrentIndex>=TaskList.length){
  334. isFinished = 1;
  335. that.onFinished();
  336. }
  337. else if (that.data.CurrentIndex <0) {
  338. wx.showToast({
  339. title: '到开始了',
  340. });
  341. that.setSoundFile(-1);
  342. }
  343. else{
  344. that.setData({
  345. TaskInfo: TaskList[that.data.CurrentIndex],
  346. CurrentIndex: that.data.CurrentIndex,
  347. });
  348. if (that.data.TestAutioPlay == 1 && that.data.TestType == "write") {
  349. that.playSound({ currentTarget: { dataset: { content: TaskList[that.data.CurrentIndex].ReadString } } })
  350. }
  351. }
  352. //console.log(that.data.TestType);
  353. //console.log(JSON.stringify(that.data.TaskInfo));
  354. },
  355. onPullDownRefresh: function () {
  356. var that = this;
  357. that.setData({
  358. IsShowAnswer: [0, 0],
  359. });
  360. wx.stopPullDownRefresh();
  361. },
  362. onShowAnswer: function (e) {
  363. var that = this;
  364. var clicktype = e.currentTarget.dataset.type;
  365. if (!(clicktype && clicktype == "all" && app.globalData.ClickType == 0)) {
  366. that.setData({
  367. IsShowAnswer: [1, 1],
  368. });
  369. if (that.data.TestAutioPlay == 1 && that.data.TestType == "read") {
  370. if (TaskList[that.data.CurrentIndex] && TaskList[that.data.CurrentIndex].ReadString)
  371. that.playSound({ currentTarget: { dataset: { content: TaskList[that.data.CurrentIndex].ReadString } } })
  372. }
  373. }
  374. },
  375. getColor: function () {
  376. var that = this;
  377. if (wx.setBackgroundColor) {
  378. wx.setBackgroundColor({
  379. backgroundColor: that.data.Color.BackColor,
  380. backgroundColorTop: that.data.Color.BackColor,
  381. backgroundColorBottom: that.data.Color.BackColor,
  382. })
  383. }
  384. if (wx.setNavigationBarColor) {
  385. wx.setNavigationBarColor({
  386. frontColor: "#ffffff",
  387. backgroundColor: that.data.Color.BackColor,
  388. })
  389. }
  390. },
  391. playSound: function (e) {
  392. clearTimeout(timeoutPlayAudio);
  393. var that = this;
  394. var str = e.currentTarget.dataset.content;
  395. if (str){
  396. var url;
  397. if (str == "recorder") {
  398. url = e.currentTarget.dataset.soundmark;
  399. }
  400. else if (str.indexOf("英 [") >= 0 || str.indexOf("美 [") >= 0 || str.indexOf("baidu.com")>0 || str.indexOf("iciba.com")>0 || str.indexOf("myqcloud.com")>0) {
  401. str = str.replace("英 [", "[");
  402. str = str.replace("美 [", "[");
  403. url = e.currentTarget.dataset.soundmark;
  404. if (!url)
  405. url=str;
  406. }
  407. else if (str.indexOf("pinyin")>0){
  408. url=str;
  409. }
  410. else if (e.currentTarget.dataset.soundmark && e.currentTarget.dataset.soundmark != "undefined") {
  411. var soundmark = e.currentTarget.dataset.soundmark;
  412. if (soundmark && soundmark.indexOf("http") < 0) {
  413. url = app.globalData.audioUrlBaidu;
  414. url = url.replace("[token]", app.globalData.BaiduToken);
  415. url = url.replace("[word]", soundmark);
  416. }
  417. else {
  418. url = soundmark;
  419. }
  420. }
  421. else {
  422. url = app.globalData.audioUrlBaidu;
  423. url = url.replace("[token]", app.globalData.BaiduToken);
  424. url = url.replace("[word]", str);
  425. }
  426. if (url.indexOf("http") > 0)
  427. url = url.substr(url.indexOf("http"));
  428. if (url.indexOf("'") > 0)
  429. url = common.ReplaceAllString(url, "'", "");
  430. url = url.replace("http://", "https://");
  431. url = encodeURI(url);
  432. if (!isPlaying || tempPlayUrl == "" || url != tempPlayUrl) {
  433. isPlaying = true;
  434. timeoutPlayAudio = setTimeout(function () {
  435. isPlaying = false;
  436. }, 60000);
  437. wx.showLoading({
  438. title: '音频下载中',
  439. mask: true,
  440. });
  441. setTimeout(function () {
  442. wx.hideLoading();
  443. }, 30000);
  444. wx.downloadFile({
  445. url: url,
  446. success(res) {
  447. wx.hideLoading();
  448. tempPlayUrlLocal = res.tempFilePath;
  449. innerAudioContext1.src = res.tempFilePath;
  450. innerAudioContext1.play();
  451. tempPlayUrl = url;
  452. },
  453. fail(err) {
  454. wx.hideLoading();
  455. if (url.indexOf("sp0.baidu.com")>0){
  456. var soundmark=url.substring(url.indexOf("text=")+5,url.indexOf("&spd"));
  457. url = app.globalData.audioUrlYoudao;
  458. url = url.replace("[word]", soundmark);
  459. tempPlayUrlLocal = url;
  460. that.audioCtx.setSrc(url);
  461. that.audioCtx.play();
  462. tempPlayUrl = url;
  463. }
  464. }
  465. });
  466. }
  467. else {
  468. if (this.audioCtx)
  469. this.audioCtx.pause();
  470. if (innerAudioContext1)
  471. innerAudioContext1.stop();
  472. isPlaying = false;
  473. }
  474. }
  475. },
  476. audioBindEnded: function () {
  477. isPlaying = false;
  478. },
  479. audioBindError: function (err) {
  480. console.log(err);
  481. innerAudioContext1.src = tempPlayUrlLocal;
  482. innerAudioContext1.play();
  483. main.getBaiduToken();
  484. },
  485. setSoundFile: function (isFinish) { //isFinish=3搁置 =2撤退 =1结束 =0正常 =-1开始
  486. var that = this;
  487. var url = "/pages/sounds/";
  488. if (isFinish == 1) {
  489. url += "end.mp3";
  490. } else if (isFinish == 2) {
  491. url += "short3.mp3";
  492. } else if (isFinish == 3) {
  493. url += "short1.mp3";
  494. } else {
  495. url += "short2.mp3";
  496. }
  497. if (isFinish >= 0) {
  498. innerAudioContext1.src = url;
  499. innerAudioContext1.play();
  500. }
  501. wx.pageScrollTo({
  502. scrollTop: 0,
  503. });
  504. },
  505. onAutoPlay:function(e){
  506. var id=e.currentTarget.dataset.id;
  507. this.setData({
  508. TestAutioPlay:id,
  509. });
  510. wx.setStorageSync("TestAutioPlay",id);
  511. },
  512. onExport:function(){
  513. var that=this;
  514. that.setSoundFile(0);
  515. while (that.data.CurrentIndex<TaskList.length) {
  516. TaskList[that.data.CurrentIndex].Result = 0;
  517. that.data.CurrentIndex++;
  518. }
  519. isFinished = 1;
  520. that.onFinished();
  521. },
  522. onFinished:function(){
  523. if (TaskList.length > 0 && this.data.CurrentIndex > 0) {
  524. var TestTask = {};
  525. if (this.data.ID)
  526. TestTask.ID=this.data.ID;
  527. TestTask.TestType = this.data.TestType;
  528. TestTask.IsFinished = isFinished;
  529. TestTask.Name = this.data.TestName1 + "#" + this.data.TestName2;
  530. //TestTask.Name = "#" + this.data.TestName2;
  531. if (this.data.UnitID)
  532. TestTask.Name+="#"+this.data.UnitID
  533. TestTask.List = TaskList;
  534. TestTask.BookID=this.data.BookID;
  535. wx.setStorageSync("TestTask", TestTask);
  536. wx.redirectTo({
  537. url: './report?type=new',
  538. });
  539. }
  540. else{
  541. wx.navigateBack({
  542. delta: 1,
  543. });
  544. }
  545. },
  546. onUnload:function(){
  547. wx.removeStorageSync("TestTask2");
  548. if (innerAudioContext1) {
  549. innerAudioContext1.stop();
  550. }
  551. },
  552. showBishunOrKaiti:function(e){
  553. var that=this;
  554. main.downloadBishunKaitiImage(
  555. e.currentTarget.dataset.url,
  556. e.currentTarget.dataset.serverurl,
  557. e.currentTarget.dataset.type,
  558. that.data.TaskInfo.ContentNew.Field,function(fields){
  559. that.data.TaskInfo.ContentNew.Field=fields;
  560. that.setData({
  561. TaskInfo: that.data.TaskInfo,
  562. });
  563. });
  564. },
  565. onShareAppMessage: function () {
  566. return {
  567. title: app.globalData.ShareTitle,
  568. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  569. imageUrl: app.globalData.ShareImage,
  570. }
  571. },
  572. })