index.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // pages/index/index.js
  2. const WX_REG = "http://wxapp2.qmth.com.cn/"
  3. const ZH_CN = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十',
  4. '十一', '十二', '十三', '十四', '十五', '十六', '十七', '十八', '十九', '二十',
  5. '三十一', '三十二', '三十三', '三十四', '三十五', '三十六', '三十七', '三十八', '三十九', '四十',
  6. '四十一', '四十二', '四十三', '四十四', '四十五', '四十六', '四十七', '四十八', '四十九', '五十'
  7. ]
  8. const app = getApp()
  9. const API = app.globalData.api
  10. Page({
  11. /**
  12. * 页面的初始数据
  13. */
  14. data: {
  15. paper: {
  16. courseId: 0,
  17. courseName: "",
  18. examRecordDataId: 0,
  19. examStudentId: 0,
  20. questionMainNumber: 0,
  21. questionOrder: 0,
  22. subNumber: 0,
  23. isTokenInValid: false
  24. },
  25. subTitle: '',
  26. questionType: '', //AUDIO PIC
  27. enableScan: true, //录音时不允许扫描
  28. },
  29. secret: '',
  30. reset: function() {
  31. this.secret = ''
  32. this.setData({
  33. paper: null,
  34. subTitle: '',
  35. questionType: '',
  36. enableScan: true,
  37. isTokenInValid: false
  38. })
  39. },
  40. scan: function (e) {
  41. console.log(e)
  42. if (!this.data.enableScan) {
  43. return false
  44. }
  45. var hasAnswer = false //是否有未提交的答案
  46. var component = this.selectComponent('#audio')
  47. if (component != null && component.data.audio != '') {
  48. hasAnswer = true
  49. }
  50. if (!hasAnswer) {
  51. component = this.selectComponent('#pic')
  52. if (component != null && component.data.pictures.length > 0) {
  53. hasAnswer = true
  54. }
  55. }
  56. if (hasAnswer) {
  57. wx.showModal({
  58. title: '提示',
  59. content: '有未提交的答案,是否放弃',
  60. showCancel: true,
  61. cancelText: '放弃',
  62. cancelColor: '#FF5252',
  63. confirmText: '取消',
  64. success(res) {
  65. if (!res.confirm) {
  66. wx.reLaunch({
  67. url: '/pages/index/index',
  68. })
  69. }
  70. }
  71. })
  72. return
  73. }
  74. const that = this
  75. wx.scanCode({
  76. onlyFromCamera: true,
  77. scanType: ['qrCode'],
  78. success(res) {
  79. console.log("res")
  80. var parameter = decodeURIComponent(res.result.replace(WX_REG, ""))
  81. console.log('parameter::' + parameter)
  82. var type = that.getQueryVariable(parameter, 'transferFileType')
  83. var host = that.getQueryVariable(parameter, 'apiServer')
  84. console.log("host::" + host);
  85. API.initBaseUrl(host);
  86. parameter = parameter.replace("&apiServer=" + host, "")
  87. if (type == 'PIC' || type == 'AUDIO') {
  88. wx.setNavigationBarTitle({
  89. title: type == 'PIC' ? '图片上传' : '音频上传'
  90. })
  91. that.setData({
  92. questionType: type
  93. })
  94. // 获取试题信息
  95. that.secret = parameter
  96. console.log(that.secret)
  97. that.checkQrcode(that.secret, true)
  98. } else {
  99. wx.showToast({
  100. title: '二维码有误!',
  101. icon: 'none',
  102. duration: 2500,
  103. mask: true
  104. })
  105. }
  106. },
  107. fail(res) {
  108. console.log(res)
  109. console.log(res != null && res.errMsg == 'scanCode:fail cancel')
  110. if (res != null && res.errMsg == 'scanCode:fail cancel') {
  111. return
  112. }
  113. wx.showToast({
  114. title: '二维码读取异常!',
  115. icon: 'none',
  116. duration: 2500,
  117. mask: true
  118. })
  119. }
  120. })
  121. },
  122. onScanEnable: function(event) {
  123. console.log('event::onScanEnable')
  124. console.log(event)
  125. this.setData({
  126. enableScan: event.detail.enabled
  127. })
  128. },
  129. checkQrcode: function (code, fromScan) {
  130. if (fromScan) {
  131. wx.showLoading({
  132. title: '正在加载',
  133. mask: true
  134. })
  135. }
  136. const that = this
  137. try {
  138. API.checkQrcode(code)
  139. .then(function (res) {
  140. console.log(res)
  141. const title = `第${ZH_CN.length >= res.questionMainNumber ? ZH_CN[res.questionMainNumber - 1] : res.questionMainNumber}大题 第${res.subNumber}小题 `
  142. that.setData({
  143. paper: res,
  144. subTitle: title
  145. })
  146. if (fromScan) {
  147. wx.hideLoading()
  148. wx.showToast({
  149. title: '扫码成功',
  150. icon: 'success',
  151. mask: true
  152. })
  153. }
  154. })
  155. .catch(function (error) {
  156. console.log(error)
  157. wx.hideLoading()
  158. var errorMsg = error
  159. if ((errorMsg && errorMsg.indexOf('request:fail') == 0) || errorMsg == undefined) {
  160. errorMsg = "网络请求失败,请稍后重试"
  161. }
  162. that.showConfirmDialog(`${errorMsg}`, '确定', function () {
  163. console.log('callback')
  164. wx.reLaunch({
  165. url: '/pages/index/index',
  166. })
  167. })
  168. })
  169. } catch(e) {
  170. console.log('========+++++++++++++')
  171. }
  172. },
  173. showConfirmDialog: function (msg, confirm, callback) {
  174. const confirmText = confirm ? confirm : '确定'
  175. wx.showModal({
  176. title: '提示',
  177. content: msg,
  178. showCancel: false,
  179. confirmText: confirmText,
  180. success(res) {
  181. if (res.confirm) {
  182. console.log('用户点击确定')
  183. if (callback) {
  184. callback.apply()
  185. }
  186. }
  187. }
  188. })
  189. },
  190. getQueryVariable: function(url, variable) {
  191. var vars = url.split("&")
  192. for (var i = 0; i < vars.length; i++) {
  193. var pair = vars[i].split("=")
  194. if (pair[0] == variable) {
  195. return pair[1]
  196. }
  197. }
  198. return (false)
  199. },
  200. tokenValidCallback: function () {
  201. if (this.data.isTokenInValid) {
  202. return;
  203. }
  204. this.setData({
  205. isTokenInValid: true
  206. })
  207. this.showConfirmDialog('二维码已过期,请重新扫描题目', '确定', function () {
  208. console.log('callback')
  209. wx.reLaunch({
  210. url: '/pages/index/index'
  211. })
  212. })
  213. },
  214. /**
  215. * 生命周期函数--监听页面加载
  216. */
  217. onLoad: function(options) {
  218. console.log(options)
  219. if (options && options.q) {
  220. const query = decodeURIComponent(options.q)
  221. /*if (query.indexOf("http") == 0) {
  222. var result = query.replace("//", '/').replace("//", '/').split('/')
  223. var baseUrl = result[0] + "//" + result[1];
  224. API.initBaseUrl(baseUrl);
  225. this.secret = query.replace(baseUrl, '').replace('/', '')
  226. }*/
  227. var type = this.getQueryVariable(decodeURIComponent(query), 'transferFileType')
  228. var host = this.getQueryVariable(decodeURIComponent(query), 'apiServer')
  229. console.log("host::" + host);
  230. API.initBaseUrl(host);
  231. this.secret = query.replace(host, '').replace('/', '')
  232. console.log(type)
  233. if (type == 'PIC' || type == 'AUDIO') {
  234. wx.setNavigationBarTitle({
  235. title: type == 'PIC' ? '图片上传' : '音频上传'
  236. })
  237. this.setData({
  238. questionType: type
  239. })
  240. } else {
  241. wx.showToast({
  242. title: '二维码有误!',
  243. icon: 'none',
  244. duration: 2500,
  245. mask: true
  246. })
  247. }
  248. }
  249. if (this.secret) {
  250. this.checkQrcode(this.secret, false)
  251. }
  252. API.setTokenValidCallback(this.tokenValidCallback)
  253. },
  254. /**
  255. * 生命周期函数--监听页面初次渲染完成
  256. */
  257. onReady: function() {
  258. },
  259. /**
  260. * 生命周期函数--监听页面显示
  261. */
  262. onShow: function() {
  263. },
  264. /**
  265. * 生命周期函数--监听页面隐藏
  266. */
  267. onHide: function() {
  268. },
  269. /**
  270. * 生命周期函数--监听页面卸载
  271. */
  272. onUnload: function() {
  273. },
  274. /**
  275. * 页面相关事件处理函数--监听用户下拉动作
  276. */
  277. onPullDownRefresh: function() {
  278. },
  279. /**
  280. * 页面上拉触底事件的处理函数
  281. */
  282. onReachBottom: function() {
  283. },
  284. /**
  285. * 用户点击右上角分享
  286. */
  287. onShareAppMessage: function() {
  288. }
  289. })