You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

509 lines
11 KiB

4 years ago
import {
host
} from '../Gdata'
import {
router_list
} from '../config'
const getRouter = function() {
var pages = getCurrentPages() //获取加载的页面
var currentPage = pages[pages.length - 1] //获取当前页面的对象
let id = ''
router_list.forEach(item => {
if (item.router_name == currentPage.route) {
id = item.id
}
})
return id
}
const formatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : `0${n}`
}
const httpUtil = (url, method, data, isLoading = true) => {
if (isLoading) {
wx.showLoading({
title: '加载中'
});
}
return new Promise(function(resolve, reject) {
wx.request({
url: host + url,
data: data,
method: method,
// header: { "Content-Type": "application/x-www-form-urlencoded" },
success: res => {
if (isLoading) {
wx.hideLoading();
}
if (200 == res.data.status) {
resolve(res.data.data);
} else {
wx.showToast({
title: '请求错误!',
icon: 'none',
duration: 1000
})
}
},
fail: res => {
wx.hideLoading();
}
})
})
}
const http = (url, method, data, isLoading = true) => {
if (isLoading) {
wx.showLoading({
title: '加载中'
});
}
return new Promise(function(resolve, reject) {
let token = getApp().globalData.token
let uid = getApp().globalData.uid
let cps = wx.getStorageSync("cps") || null
let header = []
if (cps !== null && cps.vTime >= new Date().getTime()) {
header = {
'token': token,
'uid': String(uid),
'cps': cps.value
}
} else {
header = {
'token': token,
'uid': String(uid)
}
}
// if (token && uid) {
wx.request({
url: host + url,
data: data,
method: method,
header,
success: res => {
if (isLoading) {
wx.hideLoading();
}
if (200 == res.data.status) {
resolve(res.data.data);
} else if (401 == res.data.status || 402 == res.data.status || 403 == res.data
.status) {
wx.showModal({
title: '温馨提示',
content: '未登录或信息过期,是否前往登录?',
confirmText: '前往',
success(res) {
if (res.confirm) {
getApp().globalData.gologin = true
wx.switchTab({
url: '/pages/user/user_index/user'
})
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
} else {
wx.showToast({
title: res.data.msg,
icon: 'none',
duration: 2000
})
}
},
fail: res => {
wx.hideLoading();
}
})
// } else {
// wx.showToast({
// title: '未登录\r\n点击个人中心头像登录',
// icon: 'none',
// duration: 2000
// })
// }
})
}
const login_check = n => {
if (!getApp().globalData.token || !getApp().globalData.uid || "undefined" == typeof(getApp().globalData
.token) || "undefined" == typeof(getApp().globalData.uid)) {
if (n == 1) {
wx.showToast({
title: '未登录',
icon: 'error',
duration: 2000,
complete() {
function gologin() {
wx.navigateTo({
url: '/pages/user/login/login',
})
}
setTimeout(gologin, 2000);
}
})
} else {
wx.showModal({
title: '温馨提示',
content: '您还没有登录,是否前往登录?',
confirmText: '前往',
success(res) {
if (res.confirm) {
// getApp().globalData.gologin = true
// wx.switchTab({
// url: '/pages/user/user_index/user'
// })
wx.navigateTo({
url: '/pages/user/login/login',
})
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
}
} else {
return true
}
}
const location_city = function() {
let default_city = arguments[0]
return new Promise(function(resolve, reject) {
if (default_city == "beijing") {
let longitude = 116.404368
4 years ago
let latitude = 39.924338
let map_type = 1
// #ifdef MP-ALIPAY
map_type = 2
4 years ago
// #endif
httpUtil("/api/v1/get-city", "get", {
latitude,
longitude,
type: map_type
}).then(res1 => {
resolve(res1)
})
} else {
4 years ago
uni.getLocation({
// #ifndef MP-ALIPAY
type: 'wgs84',
4 years ago
// #endif
4 years ago
success(res) {
4 years ago
console.log(res);
4 years ago
if (!res.longitude) {
reject('定位未开启')
4 years ago
setting()
return
4 years ago
}
let map_type = 1
// #ifdef MP-ALIPAY
map_type = 2
4 years ago
// #endif
httpUtil("/api/v1/get-city", "get", {
longitude: res.longitude,
latitude: res.latitude,
type: map_type
}).then(res1 => {
resolve(res1)
})
},
4 years ago
fail(res) {
// reject('定位失败,下拉重试或点击选择城市')
reject('定位未开启')
4 years ago
setting()
}
})
}
})
}
const setting = function() {
wx.getSetting({ //先查看授权情况
success: function(res) {
var statu = res.authSetting;
if (!statu['scope.userLocation']) { //判断是否授权,没有授权就提示下面的信息
wx.showModal({
4 years ago
// title: '需要获取您的地理位置,请确认授权定位,否则小程序功能将无法使用',
title: '需要获取您的地理位置,请确认授权定位,当前定位为北京',
4 years ago
cancelColor: '#666666',
success: function(tip) {
if (tip.confirm) { //查看是否点击确定
wx.openSetting({ //打开设置
success: function(data) {
if (data.authSetting[
"scope.userLocation"] ==
true) { //到这一步表示打开了位置授权
// wx.reLaunch({
// url: '/pages/index/index/index',
// })
wx.navigateBack({
delta: 0,
})
4 years ago
} else {
// #ifndef MP-ALIPAY
wx.showToast({
4 years ago
title: '授权定位失败',
4 years ago
icon: 'error',
duration: 1000
})
4 years ago
// #endif
4 years ago
// #ifdef MP-ALIPAY
my.showAuthGuide({
authType: 'LBS',
success: (res) => {
// wx.reLaunch({
// url: '/pages/index/index/index',
// })
},
fail: (error) => {},
})
// #endif
}
4 years ago
}
})
} else {
4 years ago
// wx.showToast({
// title: '重新进入小程序唤起定位授权',
// icon: 'error',
// duration: 1000
// })
4 years ago
}
}
})
} else {
//用户已授权,但是获取地理位置失败,提示用户去系统设置中打开定位
wx.showModal({
title: '',
showCancel: false,
4 years ago
content: '请在系统设置中打开定位服务',
4 years ago
confirmText: '确定',
success: function(res) {}
})
}
}
})
}
const orderSubmit = order_data => {
return new Promise(function(resolve, reject) {
// 订单来源8->微信小程序、9->抖音小程序、10->支付宝小程序
let order_type = 0
// #ifdef MP-WEIXIN
order_type = 8
// #endif
// #ifdef MP-TOUTIAO
order_type = 9
// #endif
// #ifdef MP-ALIPAY
order_type = 10
// #endif
order_data.order_type = order_type
http('/api/v1/order-submit', 'post', order_data).then((res) => {
let orderPayData = {}
// #ifdef MP-WEIXIN
orderPayData = {
timeStamp: res.timestamp,
nonceStr: res.nonceStr,
package: res.package,
signType: res.signType,
paySign: res.paySign
}
// #endif
// #ifdef MP-TOUTIAO
orderPayData = {
order_id: res.order_id,
order_token: res.order_token
}
// #endif
// #ifdef MP-ALIPAY
orderPayData = {
tradeNO: res.trade_no
}
// #endif
orderPay(orderPayData)
});
})
}
const orderPay = orderPayData => {
// #ifdef MP-WEIXIN
wx.requestPayment({
...orderPayData,
success(res) {
wx.showToast({
title: '支付成功!',
icon: 'success',
duration: 1000
})
setTimeout(function() {
uni.reLaunch({
url: '/pages/user/my_order/my_order'
});
}, 500);
},
fail(res) {
wx.showToast({
title: '支付未成功',
icon: 'error',
duration: 2000
});
setTimeout(function() {
uni.reLaunch({
url: '/pages/user/my_order/my_order'
});
}, 500);
}
});
// #endif
// #ifdef MP-TOUTIAO
let orderInfo = orderPayData
uni.pay({
orderInfo,
service: 5,
success(res) {
let title = ''
let icon = ''
switch (res.code) {
case 0:
title = '支付成功!';
icon = 'success';
break
case 1:
title = '支付超时!';
icon = 'fail';
break
case 2:
title = '支付失败!';
icon = 'fail';
break
case 3:
title = '支付关闭!';
icon = 'fail';
break
case 4:
title = '支付取消!';
icon = 'fail';
break
default:
title = '支付结束!';
icon = 'fail';
}
uni.showToast({
title,
icon,
duration: 1000
});
setTimeout(function() {
uni.reLaunch({
url: '/pages/user/my_order/my_order'
});
}, 500);
},
fail(res) {
uni.showToast({
title: '支付未成功',
icon: 'fail',
duration: 2000
});
setTimeout(function() {
uni.reLaunch({
url: '/pages/user/my_order/my_order'
});
}, 500);
}
});
// #endif
// #ifdef MP-ALIPAY
my.tradePay({
...orderPayData,
success: (res) => {
console.log(res);
let icon = 'fail'
if (res.resultCode === 9000) {
icon = 'success'
}
my.showToast({
content: res.memo,
type: icon,
duration: 2000
})
setTimeout(function() {
uni.reLaunch({
url: '/pages/user/my_order/my_order'
});
}, 500);
},
fail(res) {
my.showToast({
content: "支付未成功",
type: "fail",
duration: 2000
})
setTimeout(function() {
uni.reLaunch({
url: '/pages/user/my_order/my_order'
});
}, 500);
}
});
// #endif
4 years ago
}
const orderNoPay = order_no => {
http('/api/v1/pay', 'post', {
order_no
}).then((res) => {
let orderPayData = {}
// #ifdef MP-WEIXIN
orderPayData = {
timeStamp: res.timestamp,
nonceStr: res.nonceStr,
package: res.package,
signType: res.signType,
paySign: res.paySign
}
// #endif
// #ifdef MP-TOUTIAO
orderPayData = {
order_id: res.order_id,
order_token: res.order_token
}
// #endif
// #ifdef MP-ALIPAY
orderPayData = {
tradeNO: res.trade_no
}
// #endif
orderPay(orderPayData)
});
4 years ago
}
module.exports = {
formatTime,
httpUtil,
http,
login_check,
location_city,
getRouter,
4 years ago
orderSubmit,
4 years ago
orderNoPay
4 years ago
}