/**
* @Time 2019-12-12
* @Author Zhengjingyi
* @Action 全局公共方法
*/
// import requestUrl from './config.js'
const Tool = {
/**
* @封装公共get数据请求方法无加载动画
* @方法参数:请求地址,请求后台需要的参数字段,回调函数
* @自定义请求头信息
*/
get:function(url,data,callback){
uni.request({
url: requestUrl + url,
data:data,
header: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'X-Token': uni.getStorageSync('token') ? uni.getStorageSync('token') : 'token',
'cookie': uni.getStorageSync('sessionid')
},
method: 'GET',
success: (response) => {
if(response.statusCode !== 200){
uni.showToast({icon: 'none',title:'服务器连接错误',duration: 2000})
callback(response.statusCode)
}else{
callback(response.data)
}
},
fail: (error) => {
if (error) {
uni.showToast({icon: 'none',title: '网络错误,请稍后重试',duration: 2000})
}
}
})
},
/**
* @封装公共get数据请求方法有加载动画
* @方法参数:请求地址,请求后台需要的参数字段,回调函数
* @自定义请求头信息
*/
lodingGet:function(url,data,callback){
uni.showLoading({mask: true,title:'加载中~',})
uni.request({
url: requestUrl + url,
data:data,
header: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'X-Token': uni.getStorageSync('token') ? uni.getStorageSync('token') : 'token',
'cookie': uni.getStorageSync('sessionid')
},
method: 'GET',
success: (response) => {
if(response.statusCode !== 200){
uni.showToast({icon: 'none',title: '服务器连接错误',duration: 2000})
callback(response.statusCode)
}else{
callback(response.data)
}
},
fail: (error) => {
if (error) {
uni.showToast({icon: 'none',title: '网络错误,请稍后重试',duration: 2000})
}
},
complete: () => {
setTimeout(function () {
uni.hideLoading()
}, 250)
}
})
},
/**
* @封装公共post数据请求方法
* @方法参数:请求地址,请求后台需要的参数字段,回调函数
*/
post:function(url,data,loadingStatus,callback){
if(loadingStatus){uni.showLoading({mask: true,title:'加载中~'})}
uni.request({
url: requestUrl+url,
data:data,
header: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'X-Token': uni.getStorageSync('token') ? uni.getStorageSync('token') : 'token',
'cookie': uni.getStorageSync('sessionid')
},
method: 'POST',
success: (response) => {
if(loadingStatus){uni.hideLoading()}
const result = response.data
callback(result)
},
fail: (error) => {
uni.hideLoading()
if (error) {
uni.showToast({icon: 'none',title: '网络错误,请稍后重试',duration: 2000})
}
}
})
},
getComStorage:function(key){// 获取本地Storage
return new Promise(function(resolve,reject) {
uni.getStorage({
key: key,
success: function (res){
resolve(res.data)
}
})
})
},
setStorage:function(key,data){// 存储本地Storage
return new Promise(function(resolve,reject) {
uni.setStorage({
key: key,
data:data,
success: function (res){
}
})
})
},
getStorage:function(){// 获取本地userInfo
return new Promise(function(resolve,reject) {
uni.getStorage({
key: 'userInfo',
success: function (res){
resolve(res.data)
},
fail: function(res){
reject(false)
}
})
})
},
getStorageAddressKey:function(){// 获取本地地址信息
return new Promise(function(resolve,reject) {
uni.getStorage({
key: 'address_key',
success: function (res){
resolve(res.data)
}
})
})
},
loginStatus:function(){
// 获取用户是否登陆 1:已登陆,否则未登陆
return new Promise(function(resolve,reject) {
uni.getStorage({
key: 'userInfo',
success: function (res){
if(res.data.code == '1'){
resolve(true)
} else {
resolve(false)
}
}
})
})
},
navigateLinkJump:function(){// 统一处理根据用户类型跳转
const UserInfo = uni.getStorageSync('userInfo')
let Jump_URL = ''
let UserTypeObj = {
1: '/pages/collection/list',
2: '/pages/collection/list',
3: '/pages/collection/sms',
4: '/pages/collection/sms',
}
if(UserInfo){
/**
* 用户类型(userType) 1协销人员,2客服,3财务,4超级管理员
* 1. 协销人员跳转到收款列表页面
* 2. 客服跳转到收款列表页面
* 3. 财务人员跳转到款项识别页面
* 4. 超级管理员跳转到款项识别页面
* */
Object.keys(UserTypeObj).forEach(function(key) {
if (key == UserInfo.userType) {
Jump_URL = UserTypeObj[key]
}
})
}else{
Jump_URL = '/pages/login/login-account'
}
Tool.redirectTo(Jump_URL)
},
navigateBack:function(page){
uni.navigateBack({
delta: page
})
},
navigateTo:function(url){
//路由跳转:页面之间路由跳转
uni.navigateTo({
url:url
})
},
redirectTo:function(url){
//路由跳转:关闭当前页跳转到新页面
uni.redirectTo({
url:url
})
},
switchTabTo:function(url){
//路由跳转:底部 tab页
uni.switchTab({
url:url
})
},
isNumber:function(value){
//验证是否为数字
var patrn = /^(-)?\d+(\.\d+)?$/
if (patrn.exec(value) == null || value == '') {
return false
} else {
return true
}
},
getWindowHeight:function(){
// 获取窗口高度
const {windowHeight, pixelRatio} = wx.getSystemInfoSync()
return windowHeight
},
adaptRichTextImg:function(res){
/**
*@富文本实现图片自适应
*@style再添加自适应样式
*/
const html = res.replace(/
]*>/gi,function(match,capture){
let match1 = match.replace(/
= 1 && month <= 9) {
month = '0' + month
}
if (strDate >= 0 && strDate <= 9) {
strDate = '0' + strDate
}
if (bours >= 0 && bours <= 9) {
bours = '0' + bours
}
if (min >= 0 && min <= 9) {
min = '0' + min
}
if (s >= 0 && s <= 9) {
s = '0' + s
}
var currentdate = year + seperator1 + month + seperator1 + strDate +' ' + bours+ ':'+ min+ ':'+ s
return currentdate
}
}
/**
*@导出
*/
module.exports = {
get: Tool.get,
post: Tool.post,
lodingGet: Tool.lodingGet,
isNumber: Tool.isNumber,
FormatMoney: Tool.FormatMoney,
navigateTo: Tool.navigateTo,
redirectTo: Tool.redirectTo,
switchTabTo: Tool.switchTabTo,
navigateBack: Tool.navigateBack,
formatDate: Tool.formatDate,
loginStatus: Tool.loginStatus,
setStorage: Tool.setStorage,
getStorage: Tool.getStorage,
getComStorage: Tool.getComStorage,
navToListPage: Tool.navToListPage,
getWindowHeight: Tool.getWindowHeight,
adaptRichTextImg: Tool.adaptRichTextImg,
getStorageAddressKey: Tool.getStorageAddressKey,
regexSets: Tool.regexSets,
timestampToTime: Tool.timestampToTime,
getNowFormatDate : Tool.getNowFormatDate ,
navigateLinkJump: Tool.navigateLinkJump
}