// import qq from '@/utils/qqmap-wx-jssdk.min.js' // import QQMapWX from './qqmap-wx-jssdk.min.js'; // import QQMapWX from './qqmap-wx-jssdk.min.js'; // import OSS from "ali-oss/lib/browser"; // #ifdef MP-WEIXIN const QQMapWX=require('./qqmap-wx-jssdk.min.js') import * as QQMapwx from '@/utils/qqmap-wx-jssdk.min.js' // #endif // #ifdef MP-WEIXIN var qqmapsdk; // #endif import { imghost,host,oss } from '@/config/host.js' import {getSTS} from "@/api/other"; const api = { loginModalState: true, // #ifdef MP-WEIXIN mapLocation:()=>{ return new Promise((relove, reject) => { uni.authorize({ scope: 'scope.userFuzzyLocation', success(res) { if(res.errMsg == 'authorize:ok'){ uni.getFuzzyLocation({ type: 'wgs84', success(re) { uni.chooseLocation({ latitude:re.latitude, longitude:re.longitude, success: function (data) { let obj={ latitude:data.latitude, longitude: data.longitude, address:data.address } if(data.errMsg=='chooseLocation:ok'){ qqmapsdk=new QQMapWX({ key:'SUUBZ-MIX6C-Y7U2B-AXT6O-NZ2W5-D6F2B' }) qqmapsdk.reverseGeocoder({ location:{ latitude:data.latitude , longitude: data.longitude }, success: function(mapres){ console.log(mapres); if(mapres.message=='query ok'){ obj.name=mapres.result.formatted_addresses.recommend obj.city=mapres.result.address_component.city relove(obj) } } }) } // console.log('位置名称:' + data.name); // console.log('详细地址:' + data.address); // console.log('纬度:' + data.latitude); // console.log('经度:' + data.longitude); } }); } }) } }, fail(err) { console.log(err) } }) }) }, // #endif getloction(details){ return new Promise((relove, reject)=>{ qqmapsdk=new QQMapWX({ key:'SUUBZ-MIX6C-Y7U2B-AXT6O-NZ2W5-D6F2B' }) qqmapsdk.geocoder({ //获取表单传入地址 address: details.activedeta.city+details.activedeta.address, //地址参数,例:固定地址,address: '北京市海淀区彩和坊路海淀西大街74号' success: function(res) {//成功后的回调 console.log(res); relove(res.result) }, fail: function(error) { console.error(error); }, complete: function(res) { console.log(res); } }) }) }, urlToObj:(url)=>{ let obj = {} let str = url.slice(url.indexOf('?') + 1) let arr = str.split('&') for (let j = arr.length, i = 0; i < j; i++) { let arr_temp = arr[i].split('=') obj[arr_temp[0]] = arr_temp[1] } return obj }, upfile:(file)=>{ let token=uni.getStorageSync('token') // console.log(file); return new Promise((relove, reject) => { // console.log(file); uni.showLoading({ title:'上传中' }) uni.uploadFile({ url: `${host}/other/upload`, //仅为示例,非真实的接口地址 filePath:file, name: 'file', header: { "content-type": "application/json;charset=utf-8", "Authorization":"Bearer "+ token.token }, success: (uploadFileRes) => { uni.hideLoading() relove(JSON.parse(uploadFileRes.data).data) // console.log(JSON.parse(uploadFileRes.data).data); } }); }) }, uploadOssFile:(file)=>{ return new Promise((resolve, reject) => { getSTS({}).then(res => { // const client = new OSS(res.data); // console.log(file); uni.showLoading({ title:'上传中' }) let timer = new Date(); let address = timer.getFullYear() + '' + (timer.getMonth() + 1) + '' + timer.getDate(); address = 'upload/' + address + '/'; var imageSrc = file; let str = file.substr(file.lastIndexOf('.')); let nameStr = address + timer.getTime() + str; uni.uploadFile({ url: `${oss}`, //仅为示例,非真实的接口地址 filePath:file, name: 'file', header: { "content-type": "application/json;charset=utf-8", }, formData:Object.assign({ name:nameStr, key:nameStr, success_action_status: '200', },res.data), success: (uploadFileRes) => { uni.hideLoading() resolve ({show_path:oss+'/' + nameStr}); // relove(JSON.parse(uploadFileRes.data).data) // console.log(JSON.parse(uploadFileRes.data).data); }, fail:(res) =>{ uni.hideLoading() console.log(res) return {}; } }); }); }) }, imgurl:(url)=>{ if(url){ if(url.indexOf('http')==-1){ return imghost+url }else{ return url } }else{ return '' } }, imgurl2:(url)=>{ if(url){ if(url.indexOf('https')==-1){ console.log(url.replace('http','https')) return url.replace('http','https') }else{ return url } }else{ return '' } }, /** * 失败 */ error: (msg) => { return new Promise((relove, reject) => { uni.showToast({ title: msg, icon: "none" }) setTimeout(() => { relove(); }, 1500) }) }, /** * 成功提示 */ success: (msg) => { return new Promise((relove, reject) => { uni.showToast({ title: msg, icon: "success" }) setTimeout(() => { relove(); }, 1500) }) }, /** * 确认对话框 * @param {Object} contents * @param {Object} callback */ confirm: (contents) => { return new Promise((relove, reject) => { uni.showModal({ title: '提示', content: contents, success: function(res) { if (res.confirm) { relove(); } } }); }) }, payment: (param) => { return new Promise((relove, reject) => { uni.requestPayment({ provider: 'wxpay', timeStamp: param.timeStamp, nonceStr: param.nonceStr, package: param.package, signType: param.signType, paySign: param.paySign, success: function(res) { relove(res); }, fail: function(err) { console.log(err); api.error("支付失败"); reject(err); } }); }) }, navHeight: () => { let system,menu,systemBar,navigationBar,navPaddingTop,navHeight,headerPadding,systemBarHeight; //获取系统信息 try{ // #ifdef MP-WEIXIN uni.getSystemInfo({ success: res => { system = res } }) //获取胶囊信息 menu = uni.getMenuButtonBoundingClientRect() systemBar = system.statusBarHeight //状态栏高度 navigationBar = (menu.top - system.statusBarHeight) * 2 + menu.height //胶囊高度 navPaddingTop = systemBar; navHeight = menu.height; headerPadding = (menu.top - systemBar); systemBarHeight = (navPaddingTop + navigationBar) // #endif // #ifdef H5 navPaddingTop = 0; navHeight = 44; headerPadding = 0; systemBarHeight = 44; // this.system.windowHeight = // #endif }catch(e){ //默认高度,刘海屏 navPaddingTop = 43; navHeight = 32; headerPadding = 4; // this.system.windowHeight = '1080'; } return { navPaddingTop:navPaddingTop, navHeight:navHeight, headerPadding:headerPadding, // #ifdef MP-WEIXIN systemBarHeight:systemBarHeight, windowHeight:(system.windowHeight - navigationBar - systemBar), // #endif // #ifdef H5 systemBarHeight:systemBarHeight, windowHeight:'100vh', // #endif menu:menu }; }, wxSystemInfo: () => { let system; //获取系统信息 try{ uni.getSystemInfo({ success: res => { this.system = res } }) }catch(e){ } return { system:this.system }; }, uniSysInfo:() => { return new Promise((resolve,reject)=>{ // #ifdef MP-WEIXIN let menuButtonInfo = uni.getMenuButtonBoundingClientRect(); let sysMenuHeight = uni.$u.sys().statusBarHeight; let menuButtonHeight = menuButtonInfo.height let menuButtonPadding = menuButtonInfo.top - sysMenuHeight // #endif // #ifdef H5 let menuButtonInfo = {height:44,top:4}; let sysMenuHeight = 44; let menuButtonHeight =44 let menuButtonPadding = 0 // #endif let data = { sysMenuHeight:sysMenuHeight, menuButtonHeight:menuButtonHeight, menuButtonPadding:menuButtonPadding, sysAndMenuHeight:(sysMenuHeight+menuButtonHeight+(menuButtonPadding*2)) } uni.setStorageSync('sysInfo',data); resolve(data); }) }, logout:(reLaunch = true,confirm = true) => { return new Promise((resolve, reject) => { if(confirm){ api.confirm('是否退出登录').then(res => { api.doLogout(reLaunch) resolve(); }) }else{ api.doLogout(reLaunch); resolve(); } }) }, doLogout:(reLaunch = true)=>{ uni.showLoading('退出中'); uni.clearStorageSync(); store.commit('userInfo',{}); store.commit('userExtends',{}); api.uniSysInfo().then(() => { if(reLaunch){ uni.reLaunch({url:'/pages/Index/index'}); } uni.hideLoading(); }).catch(err => { uni.hideLoading(); }) }, navTo:(url,token = false) => { if (token) { if (uni.getStorageSync('token')) { uni.navigateTo({ url: url }) } else { uni.navigateTo({ url: '/pages/Login/index' }) } }else{ uni.navigateTo({ url: url }) } }, getFileType:(name)=> { let str = name.substr(name.lastIndexOf('.')+1).toUpperCase(); const imageType = ['JPG','JPEG','PNG','GIF','BMP','TIF','TGA','DDS','SVG','EPS','RAW','PSD']; const videoType = ['MP4','MOV','WMV','FLV','AVI','AVCHD','WEBM','MKV']; if(imageType.indexOf(str)!==-1){ return 'image'; }else if(videoType.indexOf(str)!==-1){ return 'video'; }else{ return 'none'; } }, user:(field = null) => { const user = uni.getStorageSync('user'); return user[field] || user; }, } export default api;