import common_url from './common_url.js' const http = { get: function(url, params) { return new Promise((resolve, reject) => { try { uni.request({ url: common_url + url, data: params, method: 'GET', header:{ token:uni.getStorageSync('token')||'' }, success: (res) => { //console.log('get-res', res); if (res.statusCode == 200) { resolve(res.data) } else if (res.statusCode == 401) { uni.showToast({ title: '登录过期,重新登录', icon: "none", duration: 1500 }); } else { uni.showToast({ title: res.data.message, icon: "none", duration: 1500 }); } } }) } catch (err) { reject(err) } }) }, post: function(url, params) { return new Promise((resolve, reject) => { try { uni.request({ url: common_url + url, data: params, method: 'POST', header:{ token:uni.getStorageSync('token')||'' }, success: (res) => { //console.log('post-res', res); if (res.statusCode == 200) { resolve(res.data) } else if (res.statusCode == 401) { uni.showToast({ title: '登录过期,重新登录', icon: "none", duration: 1500 }); } else { uni.showToast({ title: res.data.message, icon: "none", duration: 1500 }); } }, fail: (error) => { console.log(error) } }) } catch (err) { reject(err) } }) }, //未使用 uploadvideo: function(count) { return new Promise((resolve, reject) => { try { let token = uni.getStorageSync('Token') || ''; let header = { 'content-type': 'application/x-www-form-urlencoded' } uni.chooseVideo({ count: 1, //默认9 sourceType: ['camera', 'album'], success: function(res) { console.log('res', res) let tempFilePath = res.tempFilePath; let params = {}; uni.uploadFile({ url: common_url + '/api/common/upload', //仅为示例,非真实的接口地址 filePath: tempFilePath, name: 'file', formData: params, header: header, success: (res1) => { if (res1.statusCode == 200) { let res = JSON.parse(res1.data) resolve(res) } else { uni.showToast({ title: res1.data.message, icon: "none", duration: 1500 }); console.log(res1.data.message) } } }); } }); } catch (e) { reject(e) } }) }, //多图上传普通 uploads: function(params) { return new Promise((resolve, reject) => { try { uni.chooseImage({ count: 4, //默认9 sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], //从相册选择 success: (res) => { let tempFilePath = res.tempFilePaths; let token = uni.getStorageSync('Token') || ''; let header = { 'token': token, } let imgs = [] for (let i = 0, len = tempFilePath.length; i < len; i++) { uni.uploadFile({ url: common_url + '/common/uploadfile', filePath: tempFilePath[i], name: 'file', formData: params, header: header, success: (res1) => { let data = JSON.parse(res1.data) console.log('res1', res1, data) if (res1.statusCode == 200 && data.code == 1) { let res = data.data.url imgs.push(res) if (imgs.length == tempFilePath .length) { resolve(imgs) } } else { uni.showToast({ title: res1.data.msg, icon: "none", duration: 1500 }); } } }); } } }); } catch (e) { reject(e) } }) }, //单图上传普通 uploadone: function(params) { return new Promise((resolve, reject) => { try { uni.chooseImage({ count: 1, //默认9 sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], //从相册选择 success: (res) => { let tempFilePath = res.tempFilePaths[0]; uni.uploadFile({ url: common_url + '/common/uploadfile', filePath: tempFilePath, name: 'file', formData: params, success: (res1) => { console.log(res1) let data = JSON.parse(res1.data) if (res1.statusCode == 200 && data.code == 200) { let res = data.url resolve(res) } else { uni.showToast({ title: res1.errMsg, icon: "none", duration: 1500 }); } } }); } }); } catch (e) { reject(e) } }) }, //多图上传base64 upload: function(params) { return new Promise((resolve, reject) => { try { uni.chooseImage({ count: 4, //默认9 sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], //从相册选择 success: function(res) { if (res.tempFiles.length > 4) { uni.showToast({ title: '一次最多上传4张图片', icon: "none" }); return } let tempFilePath = res.tempFiles; let imgs = [] for (let i = 0, len = tempFilePath.length; i < len; i++) { uni.getFileSystemManager().readFile({ filePath: tempFilePath[i].path, encoding: 'base64', success: r => { if (r.errMsg.indexOf('readFile:ok') > -1) { imgs.push({ type: "image", file64Str: 'data:image/jpeg;base64,' + r.data }) if (imgs.length == tempFilePath .length) { resolve(imgs) } } } }) } } }); } catch (e) { reject(e) } }) }, //上传图片和视频base64 uploadMedia: function(params) { return new Promise((resolve, reject) => { try { uni.chooseMedia({ count: 6, //默认9 mediaType: ['image', 'video'], //可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], //从相册选择 sizeType: ['compressed'], maxDuration: 30, //视频时长 success: (res) => { if (res.errMsg == "chooseMedia:ok") { if (res.type == 'video') { //视频 let obj1 = { type: 'vid', file64Str: '', imageRequest: { type: 'cover', file64Str: "" } } let vData = res.tempFiles[0] //视频封面 uni.getFileSystemManager().readFile({ filePath: vData.thumbTempFilePath, encoding: 'base64', success: r => { if (r.errMsg.indexOf('readFile:ok') > - 1) { obj1.imageRequest.file64Str = 'data:image/jpeg;base64,' + r .data } } }) //视频内容 uni.getFileSystemManager().readFile({ filePath: vData.tempFilePath, encoding: 'base64', success: r => { if (r.errMsg.indexOf('readFile:ok') > - 1) { obj1.file64Str = 'data:video/mp4;base64,' + r .data } } }) resolve(obj1) } else { //图片 let imgArry = res.tempFiles let imgs = [] for (let i = 0, len = imgArry.length; i < len; i++) { uni.getFileSystemManager().readFile({ filePath: imgArry[i].tempFilePath, encoding: 'base64', success: r => { if (r.errMsg.indexOf( 'readFile:ok') > -1) { imgs.push({ type: "image", file64Str: 'data:image/jpeg;base64,' + r .data }) if (imgs.length == imgArry .length) { resolve(imgs) } } } }) } } } } }); } catch (e) { reject(e) } }) } } export default http;