zhengyiming
3 天以前 9453bef1fc4a3121b28ffa6617f0fbfc81d9f634
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<template>
  <NutForm
    :model-value="form"
    ref="formRef"
    :rules="rules"
    label-position="top"
    class="order-bill-recharge phone"
  >
    <Chunk borderRadiusSmall>
      <NutFormItem class="bole-form-item" prop="currentUserAccountId">
        <NutRadioGroup
          v-model="form.currentUserAccountId"
          direction="horizontal"
          class="par-account-list"
          v-if="userAccountAllList.length > 0"
          @change="handleUserAccountChange"
        >
          <NutRadio
            :label="item.id"
            shape="button"
            v-for="item in userAccountAllList"
            :key="item.id"
            >{{ item.content }}</NutRadio
          >
        </NutRadioGroup>
        <AccountCard
          v-if="userAccountAllList.length > 0"
          title="充值手机号"
          :content="form.phone"
          :remark="form.remark"
        >
          <template #action>
            <div class="account-card-action" @click="handleAddUserAccount">新增</div>
          </template>
        </AccountCard>
        <AccountAddCard v-else @click="handleAddUserAccount" />
      </NutFormItem>
    </Chunk>
 
    <NutFormItem label="选择充值金额" class="bole-form-item" prop="parValue" required>
      <NutRadioGroup v-model="form.parValue" direction="horizontal" class="parValue-radio-group">
        <NutRadio
          :label="Number(item)"
          :key="item"
          shape="button"
          v-for="item in parValueList"
          class="parValue-item"
        >
          <div class="parValue-item-inner">
            <div class="amount-wrapper">
              <div class="amount">{{ item }}</div>
              <div class="unit">元</div>
            </div>
            <div class="price-wrapper">
              <div class="price-text">折后</div>
              <div class="price">
                {{ blLifeRecharge.getRechargeParValue(item, lifePayPhoneRate) }}元
              </div>
            </div>
            <div class="discountTag" v-if="lifePayPhoneRate > 0">{{ lifePayPhoneRate }}折</div>
          </div>
        </NutRadio>
      </NutRadioGroup>
    </NutFormItem>
    <SelectPayTypeFormItem
      v-model="form.lifePayType"
      :showWeixinPay="showWeixinPay"
      :showAliPay="showAliPay"
    ></SelectPayTypeFormItem>
    <div class="common-content">
      <nut-button class="recharge-button" type="primary" @click="handleSubmit">
        <div class="recharge-button-inner">
          <div>¥{{ realParValue }}</div>
          <div class="recharge-button-text">立即充值</div>
        </div>
      </nut-button>
      <RechargeTipsView :lifePayOrderType="LifeRechargeConstants.LifePayOrderTypeEnum.话费订单" />
    </div>
    <ConfirmDialog v-model:visible="confirmDialogVisible" @ok="goPay">
      <template #info>
        <ConfirmDialogInfoItem label="充值账号" :content="form.phone" />
        <ConfirmDialogInfoItem label="充值金额" :content="`¥${form.parValue.toFixed(2)}`" danger />
        <ConfirmDialogInfoItem label="优惠金额" :content="`¥${discountParValue.toFixed(2)}`" />
        <ConfirmDialogInfoItem label="实付金额" :content="`¥${realParValue}`" danger />
      </template>
    </ConfirmDialog>
    <NutToast :msg="state.msg" v-model:visible="state.show" type="warn" cover />
  </NutForm>
</template>
 
<script setup lang="ts">
import {
  Form as NutForm,
  FormItem as NutFormItem,
  RadioGroup as NutRadioGroup,
  Radio as NutRadio,
  Input as NutInput,
  Button as NutButton,
  Toast as NutToast,
} from '@nutui/nutui-taro';
import { FormRules } from '@nutui/nutui-taro/dist/types/__VUE/form/types';
import { reactive, ref, computed, toRef } from 'vue';
import { FormValidator, initLifePayType } from '../../utils';
import {
  useLifeRechargeContext,
  BlLifeRecharge,
  LifePhoneDataCreateLifePayOrderInput,
  LifeRechargeConstants,
} from '@life-payment/core-vue';
import RechargeTipsView from '../../components/RechargeTipsView/RechargeTipsView.vue';
import ConfirmDialog from '../../components/Dialog/ConfirmDialog.vue';
import ConfirmDialogInfoItem from '../../components/Dialog/ConfirmDialogInfoItem.vue';
import { useGetRate, useGetPhoneParValue, useSetUserAccountBySelect } from '../../hooks';
import AccountAddCard from '../../components/Card/AccountAddCard.vue';
import AccountCard from '../../components/Card/AccountCard.vue';
import { usePhoneBillRechargeContext, PhoneUserAccountExtraProperties } from './context';
import SelectPayTypeFormItem from '../../components/SelectPayTypeFormItem/SelectPayTypeFormItem.vue';
import { useSelectPayType, useGetPayStatusByOrderNo } from '../../hooks/selectPayType';
import { RechargeProps } from './types';
import Chunk from '../../components/Layout/Chunk.vue';
 
defineOptions({
  name: 'PhoneBillRechargeStep2',
});
 
const props = withDefaults(defineProps<RechargeProps>(), {
  isDev: false,
});
 
const { goTo } = usePhoneBillRechargeContext();
 
const form = reactive({
  ispCode: '',
  phone: '',
  parValue: 0,
  name: '',
  currentUserAccountId: '',
  remark: '',
  lifePayType: initLifePayType(props.isInWeChat, props.isInAlipay),
});
 
const { userAccountAllList, handleUserAccountChange } = useSetUserAccountBySelect({
  lifePayOrderType: LifeRechargeConstants.LifePayOrderTypeEnum.话费订单,
  onSetUserAccount(currentUserAccount) {
    const currentUserAccountExtraProperties = JSON.parse(
      currentUserAccount.extraProperties
    ) as PhoneUserAccountExtraProperties;
    form.currentUserAccountId = currentUserAccount.id;
    form.phone = currentUserAccount.content;
    form.ispCode = currentUserAccountExtraProperties.ispCode;
    form.name = currentUserAccountExtraProperties.name;
 
    form.remark = currentUserAccount.remark;
 
    changeIspCode(form.ispCode as any);
  },
});
 
function handleAddUserAccount() {
  goTo('step1');
}
 
const emit = defineEmits<{
  (e: 'goPay', orderNo: string): void;
  (e: 'paySuccess', orderNo: string): void;
  (e: 'missName', userAccountId: string): void;
}>();
 
const { lifePayPhoneRate } = useGetRate();
const { phoneParValueList } = useGetPhoneParValue();
 
const parValueList = computed(() => {
  const _parValueList =
    phoneParValueList.value.find((x) => x.ispCode === form.ispCode)?.parValue ?? [];
  return blLifeRecharge.filterParValueList(_parValueList);
});
 
function changeIspCode(val: LifeRechargeConstants.IspCode) {
  const phoneParValueItem = phoneParValueList.value.find((x) => x.ispCode === val);
  if (phoneParValueItem && phoneParValueItem.parValue.every((x) => Number(x) !== form.parValue)) {
    form.parValue = 0;
  }
}
 
const realParValue = computed(() =>
  blLifeRecharge.getRechargeParValue(form.parValue, lifePayPhoneRate.value)
);
const discountParValue = computed(() => form.parValue - Number(realParValue.value));
 
const { blLifeRecharge } = useLifeRechargeContext();
 
const rules = reactive<FormRules>({
  parValue: [
    { required: true, message: '请选择充值金额', validator: FormValidator.validatorNumberNotNull },
  ],
  currentUserAccountId: [{ required: true, message: '请选择充值手机号' }],
  lifePayType: [{ required: true, message: '请选择支付方式' }],
});
 
const formRef = ref<any>(null);
 
function handleSubmit() {
  if (!formRef.value) return;
  formRef.value.validate().then(({ valid, errors }: any) => {
    if (valid) {
      if (!form.name) {
        emit('missName', form.currentUserAccountId);
        return;
      }
      recharge();
    }
  });
}
 
const confirmDialogVisible = ref(false);
 
function recharge() {
  confirmDialogVisible.value = true;
}
 
const { state, invokeAliPay, invokeWeixinPay } = useSelectPayType({
  getOpenId: toRef(props, 'getOpenId'),
  isInWeChat: toRef(props, 'isInWeChat'),
  isH5: toRef(props, 'isH5'),
  appId: toRef(props, 'appId'),
});
 
const currentOrderNo = ref('');
 
async function goPay() {
  try {
    let params: LifePhoneDataCreateLifePayOrderInput = {
      userId: blLifeRecharge.accountModel.userId,
      channelId: blLifeRecharge.accountModel.channlesNum,
      productData: {
        ispCode: form.ispCode,
        parValue: form.parValue,
        phone: form.phone,
        name: form.name,
      },
    };
    let res = await blLifeRecharge.services.createLifePayPhoneOrder(params);
    // emit('goPay', res.orderNo);
    if (form.lifePayType === LifeRechargeConstants.LifePayTypeEnum.WxPay) {
      await invokeWeixinPay(res.orderNo);
    } else {
      await invokeAliPay(res.orderNo);
    }
    currentOrderNo.value = res.orderNo;
  } catch (error) {}
}
 
useGetPayStatusByOrderNo({
  orderNo: currentOrderNo,
  enabled: computed(
    () =>
      form.lifePayType === LifeRechargeConstants.LifePayTypeEnum.WxPay &&
      props.isFocus &&
      !!currentOrderNo.value
  ),
  onPaySuccess: (orderNo) => {
    emit('paySuccess', orderNo);
    currentOrderNo.value = '';
  },
});
</script>