examine.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. },
  30. onLoad: function (options) {
  31. var that = this;
  32. that.setData({
  33. Category:options.category1,
  34. BookID:options.bookid,
  35. LibraryID:options.libraryid,
  36. UnitID:options.unitsid,
  37. LibraryName:options.LibraryName,
  38. BookName:options.BookName,
  39. UnitName:options.UnitName,
  40. TestTypeName:options.TestTypeName,
  41. IsShowAnswer: [0, 0],
  42. Color: main.getDetailColor(app.globalData.ColorIndex),
  43. Containnerheight: main.getWindowHeight(),
  44. });
  45. that.init(options);
  46. that.getColor();
  47. this.audioCtx = wx.createAudioContext('myAudio');
  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. });
  58. main.getData('GetMiaoguoTestExamine?UserID='+app.globalData.userInfo.UserID+'&Category1='+options.category1+'&BookID='+options.bookid+'&Name='+options.UnitName+'&UnitID='+options.unitsid+'&TestType='+options.testtype, function (data) {
  59. if (data) {
  60. TaskList=data;
  61. TaskList = common.randomArray(TaskList);
  62. that.setData({
  63. TaskInfo: TaskList[0],
  64. TaskLength: TaskList.length,
  65. CurrentIndex: 0,
  66. });
  67. //debugger;
  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. //进入下一张题卡
  77. gotoNext: function (e) {
  78. var that = this;
  79. that.setData({
  80. IsShowAnswer: [0, 0],
  81. });
  82. var btnnumber = e.currentTarget.dataset.btnnumber;
  83. if (btnnumber>="0"){
  84. that.setSoundFile(0);
  85. if (TaskList[that.data.CurrentIndex]){
  86. if (btnnumber==="4")
  87. TaskList[that.data.CurrentIndex].Result = 1;
  88. else if (btnnumber === "0")
  89. TaskList[that.data.CurrentIndex].Result = -1;
  90. else if (btnnumber === "1")
  91. TaskList[that.data.CurrentIndex].Result = 0;
  92. }
  93. that.data.CurrentIndex++;
  94. }
  95. else{
  96. that.setSoundFile(2);
  97. if (TaskList[that.data.CurrentIndex] && TaskList[that.data.CurrentIndex].Result)
  98. TaskList[that.data.CurrentIndex].Result=null;
  99. that.data.CurrentIndex--;
  100. }
  101. //结束
  102. if (that.data.CurrentIndex>=TaskList.length){
  103. isFinished = 1;
  104. that.onFinished();
  105. }
  106. else if (that.data.CurrentIndex <0) {
  107. wx.showToast({
  108. title: '到开始了',
  109. });
  110. that.setSoundFile(-1);
  111. }
  112. else{
  113. that.setData({
  114. TaskInfo: TaskList[that.data.CurrentIndex],
  115. CurrentIndex: that.data.CurrentIndex,
  116. });
  117. if (that.data.TestAutioPlay == 1 && that.data.TestType == "write") {
  118. that.playSound({ currentTarget: { dataset: { content: TaskList[that.data.CurrentIndex].ReadString } } })
  119. }
  120. }
  121. //console.log(that.data.TestType);
  122. //console.log(JSON.stringify(that.data.TaskInfo));
  123. },
  124. onPullDownRefresh: function () {
  125. var that = this;
  126. that.setData({
  127. IsShowAnswer: [0, 0],
  128. });
  129. wx.stopPullDownRefresh();
  130. },
  131. onShowAnswer: function (e) {
  132. var that = this;
  133. var clicktype = e.currentTarget.dataset.type;
  134. if (!(clicktype && clicktype == "all" && app.globalData.ClickType == 0)) {
  135. that.setData({
  136. IsShowAnswer: [1, 1],
  137. });
  138. if (that.data.TestAutioPlay == 1 && that.data.TestType == "read") {
  139. if (TaskList[that.data.CurrentIndex] && TaskList[that.data.CurrentIndex].ReadString)
  140. that.playSound({ currentTarget: { dataset: { content: TaskList[that.data.CurrentIndex].ReadString } } })
  141. }
  142. }
  143. },
  144. getColor: function () {
  145. var that = this;
  146. if (wx.setBackgroundColor) {
  147. wx.setBackgroundColor({
  148. backgroundColor: that.data.Color.BackColor,
  149. backgroundColorTop: that.data.Color.BackColor,
  150. backgroundColorBottom: that.data.Color.BackColor,
  151. })
  152. }
  153. if (wx.setNavigationBarColor) {
  154. wx.setNavigationBarColor({
  155. frontColor: "#ffffff",
  156. backgroundColor: that.data.Color.BackColor,
  157. })
  158. }
  159. },
  160. playSound: function (e) {
  161. clearTimeout(timeoutPlayAudio);
  162. var that = this;
  163. var str = e.currentTarget.dataset.content;
  164. if (str){
  165. var url;
  166. if (str == "recorder") {
  167. url = e.currentTarget.dataset.soundmark;
  168. }
  169. else if (str.indexOf("英 [") >= 0 || str.indexOf("美 [") >= 0 || str.indexOf("baidu.com")>0 || str.indexOf("iciba.com")>0 || str.indexOf("myqcloud.com")>0) {
  170. str = str.replace("英 [", "[");
  171. str = str.replace("美 [", "[");
  172. url = e.currentTarget.dataset.soundmark;
  173. if (!url)
  174. url=str;
  175. }
  176. else if (str.indexOf("pinyin")>0){
  177. url=str;
  178. }
  179. else if (e.currentTarget.dataset.soundmark && e.currentTarget.dataset.soundmark != "undefined") {
  180. var soundmark = e.currentTarget.dataset.soundmark;
  181. if (soundmark && soundmark.indexOf("http") < 0) {
  182. url = app.globalData.audioUrlBaidu;
  183. url = url.replace("[token]", app.globalData.BaiduToken);
  184. url = url.replace("[word]", soundmark);
  185. }
  186. else {
  187. url = soundmark;
  188. }
  189. }
  190. else {
  191. url = app.globalData.audioUrlBaidu;
  192. url = url.replace("[token]", app.globalData.BaiduToken);
  193. url = url.replace("[word]", str);
  194. }
  195. if (url.indexOf("http") > 0)
  196. url = url.substr(url.indexOf("http"));
  197. if (url.indexOf("'") > 0)
  198. url = common.ReplaceAllString(url, "'", "");
  199. url = url.replace("http://", "https://");
  200. url = encodeURI(url);
  201. if (!isPlaying || tempPlayUrl == "" || url != tempPlayUrl) {
  202. isPlaying = true;
  203. timeoutPlayAudio = setTimeout(function () {
  204. isPlaying = false;
  205. }, 60000);
  206. wx.showLoading({
  207. title: '音频下载中',
  208. mask: true,
  209. });
  210. setTimeout(function () {
  211. wx.hideLoading();
  212. }, 30000);
  213. wx.downloadFile({
  214. url: url,
  215. success(res) {
  216. wx.hideLoading();
  217. tempPlayUrlLocal = res.tempFilePath;
  218. innerAudioContext1.src = res.tempFilePath;
  219. innerAudioContext1.play();
  220. tempPlayUrl = url;
  221. },
  222. fail(err) {
  223. wx.hideLoading();
  224. if (url.indexOf("sp0.baidu.com")>0){
  225. var soundmark=url.substring(url.indexOf("text=")+5,url.indexOf("&spd"));
  226. url = app.globalData.audioUrlYoudao;
  227. url = url.replace("[word]", soundmark);
  228. tempPlayUrlLocal = url;
  229. that.audioCtx.setSrc(url);
  230. that.audioCtx.play();
  231. tempPlayUrl = url;
  232. }
  233. }
  234. });
  235. }
  236. else {
  237. if (this.audioCtx)
  238. this.audioCtx.pause();
  239. if (innerAudioContext1)
  240. innerAudioContext1.stop();
  241. isPlaying = false;
  242. }
  243. }
  244. },
  245. audioBindEnded: function () {
  246. isPlaying = false;
  247. },
  248. audioBindError: function (err) {
  249. console.log(err);
  250. innerAudioContext1.src = tempPlayUrlLocal;
  251. innerAudioContext1.play();
  252. main.getBaiduToken();
  253. },
  254. setSoundFile: function (isFinish) { //isFinish=3搁置 =2撤退 =1结束 =0正常 =-1开始
  255. var that = this;
  256. var url = "/pages/sounds/";
  257. if (isFinish == 1) {
  258. url += "end.mp3";
  259. } else if (isFinish == 2) {
  260. url += "short3.mp3";
  261. } else if (isFinish == 3) {
  262. url += "short1.mp3";
  263. } else {
  264. url += "short2.mp3";
  265. }
  266. if (isFinish >= 0) {
  267. innerAudioContext1.src = url;
  268. innerAudioContext1.play();
  269. }
  270. wx.pageScrollTo({
  271. scrollTop: 0,
  272. });
  273. },
  274. onAutoPlay:function(e){
  275. var id=e.currentTarget.dataset.id;
  276. this.setData({
  277. TestAutioPlay:id,
  278. });
  279. wx.setStorageSync("TestAutioPlay",id);
  280. },
  281. onExport:function(){
  282. var that=this;
  283. that.setSoundFile(0);
  284. while (that.data.CurrentIndex<TaskList.length) {
  285. TaskList[that.data.CurrentIndex].Result = 0;
  286. that.data.CurrentIndex++;
  287. }
  288. isFinished = 1;
  289. that.onFinished();
  290. },
  291. onFinished:function(){
  292. if (TaskList.length > 0 && this.data.CurrentIndex > 0) {
  293. var TestTask = {};
  294. TestTask.Category = this.data.Category;
  295. TestTask.TestType = this.data.TestType;
  296. TestTask.IsFinished = isFinished;
  297. TestTask.LibraryName = this.data.LibraryName;
  298. TestTask.BookName = this.data.BookName;
  299. TestTask.UnitName = this.data.UnitName;
  300. TestTask.TestTypeName = this.data.TestTypeName;
  301. TestTask.LibraryID=this.data.LibraryID;
  302. TestTask.BookID=this.data.BookID;
  303. TestTask.UnitID+=this.data.UnitID;
  304. TestTask.List = TaskList;
  305. app.globalData.TestTask=TestTask;
  306. wx.redirectTo({
  307. url: './report?type=new',
  308. });
  309. }
  310. else{
  311. wx.navigateBack({
  312. delta: 1,
  313. });
  314. }
  315. },
  316. onUnload:function(){
  317. wx.removeStorageSync("TestTask2");
  318. if (innerAudioContext1) {
  319. innerAudioContext1.stop();
  320. }
  321. },
  322. showBishunOrKaiti:function(e){
  323. var that=this;
  324. main.downloadBishunKaitiImage(
  325. e.currentTarget.dataset.url,
  326. e.currentTarget.dataset.serverurl,
  327. e.currentTarget.dataset.type,
  328. that.data.TaskInfo.ContentNew.Field,function(fields){
  329. that.data.TaskInfo.ContentNew.Field=fields;
  330. that.setData({
  331. TaskInfo: that.data.TaskInfo,
  332. });
  333. });
  334. },
  335. onShareAppMessage: function () {
  336. return {
  337. title: app.globalData.ShareTitle,
  338. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  339. imageUrl: app.globalData.ShareImage,
  340. }
  341. },
  342. })