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.

629 lines
17 KiB

20 hours ago
1 day ago
1 day ago
  1. <script setup>
  2. import {onMounted, reactive, ref, watch} from "vue";
  3. import {ElMessage, ElMessageBox} from "element-plus";
  4. import moment from "moment";
  5. import request from "@/util/http";
  6. /*
  7. ====================工具方法==============================
  8. */
  9. // 精网号去空格
  10. const trimJwCode = () => {
  11. if (addConsume.value.jwcode) {
  12. // 去除所有空格,并尝试转换为整数
  13. const trimmed = addConsume.value.jwcode.toString().replace(/\s/g, '');
  14. const numeric = Number(trimmed);
  15. // 如果转换为数字成功,保存为数字,否则提示错误
  16. if (!isNaN(numeric)) {
  17. addConsume.value.jwcode = numeric;
  18. } else {
  19. ElMessage.error("精网号格式不正确,请输入数字");
  20. }
  21. }
  22. }
  23. /*
  24. ====================数据=================================
  25. */
  26. //这是获取当前登录的用户信息
  27. const adminData = ref({});
  28. // 通过精网号查询用户(客户)信息 表单
  29. const user = ref({
  30. jwcode: null,
  31. name: "",
  32. market: "",
  33. historySumGold: null,
  34. historyPermanentGold: null,
  35. historyFreeGold: null,
  36. historyTaskGold: null,
  37. rechargeNum: null,
  38. consumeNum: null,
  39. firstRecharge: "",
  40. nowPermanentGold: null,
  41. nowFreeJune: null,
  42. nowTaskGold: null,
  43. nowFreeDecember: null,
  44. nowFreeGold: null,
  45. nowSumGold: null
  46. })
  47. // 这是添加消费信息的表单(金币)
  48. const addConsume = ref({
  49. // jwcode 是数字
  50. jwcode: null, //精网号
  51. goodsName: "",// 商品名称
  52. sumGold: null, // 消费金币总数
  53. freeGold: null, // 免费金币
  54. permanentGold: null, // 永久金币
  55. taskGold: null, // 任务金币
  56. remark: "",//备注
  57. adminId: null,// 当前管理员id
  58. });
  59. // 表单验证
  60. const Ref = ref(null);
  61. // 表单验证规则
  62. const rules = reactive({
  63. jwcode: [{required: true, message: "请输入精网号", trigger: "blur"}],
  64. goodsName: [{required: true, message: "请选择消费商品", trigger: "change"}], // 修改为 change
  65. sumGold: [
  66. {required: true, message: "消费金币总数不能为空", trigger: "blur"},
  67. ],
  68. });
  69. // 查询商品的表单
  70. const goods = ref([]);
  71. /*
  72. ====================方法=================================
  73. */
  74. const getAdminData = async function () {
  75. try {//await 暂停函数执行,直到请求完成
  76. const result = await request({
  77. url: "/admin/userinfo",
  78. data: {},
  79. });
  80. adminData.value = result;
  81. addConsume.value.adminId = adminData.value.id;
  82. <<<<<<< HEAD
  83. addConsume.value.name = adminData.value.adminName;
  84. =======
  85. addConsume.value.name = adminData.value.name;
  86. >>>>>>> zhangrenyuan/feature-20250623164058-金币前端
  87. console.log("请求成功", result);
  88. console.log("用户信息", adminData.value);
  89. } catch (error) {
  90. console.log("请求失败", error);
  91. }
  92. };
  93. // 输入验证函数
  94. function validateInput() {
  95. const sumGold = parseFloat(addConsume.value.sumGold);
  96. trimJwCode();
  97. if (user.value.jwcode == null) {
  98. ElMessage.error("请先查询用户信息");
  99. addConsume.value.sumGold = null;
  100. return false;
  101. }
  102. // 验证金币数值
  103. if (user.value.jwcode && (isNaN(sumGold) || sumGold <= 0)) {
  104. ElMessage.error("消费金币总数必须是大于0的数字");
  105. // 将sumGold设置为null
  106. addConsume.value.sumGold = null;
  107. return false;
  108. }
  109. // 验证金币总和
  110. const totalAvailableGold = (user.value.nowSumGold)
  111. if (user.value.jwcode && sumGold > totalAvailableGold) {
  112. ElMessage.error("消费金币总数超过可用金币总和");
  113. // 将sumGold设置为null
  114. addConsume.value.sumGold = null;
  115. return false;
  116. }
  117. return true;
  118. }
  119. // 消耗金币计算函数
  120. function calculateCoins(sumGold) {
  121. console.log("消耗金币计算函数:计算金币", sumGold);
  122. const parsedSumGold = parseFloat(sumGold);
  123. if (isNaN(parsedSumGold) || parsedSumGold <= 0 || !user.value.jwcode) {
  124. return {free: 0, permanent: 0, task: 0};
  125. }
  126. const {nowFreeGold, nowPermanentGold, nowTaskGold} = user.value;
  127. let remaining = parsedSumGold;
  128. let freeUsed = 0, permanentUsed = 0, taskUsed = 0;
  129. // 优先消耗免费金币
  130. if (nowFreeGold > 0) {
  131. freeUsed = Math.min(nowFreeGold, remaining);
  132. remaining -= freeUsed;
  133. }
  134. // 其次消耗永久金币
  135. if (remaining > 0 && nowPermanentGold > 0) {
  136. permanentUsed = Math.min(nowPermanentGold, remaining);
  137. remaining -= permanentUsed;
  138. }
  139. // 最后消耗任务金币
  140. if (remaining > 0 && nowTaskGold > 0) {
  141. taskUsed = remaining;
  142. }
  143. // 更新金币值
  144. addConsume.value.freeGold = freeUsed;
  145. addConsume.value.permanentGold = permanentUsed;
  146. addConsume.value.taskGold = taskUsed;
  147. return {free: freeUsed, permanent: permanentUsed, task: taskUsed};
  148. }
  149. // 这是添加消费信息的接口
  150. const add = async function () {
  151. try {
  152. // 验证输入数据 再验证一次
  153. if (!validateInput()) {
  154. return;
  155. }
  156. // 计算金币使用情况
  157. calculateCoins(addConsume.value.sumGold);
  158. console.log("addConsume.value", addConsume.value)
  159. // 发送POST请求
  160. const result = await request({
  161. // url: "/consume/add",
  162. url: "/consume/add",
  163. data: {
  164. ...addConsume.value,
  165. jwcode: addConsume.value.jwcode,
  166. adminId: addConsume.value.adminId,
  167. sumGold: addConsume.value.sumGold * 100,
  168. freeGold: addConsume.value.freeGold * 100,
  169. taskGold: addConsume.value.taskGold * 100,
  170. permanentGold: addConsume.value.permanentGold * 100,
  171. goodsName: addConsume.value.goodsName,
  172. remark: addConsume.value.remark
  173. }
  174. });
  175. console.log("add请求", result);
  176. // 处理响应
  177. handleResponse(result);
  178. // 重置表单
  179. resetForm();
  180. } catch (error) {
  181. console.error("请求失败", error);
  182. ElMessage.error("添加失败,请检查网络连接或联系管理员");
  183. }
  184. };
  185. // 响应处理函数
  186. function handleResponse(result) {
  187. console.log("响应结果", result)
  188. if (result.code === 200) {
  189. ElMessage.success("添加成功");
  190. console.log("请求成功", result);
  191. } else {
  192. ElMessage.error(result.msg || "添加失败,未知错误");
  193. }
  194. }
  195. // 重置表单函数
  196. function resetForm() {
  197. // 清空表单数据
  198. addConsume.value = {
  199. jwcode: null,
  200. goodsName: "",
  201. sumGold: null,
  202. freeGold: null,
  203. permanentGold: null,
  204. taskGold: null,
  205. remark: "",
  206. adminId: adminData.value.id,
  207. adminName: adminData.value.adminName,
  208. };
  209. console.log("重置表单")
  210. user.value = {
  211. jwcode: null,
  212. name: "",
  213. market: "",
  214. historySumGold: null,
  215. historyPermanentGold: null,
  216. historyFreeGold: null,
  217. historyTaskGold: null,
  218. rechargeNum: null,
  219. consumeNum: null,
  220. firstRecharge: "",
  221. nowPermanentGold: null,
  222. nowFreeJune: null,
  223. nowTaskGold: null,
  224. nowFreeDecember: null,
  225. nowFreeGold: null,
  226. nowSumGold: null
  227. }
  228. }
  229. // 添加前验证
  230. const addBefore = () => {
  231. Ref.value.validate(async (valid) => {
  232. if (valid) {
  233. ElMessageBox.confirm("确认添加?")
  234. .then(() => {
  235. console.log("这里是jwcode", addConsume.value.jwcode)
  236. add();
  237. console.log("添加成功",);
  238. addConsume.value = {};
  239. })
  240. .catch(() => {
  241. console.log("取消添加");
  242. });
  243. } else {
  244. //提示
  245. ElMessage({
  246. type: "error",
  247. message: "请检查输入内容",
  248. });
  249. }
  250. });
  251. };
  252. // 查询客户信息(通过精网号)
  253. const getUser = async function (jwcode) {
  254. trimJwCode();
  255. try {
  256. // 发送POST请求
  257. const result = await request({
  258. // url: "user/selectUser",
  259. url: "/user/selectUser", // todo 服务器改回无ip的
  260. data: {
  261. // 只需要传精网号
  262. jwcode: addConsume.value.jwcode,
  263. },
  264. });
  265. console.log("请求成功", result);
  266. if (result.code === 200) {
  267. user.value = result.data;
  268. user.value.nowPermanentGold = result.data.nowPermanentGold / 100;
  269. user.value.nowFreeGold = result.data.nowFreeGold / 100;
  270. user.value.nowSumGold = result.data.nowSumGold / 100;
  271. user.value.nowTaskGold = result.data.nowTaskGold / 100;
  272. user.value.nowFreeJune = (result.data.nowFreeJune) / 100;
  273. user.value.nowFreeDecember = (result.data.nowFreeDecember) / 100;
  274. user.value.historySumGold = (result.data.historySumGold) / 100;
  275. user.value.historyPermanentGold = (result.data.historyPermanentGold) / 100;
  276. user.value.historyFreeGold = (result.data.historyFreeGold) / 100;
  277. user.value.historyTaskGold = (result.data.historyTaskGold) / 100;
  278. }
  279. if (result.code === 0) {
  280. ElMessage.error(result.msg);
  281. } else if (result.data === null) {
  282. ElMessage.error("用户不存在");
  283. } else {
  284. console.log("用户信息", user.value);
  285. ElMessage.success(result.msg);
  286. }
  287. } catch (error) {
  288. console.log("请求失败", error);
  289. ElMessage.error("查询失败,请检查精网号是否正确");
  290. // 在这里可以处理错误逻辑,比如显示错误提示等
  291. }
  292. };
  293. // 获取商品信息(三楼接口)
  294. const getGoods = async function () {
  295. try {
  296. // 发送POST请求
  297. const result = await request({
  298. // url: "/product", // todo 后续换3楼的 2025年6月27日10:38:26 解决
  299. url: "http://39.101.133.168:8828/live_mall/api/product/all",
  300. });
  301. // 将响应结果存储到响应式数据中
  302. console.log("请求成功", result);
  303. goods.value = result.data.map(item => ({
  304. id: item.id,
  305. label: item.name,
  306. value: item.name
  307. }));
  308. } catch (error) {
  309. console.log("请求失败", error);
  310. // 在这里可以处理错误逻辑,比如显示错误提示等
  311. }
  312. };
  313. /*
  314. ====================监听=================================
  315. */
  316. // 监听消费总金额变化,自动计算三类金币
  317. watch(
  318. () => addConsume.value.sumGold,
  319. (newValue) => {
  320. const parsedNewValue = parseFloat(newValue);
  321. if (!isNaN(parsedNewValue) && parsedNewValue > 0) {
  322. const {free, permanent, task} = calculateCoins(parsedNewValue);
  323. addConsume.value.freeGold = free;
  324. addConsume.value.permanentGold = permanent;
  325. addConsume.value.taskGold = task;
  326. } else {
  327. addConsume.value.freeGold = null;
  328. addConsume.value.permanentGold = null;
  329. addConsume.value.taskGold = null;
  330. }
  331. }
  332. );
  333. /*
  334. ====================挂载=================================
  335. */
  336. // 挂载
  337. onMounted(async function () {
  338. await getAdminData();
  339. await getGoods();
  340. });
  341. </script>
  342. <template>
  343. <div>
  344. <!-- 根据activeTab切换显示内容 -->
  345. <!-- 新增消耗的布局---------------------------------------------------------- -->
  346. <!-- <div v-if="activeTab === 'addConsume'"> -->
  347. <!-- <div style="margin-bottom: 20px; font-weight: bolder">新增消费</div> -->
  348. <el-form
  349. :model="addConsume"
  350. ref="Ref"
  351. :rules="rules"
  352. label-width="auto"
  353. style="max-width: 750px;"
  354. class="form-style"
  355. >
  356. <el-form-item prop="jwcode" label="精网号">
  357. <el-input
  358. v-model="addConsume.jwcode"
  359. style="width: 220px"
  360. />
  361. <el-button
  362. type="primary"
  363. @click="getUser(addConsume.jwcode)"
  364. style="margin-left: 20px"
  365. >查询
  366. </el-button
  367. >
  368. </el-form-item>
  369. <div style="display: flex; align-items: center; gap: 20px;">
  370. <el-form-item prop="productName" label="商品名称" style="flex: 1; margin-right: 0px">
  371. <el-select
  372. v-model="addConsume.goodsName"
  373. placeholder="请选择商品"
  374. style="width: 450px"
  375. >
  376. <el-option
  377. v-for="item in goods"
  378. :key="item.value"
  379. :label="item.label"
  380. :value="item.value"
  381. />
  382. </el-select>
  383. </el-form-item>
  384. </div>
  385. <el-form-item prop="allGold" label="消费金币总数">
  386. <el-input
  387. v-model="addConsume.sumGold"
  388. style="width: 100px"
  389. @input="validateInput()"
  390. @change="calculateCoins(addConsume.sumGold)"
  391. />
  392. </el-form-item>
  393. <!-- 三类金币自动计算禁用状态不可编辑 -->
  394. <div style="display: flex; align-items: center">
  395. <el-form-item prop="permanentGold" label="永久金币" style="float: left">
  396. <el-input
  397. v-model="addConsume.permanentGold"
  398. disabled
  399. style="width: 100px; margin-left: -5px"
  400. >
  401. <template #default="scope">{{ scope.row.permanentGold }}</template>
  402. </el-input>
  403. <p style="margin-right: 0px"></p>
  404. </el-form-item>
  405. <el-form-item
  406. prop="freeCoin"
  407. label="免费金币"
  408. style="float: left; margin-left: -20px"
  409. >
  410. <el-input
  411. disabled
  412. v-model="addConsume.freeGold"
  413. style="width: 100px; margin-left: -5px"
  414. />
  415. <p style="margin-right: 0px"></p>
  416. </el-form-item>
  417. <el-form-item prop="taskGold" label="任务金币" style="margin-left: -20px">
  418. <el-input
  419. disabled
  420. v-model="addConsume.taskGold"
  421. style="width: 100px; margin-left: -5px"
  422. />
  423. <p style="margin-right: 20px"></p>
  424. </el-form-item>
  425. </div>
  426. <el-form-item prop="remark" label="备注">
  427. <el-input
  428. v-model="addConsume.remark"
  429. style="width: 300px"
  430. :rows="2"
  431. maxlength="100"
  432. show-word-limit
  433. type="textarea"
  434. />
  435. </el-form-item>
  436. <el-form-item prop="commitName" label="提交人">
  437. <el-input
  438. style="width: 300px"
  439. :value="adminData.adminName"
  440. disabled
  441. placeholder="提交人姓名"
  442. />
  443. </el-form-item>
  444. <el-button type="success" @click="resetForm()" style="margin-left: 280px">重置</el-button>
  445. <el-button type="primary" @click="addBefore"> 提交</el-button>
  446. </el-form>
  447. <!-- 客户信息栏 -->
  448. <el-card v-if="user.jwcode" style="width: 850px; float: right" class="customer-info">
  449. <el-form
  450. :model="user"
  451. label-width="auto"
  452. style="max-width: 1200px"
  453. label-position="left"
  454. >
  455. <el-text size="large" style="margin-left: 20px">客户信息</el-text>
  456. <el-row style="margin-top: 20px">
  457. <el-col :span="10">
  458. <el-form-item label="姓名:">
  459. <p>{{ user.name }}</p>
  460. </el-form-item>
  461. </el-col>
  462. <el-col :span="14">
  463. <el-form-item label="历史金币总数">
  464. <p>{{ user.historySumGold }}</p>
  465. </el-form-item>
  466. </el-col>
  467. <el-col :span="10">
  468. <el-form-item label="精网号">
  469. <p>{{ user.jwcode }}</p>
  470. </el-form-item>
  471. </el-col>
  472. <el-col :span="14">
  473. <el-form-item label="当前金币总数" style="width: 500px">
  474. <span
  475. style="color: #2fa1ff; margin-right: 5px"
  476. v-if="user.nowSumGold !== undefined"
  477. >
  478. {{ user.nowSumGold }}</span
  479. >
  480. <span
  481. style="display: inline; white-space: nowrap; color: #b1b1b1"
  482. v-if="user.nowSumGold !== null "
  483. >(永久金币:{{ user.nowPermanentGold }};免费金币:{{
  484. (user.nowFreeGold)
  485. }};任务金币:{{ user.nowTaskGold }})</span>
  486. </el-form-item>
  487. </el-col>
  488. <el-col :span="10">
  489. <el-form-item label="首次充值日期">
  490. <p v-if="user.firstRecharge">
  491. {{ moment(user.firstRecharge).format("YYYY-MM-DD HH:mm:ss") }}
  492. </p>
  493. </el-form-item>
  494. </el-col>
  495. <el-col :span="14">
  496. <el-form-item label="充值次数">
  497. <p style="color: #2fa1ff">{{ user.rechargeNum }}</p>
  498. </el-form-item>
  499. </el-col>
  500. <el-col :span="10">
  501. <el-form-item label="消费次数">
  502. <p style="color: #2fa1ff">{{ user.consumeNum }}</p>
  503. </el-form-item>
  504. </el-col>
  505. <el-col :span="10">
  506. <el-form-item label="所属门店">
  507. <p>{{ user.market }}</p>
  508. </el-form-item>
  509. </el-col>
  510. <el-col :span="14">
  511. </el-col>
  512. </el-row>
  513. </el-form>
  514. </el-card>
  515. </div>
  516. <!-- 金币消耗明细的布局------------------------------------------------------- -->
  517. <!-- <div v-else-if="activeTab === 'detail'"> -->
  518. <!-- </div>
  519. </div> -->
  520. </template>
  521. <style scoped>
  522. p {
  523. margin: 0px;
  524. }
  525. .el-form-item {
  526. margin-left: 50px;
  527. }
  528. /* 上传图片的格式 */
  529. .avatar-uploader .avatar {
  530. width: 50px;
  531. height: 50px;
  532. display: block;
  533. }
  534. </style>
  535. <style>
  536. .avatar-uploader .el-upload {
  537. border: 1px dashed var(--el-border-color);
  538. border-radius: 6px;
  539. cursor: pointer;
  540. position: relative;
  541. overflow: hidden;
  542. transition: var(--el-transition-duration-fast);
  543. }
  544. .avatar-uploader .el-upload:hover {
  545. border-color: var(--el-color-primary);
  546. }
  547. .el-icon.avatar-uploader-icon {
  548. font-size: 28px;
  549. color: #8c939d;
  550. width: 50px;
  551. height: 50px;
  552. text-align: center;
  553. }
  554. .form-style {
  555. margin-top: 50px;
  556. max-width: 50%;
  557. float: left;
  558. }
  559. .form-style2 {
  560. max-width: 60%;
  561. }
  562. p {
  563. font-size: 13px;
  564. transform: scale(1);
  565. }
  566. </style>