project.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. <template>
  2. <view class="project">
  3. <view class="top"></view>
  4. <view class="head">
  5. <span class="cuIcon-back" @click="back"></span>
  6. <view class="case">
  7. {{type==0?'修改服务项目':'选择服务项目'}}
  8. </view>
  9. </view>
  10. <view class="content">
  11. <view class="option" v-for="(item, index) in projectList" :key="index">
  12. <view class="name">
  13. <span>{{item.itemName}}</span>
  14. <u-switch v-model="item.checked" size="50rpx" @change="change(item)"></u-switch>
  15. </view>
  16. <view class="price-details" v-if="item.checked">
  17. <view class="details-wrapper" v-for="(row, rowIndex) in item.serveList" :key="rowIndex">
  18. <view class="details_item">
  19. <view class="details_name">
  20. <span class="label">明细名称</span>
  21. <u-input v-model="row.name" placeholder="请输入明细名称" :customStyle="{padding: '0', fontSize: '28rpx'}" />
  22. </view>
  23. <view class="details_name">
  24. <span class="label">项目价格</span>
  25. <u-input v-model="row.price" type="number" placeholder="请输入价格" :customStyle="{padding: '0', fontSize: '28rpx'}" />
  26. </view>
  27. <view class="action-btns">
  28. <view class="add" @click="addDetail(item)" v-if="rowIndex === item.serveList.length - 1">
  29. <image src="/static/icon/icon_plus.png" mode="aspectFit"></image>
  30. <span>添加</span>
  31. </view>
  32. <view class="delete" @click="removeDetail(item, rowIndex)" v-if="item.serveList.length > 1">
  33. <image src="/static/icon/icon_del.png" mode="aspectFit"></image>
  34. <span>删除</span>
  35. </view>
  36. </view>
  37. </view>
  38. </view>
  39. </view>
  40. </view>
  41. </view>
  42. <view class="btn_box">
  43. <view class="customer" @click="back">
  44. 取消
  45. </view>
  46. <view class="accept" @click="submit">
  47. 确定
  48. </view>
  49. </view>
  50. </view>
  51. </template>
  52. <script>
  53. export default {
  54. data() {
  55. return {
  56. checkType: '',
  57. taskId: '',
  58. orderNo: '',
  59. productName: '',
  60. subOrderId: '', //修改用
  61. projectList: [
  62. {
  63. itemName: '车辆维修费',
  64. checked: false,
  65. delFlag: '',
  66. itemMoney: null,
  67. itemContent: '',
  68. serveList: [{
  69. name: '',
  70. price: '',
  71. quantity: 1
  72. }]
  73. },
  74. {
  75. itemName: '喷漆费',
  76. checked: false,
  77. delFlag: '',
  78. itemMoney: null,
  79. itemContent: '',
  80. serveList: [{
  81. name: '',
  82. price: '',
  83. quantity: 1
  84. }]
  85. },
  86. {
  87. itemName: '零配件',
  88. checked: false,
  89. delFlag: '',
  90. itemMoney: null,
  91. itemContent: '',
  92. serveList: [{
  93. name: '',
  94. price: '',
  95. quantity: 1
  96. }]
  97. },
  98. {
  99. itemName: '车辆清洁费',
  100. checked: false,
  101. delFlag: '',
  102. itemMoney: null,
  103. itemContent: '',
  104. serveList: [{
  105. name: '',
  106. price: '',
  107. quantity: 1
  108. }]
  109. },
  110. {
  111. itemName: '其他费用',
  112. checked: false,
  113. delFlag: '',
  114. itemMoney: null,
  115. itemContent: '',
  116. serveList: [{
  117. name: '',
  118. price: '',
  119. quantity: 1
  120. }]
  121. }
  122. ],
  123. type: '',
  124. editId: ''
  125. }
  126. },
  127. methods: {
  128. //返回
  129. back() {
  130. uni.switchTab({
  131. url: '/pages/order/index'
  132. })
  133. },
  134. //关闭时置空
  135. change(item) {
  136. console.log('开关状态变化:', item)
  137. if (!item.checked) {
  138. item.itemContent = ''
  139. item.itemMoney = ''
  140. } else {
  141. // 确保选中时有serveList数据
  142. if (!item.serveList || item.serveList.length === 0) {
  143. item.serveList = [{
  144. name: '',
  145. price: '',
  146. quantity: 1
  147. }]
  148. }
  149. }
  150. },
  151. // 添加明细
  152. addDetail(item) {
  153. if (!item.serveList) {
  154. item.serveList = []
  155. }
  156. item.serveList.push({
  157. name: '',
  158. price: '',
  159. quantity: item.serveList.length + 1
  160. })
  161. console.log('添加明细后:', item.serveList)
  162. },
  163. // 删除明细
  164. removeDetail(item, index) {
  165. if (item.serveList && item.serveList.length > 1) {
  166. item.serveList.splice(index, 1)
  167. // 重新排序quantity
  168. item.serveList.forEach((row, idx) => {
  169. row.quantity = idx + 1
  170. })
  171. }
  172. },
  173. // 实时输入时校验(防止用户输入非法字符)
  174. handleInput(item) {
  175. if (item.itemMoney) {
  176. let value = item.itemMoney.toString();
  177. // 限制整数部分最多8位
  178. let parts = value.split('.');
  179. if (parts[0].length > 8) {
  180. parts[0] = parts[0].slice(0, 8);
  181. }
  182. // 限制小数部分最多两位
  183. if (parts[1]) {
  184. parts[1] = parts[1].slice(0, 2);
  185. }
  186. // 合并整数和小数部分,确保输入的格式符合要求
  187. item.itemMoney = parts.join('.');
  188. }
  189. },
  190. //输入金额
  191. validateInput(item) {
  192. if (item.itemMoney && Number(item.itemMoney) <= 0) {
  193. uni.showToast({
  194. title: '金额必须大于0',
  195. icon: 'none'
  196. });
  197. item.itemMoney = ''; // 清空或设置为默认最小值
  198. }
  199. },
  200. // 计算明细总金额
  201. calculateTotal(item) {
  202. if (!item.serveList || item.serveList.length === 0) {
  203. item.itemMoney = 0
  204. return
  205. }
  206. let total = 0
  207. item.serveList.forEach(row => {
  208. const price = parseFloat(row.price) || 0
  209. total += price
  210. })
  211. item.itemMoney = total.toFixed(2)
  212. },
  213. submit() {
  214. // 计算每个项目的总金额
  215. this.projectList.forEach(item => {
  216. this.calculateTotal(item)
  217. })
  218. let list = []
  219. this.projectList.forEach(item => {
  220. if (item.checked) {
  221. item.delFlag = 0
  222. // 如果有serveList,生成subItem数组
  223. if (item.serveList && item.serveList.length > 0) {
  224. item.subItem = item.serveList.map(row => ({
  225. itemContent: row.name || '',
  226. itemPrice: row.price || '0'
  227. }))
  228. // 计算总金额
  229. const total = item.serveList.reduce((sum, row) => {
  230. return sum + parseFloat(row.price || 0)
  231. }, 0)
  232. item.itemMoney = total.toFixed(2)
  233. } else {
  234. item.subItem = []
  235. }
  236. } else {
  237. item.delFlag = 1
  238. item.itemMoney = 0
  239. item.subItem = []
  240. }
  241. list.push({
  242. delFlag: item.delFlag,
  243. subItem: item.subItem,
  244. itemMoney: item.itemMoney || 0,
  245. itemName: item.itemName
  246. })
  247. })
  248. console.log('提交数据:', list)
  249. let data = {
  250. checkType: this.checkType,
  251. taskId: this.taskId,
  252. orderNo: this.orderNo,
  253. orderName: this.productName,
  254. merchantId: uni.getStorageSync('login_user_info').merchantId,
  255. id: this.editId,
  256. serveType: this.checkType < 20 ? '01' : '02',
  257. addMerchantServiceVoList: list
  258. }
  259. console.log('最终提交:', data)
  260. this.$http.post("/ts/merchant/addPaidService", data).then(res => {
  261. console.log(res)
  262. if (res.data.code == 200) {
  263. uni.showToast({
  264. icon: 'success',
  265. title: '添加成功'
  266. })
  267. setTimeout(() => {
  268. this.back()
  269. }, 1500)
  270. } else {
  271. uni.showToast({
  272. icon: 'none',
  273. title: res.data.message || '提交失败'
  274. })
  275. }
  276. }).catch(err => {
  277. console.error('提交错误:', err)
  278. uni.showToast({
  279. icon: 'none',
  280. title: '网络错误'
  281. })
  282. })
  283. },
  284. //回显
  285. getChange() {
  286. this.$http.get("/ts/merchant/servicesDetails?subOrderId=" + this.orderNo).then(res => {
  287. if (res.data.code == 200) {
  288. this.editId = res.data.result.id
  289. const details = res.data.result.serviceDetails || []
  290. // 更新项目列表,保持原有结构
  291. this.projectList = this.projectList.map(project => {
  292. const detail = details.find(d => d.itemName === project.itemName)
  293. if (detail) {
  294. // 将subItem转换为serveList格式用于页面显示
  295. const serveList = (detail.subItem && detail.subItem.length > 0)
  296. ? detail.subItem.map(sub => ({
  297. name: sub.itemContent || '',
  298. price: sub.itemPrice || '',
  299. quantity: 1
  300. }))
  301. : [{
  302. name: '',
  303. price: '',
  304. quantity: 1
  305. }]
  306. return {
  307. ...project,
  308. checked: detail.delFlag == 0,
  309. delFlag: detail.delFlag,
  310. itemMoney: detail.itemMoney,
  311. serveList: serveList
  312. }
  313. }
  314. return project
  315. })
  316. console.log('回显数据:', this.projectList)
  317. }
  318. }).catch(err => {
  319. console.error('获取回显数据错误:', err)
  320. })
  321. }
  322. },
  323. onLoad(options) {
  324. console.log('页面参数:', options)
  325. this.checkType = options.checkType || ''
  326. this.taskId = options.taskId || ''
  327. this.orderNo = options.orderNo || ''
  328. this.productName = options.productName || ''
  329. this.type = options.type || '1'
  330. if (this.type == 0) {
  331. this.getChange()
  332. }
  333. },
  334. onShow() {
  335. // 确保页面显示时数据正确
  336. console.log('项目列表:', this.projectList)
  337. }
  338. }
  339. </script>
  340. <style lang="scss" scoped>
  341. .project {
  342. width: 100vw;
  343. height: 100vh;
  344. background: #F8F8FA;
  345. display: flex;
  346. flex-direction: column;
  347. /*#ifdef APP-PLUS*/
  348. .top {
  349. width: 100%;
  350. height: var(--status-bar-height);
  351. background: #FFFFFF;
  352. }
  353. /*#endif*/
  354. .head {
  355. width: 100%;
  356. height: 112rpx;
  357. padding: 20rpx;
  358. display: flex;
  359. align-items: center;
  360. background: #FFFFFF;
  361. position: relative;
  362. border-bottom: 1px solid #F0F3F5;
  363. .cuIcon-back {
  364. font-size: 40rpx;
  365. margin-right: 40rpx;
  366. }
  367. .case {
  368. position: absolute;
  369. left: 50%;
  370. transform: translateX(-50%);
  371. font-size: 36rpx;
  372. font-weight: 600;
  373. }
  374. }
  375. .content {
  376. padding: 24rpx;
  377. overflow-y: auto;
  378. flex: 1;
  379. .option {
  380. width: 100%;
  381. padding: 20rpx 32rpx;
  382. background: #FFFFFF;
  383. box-shadow: 0px 2rpx 8rpx 0px rgba(214, 225, 237, 0.1);
  384. border-radius: 16rpx;
  385. margin-bottom: 16rpx;
  386. .name {
  387. width: 100%;
  388. display: flex;
  389. align-items: center;
  390. justify-content: space-between;
  391. margin-bottom: 16rpx;
  392. font-size: 32rpx;
  393. font-weight: 500;
  394. }
  395. .price-details {
  396. width: 100%;
  397. margin-top: 20rpx;
  398. .details-wrapper {
  399. margin-bottom: 24rpx;
  400. &:last-child {
  401. margin-bottom: 0;
  402. }
  403. }
  404. .details_item {
  405. width: 100%;
  406. display: flex;
  407. flex-direction: column;
  408. gap: 24rpx;
  409. .details_name {
  410. display: flex;
  411. align-items: center;
  412. justify-content: space-between;
  413. padding-bottom: 16rpx;
  414. border-bottom: 1px solid #f0f0f0;
  415. .label {
  416. font-size: 28rpx;
  417. color: #333333;
  418. width: 160rpx;
  419. flex-shrink: 0;
  420. }
  421. ::v-deep .u-input {
  422. flex: 1;
  423. height: 60rpx;
  424. }
  425. }
  426. .action-btns {
  427. display: flex;
  428. justify-content: flex-end;
  429. gap: 30rpx;
  430. padding-top: 10rpx;
  431. .add, .delete {
  432. display: flex;
  433. align-items: center;
  434. padding: 12rpx 24rpx;
  435. border-radius: 8rpx;
  436. font-size: 26rpx;
  437. cursor: pointer;
  438. image {
  439. width: 28rpx;
  440. height: 28rpx;
  441. margin-right: 8rpx;
  442. }
  443. }
  444. .add {
  445. background: #f0f7ff;
  446. color: #2D88F4;
  447. border: 1px solid #2D88F4;
  448. }
  449. .delete {
  450. background: #fff0f0;
  451. color: #FF4444;
  452. border: 1px solid #FF4444;
  453. }
  454. }
  455. }
  456. }
  457. }
  458. }
  459. .btn_box {
  460. width: 100%;
  461. height: 109rpx;
  462. background: #FFFFFF;
  463. display: flex;
  464. align-items: center;
  465. justify-content: space-around;
  466. margin-top: 10rpx;
  467. font-size: 28rpx;
  468. .customer {
  469. border-radius: 49rpx;
  470. border: 1px solid #2D88F4;
  471. width: 324rpx;
  472. height: 76rpx;
  473. display: flex;
  474. align-items: center;
  475. justify-content: center;
  476. color: #2D88F4;
  477. }
  478. .accept {
  479. display: flex;
  480. align-items: center;
  481. justify-content: center;
  482. width: 324rpx;
  483. height: 76rpx;
  484. background: linear-gradient(90deg, #77B6FF 0%, #449AFF 100%);
  485. border-radius: 49rpx;
  486. color: #FFFFFF;
  487. }
  488. }
  489. }
  490. </style>