123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- // pages/pay/pay.js
- const app = getApp();
- const { axios } = app.globalData;
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- loadding: false,//正在支付中 true
- disabled: false,//支付完毕 true
- message: '',//订单备注
- paytype: "1",// 支付方式 单选
- balance: null,//余额
- result: [],//复选框
- },
- // 请求余额
- Request () {
- let that = this;
- axios({
- url: '/option/balance/',
- method: 'get',
- success (res) {
- let { data } = res.data;
- that.setData({
- ...data[0]
- })
- }
- })
- },
- // 复选框
- checkboxonchange (e) {
- let { detail } = e;
- this.setData({
- result: detail
- })
- },
- // 提交支付
- onSubmit () {
- let { commodity, message: notes, useraddress, paytype, result } = this.data, { id: student } = useraddress, that = this, balance = '';
- if (!student) {
- wx.showToast({
- title: '请选择学生',
- icon: 'error'
- })
- return false;
- }
- if (result.indexOf('1') > -1) {
- balance = 2;
- }
- let { appid, user } = app.globalData, { openid } = user;
- function pay () {
- return new Promise(resolve => {
- axios({
- url: `/order/`,
- method: 'post',
- data: {
- appid, openid, notes, commodity, student, balance
- },
- success (res) {
- that.setData({ loadding: true })
- let { data } = res.data;
- if (!data) {
- resolve({ type: 1, value: "支付成功" });
- return false;
- }
- wx.requestPayment({
- timeStamp: data.timeStamp,
- nonceStr: data.nonceStr,
- package: data.package,
- signType: data.signType,
- paySign: data.paySign,
- success: function (res) {
- resolve({ type: 1, value: "支付成功" })
- },
- fail: function () {
- // 取消付款
- resolve({ type: 2, value: "支付失败" })
- },
- })
- }
- })
- })
- }
- pay().then(
- res => {
- that.setData({ loadding: false, disabled: true });
- wx.lxd.Notify({ type: res.type == 1 ? 'success' : '', message: res.value })
- const time = setTimeout(() => {
- wx.redirectTo({
- url: '/pages/order/order',
- })
- clearTimeout(time)
- }, 3000)
- }
- );
- },
- // 挑选地址
- ChooseAddress () {
- wx.navigateTo({
- url: `/pages/adddata/adddata?data=${JSON.stringify({ source: 'pay' })}`,
- })
- },
- // 支付方式改变
- onChange (e) {
- let { detail } = e;
- this.setData({ paytype: detail })
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- wx.lxd.Ctr({
- // nav
- title: "支付", //标题
- bgc: false,//使用更改后的颜色;配合json使用
- })
- let { data } = options;
- if (!!data) {
- data = JSON.parse(data);
- this.setData({
- ...data,
- commodity: data.id
- })
- }
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- this.Request()
- let { useraddress } = app.globalData;
- this.setData({ useraddress })
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- app.globalData.useraddress = {};
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- },
- })
|