You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

338 lines
10 KiB

2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
  1. <script setup>
  2. import { ref, onMounted, reactive, computed, watch, nextTick } from 'vue'
  3. import { ElMessage } from 'element-plus'
  4. import { Plus } from '@element-plus/icons-vue'
  5. import axios from 'axios'
  6. import { ElMessageBox } from 'element-plus'
  7. import API from '@/util/http.js'
  8. import { uploadFile } from '@/util/request.js';
  9. import request from '@/util/http.js'
  10. import moment from 'moment'
  11. import { e, range, re } from 'mathjs'
  12. import { utils, read } from 'xlsx'
  13. import throttle from 'lodash/throttle'
  14. import { useAdminStore } from "@/store/index.js";
  15. import { storeToRefs } from "pinia";
  16. import _ from 'lodash'
  17. const user = ref({})
  18. const getUser = async function (jwcode) {
  19. if (consumeForm.value.jwcode) {
  20. consumeForm.value.jwcode = consumeForm.value.jwcode.replace(/\s/g, '');
  21. } else {
  22. ElMessage.error('请先输入精网号')
  23. return false
  24. }
  25. try {
  26. const result = await API({
  27. url: '/beanUser/userCard',
  28. data: {
  29. jwcode: consumeForm.value.jwcode
  30. }
  31. })
  32. if (result.code === 0) {
  33. ElMessage.error(result.msg);
  34. } else if (result.data === null) {
  35. ElMessage.error("用户不存在");
  36. } else {
  37. user.value = result.data;
  38. console.log("用户信息", user.value);
  39. ElMessage.success("查询成功");
  40. }
  41. } catch (error) {
  42. console.log("请求失败", error);
  43. ElMessage.error("精网号错误");
  44. }
  45. }
  46. //提交禁止重复点击
  47. const addDisabled = ref(false)
  48. const consumeForm = ref({
  49. jwcode: '',
  50. permanentBean: '',
  51. freeBean: '',
  52. remark: '',
  53. adminName: ''
  54. })
  55. const formRef = ref(null)
  56. const adminStore = useAdminStore()
  57. const { adminData } = storeToRefs(adminStore)
  58. const rules = reactive({
  59. jwcode: [
  60. { required: true, message: '请输入精网号', trigger: 'blur' },
  61. {
  62. validator: (rule, value, callback) => {
  63. if (!value) {
  64. callback(new Error('精网号不能为空'));
  65. return;
  66. }
  67. if (/[^0-9]/.test(value)) {
  68. callback(new Error('精网号只能包含数字'));
  69. return;
  70. }
  71. callback();
  72. }, trigger: 'blur'
  73. }],
  74. permanentBean: [
  75. { required: true, message: '请输入付费金豆数', trigger: 'change' },
  76. {
  77. validator: (rule, value, callback) => {
  78. if (!value) {
  79. value = 0
  80. }
  81. // 检查是否为非负整数
  82. if (!/^\d+$/.test(value)) {
  83. callback(new Error('请输入非负整数'));
  84. return;
  85. }
  86. // 检查位数
  87. if (value.length > 6) {
  88. callback(new Error('整数位数不能超过6位'));
  89. return;
  90. }
  91. callback();
  92. },
  93. trigger: 'blur'
  94. }
  95. ],
  96. freeBean: [
  97. { required: true, message: '请输入免费金豆数', trigger: 'change' },
  98. {
  99. validator: (rule, value, callback) => {
  100. if (!value) {
  101. value = 0
  102. }
  103. // 检查是否为非负整数
  104. if (!/^\d+$/.test(value)) {
  105. callback(new Error('请输入非负整数'));
  106. return;
  107. }
  108. // 检查位数
  109. if (value.length > 6) {
  110. callback(new Error('整数位数不能超过6位'));
  111. return;
  112. }
  113. callback();
  114. },
  115. trigger: 'blur'
  116. }
  117. ],
  118. remark: [
  119. { required: true, message: '请输入备注', trigger: 'blur' }
  120. ]
  121. });
  122. //重置表单
  123. const deleteConsumeForm = function () {
  124. formRef.value.resetFields();
  125. }
  126. const handleConsumeForm = async () => {
  127. try {
  128. if (!consumeForm.value.permanentBean) {
  129. consumeForm.value.permanentBean = 0
  130. }
  131. if (!consumeForm.value.freeBean) {
  132. consumeForm.value.freeBean = 0
  133. }
  134. await new Promise((resolve, reject) => {
  135. formRef.value.validate((valid) => {
  136. if (valid) {
  137. if (Number(consumeForm.value.permanentBean) === 0 && Number(consumeForm.value.freeBean) === 0) {
  138. reject(new Error('付费金豆和免费金豆不能同时为0'));
  139. }
  140. resolve(); // 验证通过,继续执行后续代码
  141. } else {
  142. reject(new Error('请检查并完善表单信息')); // 验证失败,抛出错误
  143. }
  144. });
  145. });
  146. console.log('adminData', adminData.value);
  147. // 新增验证逻辑,判断输入数量是否超过用户拥有数量
  148. const inputPermanentBean = Number(consumeForm.value.permanentBean);
  149. const inputFreeBean = Number(consumeForm.value.freeBean);
  150. const userPermanentBean = Number(user.value.permanentBean) || 0;
  151. const userFreeBean = Number(user.value.freeBean) || 0;
  152. // if (inputPermanentBean > userPermanentBean) {
  153. // throw new Error('付费金豆数量超过用户当前所拥有');
  154. // }
  155. // if (inputFreeBean > userFreeBean) {
  156. // throw new Error('免费金豆数量超过用户当前所拥有');
  157. // }
  158. await ElMessageBox.confirm(
  159. '确认消耗吗?',
  160. '提示',
  161. {
  162. confirmButtonText: '确认',
  163. cancelButtonText: '取消',
  164. type: "primary",
  165. lockScroll: false,
  166. }
  167. )
  168. addDisabled.value = true
  169. const result = await request({
  170. url: '/beanConsume/reduce',
  171. data: {
  172. jwcode: consumeForm.value.jwcode,
  173. permanentBean: consumeForm.value.permanentBean,
  174. freeBean: consumeForm.value.freeBean,
  175. remark: consumeForm.value.remark,
  176. adminName: adminData.value.adminName
  177. }
  178. })
  179. addDisabled.value = false
  180. if (result.code == 200) {
  181. ElMessage.success('新增成功')
  182. deleteConsumeForm()
  183. user.value = {}
  184. } else {
  185. ElMessage.error(result.msg)
  186. }
  187. } catch (error) {
  188. console.log('金豆新增充值失败');
  189. ElMessage.error(error.message || '操作失败');
  190. }
  191. }
  192. const throttledHandleConsumeFormt = _.throttle(handleConsumeForm, 5000, {
  193. trailing: false
  194. })
  195. </script>
  196. <template>
  197. <div>
  198. <div class="userAndform">
  199. <div class="left">
  200. <el-form :model="consumeForm" :rules="rules" ref="formRef" label-width="auto" style="max-width: 600px"
  201. class="add-form">
  202. <el-form-item prop="jwcode" label="精网号" label-position="left">
  203. <el-input v-model="consumeForm.jwcode" style="width: 220px" />
  204. <el-button type="primary" @click="getUser(consumeForm.jwcode)" style="margin-left: 20px">查询</el-button>
  205. </el-form-item>
  206. <el-form-item prop="permanentBean" label="付费金豆" label-position="left">
  207. <el-input v-model="consumeForm.permanentBean" placeholder="0" style="width: 100px" />
  208. </el-form-item>
  209. <el-form-item prop="freeBean" label="免费金豆" label-position="left">
  210. <el-input v-model="consumeForm.freeBean" placeholder="0" style="width: 100px" />
  211. </el-form-item>
  212. <el-form-item prop="remark" label="备注" label-position="left">
  213. <el-input v-model="consumeForm.remark" style="width: 300px" :rows="5" maxlength="100" show-word-limit
  214. type="textarea" />
  215. </el-form-item>
  216. <!-- <el-form-item prop="adminName" label="提交人">
  217. <el-input style="width: 300px" :value="adminData.adminName" disabled placeholder="提交人姓名" />
  218. </el-form-item> -->
  219. <el-button @click="deleteConsumeForm" style="margin-left: 280px" type="success">重置</el-button>
  220. <el-button type="primary" :disabled="addDisabled" @click="handleConsumeForm"> 提交 </el-button>
  221. </el-form>
  222. </div>
  223. <div class="right">
  224. <!-- 客户信息栏 -->
  225. <el-card v-if="user.jwcode" class="customer-info">
  226. <el-form :model="user" label-width="auto" label-position="left">
  227. <el-text size="large" style="min-width: 600px;">客户信息</el-text>
  228. <!-- 第一行姓名 + 当前付费金豆 -->
  229. <el-row style="margin-top: 20px">
  230. <el-col :span="9">
  231. <el-form-item label="姓名:">
  232. <p style="color: #2fa1ff; margin-right: 5px">{{ user.name }}</p>
  233. </el-form-item>
  234. </el-col>
  235. <el-col :span="14">
  236. <el-form-item label="当前付费金豆:">
  237. <p style="color: #2fa1ff; margin-right: 5px" v-if="!isNaN(Number(user.permanentBean))">
  238. {{ Number(user.permanentBean) }}
  239. </p>
  240. <!-- 如果不是有效的数字显示默认值 -->
  241. <p v-else></p>
  242. </el-form-item>
  243. </el-col>
  244. </el-row>
  245. <!-- 第二行精网号 + 免费金豆 -->
  246. <el-row>
  247. <el-col :span="9">
  248. <el-form-item label="精网号">
  249. <p style="color: #2fa1ff; margin-right: 5px">{{ user.jwcode }}</p>
  250. </el-form-item>
  251. </el-col>
  252. <el-col :span="14">
  253. <el-form-item label="当前免费金豆:">
  254. <span style="color: #2fa1ff; margin-right: 5px" v-if="user.freeBean !== undefined">{{ user.freeBean }}
  255. </span>
  256. </el-form-item>
  257. </el-col>
  258. </el-row>
  259. <!-- 第三行消费次数 + 所属门店 -->
  260. <el-row>
  261. <el-col :span="9">
  262. <el-form-item label="所属门店">
  263. <p style="color: #2fa1ff">{{ user.market }}</p>
  264. </el-form-item>
  265. </el-col>
  266. <el-col :span="9">
  267. <el-form-item label="消耗金豆总数">
  268. <p style="color: #2fa1ff; margin-right: 5px" v-if="user.consumeSum != null">{{ user.consumeSum }}</p>
  269. <p style="color: #2fa1ff; margin-right: 5px" v-else>{{ 0 }}</p>
  270. </el-form-item>
  271. </el-col>
  272. </el-row>
  273. </el-form>
  274. </el-card>
  275. </div>
  276. </div>
  277. </div>
  278. </template>
  279. <style scoped>
  280. .userAndform {
  281. width: 100%;
  282. height: 100%;
  283. display: flex;
  284. align-items: center;
  285. justify-content: center;
  286. .left {
  287. width: 35%;
  288. display: flex;
  289. .add-form {
  290. width: 100%;
  291. margin-top: 50px;
  292. }
  293. }
  294. .right {
  295. flex: 1;
  296. margin-left: 50px;
  297. display: flex;
  298. .beautiful {
  299. width: 90%;
  300. display: flex;
  301. justify-content: center;
  302. align-items: center;
  303. padding: 0 10px;
  304. }
  305. }
  306. }
  307. .customer-info {
  308. width: 700px;
  309. float: right;
  310. margin-right: 300px;
  311. margin-top: 50px;
  312. }
  313. p {
  314. margin: 0px;
  315. }
  316. .el-form-item {
  317. margin-left: 50px;
  318. }
  319. </style>