item.js 11 KB

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