item.js 14 KB

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