item.js 17 KB

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