123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- import store from '@/store'
- import config from '@/config'
- import { getToken } from '@/utils/auth'
- import errorCode from '@/utils/errorCode'
- // import { toast, showConfirm, tansParams } from '@/utils/common'
- let timeout = 10000
- const baseUrl = config.baseUrl
- /**
- * 显示消息提示框
- * @param content 提示的标题
- */
- export function toast(content) {
- uni.showToast({
- icon: 'none',
- title: content
- })
- }
- /**
- * 显示模态弹窗
- * @param content 提示的标题
- */
- export function showConfirm(content) {
- return new Promise((resolve, reject) => {
- uni.showModal({
- title: '提示',
- content: content,
- cancelText: '取消',
- confirmText: '确定',
- success: function(res) {
- resolve(res)
- }
- })
- })
- }
- // 字典值匹配
- export function selectDictLabel(datas, value) {
- var actions = [];
- Object.keys(datas).some((key) => {
- // if (datas[key].dictValue == ('' + value)) {
- // actions.push(datas[key].dictLabel);
- // return true;
- // }
- if (datas[key].dictLabel == ('' + value)) {
- actions.push(datas[key].dictValue);
- return true;
- }
- })
- return actions.join('');
- }
- export function selectDictValue(datas, value) {
- var actions = [];
- Object.keys(datas).some((key) => {
- if (datas[key].dictValue == ('' + value)) {
- actions.push(datas[key].dictLabel);
- return true;
- }
- })
- return actions.join('');
- }
- /**
- * 参数处理
- * @param params 参数
- */
- export function tansParams(params) {
- let result = ''
- for (const propName of Object.keys(params)) {
- const value = params[propName]
- var part = encodeURIComponent(propName) + "="
- if (value !== null && value !== "" && typeof (value) !== "undefined") {
- if (typeof value === 'object') {
- for (const key of Object.keys(value)) {
- if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
- let params = propName + '[' + key + ']'
- var subPart = encodeURIComponent(params) + "="
- result += subPart + encodeURIComponent(value[key]) + "&"
- }
- }
- } else {
- result += part + encodeURIComponent(value) + "&"
- }
- }
- }
- return result
- }
- // 上传
- //上传图片
- export function uploadmore(api, filePaths, successUp, failUp, i, length, files, callback) {
- const isToken = (config.headers || {}).isToken === false
- config.header = config.header || {}
- if (getToken() && !isToken) {
- config.header['Authorization'] = 'Bearer ' + getToken()
- }
- // get请求映射params参数
- if (config.params) {
- let url = config.url + '?' + tansParams(config.params)
- url = url.slice(0, -1)
- config.url = url
- }
-
- uni.showLoading({
- title: '上传中'
- })
- var failfile = [];
- uni.uploadFile({
- timeout: config.timeout || timeout,
- url: baseUrl + api, //仅为示例,非真实的接口地址
- filePath: filePaths[i],
- name: 'file',
- header: config.header,
- formData: config.formData,
- success: function(resp) {
- uni.hideLoading();
- let result = JSON.parse(resp.data)
- const code = result.code || 200
- const msg = errorCode[code] || result.msg || errorCode['default']
- // console.log(result.fileName,8)
- if (result.code == 200) {
- successUp++;
- files[i] = result.fileName;
- } else if(result.code==401) {
- showConfirm("登录状态已过期,您可以继续留在该页面,或者重新登录?").then(res => {
- if (res.confirm) {
- store.dispatch('LogOut').then(res => {
- uni.reLaunch({ url: '/pages/login/login' })
- })
- }
- })
- callback('无效的会话,或者会话已过期,请重新登录。');
- }else{
- failfile = failfile.concat(filePaths[i])
- failUp++;
- }
- },
- fail: function(res) {
- uni.hideLoading();
- failfile = failfile.concat(filePaths[i])
- failUp++;
- },
- complete: function(rsp) {
- // console.log(rsp, filePaths[i])
- uni.hideLoading();
- i++;
- if (i == length) {
- uni.showToast({
- title: '总共' + successUp + '张上传成功,' + failUp + '张上传失败!',
- icon: 'none',
- duration: 2000
- });
- callback(files);
- } else { //递归调用upload函数
- uploadmore(api, filePaths, successUp, failUp, i, length, files, callback);
- }
- }
- });
- }
- export function clickPeople(e){//点击下载
- let _this = this;
- //下载地址
- var downloadUlr = e;
- //获取地址后缀
- var suffix = e.split(".")[e.split(".").length - 1];
-
- //判断是否为(图片或视频)
- if(e.substring(e.length - 3) == "MP4" || e.substring(e.length - 3) == "mp4" || e.substring(e.length - 3) == "jpg" || e.substring(e.length - 3) == "png"){
-
- const downloadTask = uni.downloadFile({
- url:e,
- success: res => {
- if (res.statusCode === 200) {
-
- if(res.tempFilePath.substring(res.tempFilePath.length - 3) == "mp4" || res.tempFilePath.substring(res.tempFilePath.length - 3) == "MP4"){//视频
- //保存视频到相册
- uni.saveVideoToPhotosAlbum({
- filePath: res.tempFilePath,
- success: function () {
- uni.showToast({
- title: '保存成功',
- icon: 'none',
- duration:2000,
- mask:true
- });
- },
- fail: function() {
- // this.loadProgress = 0;
- uni.showToast({
- title: '保存失败',
- icon: 'none'
- });
- }
- });
- }else{//图片
- // 图片保存到手机相册
- uni.saveImageToPhotosAlbum({
- filePath: res.tempFilePath,
- success: function() {
- uni.showToast({
- title: '保存成功',
- icon: 'none',
- duration:2000,
- mask:true
- });
- },
- fail: function() {
-
- // this.loadProgress = 0;
- uni.showToast({
- title: '保存失败',
- icon: 'none'
- });
- }
- });
-
- }
- }else{
- uni.showToast({
- title:'下载失败',
- icon:"none"
- })
- }
- },
- fail:(err) => {
-
- // this.loadProgress = 0;
- uni.showToast({
- icon:"none",
- mask:true,
- title:'下载失败'
- })
- }
- });
- // downloadTask.onProgressUpdate((res) => {
- // this.loadProgress = res.progress;
- // if (this.loadProgress < 100) {
-
- // } else {
- // this.loadProgress = 0;
- // }
- // if(res.totalBytesExpectedToWrite < 10485760){
- // this.dltDownLvNew = Math.ceil(res.totalBytesWritten / 1024) + "KB";
- // this.dltDownLvAll = Math.ceil(res.totalBytesExpectedToWrite / 1024) + "KB";
- // this.dltDownLvWc = res.progress + "%"
- // }else{
- // this.dltDownLvNew = Math.ceil(res.totalBytesWritten / 1048576) + "M"
- // this.dltDownLvAll = Math.ceil(res.totalBytesExpectedToWrite / 1048576) + "M";
- // this.dltDownLvWc = res.progress + "%"
- // }
- // });
- }else{
- //下载文件为非图片和视频的文件
- let _this = this;
-
- const downloadTaskt = uni.downloadFile({
- url:e,//下载地址接口返回
- success:(data) => {
- uni.hideLoading()
- if(data.statusCode === 200){
- var sFilePath = data.tempFilePath
- var sFileName = downloadUlr.split('/')[downloadUlr.split('/').length - 1];//原来的文件名
-
- //#ifdef APP-PLUS
- //文件保存到本地
- uni.saveFile({
- tempFilePath: data.tempFilePath,//临时路径
- success:function(res){
-
- var savedFilePath = res.savedFilePath;
- let osname = plus.os.name;
- //ios手机直接打开文件,手动存储文件到手机,Android手机从根目录创建文件夹,保存文件并改名
- if (osname == 'Android') {
- uni.showToast({
- icon:'none',
- mask:true,
- title:'保存成功1',
- duration:1000,
- });
- // _this.fSetFileName(, sFilePath,e);
- // fSetFileName(sFilePath, sFileName,urls)
- // var =
- var sFileNames = res.savedFilePath.split('/')[res.savedFilePath.split('/').length - 1];//原来的文件名
-
- var aFileUrl = sFilePath.split('/').splice(0, sFilePath.split('/').length - 1);//saveFile API保存的本地地址
-
- var url = downloadUlr;//下载地址
- // 'url下载地址(和上面的一样)'
- let dtask = plus.downloader.createDownload(url, {
- filename: "file://storage/emulated/0/Download/" + sFileNames //在手机存储更目录创建一个叫AAA的文件夹,把文件存储进去,并更改会原名
- },
- (d, status) => {
- console.log(status,77)
- if (status == 200) {
- let fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename);
- console.log(fileSaveUrl,99)
- } else {
- //下载失败
- plus.downloader.clear(); //清除下载任务
- }
- })
- dtask.start();
-
-
-
- }
- setTimeout(() => {
- //打开文档查看
- uni.openDocument({
- filePath:res.savedFilePath,
- success:function(res){
- // console.log("成功打开文件")
- },
- fail(){
- // console.log("打开文件失败")
- }
- })
- },1000)
- },
- fail:function(res){
-
- }
- });
- //#endif
- //#ifdef MP-WEIXIN
- //小程序对文件下载并不友好,不建议使用小程序当做下载器
- const FileSystemManager = wx.getFileSystemManager();
- FileSystemManager.saveFile({//下载成功后保存到本地
- tempFilePath:data.tempFilePath,
- filePath:wx.env.USER_DATA_PATH + "/" + sFileName,
- success(res2){
- if(res2.errMsg == 'saveFile:ok'){
-
- // 判断手机平台是 Android 还是 IOS
- if (uni.getSystemInfoSync().platform === 'android') {
- // console.log('运行Android上')
- uni.showModal({
- title:"保存地址为",
- content:"手机存储/Android/data/com.tencent.mm/MicroMsg/wxanewfiles"
- })
- } else {
- // console.log('运行iOS上')
- uni.showToast({
- title:"请转移APP下载",
- icon:"none"
- })
- }
-
- }else{
- uni.showToast({
- title:"下载失败",
- icon:"none"
- })
- }
- },
- fail(){
- uni.showToast({
- title:"下载失败",
- icon:"none"
- })
- }
- })
- //#endif
-
- }else{
- this.loadProgress = 0;
- uni.showToast({
- icon:"none",
- mask:true,
- title:"下载失败"
- })
- }
- },
- fail:(err) => {
- this.arcZzShow = false;
- this.loadProgress = 0;
- uni.showToast({
- icon:"none",
- mask:true,
- title:"下载失败"
- })
- }
- })
- // downloadTaskt.onProgressUpdate((res) => {
- // this.loadProgress = res.progress;
- // if (this.loadProgress < 100) {
-
- // } else {
- // this.loadProgress = 0;
- // }
- // if(res.totalBytesExpectedToWrite < 10485760){
- // this.dltDownLvNew = Math.ceil(res.totalBytesWritten / 1024) + "KB";
- // this.dltDownLvAll = Math.ceil(res.totalBytesExpectedToWrite / 1024) + "KB";
- // this.dltDownLvWc = res.progress + "%"
- // }else{
- // this.dltDownLvNew = Math.ceil(res.totalBytesWritten / 1048576) + "M"
- // this.dltDownLvAll = Math.ceil(res.totalBytesExpectedToWrite / 1048576) + "M";
- // this.dltDownLvWc = res.progress + "%"
- // }
-
- // });
- }
-
-
- }
- // 下载改名
- export function fSetFileName(sFilePath, sFileName,urls) {
- var sFileName = sFileName.split('/')[sFileName.split('/').length - 1];//原来的文件名
-
- var aFileUrl = sFilePath.split('/').splice(0, sFilePath.split('/').length - 1);//saveFile API保存的本地地址
-
- var url = urls;//下载地址
- // 'url下载地址(和上面的一样)'
- let dtask = plus.downloader.createDownload(url, {
- filename: "file://storage/emulated/0/AAA/" + sFileName //在手机存储更目录创建一个叫AAA的文件夹,把文件存储进去,并更改会原名
- },
- (d, status) => {
- console.log(status,77)
- if (status == 200) {
- let fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename);
- console.log(fileSaveUrl,99)
- } else {
- //下载失败
- plus.downloader.clear(); //清除下载任务
- }
- })
- dtask.start();
- }
|