453 lines
17 KiB
JavaScript
453 lines
17 KiB
JavaScript
import { CentToString } from "oak-domain/lib/utils/money";
|
|
import { generateNewIdAsync } from "oak-domain/lib/utils/uuid";
|
|
import assert from 'assert';
|
|
import { DATA_SUBSCRIBER_KEYS } from "../../../config/constants";
|
|
export default OakComponent({
|
|
entity: 'account',
|
|
isList: false,
|
|
projection: {
|
|
id: 1,
|
|
total: 1,
|
|
avail: 1,
|
|
ofSystemId: 1,
|
|
entity: 1,
|
|
entityId: 1,
|
|
deposit$account: {
|
|
$entity: 'deposit',
|
|
data: {
|
|
id: 1,
|
|
iState: 1,
|
|
pay$deposit: {
|
|
$entity: 'pay',
|
|
data: {
|
|
id: 1,
|
|
},
|
|
filter: {
|
|
iState: 'paying',
|
|
},
|
|
},
|
|
},
|
|
filter: {
|
|
iState: {
|
|
$in: ['depositing', 'shipped'],
|
|
},
|
|
},
|
|
},
|
|
accountOper$account: {
|
|
$entity: 'accountOper',
|
|
data: {
|
|
id: 1,
|
|
type: 1,
|
|
totalPlus: 1,
|
|
availPlus: 1,
|
|
},
|
|
sorter: [
|
|
{
|
|
$attr: {
|
|
$$createAt$$: 1,
|
|
},
|
|
$direction: 'desc',
|
|
}
|
|
],
|
|
indexFrom: 0,
|
|
count: 10,
|
|
}
|
|
},
|
|
properties: {
|
|
depositMinCent: 0,
|
|
depositMaxCent: 1000000,
|
|
autoStartPay: false,
|
|
onGoToUnfinishedPay: (payId) => undefined,
|
|
onWithdraw: () => undefined,
|
|
textColor: '#B7966E',
|
|
priceColor: '#8A6321',
|
|
bgColor: '#EAD8BC',
|
|
preWithdraw: () => true,
|
|
onGoToHistory: () => undefined,
|
|
},
|
|
formData({ data, features }) {
|
|
let unfinishedDepositId = data?.deposit$account?.filter((ele) => ele.iState === 'depositing')?.[0]?.id; //未完成支付的充值
|
|
let unreceivedDepositId = data?.deposit$account?.filter((ele) => ele.iState === 'shipped')?.[0]?.id; //未确认收货的充值
|
|
if (data?.id) {
|
|
const deposits = features.cache.get('deposit', {
|
|
data: {
|
|
id: 1,
|
|
iState: 1,
|
|
pay$deposit: {
|
|
$entity: 'pay',
|
|
data: {
|
|
id: 1,
|
|
},
|
|
filter: {
|
|
iState: 'paying',
|
|
},
|
|
},
|
|
},
|
|
filter: {
|
|
iState: {
|
|
$in: ['depositing', 'shipped'],
|
|
},
|
|
accountId: data?.id,
|
|
}
|
|
});
|
|
if (deposits && deposits.length > 0) {
|
|
unfinishedDepositId = deposits?.filter((ele) => ele.iState === 'depositing')?.[0]?.id; //未完成支付的充值
|
|
unreceivedDepositId = deposits?.filter((ele) => ele.iState === 'shipped')?.[0]?.id; //未确认收货的充值
|
|
}
|
|
}
|
|
return {
|
|
account: data,
|
|
availStr: data?.avail && CentToString(data.avail, 2),
|
|
totalStr: data?.total && CentToString(data.total, 2),
|
|
loanStr: (data?.avail !== undefined && data?.total !== undefined) && CentToString(data.total - data.avail, 2),
|
|
unfinishedDepositId,
|
|
unreceivedDepositId,
|
|
};
|
|
},
|
|
actions: ['deposit', 'withdraw'],
|
|
methods: {
|
|
async newDeposit() {
|
|
const { autoStartPay } = this.props;
|
|
const { depPrice, depositChannel, depositLoss } = this.state;
|
|
const payId = await generateNewIdAsync();
|
|
this.setDepositing(true);
|
|
try {
|
|
await this.execute(undefined, undefined, undefined, [
|
|
{
|
|
entity: 'account',
|
|
operation: {
|
|
id: await generateNewIdAsync(),
|
|
action: 'deposit',
|
|
data: {
|
|
deposit$account: [
|
|
{
|
|
id: await generateNewIdAsync(),
|
|
action: 'create',
|
|
data: {
|
|
id: await generateNewIdAsync(),
|
|
price: depPrice,
|
|
loss: depositLoss[0] || 0,
|
|
creatorId: this.features.token.getUserId(),
|
|
pay$deposit: [
|
|
{
|
|
id: await generateNewIdAsync(),
|
|
action: 'create',
|
|
data: {
|
|
id: payId,
|
|
autoStart: !!autoStartPay,
|
|
price: depPrice,
|
|
entity: depositChannel.entity,
|
|
entityId: depositChannel.entityId,
|
|
},
|
|
}
|
|
]
|
|
}
|
|
}
|
|
]
|
|
},
|
|
filter: {
|
|
id: this.props.oakId,
|
|
}
|
|
}
|
|
}
|
|
]);
|
|
this.setPayRefreshing(true);
|
|
const delay = 2000;
|
|
const wait = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
const pollUntilPaying = async () => {
|
|
while (true) {
|
|
const { data: [pay] } = await this.features.cache.refresh('pay', {
|
|
data: {
|
|
id: 1,
|
|
applicationId: 1,
|
|
price: 1,
|
|
meta: 1,
|
|
iState: 1,
|
|
paid: 1,
|
|
refunded: 1,
|
|
timeoutAt: 1,
|
|
forbidRefundAt: 1,
|
|
externalId: 1,
|
|
entity: 1,
|
|
entityId: 1,
|
|
creatorId: 1,
|
|
phantom3: 1,
|
|
},
|
|
filter: {
|
|
id: payId,
|
|
}
|
|
});
|
|
if (pay.iState === 'paying') {
|
|
this.setPayRefreshing(false);
|
|
return;
|
|
}
|
|
await wait(delay);
|
|
}
|
|
};
|
|
await pollUntilPaying();
|
|
this.setDepositing(false);
|
|
}
|
|
catch (err) {
|
|
this.setDepositing(false);
|
|
throw err;
|
|
}
|
|
this.onDepositModalClose();
|
|
const { onGoToUnfinishedPay } = this.props;
|
|
onGoToUnfinishedPay && onGoToUnfinishedPay(payId);
|
|
},
|
|
setPayRefreshing(payRefreshing) {
|
|
this.setState({ payRefreshing });
|
|
},
|
|
setDepositOpen(depositOpen) {
|
|
this.setState({ depositOpen }, () => {
|
|
setTimeout(() => {
|
|
this.setState({
|
|
focus: depositOpen
|
|
});
|
|
}, 333);
|
|
});
|
|
},
|
|
setUfOpen(ufOpen) {
|
|
this.setState({ ufOpen });
|
|
},
|
|
setDepPrice(depPrice) {
|
|
this.setState({ depPrice }, () => this.setDepositLoss());
|
|
},
|
|
setDepositChannel(depositChannel) {
|
|
const channel = depositChannel ? depositChannel : null;
|
|
this.setState({ depositChannel: channel }, () => this.setDepositLoss());
|
|
},
|
|
setDepositLoss() {
|
|
const { depPrice, depositChannel } = this.state;
|
|
if (depPrice && depositChannel) {
|
|
const depositLoss = this.features.pay.calcDepositLoss(depPrice, depositChannel);
|
|
this.setState({ depositLoss });
|
|
}
|
|
else {
|
|
this.setState({ depositLoss: [0, '', undefined] });
|
|
}
|
|
},
|
|
setDepositing(depositing) {
|
|
this.setState({ depositing });
|
|
},
|
|
async onDepositClick() {
|
|
const { preWithdraw } = this.props;
|
|
if (preWithdraw) {
|
|
if (!preWithdraw()) {
|
|
return;
|
|
}
|
|
}
|
|
const { unfinishedDepositId } = this.state;
|
|
if (unfinishedDepositId) {
|
|
const { data: deposit } = await this.features.cache.refresh('deposit', {
|
|
data: {
|
|
id: 1,
|
|
iState: 1,
|
|
},
|
|
filter: {
|
|
id: unfinishedDepositId,
|
|
}
|
|
});
|
|
if (deposit[0].iState === 'depositing') {
|
|
this.setUfOpen(true);
|
|
}
|
|
else {
|
|
this.setDepositOpen(true);
|
|
}
|
|
}
|
|
else {
|
|
this.setDepositOpen(true);
|
|
}
|
|
},
|
|
onDepositModalClose() {
|
|
this.setDepositOpen(false);
|
|
this.setDepPrice(null);
|
|
this.setDepositChannel(null);
|
|
},
|
|
onUfModalClose() {
|
|
this.setUfOpen(false);
|
|
},
|
|
async onUnfinishedDepositClick() {
|
|
const { onGoToUnfinishedPay } = this.props;
|
|
const { unfinishedDepositId } = this.state;
|
|
const { data: [pay] } = await this.features.cache.refresh('pay', {
|
|
data: {
|
|
id: 1,
|
|
},
|
|
filter: {
|
|
depositId: unfinishedDepositId,
|
|
iState: 'paying',
|
|
},
|
|
});
|
|
onGoToUnfinishedPay && onGoToUnfinishedPay(pay.id);
|
|
},
|
|
onWithdrawClick() {
|
|
this.props.onWithdraw();
|
|
},
|
|
gotoHistoryMp() {
|
|
this.props.onGoToHistory();
|
|
},
|
|
async onUnreceivedDepositClickMp(noMessage) {
|
|
const { unreceivedDepositId } = this.state;
|
|
const { data: [deposit] } = await this.features.cache.refresh('deposit', {
|
|
data: {
|
|
id: 1,
|
|
iState: 1,
|
|
pay$deposit: {
|
|
$entity: 'pay',
|
|
data: {
|
|
id: 1,
|
|
meta: 1,
|
|
iState: 1,
|
|
entity: 1,
|
|
entityId: 1,
|
|
},
|
|
filter: {
|
|
entity: 'wpProduct',
|
|
iState: 'paid',
|
|
}
|
|
},
|
|
shipId: 1,
|
|
ship: {
|
|
id: 1,
|
|
iState: 1,
|
|
}
|
|
},
|
|
filter: {
|
|
id: unreceivedDepositId,
|
|
}
|
|
});
|
|
const ship = deposit?.ship;
|
|
if (ship && ship.id) {
|
|
const transactionId = deposit?.pay$deposit?.[0]?.meta?.transaction_id;
|
|
console.log('transaction_id', transactionId);
|
|
if (process.env.NODE_ENV === 'development') {
|
|
if (ship.iState === 'receiving') {
|
|
await this.features.cache.operate('ship', [{
|
|
id: await generateNewIdAsync(),
|
|
action: 'succeedReceiving',
|
|
data: {},
|
|
filter: {
|
|
id: ship.id,
|
|
},
|
|
}]);
|
|
this.setMessage({
|
|
type: 'success',
|
|
content: this.t('ship.success')
|
|
});
|
|
return;
|
|
}
|
|
}
|
|
if (transactionId) {
|
|
if (wx.openBusinessView) {
|
|
wx.openBusinessView({
|
|
businessType: 'weappOrderConfirm',
|
|
extraData: {
|
|
transaction_id: transactionId,
|
|
},
|
|
success: async () => {
|
|
console.log('success');
|
|
// const { result: mpShipState } = await this.features.cache.exec('getMpShipState', {
|
|
// shipId: ship.id,
|
|
// })
|
|
// if (mpShipState === 'received') {
|
|
// await this.features.cache.operate('ship',
|
|
// [{
|
|
// id: await generateNewIdAsync(),
|
|
// action: 'succeedReceiving',
|
|
// data: {},
|
|
// filter: {
|
|
// id: ship.id,
|
|
// },
|
|
// }]
|
|
// );
|
|
// this.setMessage({
|
|
// type: 'success',
|
|
// content: this.t('ship.success')
|
|
// });
|
|
// } else {
|
|
// console.log(mpShipState);
|
|
// this.reRender();
|
|
// // this.setMessage({
|
|
// // type: 'warning',
|
|
// // content: this.t('ship.wait'),
|
|
// // })
|
|
// }
|
|
const { result } = await this.features.cache.exec('shipConfirmSuccess', {
|
|
shipId: ship.id,
|
|
});
|
|
if (result) {
|
|
this.setMessage({
|
|
type: 'success',
|
|
content: this.t('ship.success')
|
|
});
|
|
}
|
|
else {
|
|
this.reRender();
|
|
}
|
|
},
|
|
fail: () => {
|
|
console.log('fail');
|
|
this.setMessage({
|
|
type: 'warning',
|
|
content: this.t('ship.fail'),
|
|
});
|
|
},
|
|
complete: () => {
|
|
}
|
|
});
|
|
}
|
|
else {
|
|
//引导用户升级微信版本
|
|
this.setMessage({
|
|
type: 'warning',
|
|
content: this.t('ship.update wechat'),
|
|
});
|
|
}
|
|
}
|
|
else {
|
|
if (!noMessage) {
|
|
this.setMessage({
|
|
type: 'warning',
|
|
content: this.t('no transactionId'),
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
},
|
|
data: {
|
|
depositOpen: false,
|
|
ufOpen: false,
|
|
depPrice: null,
|
|
depositChannel: null,
|
|
depositLoss: [0, '', {}],
|
|
depositing: false,
|
|
setDepPriceMp(price) { this.setDepPrice(price); },
|
|
setDepositChannelMp(depositChannel) { this.setDepositChannel(depositChannel); },
|
|
focus: false,
|
|
unsub: undefined,
|
|
payRefreshing: false,
|
|
},
|
|
lifetimes: {
|
|
async ready() {
|
|
const { oakId } = this.props;
|
|
assert(typeof oakId === 'string');
|
|
const unsub = await this.subDataEvents([`${DATA_SUBSCRIBER_KEYS.accountNumberChanged}-${oakId}`, `${DATA_SUBSCRIBER_KEYS.depositStateChanged}-${oakId}`]);
|
|
this.setState({
|
|
unsub,
|
|
});
|
|
},
|
|
detached() {
|
|
const { unsub } = this.state;
|
|
unsub && unsub();
|
|
},
|
|
async mature() {
|
|
const { unreceivedDepositId } = this.state;
|
|
if (!!unreceivedDepositId && process.env.OAK_PLATFORM === 'wechatMp') {
|
|
await this.onUnreceivedDepositClickMp(true);
|
|
}
|
|
},
|
|
}
|
|
});
|