examine.js 11 KB

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