item.js 15 KB

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