item.js 13 KB

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