item.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. });
  165. var wordStr="";
  166. if (options.bookid==100){
  167. 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";
  168. }
  169. else{
  170. var arrEnglist=wx.getStorageSync("EnglishAll");
  171. if (arrEnglist)
  172. arrEnglist=JSON.parse(arrEnglist);
  173. else
  174. arrEnglist=[];
  175. for(var i=0;i<arrEnglist.length;i++){
  176. if (arrEnglist[i].ID==Number(options.bookid)){
  177. for(var j=0;j<arrEnglist[i].Units.length;j++){
  178. if (arrEnglist[i].Units[j].ID==options.unitsid){
  179. wordStr=arrEnglist[i].Units[j].Words.join(",");
  180. break;
  181. }
  182. }
  183. }
  184. }
  185. }
  186. main.getData('GetTestEnglishWords?UserID='+app.globalData.userInfo.UserID+'&BookID='+options.bookid+'&LessonID='+options.unitsid+'&TestType='+options.testtype+'&Words=' + wordStr, function (data) {
  187. if (data) {
  188. TaskList = [];
  189. var words = data;
  190. for (var k = 0; k < words.length; k++) {
  191. var taskInfo = words[k];
  192. if (options.testtype=="read"){
  193. taskInfo.TagWidth=212;
  194. taskInfo.FontSize=108;
  195. }
  196. else{
  197. taskInfo.TagWidth=186;
  198. taskInfo.FontSize=36;
  199. }
  200. taskInfo.ContentNew = main.changeStringToView(taskInfo.Content);
  201. TaskList.push(taskInfo);
  202. }
  203. //TaskList = common.randomArray(TaskList);
  204. that.setData({
  205. TaskInfo: TaskList[0],
  206. TaskLength: TaskList.length,
  207. CurrentIndex: 0,
  208. });
  209. common.getStorageValue(that, "TestAutioPlay", 1, function () {
  210. if (that.data.TestAutioPlay == 1 && that.data.TestType == "write") {
  211. that.playSound({ currentTarget: { dataset: { content: TaskList[that.data.CurrentIndex].ReadString } } })
  212. }
  213. });
  214. }
  215. });
  216. },
  217. //进入下一张题卡
  218. gotoNext: function (e) {
  219. var that = this;
  220. that.setData({
  221. IsShowAnswer: [0, 0],
  222. });
  223. var btnnumber = e.currentTarget.dataset.btnnumber;
  224. if (btnnumber>="0"){
  225. that.setSoundFile(0);
  226. if (btnnumber==="4")
  227. TaskList[that.data.CurrentIndex].Result = 1;
  228. else if (btnnumber === "0")
  229. TaskList[that.data.CurrentIndex].Result = -1;
  230. else if (btnnumber === "1")
  231. TaskList[that.data.CurrentIndex].Result = 0;
  232. that.data.CurrentIndex++;
  233. }
  234. else{
  235. that.setSoundFile(2);
  236. if (TaskList[that.data.CurrentIndex].Result)
  237. TaskList[that.data.CurrentIndex].Result=null;
  238. that.data.CurrentIndex--;
  239. }
  240. //结束
  241. if (that.data.CurrentIndex>=TaskList.length){
  242. isFinished = 1;
  243. that.onFinished();
  244. }
  245. else if (that.data.CurrentIndex <0) {
  246. wx.showToast({
  247. title: '到开始了',
  248. });
  249. that.setSoundFile(-1);
  250. }
  251. else{
  252. that.setData({
  253. TaskInfo: TaskList[that.data.CurrentIndex],
  254. CurrentIndex: that.data.CurrentIndex,
  255. });
  256. if (that.data.TestAutioPlay == 1 && that.data.TestType == "write") {
  257. that.playSound({ currentTarget: { dataset: { content: TaskList[that.data.CurrentIndex].ReadString } } })
  258. }
  259. }
  260. //console.log(that.data.TestType);
  261. //console.log(JSON.stringify(that.data.TaskInfo));
  262. },
  263. onPullDownRefresh: function () {
  264. var that = this;
  265. that.setData({
  266. IsShowAnswer: [0, 0],
  267. });
  268. wx.stopPullDownRefresh();
  269. },
  270. onShowAnswer: function (e) {
  271. var that = this;
  272. var clicktype = e.currentTarget.dataset.type;
  273. if (!(clicktype && clicktype == "all" && app.globalData.ClickType == 0)) {
  274. that.setData({
  275. IsShowAnswer: [1, 1],
  276. });
  277. if (that.data.TestAutioPlay == 1 && that.data.TestType == "read") {
  278. that.playSound({ currentTarget: { dataset: { content: TaskList[that.data.CurrentIndex].ReadString } } })
  279. }
  280. }
  281. },
  282. getColor: function () {
  283. var that = this;
  284. if (wx.setBackgroundColor) {
  285. wx.setBackgroundColor({
  286. backgroundColor: that.data.Color.BackColor,
  287. backgroundColorTop: that.data.Color.BackColor,
  288. backgroundColorBottom: that.data.Color.BackColor,
  289. })
  290. }
  291. if (wx.setNavigationBarColor) {
  292. wx.setNavigationBarColor({
  293. frontColor: "#ffffff",
  294. backgroundColor: that.data.Color.BackColor,
  295. })
  296. }
  297. },
  298. playSound: function (e) {
  299. clearTimeout(timeoutPlayAudio);
  300. var that = this;
  301. var str = e.currentTarget.dataset.content;
  302. var url;
  303. if (str == "recorder") {
  304. url = e.currentTarget.dataset.soundmark;
  305. }
  306. else if (str.indexOf("英 [") >= 0 || str.indexOf("美 [") >= 0 || str.indexOf("baidu.com")>0 || str.indexOf("iciba.com")>0) {
  307. str = str.replace("英 [", "[");
  308. str = str.replace("美 [", "[");
  309. url = e.currentTarget.dataset.soundmark;
  310. if (!url)
  311. url=str;
  312. }
  313. else if (e.currentTarget.dataset.soundmark && e.currentTarget.dataset.soundmark != "undefined") {
  314. var soundmark = e.currentTarget.dataset.soundmark;
  315. if (soundmark && soundmark.indexOf("http") < 0) {
  316. url = app.globalData.audioUrlBaidu;
  317. url = url.replace("[token]", app.globalData.BaiduToken);
  318. url = url.replace("[word]", soundmark);
  319. }
  320. else {
  321. url = soundmark;
  322. }
  323. }
  324. else {
  325. url = app.globalData.audioUrlBaidu;
  326. url = url.replace("[token]", app.globalData.BaiduToken);
  327. url = url.replace("[word]", str);
  328. }
  329. if (url.indexOf("http") > 0)
  330. url = url.substr(url.indexOf("http"));
  331. if (url.indexOf("'") > 0)
  332. url = common.ReplaceAllString(url, "'", "");
  333. url = url.replace("http://", "https://");
  334. url = encodeURI(url);
  335. if (!isPlaying || tempPlayUrl == "" || url != tempPlayUrl) {
  336. isPlaying = true;
  337. timeoutPlayAudio = setTimeout(function () {
  338. isPlaying = false;
  339. }, 60000);
  340. wx.showLoading({
  341. title: '音频下载中',
  342. mask: true,
  343. });
  344. setTimeout(function () {
  345. wx.hideLoading();
  346. }, 30000);
  347. wx.downloadFile({
  348. url: url,
  349. success(res) {
  350. wx.hideLoading();
  351. if (res.statusCode === 200) {
  352. tempPlayUrlLocal = res.tempFilePath;
  353. if (that.audioCtx){
  354. that.audioCtx.setSrc(res.tempFilePath);
  355. that.audioCtx.play();
  356. }
  357. tempPlayUrl = url;
  358. }
  359. },
  360. fail(err) {
  361. wx.hideLoading();
  362. }
  363. });
  364. }
  365. else {
  366. this.audioCtx.pause();
  367. if (innerAudioContext1)
  368. innerAudioContext1.stop();
  369. isPlaying = false;
  370. }
  371. },
  372. audioBindEnded: function () {
  373. isPlaying = false;
  374. },
  375. audioBindError: function (err) {
  376. console.log(err);
  377. innerAudioContext1.src = tempPlayUrlLocal;
  378. innerAudioContext1.play();
  379. main.getBaiduToken();
  380. },
  381. setSoundFile: function (isFinish) { //isFinish=3搁置 =2撤退 =1结束 =0正常 =-1开始
  382. var that = this;
  383. var url = "/pages/sounds/";
  384. if (isFinish == 1) {
  385. url += "end.mp3";
  386. } else if (isFinish == 2) {
  387. url += "short3.mp3";
  388. } else if (isFinish == 3) {
  389. url += "short1.mp3";
  390. } else {
  391. url += "short2.mp3";
  392. }
  393. if (isFinish >= 0) {
  394. innerAudioContext1.src = url;
  395. innerAudioContext1.play();
  396. }
  397. wx.pageScrollTo({
  398. scrollTop: 0,
  399. });
  400. },
  401. onAutoPlay:function(e){
  402. var id=e.currentTarget.dataset.id;
  403. this.setData({
  404. TestAutioPlay:id,
  405. });
  406. wx.setStorageSync("TestAutioPlay",id);
  407. },
  408. onFinished:function(){
  409. if (TaskList.length > 0 && this.data.CurrentIndex > 0) {
  410. var TestTask = {};
  411. if (this.data.ID)
  412. TestTask.ID=this.data.ID;
  413. TestTask.TestType = this.data.TestType;
  414. TestTask.IsFinished = isFinished;
  415. TestTask.Name = this.data.TestName1 + "#" + this.data.TestName2;
  416. TestTask.List = TaskList;
  417. TestTask.BookID=this.data.BookID;
  418. wx.setStorageSync("TestTask", TestTask);
  419. wx.redirectTo({
  420. url: './report?type=new',
  421. });
  422. }
  423. else{
  424. wx.navigateBack({
  425. delta: 1,
  426. });
  427. }
  428. },
  429. onUnload:function(){
  430. wx.removeStorageSync("TestTask2");
  431. if (innerAudioContext1) {
  432. innerAudioContext1.stop();
  433. }
  434. },
  435. onShareAppMessage: function () {
  436. return {
  437. title: app.globalData.ShareTitle,
  438. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  439. imageUrl: app.globalData.ShareImage,
  440. }
  441. },
  442. })