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.

635 lines
16 KiB

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