api.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. ///<reference path="../typings/wx/lib.wx.api.d.ts"/>
  2. declare namespace tools {
  3. export interface Entity {
  4. }
  5. export interface Result<T extends Entity> {
  6. code?: number | string,
  7. desc?: string,
  8. data?: T | T[]
  9. }
  10. export interface UpyunResult {
  11. code: number,
  12. message?: string,
  13. time: number,
  14. url: string
  15. }
  16. export interface CheckQrResult extends Entity {
  17. /**
  18. * 课程ID
  19. */
  20. courseId: number,
  21. /**
  22. * 课程名称
  23. */
  24. courseName: string,
  25. /**
  26. * 考试记录DataID
  27. */
  28. examRecordDataId: number,
  29. /**
  30. * 考生ID
  31. */
  32. examStudentId: number,
  33. /**
  34. * 考试试题大题号
  35. */
  36. questionMainNumber: number,
  37. /**
  38. * 考试试题题序
  39. */
  40. questionOrder: number,
  41. /**
  42. * 登录信息key
  43. */
  44. key: string,
  45. /**
  46. * 登录信息token
  47. */
  48. token: string,
  49. subNumber: number,
  50. }
  51. export interface SignResult extends Entity {
  52. accessUrl: string,
  53. formUrl: string,
  54. fsType: string,
  55. uploadUrl: string,
  56. formParams: object,
  57. signIdentifier: string
  58. }
  59. export interface NotifyResult extends Entity {
  60. filePath: string,
  61. result: boolean
  62. }
  63. export interface AudioAnswer extends Entity {
  64. examRecordDataId: number,
  65. mainNumber: number,
  66. order: number,
  67. studentAnswer: string,
  68. subNumber: string
  69. }
  70. }
  71. interface ApiConfigOption {
  72. ver: number,
  73. versionName: string,
  74. baseUrl: string
  75. }
  76. interface RequestOption {
  77. url: string,
  78. parameter: any
  79. }
  80. type TokenValidCallback = () => void;
  81. interface API {
  82. configOption: ApiConfigOption
  83. version(): string
  84. setTokenValidCallback(callback: TokenValidCallback): void
  85. checkQrcode(secret: string): Promise<tools.CheckQrResult>
  86. getPaperStruct(examRecordDataId: number) : Promise<any>
  87. getSign(examRecordDataId: number, examStudentId: number, order: number, fileMd5: string, suffix: string) : Promise<tools.SignResult>
  88. uploadFile(filePath: string, formParams: object, uploadUrl: string) : Promise<tools.UpyunResult>
  89. notifyServer(examRecordDataId: number, examStudentId: number, order: number, filePath: string, transferFileType: string) : Promise<string>
  90. getNotifyResult(acknowledgeId: string) : Promise<boolean>
  91. getUploadedAudioAnswerList(examRecordDataId: number) : Promise<tools.AudioAnswer[]>
  92. getRequest(option: RequestOption, auth: boolean): Promise<any>
  93. postRequest(option: RequestOption, auth: boolean): Promise<any>
  94. }
  95. export class Api implements API {
  96. //ecs.qmth.com.cn:8878
  97. //http://192.168.10.39:8003
  98. //https://ecs.qmth.com.cn:8878
  99. //ecs-test.qmth.com.cn
  100. //http://ecs-dev.qmth.com.cn
  101. //'https://org0.ea100.com.cn'
  102. //https://ecs-test.ea100.com.cn https://ecs-test.ea100.com.cn
  103. configOption: ApiConfigOption = { ver: 1, versionName: 'v1.0.0', baseUrl: 'http://192.168.10.41:8003' }
  104. private key: string = ''
  105. private token: string = ''
  106. private tokenValidCallback: TokenValidCallback = () => {}
  107. version(): string {
  108. return this.configOption.versionName
  109. }
  110. init(config: ApiConfigOption) {
  111. this.configOption = config
  112. }
  113. initBaseUrl(baseUrl: string) {
  114. this.configOption.baseUrl = baseUrl.replace(':443', '')
  115. console.log("initBaseUri:" + this.configOption.baseUrl)
  116. }
  117. /**
  118. * 授权成功后配置
  119. * @param {number} uid
  120. * @param {string} token
  121. * @param {number} partitionId
  122. */
  123. configAuthorize(key: string, token: string) {
  124. this.key = key
  125. this.token = token
  126. }
  127. setTokenValidCallback(callback: TokenValidCallback): void {
  128. this.tokenValidCallback = callback
  129. }
  130. checkQrcode(secret: string): Promise<any> {
  131. const that = this
  132. return new Promise<tools.CheckQrResult>((resolve, reject) => {
  133. return this.postRequest({
  134. url: '/api/ecs_oe_student/examControl/checkQrCode',
  135. parameter: {
  136. qrCode: secret
  137. }
  138. }, false).then(res => {
  139. console.log('then')
  140. if (res.key && res.token) {
  141. that.configAuthorize(res.key, res.token)
  142. // model transformer
  143. var result = {
  144. courseId: res.courseId,
  145. courseName: res.courseName,
  146. examRecordDataId: res.examRecordDataId,
  147. examStudentId: res.examStudentId,
  148. questionMainNumber: res.questionMainNumber,
  149. questionOrder: res.questionOrder,
  150. subNumber: res.subNumber
  151. }
  152. resolve((result as tools.CheckQrResult))
  153. } else {
  154. reject('二维码已失效')
  155. }
  156. }, error => {
  157. reject(error)
  158. }).catch( exceptions => {
  159. reject(exceptions)
  160. })
  161. })
  162. }
  163. getPaperStruct(examRecordDataId: number) : Promise<any> {
  164. return new Promise((resovle, reject) => {
  165. return this.getRequest({
  166. url: '/api/ecs_oe_student/examRecordPaperStruct/getExamRecordPaperStruct',
  167. parameter: {
  168. examRecordDataId: examRecordDataId
  169. }
  170. }).then( res => {
  171. resovle(res)
  172. }, error => {
  173. reject(error)
  174. }).catch( exceptions => {
  175. reject(exceptions)
  176. })
  177. })
  178. }
  179. getSign(examRecordDataId: number, examStudentId: number, order: number, fileMd5: string, suffix: string) : Promise<tools.SignResult> {
  180. return new Promise<tools.SignResult>((resolve, reject) => {
  181. return this.postRequest({
  182. url: '/api/ecs_oe_student/examControl/yunSignature',
  183. parameter: {
  184. examRecordDataId: examRecordDataId,
  185. examStudentId: examStudentId,
  186. order: order,
  187. fileMd5: fileMd5,
  188. fileSuffix: suffix
  189. }
  190. }).then(res => {
  191. var result = {
  192. accessUrl: res.accessUrl,
  193. formUrl: res.formUrl,
  194. fsType: res.fsType,
  195. uploadUrl: res.uploadUrl,
  196. formParams: res.formParams,
  197. signIdentifier: res.signIdentifier,
  198. }
  199. resolve(result)
  200. }, error => {
  201. reject(error)
  202. })
  203. })
  204. }
  205. uploadFile(filePath: string, formParams: object, uploadUrl: string) : Promise<any> {
  206. return new Promise(function (resolve, reject) {
  207. wx.uploadFile({
  208. url: uploadUrl,
  209. filePath: filePath,
  210. name: 'file',
  211. formData: formParams,
  212. success: res => {
  213. resolve(res)
  214. },
  215. fail: error => {
  216. if (error.errMsg == 'uploadFile:fail url not in domain list') {
  217. reject('请点击右上角"•••"按钮开启调试模式')
  218. } else {
  219. reject(error.errMsg)
  220. }
  221. }
  222. })
  223. })
  224. }
  225. notifyServer(examRecordDataId: number, examStudentId: number,
  226. order: number, filePath: string, transferFileType: string) : Promise<string> {
  227. return new Promise((resovle, reject) => {
  228. return this.postRequest({
  229. url: '/api/ecs_oe_student/examControl/saveUploadedFile',
  230. parameter: {
  231. examRecordDataId: examRecordDataId,
  232. examStudentId: examStudentId,
  233. order: order,
  234. filePath: filePath,
  235. transferFileType: transferFileType
  236. }
  237. }).then( res => {
  238. resovle(res)
  239. }, error => {
  240. console.log('===================')
  241. reject(error)
  242. }).catch( exceptions => {
  243. console.log('===================')
  244. reject(exceptions)
  245. })
  246. })
  247. }
  248. getNotifyResult(acknowledgeId: string) : Promise<any> {
  249. return new Promise((resovle, reject) => {
  250. return this.postRequest({
  251. url: '/api/ecs_oe_student/examControl/getUploadedFileAcknowledgeStatus',
  252. parameter: {
  253. acknowledgeId: acknowledgeId
  254. }
  255. }).then( res => {
  256. resovle(res)
  257. }, error => {
  258. reject(error)
  259. }).catch( exceptions => {
  260. reject(exceptions)
  261. })
  262. })
  263. }
  264. getUploadedAudioAnswerList(examRecordDataId: number) : Promise<tools.AudioAnswer[]> {
  265. return new Promise((resolve, reject) => {
  266. return this.postRequest({
  267. url: '/api/ecs_oe_student/examControl/getUploadedAudioAnswerList',
  268. parameter: {
  269. examRecordDataId: examRecordDataId
  270. }
  271. }).then( res => {
  272. resolve(res as tools.AudioAnswer[])
  273. }, error => {
  274. reject(error)
  275. }).catch(e => {
  276. reject(e)
  277. })
  278. })
  279. }
  280. getRequest(option: RequestOption, auth: boolean = true): Promise<any> {
  281. const that = this
  282. const header = auth ? {
  283. 'Content-Type': 'application/x-www-form-urlencoded',
  284. key: that.key,
  285. token: that.token
  286. } : {
  287. 'Content-Type': 'application/x-www-form-urlencoded',
  288. }
  289. return new Promise(function (resolve, reject) {
  290. wx.request({
  291. url: that.configOption.baseUrl + option.url,
  292. method: 'GET',
  293. header: header,
  294. data: option.parameter,
  295. success: res => {
  296. console.log("request success", res);
  297. if (that.isTokenInValidCode(res.statusCode)) {
  298. reject('token失效')
  299. // token失效
  300. if (that.tokenValidCallback) {
  301. that.tokenValidCallback()
  302. }
  303. } else if (res.statusCode !== 200) {
  304. reject((res.data as tools.Result<any>).desc)
  305. } else {
  306. resolve(res.data)
  307. }
  308. },
  309. fail: function (res) {
  310. console.log("request fail", res);
  311. if (res.errMsg == 'request:fail url not in domain list') {
  312. reject('请点击右上角"•••"按钮开启调试模式')
  313. } else if (res.errMsg == 'request:fail ') {
  314. reject("网络请求失败");
  315. } else {
  316. reject(res.errMsg);
  317. }
  318. }
  319. })
  320. })
  321. }
  322. postRequest(option: RequestOption, auth: boolean = true): Promise<any> {
  323. const that = this
  324. const header = auth ? {
  325. 'Content-Type': 'application/x-www-form-urlencoded',
  326. key: that.key,
  327. token: that.token
  328. } : {
  329. 'Content-Type': 'application/x-www-form-urlencoded',
  330. }
  331. console.log(that.configOption.baseUrl + option.url);
  332. console.log(option)
  333. return new Promise(function (resolve, reject) {
  334. wx.request({
  335. url: that.configOption.baseUrl + option.url,
  336. method: 'POST',
  337. header: header,
  338. data: option.parameter,
  339. success: res => {
  340. console.log("request success", res);
  341. if (that.isTokenInValidCode(res.statusCode)) {
  342. reject('token失效')
  343. // token失效
  344. if (that.tokenValidCallback) {
  345. that.tokenValidCallback()
  346. }
  347. } else if (res.statusCode == 503) { //接口限流,自动重试
  348. that._postRequest(option, auth, 0, resolve, reject)
  349. } else if (res.statusCode !== 200) {
  350. reject((res.data as tools.Result<any>).desc)
  351. } else {
  352. resolve(res.data)
  353. }
  354. },
  355. fail: function (res) {
  356. console.log("request fail", res);
  357. if (res.errMsg == 'request:fail url not in domain list') {
  358. reject('请点击右上角"•••"按钮开启调试模式')
  359. } else if (res.errMsg == 'request:fail ') {
  360. reject("网络请求失败");
  361. } else {
  362. reject(res.errMsg);
  363. }
  364. }
  365. })
  366. })
  367. }
  368. _postRequest(option: RequestOption, auth: boolean = true, retry: number = 0, resolve: (value?: any | PromiseLike<any>) => void, reject: (reason?: any) => void) {
  369. const that = this
  370. const header = auth ? {
  371. 'Content-Type': 'application/x-www-form-urlencoded',
  372. key: that.key,
  373. token: that.token
  374. } : {
  375. 'Content-Type': 'application/x-www-form-urlencoded',
  376. }
  377. console.log(option)
  378. if (retry > 0) {
  379. console.log('正在重试'+retry)
  380. }
  381. wx.request({
  382. url: that.configOption.baseUrl + option.url,
  383. method: 'POST',
  384. header: header,
  385. data: option.parameter,
  386. success: res => {
  387. console.log("request success", res);
  388. if (that.isTokenInValidCode(res.statusCode)) {
  389. reject('token失效')
  390. // token失效
  391. if (that.tokenValidCallback) {
  392. that.tokenValidCallback()
  393. }
  394. } else if (res.statusCode == 503) { //接口限流,自动重试
  395. if (retry < 10) {
  396. console.log('200ms重试')
  397. setTimeout(() => {
  398. that._postRequest(option, auth, retry+1, resolve, reject)
  399. }, 200);
  400. } else {
  401. reject('服务器繁忙,请稍后重试!')
  402. }
  403. } else if (res.statusCode !== 200) {
  404. reject((res.data as tools.Result<any>).desc)
  405. } else {
  406. resolve(res.data)
  407. }
  408. },
  409. fail: function (res) {
  410. console.log("request fail", res);
  411. if (res.errMsg == 'request:fail url not in domain list') {
  412. reject('请点击右上角"•••"按钮开启调试模式')
  413. } else if (res.errMsg == 'request:fail ') {
  414. reject("网络请求失败");
  415. } else {
  416. reject(res.errMsg);
  417. }
  418. }
  419. })
  420. }
  421. private isTokenInValidCode(code: number) : boolean {
  422. return code === 401 || code === 403
  423. }
  424. }