ZFB-welfareOfficer/pages/index/index.js

79 lines
2.2 KiB
JavaScript
Raw Normal View History

2024-07-11 10:04:48 +08:00
import { getInfo, receiveRed } from "../../api";
import { Base64 } from '../../utils/base64';
Page({
data: {
isOpen: false,
order_number: '',
info: {
batch_goods_name: "现金红包",
cash_amount_type_text: "固额红包",
end_time: "2023-06-30 23:59:59",
receive_price: 0.01,
receive_status: 0,
wishing: "恭喜发财",
},
auth_code: '',
},
onLoad({ order_number }) {
/* 直接通过小程序打开,判断是否有 order_number */
if (order_number) {
this.setData({ order_number: Base64.decode(order_number) })
} else {
const { data } = my.getStorageSync({ key: "order_number" })
data && this.setData({ order_number: data })
}
/* 获取详情 */
if (!this.data.order_number) return
my.showLoading()
getInfo({ order_number: this.data.order_number })
.then(({ code, data, message }) => {
my.hideLoading()
if (code == 200) {
my.setStorageSync({ key: 'order_number', data: this.data.order_number, })
this.setData({ info: data })
if ([1, 2].includes(data.receive_status)) {
this.setData({ isOpen: true })
}
if (data.receive_status === 3) {
my.alert({ title: '温馨提示', content: data.receive_status_text })
}
} else {
my.alert({ title: '温馨提示', content: message })
}
})
},
/* 授权 */
getAuthCode() {
my.getAuthCode({
scopes: 'auth_user',
success: ({ authCode }) => {
this.setData({ auth_code: authCode })
this.receive()
},
});
},
/* 领取红包 */
receive() {
/* 订单号存在以及订单未领取才可操作 */
if (this.data.order_number && this.data.info.receive_status === 0) {
my.showLoading()
receiveRed({ order_number: this.data.order_number, auth_code: this.data.auth_code })
.then(({ code, message }) => {
if (code === 200) {
const info = this.data.info
info.receive_status = 5
this.setData({ info, isOpen: true })
}
my.hideLoading()
my.alert({ title: '温馨提示', content: message })
})
} else {
this.setData({ isOpen: true })
}
}
});