item.js 11 KB

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