item.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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=336;
  221. if (options.bookid>="74")
  222. taskInfo.TagWidth=308;
  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>=12)
  281. taskInfo.FontSize=64;
  282. }
  283. else{
  284. taskInfo.TagWidth=186;
  285. taskInfo.FontSize=36;
  286. }
  287. taskInfo.ContentNew = main.changeStringToView(taskInfo.Content);
  288. TaskList.push(taskInfo);
  289. }
  290. //TaskList = common.randomArray(TaskList);
  291. that.setData({
  292. TaskInfo: TaskList[0],
  293. TaskLength: TaskList.length,
  294. CurrentIndex: 0,
  295. });
  296. common.getStorageValue(that, "TestAutioPlay", 1, function () {
  297. if (that.data.TestAutioPlay == 1 && that.data.TestType == "write") {
  298. that.playSound({ currentTarget: { dataset: { content: TaskList[that.data.CurrentIndex].ReadString } } })
  299. }
  300. });
  301. }
  302. });
  303. },
  304. //进入下一张题卡
  305. gotoNext: function (e) {
  306. var that = this;
  307. that.setData({
  308. IsShowAnswer: [0, 0],
  309. });
  310. var btnnumber = e.currentTarget.dataset.btnnumber;
  311. if (btnnumber>="0"){
  312. that.setSoundFile(0);
  313. if (TaskList[that.data.CurrentIndex]){
  314. if (btnnumber==="4")
  315. TaskList[that.data.CurrentIndex].Result = 1;
  316. else if (btnnumber === "0")
  317. TaskList[that.data.CurrentIndex].Result = -1;
  318. else if (btnnumber === "1")
  319. TaskList[that.data.CurrentIndex].Result = 0;
  320. }
  321. that.data.CurrentIndex++;
  322. }
  323. else{
  324. that.setSoundFile(2);
  325. if (TaskList[that.data.CurrentIndex] && TaskList[that.data.CurrentIndex].Result)
  326. TaskList[that.data.CurrentIndex].Result=null;
  327. that.data.CurrentIndex--;
  328. }
  329. //结束
  330. if (that.data.CurrentIndex>=TaskList.length){
  331. isFinished = 1;
  332. that.onFinished();
  333. }
  334. else if (that.data.CurrentIndex <0) {
  335. wx.showToast({
  336. title: '到开始了',
  337. });
  338. that.setSoundFile(-1);
  339. }
  340. else{
  341. that.setData({
  342. TaskInfo: TaskList[that.data.CurrentIndex],
  343. CurrentIndex: that.data.CurrentIndex,
  344. });
  345. if (that.data.TestAutioPlay == 1 && that.data.TestType == "write") {
  346. that.playSound({ currentTarget: { dataset: { content: TaskList[that.data.CurrentIndex].ReadString } } })
  347. }
  348. }
  349. //console.log(that.data.TestType);
  350. //console.log(JSON.stringify(that.data.TaskInfo));
  351. },
  352. onPullDownRefresh: function () {
  353. var that = this;
  354. that.setData({
  355. IsShowAnswer: [0, 0],
  356. });
  357. wx.stopPullDownRefresh();
  358. },
  359. onShowAnswer: function (e) {
  360. var that = this;
  361. var clicktype = e.currentTarget.dataset.type;
  362. if (!(clicktype && clicktype == "all" && app.globalData.ClickType == 0)) {
  363. that.setData({
  364. IsShowAnswer: [1, 1],
  365. });
  366. if (that.data.TestAutioPlay == 1 && that.data.TestType == "read") {
  367. if (TaskList[that.data.CurrentIndex] && TaskList[that.data.CurrentIndex].ReadString)
  368. that.playSound({ currentTarget: { dataset: { content: TaskList[that.data.CurrentIndex].ReadString } } })
  369. }
  370. }
  371. },
  372. getColor: function () {
  373. var that = this;
  374. if (wx.setBackgroundColor) {
  375. wx.setBackgroundColor({
  376. backgroundColor: that.data.Color.BackColor,
  377. backgroundColorTop: that.data.Color.BackColor,
  378. backgroundColorBottom: that.data.Color.BackColor,
  379. })
  380. }
  381. if (wx.setNavigationBarColor) {
  382. wx.setNavigationBarColor({
  383. frontColor: "#ffffff",
  384. backgroundColor: that.data.Color.BackColor,
  385. })
  386. }
  387. },
  388. playSound: function (e) {
  389. clearTimeout(timeoutPlayAudio);
  390. var that = this;
  391. var str = e.currentTarget.dataset.content;
  392. if (str){
  393. var url;
  394. if (str == "recorder") {
  395. url = e.currentTarget.dataset.soundmark;
  396. }
  397. else if (str.indexOf("英 [") >= 0 || str.indexOf("美 [") >= 0 || str.indexOf("baidu.com")>0 || str.indexOf("iciba.com")>0 || str.indexOf("myqcloud.com")>0) {
  398. str = str.replace("英 [", "[");
  399. str = str.replace("美 [", "[");
  400. url = e.currentTarget.dataset.soundmark;
  401. if (!url)
  402. url=str;
  403. }
  404. else if (str.indexOf("pinyin")>0){
  405. url=str;
  406. }
  407. else if (e.currentTarget.dataset.soundmark && e.currentTarget.dataset.soundmark != "undefined") {
  408. var soundmark = e.currentTarget.dataset.soundmark;
  409. if (soundmark && soundmark.indexOf("http") < 0) {
  410. url = app.globalData.audioUrlBaidu;
  411. url = url.replace("[token]", app.globalData.BaiduToken);
  412. url = url.replace("[word]", soundmark);
  413. }
  414. else {
  415. url = soundmark;
  416. }
  417. }
  418. else {
  419. url = app.globalData.audioUrlBaidu;
  420. url = url.replace("[token]", app.globalData.BaiduToken);
  421. url = url.replace("[word]", str);
  422. }
  423. if (url.indexOf("http") > 0)
  424. url = url.substr(url.indexOf("http"));
  425. if (url.indexOf("'") > 0)
  426. url = common.ReplaceAllString(url, "'", "");
  427. url = url.replace("http://", "https://");
  428. url = encodeURI(url);
  429. if (!isPlaying || tempPlayUrl == "" || url != tempPlayUrl) {
  430. isPlaying = true;
  431. timeoutPlayAudio = setTimeout(function () {
  432. isPlaying = false;
  433. }, 60000);
  434. wx.showLoading({
  435. title: '音频下载中',
  436. mask: true,
  437. });
  438. setTimeout(function () {
  439. wx.hideLoading();
  440. }, 30000);
  441. wx.downloadFile({
  442. url: url,
  443. success(res) {
  444. wx.hideLoading();
  445. tempPlayUrlLocal = res.tempFilePath;
  446. innerAudioContext1.src = res.tempFilePath;
  447. innerAudioContext1.play();
  448. tempPlayUrl = url;
  449. },
  450. fail(err) {
  451. wx.hideLoading();
  452. if (url.indexOf("sp0.baidu.com")>0){
  453. var soundmark=url.substring(url.indexOf("text=")+5,url.indexOf("&spd"));
  454. url = app.globalData.audioUrlYoudao;
  455. url = url.replace("[word]", soundmark);
  456. tempPlayUrlLocal = url;
  457. that.audioCtx.setSrc(url);
  458. that.audioCtx.play();
  459. tempPlayUrl = url;
  460. }
  461. }
  462. });
  463. }
  464. else {
  465. if (this.audioCtx)
  466. this.audioCtx.pause();
  467. if (innerAudioContext1)
  468. innerAudioContext1.stop();
  469. isPlaying = false;
  470. }
  471. }
  472. },
  473. audioBindEnded: function () {
  474. isPlaying = false;
  475. },
  476. audioBindError: function (err) {
  477. console.log(err);
  478. innerAudioContext1.src = tempPlayUrlLocal;
  479. innerAudioContext1.play();
  480. main.getBaiduToken();
  481. },
  482. setSoundFile: function (isFinish) { //isFinish=3搁置 =2撤退 =1结束 =0正常 =-1开始
  483. var that = this;
  484. var url = "/pages/sounds/";
  485. if (isFinish == 1) {
  486. url += "end.mp3";
  487. } else if (isFinish == 2) {
  488. url += "short3.mp3";
  489. } else if (isFinish == 3) {
  490. url += "short1.mp3";
  491. } else {
  492. url += "short2.mp3";
  493. }
  494. if (isFinish >= 0) {
  495. innerAudioContext1.src = url;
  496. innerAudioContext1.play();
  497. }
  498. wx.pageScrollTo({
  499. scrollTop: 0,
  500. });
  501. },
  502. onAutoPlay:function(e){
  503. var id=e.currentTarget.dataset.id;
  504. this.setData({
  505. TestAutioPlay:id,
  506. });
  507. wx.setStorageSync("TestAutioPlay",id);
  508. },
  509. onFinished:function(){
  510. if (TaskList.length > 0 && this.data.CurrentIndex > 0) {
  511. var TestTask = {};
  512. if (this.data.ID)
  513. TestTask.ID=this.data.ID;
  514. TestTask.TestType = this.data.TestType;
  515. TestTask.IsFinished = isFinished;
  516. TestTask.Name = this.data.TestName1 + "#" + this.data.TestName2;
  517. if (this.data.UnitID)
  518. TestTask.Name+="#"+this.data.UnitID
  519. TestTask.List = TaskList;
  520. TestTask.BookID=this.data.BookID;
  521. wx.setStorageSync("TestTask", TestTask);
  522. wx.redirectTo({
  523. url: './report?type=new',
  524. });
  525. }
  526. else{
  527. wx.navigateBack({
  528. delta: 1,
  529. });
  530. }
  531. },
  532. onUnload:function(){
  533. wx.removeStorageSync("TestTask2");
  534. if (innerAudioContext1) {
  535. innerAudioContext1.stop();
  536. }
  537. },
  538. showBishunOrKaiti:function(e){
  539. var that=this;
  540. main.downloadBishunKaitiImage(
  541. e.currentTarget.dataset.url,
  542. e.currentTarget.dataset.serverurl,
  543. e.currentTarget.dataset.type,
  544. that.data.TaskInfo.ContentNew.Field,function(fields){
  545. that.data.TaskInfo.ContentNew.Field=fields;
  546. that.setData({
  547. TaskInfo: that.data.TaskInfo,
  548. });
  549. });
  550. },
  551. onShareAppMessage: function () {
  552. return {
  553. title: app.globalData.ShareTitle,
  554. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  555. imageUrl: app.globalData.ShareImage,
  556. }
  557. },
  558. })