254 lines
5.9 KiB
JavaScript

var util = require('./util')
var base_url = 'https://qr.lacus.site'
const getyiyian = callBack => {
wx.request({
url: 'https://v1.hitokoto.cn/',
success: function (res) {
if (res.data.hitokoto.length > 30) { //如果长度大于30就重新获取
getyiyian(callBack);
} else {
callBack && callBack()
wx.setStorageSync('yiyan', res.data);
}
}
})
}
const login = callBack => {
wx.login({
success: function (res) {
if (res.code) {
wx.request({
url: base_url + '/login',
method: 'POST',
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: {
code: res.code,
sign: util.getMD5(res.code)
},
success: function (res) {
if (res.data.errcode !== 200) {
wx.hideLoading()
wx.showToast({
title: "登录失败",
icon: "none"
})
} else {
wx.hideLoading()
callBack && callBack(JSON.parse(res.data.userInfo),res.data.openid)
}
},
fail: function () {
wx.hideLoading();
wx.showToast({
title: "登录失败",
icon: "none"
})
}
})
} else {
wx.hideLoading();
wx.showToast({
title: res.errMsg,
icon: "none"
})
}
}
})
}
const createOrder = (totalFee, data, callBack) => {
wx.request({
url: base_url + '/newOrder',
method: 'POST',
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: {
totalFee: totalFee,
data:JSON.stringify(data),
},
success: function (res) {
if (res.data.errcode !== 200) {
wx.hideLoading();
wx.showToast({
title: "创建订单失败",
icon: "none"
})
} else {
callBack && callBack(res.data.params);
}
},
})
}
const delCode = (id, callBack) => {
wx.request({
url: base_url + '/del',
method: 'POST',
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: {
id: id,
sign: util.getMD5(id)
},
success: function (res) {
if (res.data.errcode !== 200) {
wx.hideLoading();
wx.showToast({
title: "删除失败",
icon: "none"
})
} else {
wx.hideLoading();
callBack && callBack()
}
},
fail: function () {
wx.hideLoading();
wx.showToast({
title: "删除失败",
icon: "none"
})
}
})
}
const reflash = (openId, callBack) => {
wx.request({
url: base_url + '/reflash',
method: 'POST',
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: {
openId: openId,
sign: util.getMD5(openId)
},
success: function (res) {
if(res.data.errcode !== 200) {
wx.hideLoading();
wx.showToast({
title: "刷新失败",
icon: "none"
})
} else {
callBack && callBack(JSON.parse(res.data.userInfo))
}
},
fail: function () {
wx.hideLoading();
wx.showToast({
title: "刷新失败",
icon: "none"
})
}
})
}
const checkOrder = (out_trade_no, callBack, cancelCallBack) => {
wx.request({
url: base_url + '/checkOrder',
method: 'POST',
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: {
out_trade_no: out_trade_no
},
success: function (res) {
if(res.data.errcode !== 200) {
// 后端还没收到payjs的回调
wx.hideLoading();
wx.showModal({
title: '警告',
content: '未查询到订单信息,您付款成功了吗?',
confirmText:'付完了',
cancelText:'还没有',
success (res) {
if (res.confirm) {
checkOrder(out_trade_no, callBack, cancelCallBack)
} else if (res.cancel) {
console.log('用户点击取消')
cancelCallBack && cancelCallBack()
}
}
})
} else {
wx.hideLoading();
callBack && callBack(res.data.order_id)
}
},
fail: function () {
wx.hideLoading();
wx.showToast({
title: "订单状态获取失败",
icon: "none"
})
}
})
}
const getConfig = (callback) => {
wx.request({
url: base_url + '/config',
method: 'POST',
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: {
},
success: function (res) {
if(res.data.errcode == 200) {
delete res.data.errcode
callback && callback(res.data)
} else {
delete res.data.errcode
callback && callback(res.data)
}
},
fail: function () {
callback && callback(false)
}
})
}
const createCode = (data, callBack) => {
wx.request({
url: base_url + '/add',
method: 'POST',
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: {
data:JSON.stringify(data),
},
success: function (res) {
if (res.data.errcode !== 200) {
wx.hideLoading();
wx.showToast({
title: "创建代码失败",
icon: "none"
})
console.log(data,res)
} else {
callBack && callBack(res.data.id);
}
},
})
}
module.exports = {
getyiyian: getyiyian, // callBack()
login: login, // callBack(res.data)
createOrder: createOrder, // totalFee, data, callBack(res.data.params)
delCode: delCode, // id, callBack()
reflash: reflash, // openId, callBack(userInfo)
checkOrder: checkOrder, // out_trade_no, callBack(order_id)
getConfig: getConfig, // callback
createCode:createCode
}