123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- ///<reference path="../typings/wx/lib.wx.api.d.ts"/>
- declare namespace tools {
- export interface Entity {
- }
- export interface Result<T extends Entity> {
- code?: number | string,
- desc?: string,
- data?: T | T[]
- }
- export interface UpyunResult {
- code: number,
- message?: string,
- time: number,
- url: string
- }
- export interface CheckQrResult extends Entity {
- /**
- * 课程ID
- */
- courseId: number,
- /**
- * 课程名称
- */
- courseName: string,
- /**
- * 考试记录DataID
- */
- examRecordDataId: number,
- /**
- * 考生ID
- */
- examStudentId: number,
- /**
- * 考试试题大题号
- */
- questionMainNumber: number,
- /**
- * 考试试题题序
- */
- questionOrder: number,
- /**
- * 登录信息key
- */
- key: string,
- /**
- * 登录信息token
- */
- token: string,
- subNumber: number,
- }
- export interface SignResult extends Entity {
- accessUrl: string,
- formUrl: string,
- fsType: string,
- uploadUrl: string,
- formParams: object,
- signIdentifier: string
- }
- export interface NotifyResult extends Entity {
- filePath: string,
- result: boolean
- }
- export interface AudioAnswer extends Entity {
- examRecordDataId: number,
- mainNumber: number,
- order: number,
- studentAnswer: string,
- subNumber: string
- }
- }
- interface ApiConfigOption {
- ver: number,
- versionName: string,
- baseUrl: string
- }
- interface RequestOption {
- url: string,
- parameter: any
- }
- type TokenValidCallback = () => void;
- interface API {
- configOption: ApiConfigOption
- version(): string
- setTokenValidCallback(callback: TokenValidCallback): void
- checkQrcode(secret: string): Promise<tools.CheckQrResult>
- getPaperStruct(examRecordDataId: number) : Promise<any>
- getSign(examRecordDataId: number, examStudentId: number, order: number, fileMd5: string, suffix: string) : Promise<tools.SignResult>
- uploadFile(filePath: string, formParams: object, uploadUrl: string) : Promise<tools.UpyunResult>
- notifyServer(examRecordDataId: number, examStudentId: number, order: number, filePath: string, transferFileType: string) : Promise<string>
- getNotifyResult(acknowledgeId: string) : Promise<boolean>
- getUploadedAudioAnswerList(examRecordDataId: number) : Promise<tools.AudioAnswer[]>
- getRequest(option: RequestOption, auth: boolean): Promise<any>
- postRequest(option: RequestOption, auth: boolean): Promise<any>
- }
- export class Api implements API {
- //ecs.qmth.com.cn:8878
- //http://192.168.10.39:8003
- //https://ecs.qmth.com.cn:8878
- //ecs-test.qmth.com.cn
- //http://ecs-dev.qmth.com.cn
- //'https://org0.ea100.com.cn'
- //https://ecs-test.ea100.com.cn https://ecs-test.ea100.com.cn
- configOption: ApiConfigOption = { ver: 1, versionName: 'v1.0.0', baseUrl: 'http://192.168.10.41:8003' }
- private key: string = ''
- private token: string = ''
- private tokenValidCallback: TokenValidCallback = () => {}
- version(): string {
- return this.configOption.versionName
- }
- init(config: ApiConfigOption) {
- this.configOption = config
- }
- initBaseUrl(baseUrl: string) {
- this.configOption.baseUrl = baseUrl.replace(':443', '')
- console.log("initBaseUri:" + this.configOption.baseUrl)
- }
- /**
- * 授权成功后配置
- * @param {number} uid
- * @param {string} token
- * @param {number} partitionId
- */
- configAuthorize(key: string, token: string) {
- this.key = key
- this.token = token
- }
- setTokenValidCallback(callback: TokenValidCallback): void {
- this.tokenValidCallback = callback
- }
- checkQrcode(secret: string): Promise<any> {
- const that = this
- return new Promise<tools.CheckQrResult>((resolve, reject) => {
- return this.postRequest({
- url: '/api/ecs_oe_student/examControl/checkQrCode',
- parameter: {
- qrCode: secret
- }
- }, false).then(res => {
- console.log('then')
- if (res.key && res.token) {
- that.configAuthorize(res.key, res.token)
- // model transformer
- var result = {
- courseId: res.courseId,
- courseName: res.courseName,
- examRecordDataId: res.examRecordDataId,
- examStudentId: res.examStudentId,
- questionMainNumber: res.questionMainNumber,
- questionOrder: res.questionOrder,
- subNumber: res.subNumber
- }
- resolve((result as tools.CheckQrResult))
- } else {
- reject('二维码已失效')
- }
- }, error => {
- reject(error)
- }).catch( exceptions => {
- reject(exceptions)
- })
- })
- }
- getPaperStruct(examRecordDataId: number) : Promise<any> {
- return new Promise((resovle, reject) => {
- return this.getRequest({
- url: '/api/ecs_oe_student/examRecordPaperStruct/getExamRecordPaperStruct',
- parameter: {
- examRecordDataId: examRecordDataId
- }
- }).then( res => {
- resovle(res)
- }, error => {
- reject(error)
- }).catch( exceptions => {
- reject(exceptions)
- })
- })
- }
- getSign(examRecordDataId: number, examStudentId: number, order: number, fileMd5: string, suffix: string) : Promise<tools.SignResult> {
- return new Promise<tools.SignResult>((resolve, reject) => {
- return this.postRequest({
- url: '/api/ecs_oe_student/examControl/yunSignature',
- parameter: {
- examRecordDataId: examRecordDataId,
- examStudentId: examStudentId,
- order: order,
- fileMd5: fileMd5,
- fileSuffix: suffix
- }
- }).then(res => {
- var result = {
- accessUrl: res.accessUrl,
- formUrl: res.formUrl,
- fsType: res.fsType,
- uploadUrl: res.uploadUrl,
- formParams: res.formParams,
- signIdentifier: res.signIdentifier,
- }
- resolve(result)
- }, error => {
- reject(error)
- })
- })
- }
- uploadFile(filePath: string, formParams: object, uploadUrl: string) : Promise<any> {
- return new Promise(function (resolve, reject) {
- wx.uploadFile({
- url: uploadUrl,
- filePath: filePath,
- name: 'file',
- formData: formParams,
- success: res => {
- resolve(res)
- },
- fail: error => {
- if (error.errMsg == 'uploadFile:fail url not in domain list') {
- reject('请点击右上角"•••"按钮开启调试模式')
- } else {
- reject(error.errMsg)
- }
- }
- })
- })
- }
- notifyServer(examRecordDataId: number, examStudentId: number,
- order: number, filePath: string, transferFileType: string) : Promise<string> {
- return new Promise((resovle, reject) => {
- return this.postRequest({
- url: '/api/ecs_oe_student/examControl/saveUploadedFile',
- parameter: {
- examRecordDataId: examRecordDataId,
- examStudentId: examStudentId,
- order: order,
- filePath: filePath,
- transferFileType: transferFileType
- }
- }).then( res => {
- resovle(res)
- }, error => {
- console.log('===================')
- reject(error)
- }).catch( exceptions => {
- console.log('===================')
- reject(exceptions)
- })
- })
- }
- getNotifyResult(acknowledgeId: string) : Promise<any> {
- return new Promise((resovle, reject) => {
- return this.postRequest({
- url: '/api/ecs_oe_student/examControl/getUploadedFileAcknowledgeStatus',
- parameter: {
- acknowledgeId: acknowledgeId
- }
- }).then( res => {
- resovle(res)
- }, error => {
- reject(error)
- }).catch( exceptions => {
- reject(exceptions)
- })
- })
- }
- getUploadedAudioAnswerList(examRecordDataId: number) : Promise<tools.AudioAnswer[]> {
- return new Promise((resolve, reject) => {
- return this.postRequest({
- url: '/api/ecs_oe_student/examControl/getUploadedAudioAnswerList',
- parameter: {
- examRecordDataId: examRecordDataId
- }
- }).then( res => {
- resolve(res as tools.AudioAnswer[])
- }, error => {
- reject(error)
- }).catch(e => {
- reject(e)
- })
- })
- }
- getRequest(option: RequestOption, auth: boolean = true): Promise<any> {
- const that = this
- const header = auth ? {
- 'Content-Type': 'application/x-www-form-urlencoded',
- key: that.key,
- token: that.token
- } : {
- 'Content-Type': 'application/x-www-form-urlencoded',
- }
- return new Promise(function (resolve, reject) {
- wx.request({
- url: that.configOption.baseUrl + option.url,
- method: 'GET',
- header: header,
- data: option.parameter,
- success: res => {
- console.log("request success", res);
- if (that.isTokenInValidCode(res.statusCode)) {
- reject('token失效')
- // token失效
- if (that.tokenValidCallback) {
- that.tokenValidCallback()
- }
- } else if (res.statusCode !== 200) {
- reject((res.data as tools.Result<any>).desc)
- } else {
- resolve(res.data)
- }
- },
- fail: function (res) {
- console.log("request fail", res);
- if (res.errMsg == 'request:fail url not in domain list') {
- reject('请点击右上角"•••"按钮开启调试模式')
- } else if (res.errMsg == 'request:fail ') {
- reject("网络请求失败");
- } else {
- reject(res.errMsg);
- }
- }
- })
- })
- }
- postRequest(option: RequestOption, auth: boolean = true): Promise<any> {
- const that = this
- const header = auth ? {
- 'Content-Type': 'application/x-www-form-urlencoded',
- key: that.key,
- token: that.token
- } : {
- 'Content-Type': 'application/x-www-form-urlencoded',
- }
- console.log(that.configOption.baseUrl + option.url);
- console.log(option)
- return new Promise(function (resolve, reject) {
- wx.request({
- url: that.configOption.baseUrl + option.url,
- method: 'POST',
- header: header,
- data: option.parameter,
- success: res => {
- console.log("request success", res);
-
- if (that.isTokenInValidCode(res.statusCode)) {
- reject('token失效')
- // token失效
- if (that.tokenValidCallback) {
- that.tokenValidCallback()
- }
- } else if (res.statusCode == 503) { //接口限流,自动重试
- that._postRequest(option, auth, 0, resolve, reject)
- } else if (res.statusCode !== 200) {
- reject((res.data as tools.Result<any>).desc)
- } else {
- resolve(res.data)
- }
- },
- fail: function (res) {
- console.log("request fail", res);
- if (res.errMsg == 'request:fail url not in domain list') {
- reject('请点击右上角"•••"按钮开启调试模式')
- } else if (res.errMsg == 'request:fail ') {
- reject("网络请求失败");
- } else {
- reject(res.errMsg);
- }
- }
- })
- })
- }
- _postRequest(option: RequestOption, auth: boolean = true, retry: number = 0, resolve: (value?: any | PromiseLike<any>) => void, reject: (reason?: any) => void) {
- const that = this
- const header = auth ? {
- 'Content-Type': 'application/x-www-form-urlencoded',
- key: that.key,
- token: that.token
- } : {
- 'Content-Type': 'application/x-www-form-urlencoded',
- }
- console.log(option)
- if (retry > 0) {
- console.log('正在重试'+retry)
- }
- wx.request({
- url: that.configOption.baseUrl + option.url,
- method: 'POST',
- header: header,
- data: option.parameter,
- success: res => {
- console.log("request success", res);
- if (that.isTokenInValidCode(res.statusCode)) {
- reject('token失效')
- // token失效
- if (that.tokenValidCallback) {
- that.tokenValidCallback()
- }
- } else if (res.statusCode == 503) { //接口限流,自动重试
- if (retry < 10) {
- console.log('200ms重试')
- setTimeout(() => {
- that._postRequest(option, auth, retry+1, resolve, reject)
- }, 200);
- } else {
- reject('服务器繁忙,请稍后重试!')
- }
- } else if (res.statusCode !== 200) {
- reject((res.data as tools.Result<any>).desc)
- } else {
- resolve(res.data)
- }
- },
- fail: function (res) {
- console.log("request fail", res);
- if (res.errMsg == 'request:fail url not in domain list') {
- reject('请点击右上角"•••"按钮开启调试模式')
- } else if (res.errMsg == 'request:fail ') {
- reject("网络请求失败");
- } else {
- reject(res.errMsg);
- }
- }
- })
- }
- private isTokenInValidCode(code: number) : boolean {
- return code === 401 || code === 403
- }
- }
|