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.

510 lines
11 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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
let latitude = 39.924338
let map_type = 1
// #ifdef MP-ALIPAY
map_type = 2
// #endif
httpUtil("/api/v1/get-city", "get", {
latitude,
longitude,
type: map_type
}).then(res1 => {
resolve(res1)
})
} else {
uni.getLocation({
// #ifndef MP-ALIPAY
type: 'wgs84',
// #endif
success(res) {
console.log(res);
if (!res.longitude) {
reject('定位未开启')
setting()
return
}
let map_type = 1
// #ifdef MP-ALIPAY
map_type = 2
// #endif
httpUtil("/api/v1/get-city", "get", {
longitude: res.longitude,
latitude: res.latitude,
type: map_type
}).then(res1 => {
resolve(res1)
})
},
fail(res) {
// reject('定位失败,下拉重试或点击选择城市')
reject('定位未开启')
setting()
}
})
}
})
}
const setting = function() {
wx.getSetting({ //先查看授权情况
success: function(res) {
var statu = res.authSetting;
if (!statu['scope.userLocation']) { //判断是否授权,没有授权就提示下面的信息
wx.showModal({
// title: '需要获取您的地理位置,请确认授权定位,否则小程序功能将无法使用',
title: '需要获取您的地理位置,请确认授权定位,当前定位为北京',
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,
})
} else {
// #ifndef MP-ALIPAY
wx.showToast({
title: '授权定位失败',
icon: 'error',
duration: 1000
})
// #endif
// #ifdef MP-ALIPAY
my.showAuthGuide({
authType: 'LBS',
success: (res) => {
// wx.reLaunch({
// url: '/pages/index/index/index',
// })
},
fail: (error) => {},
})
// #endif
}
}
})
} else {
// wx.showToast({
// title: '重新进入小程序唤起定位授权',
// icon: 'error',
// duration: 1000
// })
}
}
})
} else {
//用户已授权,但是获取地理位置失败,提示用户去系统设置中打开定位
wx.showModal({
title: '',
showCancel: false,
content: '请在系统设置中打开定位服务',
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
}
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)
});
}
module.exports = {
formatTime,
httpUtil,
http,
login_check,
location_city,
getRouter,
orderSubmit,
orderNoPay
}