item.js 14 KB

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