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.

633 lines
16 KiB

  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. disabled
  386. v-model="addConsume.permanentGold"
  387. style="width: 100px; margin-left: -5px"
  388. />
  389. <p style="margin-right: 0px"></p>
  390. </el-form-item>
  391. <el-form-item
  392. prop="freeCoin"
  393. label="免费金币"
  394. style="float: left; margin-left: -20px"
  395. >
  396. <el-input
  397. disabled
  398. v-model="addConsume.freeGold"
  399. style="width: 100px; margin-left: -5px"
  400. />
  401. <p style="margin-right: 0px"></p>
  402. </el-form-item>
  403. <el-form-item prop="taskGold" label="任务金币" style="margin-left: -20px">
  404. <el-input
  405. disabled
  406. v-model="addConsume.taskGold"
  407. style="width: 100px; margin-left: -5px"
  408. />
  409. <p style="margin-right: 20px"></p>
  410. </el-form-item>
  411. </div>
  412. <el-form-item prop="remark" label="备注">
  413. <el-input
  414. v-model="addConsume.remark"
  415. style="width: 300px"
  416. :rows="2"
  417. maxlength="100"
  418. show-word-limit
  419. type="textarea"
  420. />
  421. </el-form-item>
  422. <el-form-item prop="commitName" label="提交人">
  423. <el-input
  424. style="width: 300px"
  425. :value="adminData.name"
  426. disabled
  427. placeholder="提交人姓名"
  428. />
  429. </el-form-item>
  430. <el-button type="success" @click="resetForm()" style="margin-left: 280px">重置</el-button>
  431. <el-button type="primary" @click="addBefore"> 提交</el-button>
  432. </el-form>
  433. <!-- 客户信息栏 -->
  434. <el-card v-if="user.jwcode" style="width: 850px; float: right" class="customer-info">
  435. <el-form
  436. :model="user"
  437. label-width="auto"
  438. style="max-width: 1200px"
  439. label-position="left"
  440. >
  441. <el-text size="large" style="margin-left: 20px">客户信息</el-text>
  442. <el-row style="margin-top: 20px">
  443. <el-col :span="10">
  444. <el-form-item label="姓名:">
  445. <p>{{ user.name }}</p>
  446. </el-form-item>
  447. </el-col>
  448. <el-col :span="14">
  449. <el-form-item label="历史金币总数">
  450. <p>{{ user.historySumGold / 100 }}</p>
  451. </el-form-item>
  452. </el-col>
  453. <el-col :span="10">
  454. <el-form-item label="精网号">
  455. <p>{{ user.jwcode }}</p>
  456. </el-form-item>
  457. </el-col>
  458. <el-col :span="14">
  459. <el-form-item label="当前金币总数" style="width: 500px">
  460. <span
  461. style="color: #2fa1ff; margin-right: 5px"
  462. v-if="user.nowSumGold !== undefined"
  463. >
  464. {{ user.nowSumGold / 100 }}</span
  465. >
  466. <span
  467. style="display: inline; white-space: nowrap; color: #b1b1b1"
  468. v-if="user.nowSumGold !== null "
  469. >(永久金币:{{ user.nowPermanentGold / 100 }};免费金币:{{
  470. (user.nowFreeGold) / 100
  471. }};任务金币:{{ user.nowTaskGold / 100 }})</span>
  472. </el-form-item>
  473. </el-col>
  474. <el-col :span="10">
  475. <el-form-item label="首次充值日期">
  476. <p v-if="user.firstRecharge">
  477. {{ moment(user.firstRecharge).format("YYYY-MM-DD HH:mm:ss") }}
  478. </p>
  479. </el-form-item>
  480. </el-col>
  481. <el-col :span="14">
  482. <el-form-item label="充值次数">
  483. <p style="color: #2fa1ff">{{ user.rechargeNum }}</p>
  484. </el-form-item>
  485. </el-col>
  486. <el-col :span="10">
  487. <el-form-item label="消费次数">
  488. <p style="color: #2fa1ff">{{ user.consumeNum }}</p>
  489. </el-form-item>
  490. </el-col>
  491. <el-col :span="10">
  492. <el-form-item label="所属门店">
  493. <p>{{ user.market }}</p>
  494. </el-form-item>
  495. </el-col>
  496. <el-col :span="14">
  497. </el-col>
  498. </el-row>
  499. </el-form>
  500. </el-card>
  501. </div>
  502. <!-- 金币消耗明细的布局------------------------------------------------------- -->
  503. <!-- <div v-else-if="activeTab === 'detail'"> -->
  504. <!-- </div>
  505. </div> -->
  506. </template>
  507. <style scoped>
  508. p {
  509. margin: 0px;
  510. }
  511. .el-form-item {
  512. margin-left: 50px;
  513. }
  514. /* 上传图片的格式 */
  515. .avatar-uploader .avatar {
  516. width: 50px;
  517. height: 50px;
  518. display: block;
  519. }
  520. </style>
  521. <style>
  522. .avatar-uploader .el-upload {
  523. border: 1px dashed var(--el-border-color);
  524. border-radius: 6px;
  525. cursor: pointer;
  526. position: relative;
  527. overflow: hidden;
  528. transition: var(--el-transition-duration-fast);
  529. }
  530. .avatar-uploader .el-upload:hover {
  531. border-color: var(--el-color-primary);
  532. }
  533. .el-icon.avatar-uploader-icon {
  534. font-size: 28px;
  535. color: #8c939d;
  536. width: 50px;
  537. height: 50px;
  538. text-align: center;
  539. }
  540. .form-style {
  541. margin-top: 50px;
  542. max-width: 50%;
  543. float: left;
  544. }
  545. .form-style2 {
  546. max-width: 60%;
  547. }
  548. p {
  549. font-size: 13px;
  550. transform: scale(1);
  551. }
  552. </style>