examine.js 11 KB

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