item.js 17 KB

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