item.js 18 KB

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