item.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. var url;
  308. if (str == "recorder") {
  309. url = e.currentTarget.dataset.soundmark;
  310. }
  311. else if (str.indexOf("英 [") >= 0 || str.indexOf("美 [") >= 0 || str.indexOf("baidu.com")>0 || str.indexOf("iciba.com")>0) {
  312. str = str.replace("英 [", "[");
  313. str = str.replace("美 [", "[");
  314. url = e.currentTarget.dataset.soundmark;
  315. if (!url)
  316. url=str;
  317. }
  318. else if (e.currentTarget.dataset.soundmark && e.currentTarget.dataset.soundmark != "undefined") {
  319. var soundmark = e.currentTarget.dataset.soundmark;
  320. if (soundmark && soundmark.indexOf("http") < 0) {
  321. url = app.globalData.audioUrlBaidu;
  322. url = url.replace("[token]", app.globalData.BaiduToken);
  323. url = url.replace("[word]", soundmark);
  324. }
  325. else {
  326. url = soundmark;
  327. }
  328. }
  329. else {
  330. url = app.globalData.audioUrlBaidu;
  331. url = url.replace("[token]", app.globalData.BaiduToken);
  332. url = url.replace("[word]", str);
  333. }
  334. if (url.indexOf("http") > 0)
  335. url = url.substr(url.indexOf("http"));
  336. if (url.indexOf("'") > 0)
  337. url = common.ReplaceAllString(url, "'", "");
  338. url = url.replace("http://", "https://");
  339. url = encodeURI(url);
  340. if (!isPlaying || tempPlayUrl == "" || url != tempPlayUrl) {
  341. isPlaying = true;
  342. timeoutPlayAudio = setTimeout(function () {
  343. isPlaying = false;
  344. }, 60000);
  345. wx.showLoading({
  346. title: '音频下载中',
  347. mask: true,
  348. });
  349. setTimeout(function () {
  350. wx.hideLoading();
  351. }, 30000);
  352. wx.downloadFile({
  353. url: url,
  354. success(res) {
  355. wx.hideLoading();
  356. if (res.statusCode === 200) {
  357. tempPlayUrlLocal = res.tempFilePath;
  358. if (that.audioCtx){
  359. that.audioCtx.setSrc(res.tempFilePath);
  360. that.audioCtx.play();
  361. }
  362. tempPlayUrl = url;
  363. }
  364. },
  365. fail(err) {
  366. wx.hideLoading();
  367. }
  368. });
  369. }
  370. else {
  371. this.audioCtx.pause();
  372. if (innerAudioContext1)
  373. innerAudioContext1.stop();
  374. isPlaying = false;
  375. }
  376. },
  377. audioBindEnded: function () {
  378. isPlaying = false;
  379. },
  380. audioBindError: function (err) {
  381. console.log(err);
  382. innerAudioContext1.src = tempPlayUrlLocal;
  383. innerAudioContext1.play();
  384. main.getBaiduToken();
  385. },
  386. setSoundFile: function (isFinish) { //isFinish=3搁置 =2撤退 =1结束 =0正常 =-1开始
  387. var that = this;
  388. var url = "/pages/sounds/";
  389. if (isFinish == 1) {
  390. url += "end.mp3";
  391. } else if (isFinish == 2) {
  392. url += "short3.mp3";
  393. } else if (isFinish == 3) {
  394. url += "short1.mp3";
  395. } else {
  396. url += "short2.mp3";
  397. }
  398. if (isFinish >= 0) {
  399. innerAudioContext1.src = url;
  400. innerAudioContext1.play();
  401. }
  402. wx.pageScrollTo({
  403. scrollTop: 0,
  404. });
  405. },
  406. onAutoPlay:function(e){
  407. var id=e.currentTarget.dataset.id;
  408. this.setData({
  409. TestAutioPlay:id,
  410. });
  411. wx.setStorageSync("TestAutioPlay",id);
  412. },
  413. onFinished:function(){
  414. if (TaskList.length > 0 && this.data.CurrentIndex > 0) {
  415. var TestTask = {};
  416. if (this.data.ID)
  417. TestTask.ID=this.data.ID;
  418. TestTask.TestType = this.data.TestType;
  419. TestTask.IsFinished = isFinished;
  420. TestTask.Name = this.data.TestName1 + "#" + this.data.TestName2;
  421. if (this.data.UnitID)
  422. TestTask.Name+="#"+this.data.UnitID
  423. TestTask.List = TaskList;
  424. TestTask.BookID=this.data.BookID;
  425. wx.setStorageSync("TestTask", TestTask);
  426. wx.redirectTo({
  427. url: './report?type=new',
  428. });
  429. }
  430. else{
  431. wx.navigateBack({
  432. delta: 1,
  433. });
  434. }
  435. },
  436. onUnload:function(){
  437. wx.removeStorageSync("TestTask2");
  438. if (innerAudioContext1) {
  439. innerAudioContext1.stop();
  440. }
  441. },
  442. onShareAppMessage: function () {
  443. return {
  444. title: app.globalData.ShareTitle,
  445. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  446. imageUrl: app.globalData.ShareImage,
  447. }
  448. },
  449. })