ConsumptionTopUpLogMobileForm.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <template>
  2. <el-form
  3. ref="formRef"
  4. :model="formData"
  5. :rules="formRules"
  6. label-width="120px"
  7. v-loading="formLoading"
  8. :class="{ 'mobile-card': mobile }"
  9. >
  10. <el-row>
  11. <el-col :span="24">
  12. <el-form-item label="转账凭证附件" prop="attachment">
  13. <UploadImg v-model="formData.attachment" :limit="1" />
  14. </el-form-item>
  15. </el-col>
  16. </el-row>
  17. <el-row>
  18. <el-col :span="24">
  19. <el-form-item label="用户" prop="userId">
  20. <el-select
  21. v-model="formData.userId"
  22. clearable
  23. filterable
  24. remote
  25. reserve-keyword
  26. placeholder="请输入用户名称"
  27. :remote-method="debouncedRemoteMethod"
  28. :loading="UserSearchLoading"
  29. style="width: 240px"
  30. >
  31. <el-option
  32. v-for="item in UserSearchOptions"
  33. :key="item.value"
  34. :label="item.label"
  35. :value="item.value"
  36. >
  37. <div class="flex items-center">
  38. <el-avatar :size="28" :src="item.avatar" />
  39. <span class="m-l-2">{{ item.username }}({{ item.mobile }})</span>
  40. </div>
  41. </el-option>
  42. </el-select>
  43. </el-form-item>
  44. </el-col>
  45. </el-row>
  46. <el-row>
  47. <el-col :span="12">
  48. <el-form-item label="充值金额" prop="integralPoints">
  49. <el-input
  50. v-model.number="formData.integralPoints"
  51. placeholder="请输入充值金额"
  52. :disabled="!formData.userId"
  53. @input="(val) => (formData.integralPoints = val.replace(/[^\d]/g, ''))"
  54. :maxlength="9"
  55. />
  56. </el-form-item>
  57. </el-col>
  58. <el-col :span="12">
  59. <el-form-item label="实到积分" prop="">
  60. <span style="height: 30px;line-height: 30px;">{{ consumptionPoints }}</span>
  61. </el-form-item>
  62. </el-col>
  63. </el-row>
  64. <el-row>
  65. <el-col :span="12">
  66. <el-form-item label="手机号" prop="mobile">
  67. <span style="height: 30px;line-height: 30px;">{{ formData.smsCodeUseReqDTO.mobile }}</span>
  68. <el-button
  69. v-if="mobileCodeTimer <= 0"
  70. @click="getSmsCode"
  71. type="primary"
  72. class="m-l-2"
  73. >
  74. {{ t('login.getSmsCode') }}
  75. </el-button>
  76. <el-button :disabled="true" v-if="mobileCodeTimer > 0" class="m-l-2">
  77. {{ mobileCodeTimer }}秒后可重新获取
  78. </el-button>
  79. </el-form-item>
  80. </el-col>
  81. <el-col :span="12">
  82. <el-form-item label="验证码" prop="smsCodeUseReqDTO.code">
  83. <el-input
  84. v-model.number="formData.smsCodeUseReqDTO.code"
  85. placeholder="请输入验证码"
  86. max="4"
  87. />
  88. </el-form-item>
  89. </el-col>
  90. </el-row>
  91. </el-form>
  92. <div class="bottom" >
  93. <el-button @click="goBack" :disabled="formLoading">返回</el-button>
  94. <el-button @click="submitForm" type="primary" :disabled="formLoading">确定</el-button>
  95. </div>
  96. </template>
  97. <script setup lang="ts">
  98. import { ConsumptionTopUpLogApi, ConsumptionTopUpLogVO } from '@/api/system/distri/consumption'
  99. import * as UserApi from '@/api/member/user'
  100. import { getUserProfile } from '@/api/system/user/profile'
  101. import { debounce } from 'lodash-es'
  102. import { sendSmsCode } from '@/api/login'
  103. import { useAppStore } from '@/store/modules/app'
  104. const appStore = useAppStore()
  105. const mobile = computed(() => appStore.getMobile)
  106. /** 平台消费分充值记录 表单 */
  107. defineOptions({ name: 'ConsumptionTopUpLogForm' })
  108. const { t } = useI18n() // 国际化
  109. const message = useMessage() // 消息弹窗
  110. const { currentRoute, push } = useRouter() // 路由跳转
  111. const dialogVisible = ref(false) // 弹窗的是否展示
  112. const dialogTitle = ref('') // 弹窗的标题
  113. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  114. const UserSearchLoading = ref(false)
  115. const UserSearchList = ref([])
  116. const UserSearchOptions = ref([])
  117. const UserSearchValue = ref([])
  118. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  119. const formData = reactive({
  120. id: undefined,
  121. userId: undefined,
  122. attachment: undefined,
  123. integralPoints: undefined,
  124. practicalConsumptionPoints: undefined,
  125. userName: undefined,
  126. smsCodeUseReqDTO: {
  127. mobile: '',
  128. scene: '10',
  129. code: ''
  130. }
  131. })
  132. // 初始化或重置表单数据的函数
  133. const resetFormData = () => {
  134. formData.id = undefined
  135. // formData.userId = undefined;
  136. formData.attachment = undefined
  137. formData.integralPoints = undefined
  138. formData.practicalConsumptionPoints = undefined
  139. formData.userName = undefined
  140. formData.smsCodeUseReqDTO = {
  141. mobile: '',
  142. scene: '10', // 默认值
  143. code: ''
  144. }
  145. }
  146. const formRules = reactive({
  147. integralPoints: [{ required: true, message: '充值金额不能为空', trigger: 'blur' }],
  148. userId: [{ required: true, message: '用户不能为空', trigger: 'blur' }],
  149. // attachment: [{ required: true, message: '转账凭证附件不能为空', trigger: 'blur' }],
  150. 'smsCodeUseReqDTO.code': [{ required: true, message: '验证码不能为空', trigger: 'blur' }]
  151. })
  152. const formRef = ref() // 表单 Ref
  153. const goBack = ()=>{
  154. push({
  155. name: 'Consumption'
  156. })
  157. }
  158. const submitForm = async () => {
  159. // 校验表单
  160. await formRef.value.validate()
  161. // 提交请求
  162. formLoading.value = true
  163. try {
  164. await ConsumptionTopUpLogApi.createConsumptionTopUpLog(formData)
  165. message.success(t('common.createSuccess'))
  166. // 发送操作成功的事件
  167. goBack()
  168. } finally {
  169. formLoading.value = false
  170. }
  171. }
  172. // 实际请求方法
  173. const remoteMethod = async (query: string) => {
  174. if (query) {
  175. UserSearchLoading.value = true
  176. try {
  177. const response = await UserApi.getUserListByUsername({ username: query })
  178. // console.log(response)
  179. UserSearchOptions.value = response.map((item: any) => ({
  180. value: item.id,
  181. label: item.username,
  182. mobile: item.mobile,
  183. avatar: item.avatar,
  184. username: item.username
  185. }))
  186. } catch (error) {
  187. console.error('Error fetching data:', error)
  188. } finally {
  189. UserSearchLoading.value = false
  190. }
  191. } else {
  192. }
  193. }
  194. // 使用 lodash 防抖,延迟 200ms 触发请求
  195. const debouncedRemoteMethod = debounce(remoteMethod, 200)
  196. const state = reactive({
  197. hide: true,
  198. isFirstConsumption: false,
  199. minimumConsumptionPoints: 0,
  200. consumptionMagnification: '',
  201. triggerMagnificationPoints:'',
  202. })
  203. // 根据 value 查找 username
  204. const findUsernameByValue = (value) => {
  205. const user = UserSearchOptions.value.find((item) => item.value === value)
  206. return user ? user.username : null
  207. }
  208. async function isUserFirstRecharge(userId: number) {
  209. state.hide = false
  210. const data = await ConsumptionTopUpLogApi.isUserFirstRecharge({ userId })
  211. state.isFirstConsumption = data.isFirstConsumption
  212. state.minimumConsumptionPoints = data.minimumConsumptionPoints
  213. state.consumptionMagnification = data.consumptionMagnification
  214. state.triggerMagnificationPoints = data.triggerMagnificationPoints
  215. }
  216. // 监听 el-select 选项值的变化
  217. watch(
  218. () => formData.userId,
  219. (newValue) => {
  220. if (!newValue) {
  221. return
  222. }
  223. isUserFirstRecharge(newValue)
  224. formData.userName = findUsernameByValue(newValue)
  225. }
  226. )
  227. const consumptionPoints = computed(() => {
  228. if (!formData.integralPoints) {
  229. return 0
  230. }
  231. let point;
  232. if(formData.integralPoints >= parseFloat(state.triggerMagnificationPoints)){
  233. point = (formData.integralPoints * parseFloat(state.consumptionMagnification)).toFixed(2)
  234. }else{
  235. point = formData.integralPoints
  236. }
  237. return point
  238. })
  239. const smsVO = reactive({
  240. smsCode: {
  241. mobile: '',
  242. scene: 10
  243. }
  244. })
  245. const mobileCodeTimer = ref(0)
  246. const getSmsCode = async () => {
  247. smsVO.smsCode.mobile = formData.smsCodeUseReqDTO.mobile
  248. await sendSmsCode(smsVO.smsCode).then(async () => {
  249. message.success(t('login.SmsSendMsg'))
  250. // 设置倒计时
  251. mobileCodeTimer.value = 60
  252. let msgTimer = setInterval(() => {
  253. mobileCodeTimer.value = mobileCodeTimer.value - 1
  254. if (mobileCodeTimer.value <= 0) {
  255. clearInterval(msgTimer)
  256. }
  257. }, 1000)
  258. })
  259. }
  260. // 获取当前登录用户的手机号并且回填
  261. const getUserInfo = async () => {
  262. const users = await getUserProfile()
  263. formData.smsCodeUseReqDTO.mobile = users.mobile
  264. }
  265. onMounted( async() => {
  266. await getUserInfo()
  267. })
  268. </script>
  269. <style src="@/styles/MobileTab.css" lang="scss" scoped></style>
  270. <style scoped>
  271. ::v-deep .el-form-item{
  272. /* border-bottom: 1px solid #ebeef5; */
  273. margin: 0;
  274. padding: 14px 0 ;
  275. }
  276. .w-31{
  277. flex: 1;
  278. font-size: 0.8rem
  279. }
  280. .bottom {
  281. position: fixed;
  282. bottom: 0;
  283. left: 0;
  284. width: 100%;
  285. background: white;
  286. padding: 1rem;
  287. box-sizing: border-box;
  288. display: flex;
  289. .el-button {
  290. flex: 1;
  291. }
  292. }
  293. </style>