project.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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,生成说明
  223. if (item.serveList && item.serveList.length > 0) {
  224. const detailNames = item.serveList.map(row => row.name).filter(name => name).join('、')
  225. if (detailNames) {
  226. item.itemContent = detailNames
  227. }
  228. }
  229. } else {
  230. item.delFlag = 1
  231. item.itemMoney = 0
  232. }
  233. list.push({
  234. delFlag: item.delFlag,
  235. itemContent: item.itemContent || '',
  236. itemMoney: item.itemMoney || 0,
  237. itemName: item.itemName
  238. })
  239. })
  240. console.log('提交数据:', list)
  241. let data = {
  242. checkType: this.checkType,
  243. taskId: this.taskId,
  244. orderNo: this.orderNo,
  245. orderName: this.productName,
  246. merchantId: uni.getStorageSync('login_user_info').merchantId,
  247. id: this.editId,
  248. serveType: this.checkType < 20 ? '01' : '02',
  249. addMerchantServiceVoList: list
  250. }
  251. console.log('最终提交:', data)
  252. this.$http.post("/ts/merchant/addPaidService", data).then(res => {
  253. console.log(res)
  254. if (res.data.code == 200) {
  255. uni.showToast({
  256. icon: 'success',
  257. title: '添加成功'
  258. })
  259. setTimeout(() => {
  260. this.back()
  261. }, 1500)
  262. } else {
  263. uni.showToast({
  264. icon: 'none',
  265. title: res.data.message || '提交失败'
  266. })
  267. }
  268. }).catch(err => {
  269. console.error('提交错误:', err)
  270. uni.showToast({
  271. icon: 'none',
  272. title: '网络错误'
  273. })
  274. })
  275. },
  276. //回显
  277. getChange() {
  278. this.$http.get("/ts/merchant/servicesDetails?subOrderId=" + this.orderNo).then(res => {
  279. if (res.data.code == 200) {
  280. this.editId = res.data.result.id
  281. const details = res.data.result.serviceDetails || []
  282. // 更新项目列表,保持原有结构
  283. this.projectList = this.projectList.map(project => {
  284. const detail = details.find(d => d.itemName === project.itemName)
  285. if (detail) {
  286. return {
  287. ...project,
  288. checked: detail.delFlag == 0,
  289. delFlag: detail.delFlag,
  290. itemMoney: detail.itemMoney,
  291. itemContent: detail.itemContent || '',
  292. serveList: detail.serveList || [{
  293. name: detail.itemContent || '',
  294. price: detail.itemMoney || '',
  295. quantity: 1
  296. }]
  297. }
  298. }
  299. return project
  300. })
  301. console.log('回显数据:', this.projectList)
  302. }
  303. }).catch(err => {
  304. console.error('获取回显数据错误:', err)
  305. })
  306. }
  307. },
  308. onLoad(options) {
  309. console.log('页面参数:', options)
  310. this.checkType = options.checkType || ''
  311. this.taskId = options.taskId || ''
  312. this.orderNo = options.orderNo || ''
  313. this.productName = options.productName || ''
  314. this.type = options.type || '1'
  315. if (this.type == 0) {
  316. this.getChange()
  317. }
  318. },
  319. onShow() {
  320. // 确保页面显示时数据正确
  321. console.log('项目列表:', this.projectList)
  322. }
  323. }
  324. </script>
  325. <style lang="scss" scoped>
  326. .project {
  327. width: 100vw;
  328. height: 100vh;
  329. background: #F8F8FA;
  330. display: flex;
  331. flex-direction: column;
  332. /*#ifdef APP-PLUS*/
  333. .top {
  334. width: 100%;
  335. height: var(--status-bar-height);
  336. background: #FFFFFF;
  337. }
  338. /*#endif*/
  339. .head {
  340. width: 100%;
  341. height: 112rpx;
  342. padding: 20rpx;
  343. display: flex;
  344. align-items: center;
  345. background: #FFFFFF;
  346. position: relative;
  347. border-bottom: 1px solid #F0F3F5;
  348. .cuIcon-back {
  349. font-size: 40rpx;
  350. margin-right: 40rpx;
  351. }
  352. .case {
  353. position: absolute;
  354. left: 50%;
  355. transform: translateX(-50%);
  356. font-size: 36rpx;
  357. font-weight: 600;
  358. }
  359. }
  360. .content {
  361. padding: 24rpx;
  362. overflow-y: auto;
  363. flex: 1;
  364. .option {
  365. width: 100%;
  366. padding: 20rpx 32rpx;
  367. background: #FFFFFF;
  368. box-shadow: 0px 2rpx 8rpx 0px rgba(214, 225, 237, 0.1);
  369. border-radius: 16rpx;
  370. margin-bottom: 16rpx;
  371. .name {
  372. width: 100%;
  373. display: flex;
  374. align-items: center;
  375. justify-content: space-between;
  376. margin-bottom: 16rpx;
  377. font-size: 32rpx;
  378. font-weight: 500;
  379. }
  380. .price-details {
  381. width: 100%;
  382. margin-top: 20rpx;
  383. .details-wrapper {
  384. margin-bottom: 24rpx;
  385. &:last-child {
  386. margin-bottom: 0;
  387. }
  388. }
  389. .details_item {
  390. width: 100%;
  391. display: flex;
  392. flex-direction: column;
  393. gap: 24rpx;
  394. .details_name {
  395. display: flex;
  396. align-items: center;
  397. justify-content: space-between;
  398. padding-bottom: 16rpx;
  399. border-bottom: 1px solid #f0f0f0;
  400. .label {
  401. font-size: 28rpx;
  402. color: #333333;
  403. width: 160rpx;
  404. flex-shrink: 0;
  405. }
  406. ::v-deep .u-input {
  407. flex: 1;
  408. height: 60rpx;
  409. }
  410. }
  411. .action-btns {
  412. display: flex;
  413. justify-content: flex-end;
  414. gap: 30rpx;
  415. padding-top: 10rpx;
  416. .add, .delete {
  417. display: flex;
  418. align-items: center;
  419. padding: 12rpx 24rpx;
  420. border-radius: 8rpx;
  421. font-size: 26rpx;
  422. cursor: pointer;
  423. image {
  424. width: 28rpx;
  425. height: 28rpx;
  426. margin-right: 8rpx;
  427. }
  428. }
  429. .add {
  430. background: #f0f7ff;
  431. color: #2D88F4;
  432. border: 1px solid #2D88F4;
  433. }
  434. .delete {
  435. background: #fff0f0;
  436. color: #FF4444;
  437. border: 1px solid #FF4444;
  438. }
  439. }
  440. }
  441. }
  442. }
  443. }
  444. .btn_box {
  445. width: 100%;
  446. height: 109rpx;
  447. background: #FFFFFF;
  448. display: flex;
  449. align-items: center;
  450. justify-content: space-around;
  451. margin-top: 10rpx;
  452. font-size: 28rpx;
  453. .customer {
  454. border-radius: 49rpx;
  455. border: 1px solid #2D88F4;
  456. width: 324rpx;
  457. height: 76rpx;
  458. display: flex;
  459. align-items: center;
  460. justify-content: center;
  461. color: #2D88F4;
  462. }
  463. .accept {
  464. display: flex;
  465. align-items: center;
  466. justify-content: center;
  467. width: 324rpx;
  468. height: 76rpx;
  469. background: linear-gradient(90deg, #77B6FF 0%, #449AFF 100%);
  470. border-radius: 49rpx;
  471. color: #FFFFFF;
  472. }
  473. }
  474. }
  475. </style>