import { HttpRequest, StoreAssist, UploadAssist, ResultData } from '@/common/Common'; import { BaseConfig } from './BaseConfig'; export class Service extends BaseConfig { // 获取是否后台 static getIsHede () { let isHede = this.GetStorageCache('isHede') if (isHede == null || isHede == '') { return false; } else { return isHede; } } //获取API地址 static ApiUrl(path : string) { return `${this.servesUrl}${path}`; } //获取图片地址 static GetpayImg(path : string) { if (path.startsWith('http') || path.startsWith('https')) { return path; } else { return `${this.payuploadUrl}${path}`; } } //获取图标地址 static GetIconImg(path : string) { return path if (path.startsWith('http') || path.startsWith('https')) { return path; } else { return `${this.imgUrl}${path}`; } } //获取图片地址 static GetMateUrlByImg(path : string) { return path if (path.startsWith('http') || path.startsWith('https')) { return path; } else { return `${this.imgUrl}${path}`; } } //获取音视频地址 static GetMateUrlByMedia(path : string) { if (path.startsWith('http') || path.startsWith('https')) { return path; } else { return `${this.mediaUrl}${path}`; } } //获取登录账号token static GetUserToken() { return Service.GetStorageCache('token'); } // 获取登录状态 static GetUserIsLogin() { var token = this.GetUserToken(); if (token == null || token == '') { return false; } else { return true; } } //设置登录账户Token static SetUserToken(token : string) { this.SetStorageCache('token', token); } //清理登录账户Token static OffUserToken() { Service.DelStorageCache('token'); uni.$emit('ImComOff', 'user'); this.ClearUserStateData(); } //获取登录账号状态信息 static GetUserStateData() { return Service.GetStorageCache('StateDomain'); } //设置当前登录账号状态信息 static SetUserStateData() { return Service.GetStorageCache('StateDomain'); } //清理当前登录账号状态信息 static ClearUserStateData() { Service.DelStorageCache('StateDomain'); } //获取当前客户端ID static GetUserClientId() { return this.GetStorageCache('ClientId'); } //保存当前客户端ID static SetUserClientId(clientId: string) { this.SetStorageCache('ClientId', clientId); } //获取缓存 static GetStorageCache(key : string) { return StoreAssist.Get(key); } //删除缓存 static DelStorageCache(key : string) { StoreAssist.Delete(key); } //设置缓存 static SetStorageCache(key : string, data : any) { StoreAssist.Set(key, data); } /*****以下是基础方法调用与拦截器*****/ static Request(url : string, method : 'GET' | 'POST' | 'PUT' | undefined, data : object | any) { const token = Service.GetUserToken(); const _url = Service.ApiUrl(url); var result = HttpRequest.RequestWithToken(_url, method, token, data).then((retResult : any) => { if (retResult.statusCode == '200') { var obj = retResult.data; if (obj.code == 401) { //过期 this.OffUserToken(); this.Msg('登录过期,请重新登录') this.GoPage('/pages/my/login') return Promise.reject(); } else if (obj.code == 40101) { //失效 this.OffUserToken(); this.GoPageDelse('/pages/mine/login/login'); return Promise.reject(); } else if (obj.code == 1004) { //资源不存在 this.GoPageDelse('/pages/AppSet/404/404'); return Promise.reject(); // return new ResultData(-1, '', ''); } else if (obj.code == 40188) { //无权限 this.GoPageDelse('/pages/AppSet/40188/40188'); return Promise.reject(); // return new ResultData(-1, '', ''); } else if (obj.code == 1008) { //业务提示 return new ResultData(obj.code, obj.msg, obj.data); } else { return new ResultData(obj.code, obj.msg, obj.data); } } else { return new ResultData(-1, '', ''); } }); return result; } /*****以下是腾讯云oss上传*****/ static UpLoadMedia(code : string, fileName : string, desire : string, path : string) { var result = this.Request(this.uploadUrl, 'GET', { code, fileName, desire }).then((retResult) => { if (retResult.code == 0) { var upOk = UploadAssist.Upload(retResult.data.url, path, retResult.data.cosData).then((upRet : any) => { if (upRet.statusCode == 200) { const retData : any = { code: retResult.data.code, file: retResult.data.file, cache: retResult.data.cache }; return new ResultData(0, '上传成功!', retData); } else { this.Msg('上传失败!'); return new ResultData(-1, '', ''); } }); return upOk; } else { this.Msg('上传失败!'); return new ResultData(-1, retResult.msg,retResult.data); } }); return result; } /***********消息操作**************/ static Msg(message : any, icon ?: any) : void { if (icon != null) { uni.showToast({ title: message, icon: icon }); } else { uni.showToast({ title: message, icon: 'none' }); } } static Alert(msg : string, cb ?: any) { uni.showModal({ title: '提示', content: msg, showCancel: false, cancelText: '取消', confirmText: '确定', success: res => { if (res.confirm) { cb && cb(); } } }) } static LoadIng(text : any) : void { uni.showLoading({ title: text, icon: 'none' }); } static LoadClose() : void { uni.hideLoading(); } /**********跳转操作*********/ static GoPageTab(path : string) : void { uni.switchTab({ url: path }); } /**********跳转操作*********/ static GoPage(path : string) : void { uni.navigateTo({ url: path, //跳转的页面 success: function (res) { // 通过eventChannel向被打开页面传送数据 } }); } /**********跳转并删除当前页面操作*********/ static GoPageDelse(path : string) : void { uni.redirectTo({ url: path //跳转的页面 }); } /**********返回上一页*********/ static GoPageBack() : void { uni.navigateBack({ delta: 1 }); } /*****获取图片base64*****/ static UpLoadMediaBase64(path : string) { return new Promise(function (resolve, reject) { uni.uploadFile({ url: 'http://cloud.pccsh.com/DefUp/UploadFileImgBase64', //仅为示例,非真实的接口地址 filePath: path, name: 'file', success: (uploadFileRes) => { resolve(uploadFileRes); }, fail: (err) => { reject(err); } }); }); } /*****获取图片位置信息*****/ //获取时间戳 static GetTimeSpan(milliSecond : number) { return Date.now() + milliSecond; } // 时间戳处理 static formatDate(time : any, type : number) : string { const date = new Date(time); const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始,所以加1,并用0填充 const day = String(date.getDate()).padStart(2, '0'); // 用0填充 const hours = String(date.getHours()).padStart(2, '0'); // 用0填充 const minutes = String(date.getMinutes()).padStart(2, '0'); // 用0填充 const seconds = String(date.getSeconds()).padStart(2, '0'); // 用0填充 if (type == 0) { return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; } else if (type == 1) { return `${year}-${month}-${day} ${hours}:${minutes}`; } else if (type == 2) { return `${year}-${month}-${day}`; } else if (type == 3) { return `${hours}:${minutes}`; } else if (type == 4) { return `${year}${month}${day}`; } else { return `${hours}:${minutes}`; } } /*****节流*****/ static throttle(fn: () => void, time: number) { let canRun: boolean = true; return function () { if (!canRun) return; canRun = false; setTimeout(() => { fn(); //可以不执行 canRun = true; }, time); }; } /*****防抖*****/ static debounce void>(fn: T, time: number): (...args: Parameters) => void { let timerId: NodeJS.Timeout | null = null; return (...args: Parameters) => { if (timerId) { clearTimeout(timerId); } timerId = setTimeout(() => { fn(...args); // 执行传入的函数 timerId = null; // 清除定时器ID }, time); }; } // 普通图片上传 static uploadH5(path, dic, callback) { console.log(this.payuploadUrl,'xxx') uni.uploadFile({ url: this.payuploadUrl+'/Upload/Upload', method: "POST", header: { 'Authorization': 'Bearer ' + Service.GetUserToken(), }, formData: { "path": dic, }, filePath: path, name: 'file', success: (data) => { let info = data.data callback(info) } }) } }