193 lines
4.4 KiB
JavaScript
193 lines
4.4 KiB
JavaScript
var util = require('./util')
|
|
|
|
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: 'https://qr.powerrain.cn/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 createOder = (totalFee, data, callBack) => {
|
|
wx.request({
|
|
url: 'https://qr.powerrain.cn/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: 'https://qr.powerrain.cn/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: 'https://qr.powerrain.cn/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 {
|
|
wx.hideLoading();
|
|
callBack && callBack(JSON.parse(res.data.userInfo))
|
|
}
|
|
},
|
|
fail: function () {
|
|
wx.hideLoading();
|
|
wx.showToast({
|
|
title: "刷新失败",
|
|
icon: "none"
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
const checkOrder = (out_trade_no, callBack) => {
|
|
wx.request({
|
|
url: 'https://qr.powerrain.cn/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) {
|
|
wx.hideLoading();
|
|
wx.showToast({
|
|
title: "订单状态获取失败",
|
|
icon: "none"
|
|
})
|
|
} else {
|
|
wx.hideLoading();
|
|
callBack && callBack(res.data.order_id)
|
|
}
|
|
},
|
|
fail: function () {
|
|
wx.hideLoading();
|
|
wx.showToast({
|
|
title: "订单状态获取失败",
|
|
icon: "none"
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
module.exports = {
|
|
getyiyian: getyiyian, // callBack()
|
|
login: login, // callBack(res.data)
|
|
createOder: createOder, // totalFee, data, callBack(res.data.params)
|
|
delCode: delCode, // id, callBack()
|
|
reflash: reflash, // openId, callBack(userInfo)
|
|
checkOrder: checkOrder // out_trade_no, callBack(order_id)
|
|
}
|