create_order.vue 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. <template>
  2. <view class="order-container">
  3. <!-- 用户信息卡片 -->
  4. <view class="info-card">
  5. <view class="address-item">
  6. <view class="img-status-text">
  7. <image src="/static/img/icon-send.png" mode="" class="img"></image>
  8. </view>
  9. <view class="user-info">
  10. <AddressInfo v-if="addressSend.addressId" :address="addressSend" />
  11. <view v-else class="create-btn" @click="handleAddAddress('sender')">新建寄件人</view>
  12. </view>
  13. <view class="img-status-text right" @click="openAddressBook('sender')">
  14. <image src="/static/img/create-order-address.png" mode="" class="address-image"></image>
  15. <view class="status-text">地址薄</view>
  16. </view>
  17. </view>
  18. <view class="address-item">
  19. <view class="img-status-text">
  20. <image src="/static/img/create-order-change.png" mode="" class="img-change"></image>
  21. </view>
  22. <view class="user-info">
  23. <view class="line"></view>
  24. </view>
  25. </view>
  26. <view class="address-item">
  27. <view class="img-status-text">
  28. <image src="/static/img/icon-receive.png" mode="" class="img"></image>
  29. </view>
  30. <view class="user-info">
  31. <AddressInfo v-if="addressReceive.addressId" :address="addressReceive" />
  32. <view v-else class="create-btn" @click="handleAddAddress('receiver')">新建收件人</view>
  33. </view>
  34. <view class="img-status-text right" @click="openAddressBook('receiver')">
  35. <image src="/static/img/create-order-address.png" mode="" class="address-image"></image>
  36. <view class="status-text">地址薄</view>
  37. </view>
  38. </view>
  39. </view>
  40. <view class="pickup-title">上门取件</view>
  41. <!-- 物品信息 -->
  42. <view class="goods-card">
  43. <view class="goods-item">
  44. <view class="item-label required">期望上门时间</view>
  45. <view class="time-value" @click="handleTimeClick">
  46. <text class="value">{{ selectedTimeLabel }}</text>
  47. <u-icon class="arrow" name='arrow-right' size="18"></u-icon>
  48. </view>
  49. </view>
  50. <view class="goods-item">
  51. <view class="item-label required">快递类型</view>
  52. <view class="item-control">
  53. <picker :range="expressTypes" :range-key="'label'" @change="onExpressTypeChange"
  54. :value="expressTypeIndex" :disabled="!expressTypes.length">
  55. <view class="picker-text">
  56. {{ expressTypes[expressTypeIndex]?.label || (expressTypes.length ? '请选择快递类型' : '加载中...') }}
  57. </view>
  58. </picker>
  59. <u-icon class="arrow" name='arrow-right' size="18"></u-icon>
  60. </view>
  61. </view>
  62. <view class="goods-item">
  63. <view class="item-label required">物品信息</view>
  64. <view class="item-control">
  65. <input class="input-field" placeholder="请输入物品信息" placeholder-class="placeholder" v-model="goodsInfo"
  66. maxlength="50" @input="onGoodsInfoInput" />
  67. </view>
  68. </view>
  69. <!-- 总体重 -->
  70. <view class="goods-item">
  71. <view class="item-label">总体重(KG)</view>
  72. <view class="item-control btn">
  73. <view class="control-btn minus" @click="decreaseWeight">-</view>
  74. <view class="control-value">
  75. <input class="input-field" placeholder="" placeholder-class="placeholder" :value="weight"
  76. @input="handleWeightInput" type="digit" />
  77. </view>
  78. <view class="control-btn plus" @click="increaseWeight">+</view>
  79. </view>
  80. </view>
  81. <!-- 总体积 -->
  82. <view class="goods-item">
  83. <view class="item-label">总体积(m³)</view>
  84. <view class="item-control btn">
  85. <view class="control-btn minus" @click="decreaseVolume">-</view>
  86. <view class="control-value">
  87. <input class="input-field" placeholder="" placeholder-class="placeholder" :value="volume"
  88. @input="handleVolumeInput" type="digit" />
  89. </view>
  90. <view class="control-btn plus" @click="increaseVolume">+</view>
  91. </view>
  92. </view>
  93. <!-- 件数 -->
  94. <view class="goods-item">
  95. <view class="item-label">件数(件)</view>
  96. <view class="item-control btn">
  97. <view class="control-btn minus" @click="decreaseQuantity">-</view>
  98. <view class="control-value">
  99. <input class="input-field" placeholder="" placeholder-class="placeholder" :value="quantity"
  100. @input="handleQuantityInput" type="number" />
  101. </view>
  102. <view class="control-btn plus" @click="increaseQuantity">+</view>
  103. </view>
  104. </view>
  105. </view>
  106. <!-- 增值服务标题(带折叠箭头) -->
  107. <view class="pickup-title" style="display: flex; justify-content: space-between; align-items: center;">
  108. <text>增值服务</text>
  109. <u-icon :name="showValueAdded ? 'arrow-up' : 'arrow-down'" size="18" color="#999" @click="showValueAdded = !showValueAdded"></u-icon>
  110. </view>
  111. <!-- 增值服务卡片(折叠内容) -->
  112. <view class="goods-card">
  113. <block v-if="showValueAdded">
  114. <!-- 展开状态:显示所有增值服务项 -->
  115. <view class="goods-item" v-if="product === '1'">
  116. <view class="item-label">包装服务</view>
  117. <view class="item-control">
  118. <switch color="#007AFF" :checked="valueServices.isPack" @change="onPackagingChange" />
  119. </view>
  120. </view>
  121. <view class="goods-item">
  122. <view class="item-label">保价</view>
  123. <view class="item-control">
  124. <switch color="#007AFF" :checked="insuranceAmountChecked" @change="onInsuranceChange" />
  125. </view>
  126. </view>
  127. <view v-if="insuranceAmountChecked" class="goods-item">
  128. <view class="item-label">保价金额(元)</view>
  129. <view class="item-control">
  130. <input class="input-field" placeholder="请输入保价金额" placeholder-class="placeholder"
  131. v-model="insuranceAmount" type="digit" maxlength="10" @input="validateInsuranceAmount" />
  132. </view>
  133. </view>
  134. <!-- 超长超重(顺丰/京东) -->
  135. <!-- <view class="goods-item" v-if="product === '2'">
  136. <view class="item-label">超长超重</view>
  137. <view class="item-control">
  138. <switch color="#007AFF" :checked="valueServices.isOverLongWeight" @change="onOverweightChange" />
  139. </view>
  140. </view> -->
  141. <!-- 签单返还 -->
  142. <view class="goods-item" v-if="product === '1'">
  143. <view class="item-label">签单返还</view>
  144. <view class="item-control">
  145. <switch color="#007AFF" :checked="valueServices.isReceiptCollect" @change="onSignReturnChange" />
  146. </view>
  147. </view>
  148. <!-- 顺丰:签单返还类型选择 -->
  149. <view v-if="product === '2' && valueServices.isReceiptCollect" class="goods-item">
  150. <view class="item-label">返还类型</view>
  151. <view class="item-control">
  152. <picker :range="receiptReturnTypes" :range-key="'label'" @change="onReceiptReturnTypeChange"
  153. :value="receiptReturnTypeIndex">
  154. <view class="picker-text">{{ receiptReturnTypes[receiptReturnTypeIndex]?.label || '请选择返还类型' }}
  155. </view>
  156. </picker>
  157. <u-icon class="arrow" name='arrow-right' size="18"></u-icon>
  158. </view>
  159. </view>
  160. <!-- 打木架(仅顺丰) -->
  161. <!-- <view v-if="product === '2'" class="goods-item">
  162. <view class="item-label">打木架</view>
  163. <view class="item-control">
  164. <switch color="#007AFF" :checked="valueServices.isWoodenCrate" @change="onWoodenFrameChange" />
  165. </view>
  166. </view> -->
  167. </block>
  168. <block v-else>
  169. <!-- 折叠状态:显示一个可点击的展开提示行 -->
  170. <view class="goods-item" @click="showValueAdded = true">
  171. <view class="item-label">增值服务</view>
  172. <view class="item-control">
  173. <text class="picker-text">点击展开</text>
  174. <u-icon name="arrow-down" size="18" color="#999"></u-icon>
  175. </view>
  176. </view>
  177. </block>
  178. </view>
  179. <!-- 协议同意 -->
  180. <view class="agreement-card">
  181. <!-- <view class="agreement-item">
  182. <checkbox color="#1B64F0" :checked="agreed" @click="toggleAgreement" style="transform:scale(0.8)" />
  183. <text class="agreement-text">我已理解并同意遵守《快件服务协议》</text>
  184. </view> -->
  185. <view class="price-notice">
  186. 实际费用以快递员核实为准
  187. </view>
  188. </view>
  189. <view class="add-btn-container">
  190. <!-- 下单按钮 -->
  191. <view class="submit-btn" :class="{ disabled: !agreed }" :disabled="!agreed" @click="submitOrder">
  192. 提交订单
  193. </view>
  194. </view>
  195. <!-- 安全区域占位 -->
  196. <view class="safe-area"></view>
  197. <!-- 时间选择弹窗 -->
  198. <TimePopup :visible="showTimePicker" @close="showTimePicker = false" @confirm="handleTimeConfirm" />
  199. </view>
  200. </template>
  201. <script setup>
  202. import {
  203. ref,
  204. computed,
  205. onMounted,
  206. onUnmounted,
  207. watch,
  208. } from 'vue'
  209. import {
  210. onLoad
  211. } from '@dcloudio/uni-app'
  212. import TimePopup from './components/TimePopup.vue'
  213. import AddressInfo from '@/components/AddressInfo.vue'
  214. import {
  215. createOrder,
  216. dictList
  217. } from '../../api/order'
  218. import {
  219. getDefaultAddress
  220. } from '../../api/address'
  221. // ==================== 数据存储 ====================
  222. const getAddressFromStorage = (key) => {
  223. const address = uni.getStorageSync(key)
  224. return address || {
  225. id: '',
  226. name: '',
  227. phone: '',
  228. address: '',
  229. provinceName: '',
  230. cityName: '',
  231. countyName: '',
  232. isDefault: false
  233. }
  234. }
  235. const saveAddressToStorage = (key, address) => {
  236. uni.setStorageSync(key, address)
  237. }
  238. // ==================== 页面参数 ====================
  239. const product = ref('1') // 1-京东,2-顺丰
  240. // ==================== 地址信息 ====================
  241. const addressSend = ref(getAddressFromStorage('senderAddress'))
  242. const addressReceive = ref(getAddressFromStorage('receiverAddress'))
  243. // ==================== 物品信息 ====================
  244. const goodsInfo = ref('')
  245. const weight = ref(0.5)
  246. const volume = ref(0.001)
  247. const quantity = ref(1)
  248. // ==================== 时间选择 ====================
  249. const showTimePicker = ref(false)
  250. const selectedTimeData = ref({
  251. startTime:undefined,
  252. endTime:undefined
  253. })
  254. const selectedTimeLabel = computed(() => {
  255. if (!selectedTimeData.value.startTime) return '一小时内'
  256. const {
  257. dateLabel,
  258. timeLabel,
  259. isToday,
  260. isImmediate
  261. } = selectedTimeData.value
  262. if (isToday && isImmediate) return '一小时内'
  263. return `${dateLabel} ${timeLabel}`
  264. })
  265. // ==================== 快递类型字典(动态获取)====================
  266. const jdDictList = ref([]) // 京东快递类型字典
  267. const sfDictList = ref([]) // 顺丰快递类型字典
  268. const expressTypeIndex = ref(0)
  269. const expressTypes = ref([]) // 当前显示的快递类型列表(已转换为 {label, value} 格式)
  270. // 从后端获取字典数据
  271. const fetchDictData = async () => {
  272. try {
  273. uni.showLoading({
  274. title: '加载字典...'
  275. })
  276. // 并发请求两个字典
  277. const [jdRes, sfRes] = await Promise.all([
  278. dictList('jd_logistics_product_code'),
  279. dictList('sf_logistics_product_code')
  280. ])
  281. if (jdRes.code === 200) {
  282. jdDictList.value = jdRes.data || []
  283. }
  284. if (sfRes.code === 200) {
  285. sfDictList.value = sfRes.data || []
  286. }
  287. updateExpressTypes()
  288. } catch (error) {
  289. console.error('获取快递类型字典失败', error)
  290. uni.showToast({
  291. title: '字典加载失败',
  292. icon: 'none'
  293. })
  294. } finally {
  295. uni.hideLoading()
  296. }
  297. }
  298. // 根据 product 更新 expressTypes
  299. const updateExpressTypes = () => {
  300. let sourceList = []
  301. if (product.value === '1') {
  302. sourceList = jdDictList.value
  303. } else if (product.value === '2') {
  304. sourceList = sfDictList.value
  305. }
  306. // 转换为 picker 需要的格式:{ label: dictLabel, value: dictValue }
  307. expressTypes.value = sourceList.map(item => ({
  308. label: item.dictLabel,
  309. value: item.dictValue
  310. }))
  311. // 重置选中索引
  312. expressTypeIndex.value = 0
  313. }
  314. // ==================== 增值服务 ====================
  315. const valueServices = ref({
  316. isPack: false,
  317. isOverLongWeight: false,
  318. isReceiptCollect: false,
  319. isWoodenCrate: false
  320. })
  321. // 保价金额
  322. const insuranceAmountChecked = ref(false)
  323. const insuranceAmount = ref('')
  324. const insuranceAmountError = ref('')
  325. // 签单返还类型(顺丰)
  326. const receiptReturnTypeIndex = ref(0)
  327. const receiptReturnTypes = ref([{
  328. label: '电子凭证',
  329. value: 'electronic'
  330. },
  331. {
  332. label: '纸质凭证',
  333. value: 'paper'
  334. }
  335. ])
  336. // ==================== 折叠控制 ====================
  337. const showValueAdded = ref(false) // 默认折叠
  338. // ==================== 协议 ====================
  339. const agreed = ref(true)
  340. // ==================== 表单操作 ====================
  341. // 重量
  342. const increaseWeight = () => {
  343. weight.value = parseFloat((weight.value + 0.1).toFixed(3))
  344. if (weight.value > 100) weight.value = 100
  345. }
  346. const decreaseWeight = () => {
  347. weight.value = parseFloat((weight.value - 0.1).toFixed(3))
  348. if (weight.value < 0.1) weight.value = 0.1
  349. }
  350. const handleWeightInput = (e) => {
  351. let value = e.detail.value.replace(/[^\d.]/g, '')
  352. const parts = value.split('.')
  353. if (parts.length > 2) value = parts[0] + '.' + parts.slice(1).join('')
  354. if (value.includes('.')) {
  355. const decimal = value.split('.')[1]
  356. if (decimal && decimal.length > 3) value = parseFloat(value).toFixed(3)
  357. }
  358. const num = parseFloat(value) || 0.1
  359. weight.value = Math.max(0.1, Math.min(100, num))
  360. }
  361. // 体积
  362. const increaseVolume = () => {
  363. volume.value = parseFloat((volume.value + 0.001).toFixed(3))
  364. // if (volume.value > 10) volume.value = 10
  365. }
  366. const decreaseVolume = () => {
  367. volume.value = parseFloat((volume.value - 0.001).toFixed(3))
  368. if (volume.value < 0.001) volume.value = 0.001
  369. }
  370. const handleVolumeInput = (e) => {
  371. let value = e.detail.value.replace(/[^\d.]/g, '')
  372. const parts = value.split('.')
  373. if (parts.length > 2) value = parts[0] + '.' + parts.slice(1).join('')
  374. if (value.includes('.')) {
  375. const decimal = value.split('.')[1]
  376. if (decimal && decimal.length > 3) value = parseFloat(value).toFixed(3)
  377. }
  378. const num = parseFloat(value) || 0.001
  379. volume.value = Math.max(0.001, Math.min(10, num))
  380. }
  381. // 件数
  382. const increaseQuantity = () => {
  383. quantity.value += 1
  384. // if (quantity.value > 99) quantity.value = 99
  385. }
  386. const decreaseQuantity = () => {
  387. quantity.value -= 1
  388. if (quantity.value < 1) quantity.value = 1
  389. }
  390. const handleQuantityInput = (e) => {
  391. let value = e.detail.value.replace(/\D/g, '')
  392. const int = parseInt(value) || 1
  393. // quantity.value = Math.max(1, Math.min(99, int))
  394. }
  395. // 物品信息
  396. const onGoodsInfoInput = (e) => {
  397. goodsInfo.value = e.detail.value
  398. if (goodsInfo.value.length > 50) goodsInfo.value = goodsInfo.value.substring(0, 50)
  399. }
  400. // 快递类型
  401. const onExpressTypeChange = (e) => {
  402. expressTypeIndex.value = e.detail.value
  403. }
  404. // 增值服务开关
  405. const onPackagingChange = (e) => {
  406. valueServices.value.isPack = e.detail.value
  407. }
  408. const onOverweightChange = (e) => {
  409. valueServices.value.isOverLongWeight = e.detail.value
  410. }
  411. const onWoodenFrameChange = (e) => {
  412. valueServices.value.isWoodenCrate = e.detail.value
  413. }
  414. const onInsuranceChange = (e) => {
  415. insuranceAmountChecked.value = e.detail.value
  416. if (e.detail.value) {
  417. insuranceAmount.value = '0'
  418. } else {
  419. insuranceAmount.value = ''
  420. insuranceAmountError.value = ''
  421. }
  422. }
  423. const onSignReturnChange = (e) => {
  424. valueServices.value.isReceiptCollect = e.detail.value
  425. if (!e.detail.value) {
  426. receiptReturnTypeIndex.value = 0
  427. }
  428. }
  429. const onReceiptReturnTypeChange = (e) => {
  430. receiptReturnTypeIndex.value = e.detail.value
  431. }
  432. // 保价金额验证
  433. const validateInsuranceAmount = () => {
  434. if (!insuranceAmount.value) return true
  435. const amount = parseFloat(insuranceAmount.value)
  436. if (isNaN(amount) || amount < 0) {
  437. insuranceAmountError.value = '保价金额必须为数字且大于0'
  438. return false
  439. }
  440. // if (amount > 1000000) {
  441. // insuranceAmountError.value = '保价金额不能超过100万'
  442. // return false
  443. // }
  444. const decimal = insuranceAmount.value.toString().split('.')[1]
  445. if (decimal && decimal.length > 2) {
  446. insuranceAmountError.value = '最多两位小数'
  447. return false
  448. }
  449. insuranceAmountError.value = ''
  450. return true
  451. }
  452. // 时间选择
  453. const handleTimeClick = () => {
  454. showTimePicker.value = true
  455. }
  456. const handleTimeConfirm = (time) => {
  457. selectedTimeData.value = time
  458. showTimePicker.value = false
  459. }
  460. // 地址簿相关
  461. const openAddressBook = (type) => {
  462. uni.navigateTo({
  463. url: `/pages/address/address_list?addType=${type}&from=order`
  464. })
  465. }
  466. const handleAddAddress = (type) => {
  467. uni.navigateTo({
  468. url: `/pages/address/edit?addType=${type}&from=order`
  469. })
  470. }
  471. // 协议切换
  472. const toggleAgreement = () => {
  473. agreed.value = !agreed.value
  474. }
  475. // ==================== 表单验证(增强) ====================
  476. const validateForm = () => {
  477. // 地址
  478. if (!addressSend.value.addressId) {
  479. uni.showToast({
  480. title: '请选择寄件人地址',
  481. icon: 'none'
  482. });
  483. return false
  484. }
  485. if (!addressReceive.value.addressId) {
  486. uni.showToast({
  487. title: '请选择收件人地址',
  488. icon: 'none'
  489. });
  490. return false
  491. }
  492. // 时间
  493. if (!selectedTimeData.value.startTime) {
  494. // 这没有数据的时候 默认一个小时内 获取当前时间
  495. selectedTimeData.value.startTime = twentyMinutesLater()
  496. selectedTimeData.value.endTime = oneHourLater()
  497. }
  498. if (isBeforeNineAM(selectedTimeData.value.startTime) || isAfterEightPM(selectedTimeData.value.startTime)) {
  499. // 不在工作时间内
  500. uni.showToast({
  501. title: '请选择合适的寄件时间',
  502. icon: 'none'
  503. });
  504. selectedTimeData.value.startTime = undefined
  505. selectedTimeData.value.endTime = undefined
  506. return false
  507. }
  508. if (isAfterEightPM(selectedTimeData.value.endTime)) {
  509. uni.showToast({
  510. title: '请选择合适的寄件时间',
  511. icon: 'none'
  512. });
  513. selectedTimeData.value.startTime = undefined
  514. selectedTimeData.value.endTime = undefined
  515. return false
  516. }
  517. // 快递类型
  518. if (!expressTypes.value.length) {
  519. uni.showToast({
  520. title: '快递类型字典加载中,请稍后',
  521. icon: 'none'
  522. });
  523. return false
  524. }
  525. if (!expressTypes.value[expressTypeIndex.value]) {
  526. uni.showToast({
  527. title: '请选择快递类型',
  528. icon: 'none'
  529. });
  530. return false
  531. }
  532. // 物品信息
  533. if (!goodsInfo.value.trim()) {
  534. uni.showToast({
  535. title: '请输入物品信息',
  536. icon: 'none'
  537. });
  538. return false
  539. }
  540. if (goodsInfo.value.length > 50) {
  541. uni.showToast({
  542. title: '物品信息不能超过50字',
  543. icon: 'none'
  544. });
  545. return false
  546. }
  547. // 重量/体积/件数范围
  548. if (weight.value < 0) {
  549. uni.showToast({
  550. title: '请输入正确的重量',
  551. icon: 'none'
  552. });
  553. return false
  554. }
  555. if (volume.value < 0) {
  556. uni.showToast({
  557. title: '请输入正确的体积',
  558. icon: 'none'
  559. });
  560. return false
  561. }
  562. if (quantity.value < 1) {
  563. uni.showToast({
  564. title: '请输入正确的件数',
  565. icon: 'none'
  566. });
  567. return false
  568. }
  569. // 保价金额验证
  570. if (insuranceAmount.value !== '' && insuranceAmount.value !== null) {
  571. if (!validateInsuranceAmount()) {
  572. uni.showToast({
  573. title: insuranceAmountError.value || '保价金额无效',
  574. icon: 'none'
  575. })
  576. return false
  577. }
  578. }
  579. // 顺丰签单返还类型
  580. if (product.value === '2' && valueServices.value.isReceiptCollect && !receiptReturnTypes.value[
  581. receiptReturnTypeIndex.value]) {
  582. uni.showToast({
  583. title: '请选择签单返还类型',
  584. icon: 'none'
  585. });
  586. return false
  587. }
  588. // 协议
  589. if (!agreed.value) {
  590. uni.showToast({
  591. title: '请同意服务协议',
  592. icon: 'none'
  593. });
  594. return false
  595. }
  596. return true
  597. }
  598. const oneHourLater = () => {
  599. const now = new Date()
  600. return now.getHours() + now.getMinutes() / 60 + 1
  601. }
  602. const twentyMinutesLater = () => {
  603. const now = new Date()
  604. now.setMinutes(now.getMinutes() + 1)
  605. return now
  606. }
  607. function isBeforeNineAM(date) {
  608. return date.getHours() < 9;
  609. }
  610. function isAfterEightPM(date) {
  611. return date.getHours() > 20 || (date.getHours() === 20 && date.getMinutes() > 0);
  612. }
  613. const isSubmit = ref(false)
  614. // ==================== 提交订单 ====================
  615. const submitOrder = async () => {
  616. if (!validateForm()) return
  617. if (isSubmit.value) return
  618. isSubmit.value = true
  619. // 构建增值服务JSON
  620. const addedService = {
  621. isPack: valueServices.value.isPack || null,
  622. guaranteeMoney: insuranceAmount.value && insuranceAmount.value !== '' ? parseFloat(insuranceAmount
  623. .value) : null,
  624. isReceiptCollect: product.value === '2' ?
  625. (valueServices.value.isReceiptCollect ? receiptReturnTypes.value[receiptReturnTypeIndex.value]
  626. ?.value : null) : (valueServices.value.isReceiptCollect ? true : null),
  627. isOverLongWeight: valueServices.value.isOverLongWeight || null,
  628. isWoodenCrate: valueServices.value.isWoodenCrate || null
  629. }
  630. // 移除null字段
  631. Object.keys(addedService).forEach(key => {
  632. if (addedService[key] === null || addedService[key] === undefined) {
  633. delete addedService[key]
  634. }
  635. })
  636. const orderData = {
  637. orderType: product.value,
  638. senderName: addressSend.value.contactName,
  639. senderPhone: addressSend.value.contactPhone,
  640. senderProvince: addressSend.value.provinceName,
  641. senderCity: addressSend.value.cityName,
  642. senderCounty: addressSend.value.countyName,
  643. senderAddress: addressSend.value.detailedAddress,
  644. receiverName: addressReceive.value.contactName,
  645. receiverPhone: addressReceive.value.contactPhone,
  646. receiverProvince: addressReceive.value.provinceName,
  647. receiverCity: addressReceive.value.cityName,
  648. receiverCounty: addressReceive.value.countyName,
  649. receiverAddress: addressReceive.value.detailedAddress,
  650. goodsName: goodsInfo.value,
  651. goodsWeight: weight.value,
  652. goodsVolume: volume.value,
  653. goodsQty: quantity.value,
  654. sendStartTime: selectedTimeData.value.startTime,
  655. sendEndTime: selectedTimeData.value.endTime,
  656. productCode: expressTypes.value[expressTypeIndex.value]?.value, // 直接从动态字典取 dictValue
  657. addedService: JSON.stringify(addedService)
  658. }
  659. uni.showLoading({
  660. title: '提交中...',
  661. mask: true
  662. })
  663. try {
  664. const res = await createOrder(orderData)
  665. uni.hideLoading()
  666. if (res.code === 200) {
  667. uni.showToast({
  668. title: '下单成功',
  669. icon: 'success',
  670. mask: true
  671. })
  672. uni.switchTab({
  673. url: '/pages/order/index'
  674. })
  675. } else {
  676. isSubmit.value = false
  677. uni.showToast({
  678. title: res.msg || '下单失败',
  679. icon: 'none'
  680. })
  681. }
  682. } catch (error) {
  683. isSubmit.value = false
  684. uni.showToast({
  685. title: error,
  686. icon: 'none'
  687. })
  688. console.error('订单提交失败', error)
  689. } finally {
  690. // uni.hideLoading()
  691. }
  692. }
  693. // ==================== 获取默认寄件地址 ====================
  694. const fetchDefaultSenderAddress = async () => {
  695. try {
  696. const res = await getDefaultAddress()
  697. if (res.code === 200 && res.data) {
  698. addressSend.value = res.data
  699. addressSend.value.defaultFlag = res.data.defaultFlag === '1'
  700. saveAddressToStorage('senderAddress', res.data)
  701. }
  702. } catch (error) {
  703. console.error('获取默认地址失败', error)
  704. }
  705. }
  706. // ==================== 生命周期 ====================
  707. onLoad((option) => {
  708. if (option.product) {
  709. product.value = option.product
  710. }
  711. // 获取快递类型字典
  712. fetchDictData()
  713. })
  714. onMounted(() => {
  715. // 监听地址选择返回
  716. uni.$on('addressSelected', (data) => {
  717. if (data.type === 'sender') {
  718. addressSend.value = data.address
  719. saveAddressToStorage('senderAddress', data.address)
  720. } else if (data.type === 'receiver') {
  721. addressReceive.value = data.address
  722. saveAddressToStorage('receiverAddress', data.address)
  723. }
  724. })
  725. if (!addressSend.addressId) {
  726. fetchDefaultSenderAddress()
  727. }
  728. })
  729. // 组件卸载时取消监听
  730. onUnmounted(() => {
  731. uni.$off('addressSelected')
  732. })
  733. </script>
  734. <style lang="scss" scoped>
  735. /* 完全保持原有样式,未做任何修改 */
  736. .order-container {
  737. min-height: 100vh;
  738. background-color: #F5F7FA;
  739. padding-bottom: 180rpx;
  740. box-sizing: border-box;
  741. }
  742. .info-card {
  743. margin: 0rpx 20rpx;
  744. background: #fff;
  745. border-radius: 32rpx;
  746. padding: 20rpx;
  747. margin-bottom: 20rpx;
  748. .address-item {
  749. display: flex;
  750. align-items: center;
  751. &:first-child {
  752. padding-bottom: 20rpx;
  753. }
  754. &:last-child {
  755. padding-top: 20rpx;
  756. }
  757. }
  758. .user-info {
  759. flex: 1;
  760. margin-left: 26rpx;
  761. .line {
  762. width: 590rpx;
  763. height: 3rpx;
  764. background-color: #F1F3F8;
  765. }
  766. .create-btn {
  767. height: 88rpx;
  768. font-weight: 400;
  769. font-size: 40rpx;
  770. color: #999999;
  771. line-height: 88rpx;
  772. }
  773. }
  774. .right {
  775. margin-left: 20rpx;
  776. }
  777. .img-status-text {
  778. align-self: center;
  779. text-align: center;
  780. flex-shrink: 0;
  781. .img {
  782. width: 56rpx;
  783. height: 56rpx;
  784. }
  785. .img-change {
  786. width: 40rpx;
  787. height: 40rpx;
  788. }
  789. .address-image {
  790. width: 48rpx;
  791. height: 48rpx;
  792. }
  793. .status-text {
  794. font-size: 28rpx;
  795. color: #333;
  796. font-weight: 400;
  797. }
  798. }
  799. }
  800. .pickup-title {
  801. margin: 0rpx 20rpx;
  802. font-size: 32rpx;
  803. font-weight: 600;
  804. color: #333;
  805. margin-bottom: 20rpx;
  806. }
  807. .goods-card {
  808. margin: 0rpx 20rpx;
  809. background-color: #fff;
  810. border-radius: 32rpx;
  811. padding: 0rpx 20rpx;
  812. margin-bottom: 20rpx;
  813. }
  814. .goods-item {
  815. display: flex;
  816. justify-content: space-between;
  817. align-items: center;
  818. height: 100rpx;
  819. border-bottom: 1rpx solid #F1F3F8;
  820. &:last-child {
  821. border-bottom: none;
  822. margin-bottom: 0;
  823. }
  824. .item-label {
  825. font-size: 28rpx;
  826. color: #666;
  827. font-weight: 400;
  828. &.required::before {
  829. content: '*';
  830. color: #ff4444;
  831. margin-right: 8rpx;
  832. }
  833. }
  834. .item-control {
  835. display: flex;
  836. align-items: center;
  837. .picker-text {
  838. font-size: 28rpx;
  839. color: #333;
  840. text-align: right;
  841. margin-right: 8rpx;
  842. }
  843. &.btn {
  844. background: #fff;
  845. border: 1rpx solid #dcdfe6;
  846. border-radius: 5rpx;
  847. height: 59rpx;
  848. width: 266rpx;
  849. box-sizing: border-box;
  850. }
  851. .control-btn {
  852. width: 56rpx;
  853. height: 56rpx;
  854. background: #F5F7FA;
  855. border-radius: 4rpx;
  856. display: flex;
  857. align-items: center;
  858. justify-content: center;
  859. font-size: 36rpx;
  860. color: #666;
  861. &.minus {
  862. color: #999;
  863. border-right: 1rpx solid #dcdfe6;
  864. }
  865. &.plus {
  866. color: #999;
  867. border-left: 1rpx solid #dcdfe6;
  868. }
  869. }
  870. .control-value {
  871. width: 154rpx;
  872. text-align: center;
  873. font-size: 24rpx;
  874. color: #333;
  875. .input-field {
  876. width: 100%;
  877. height: 100rpx;
  878. line-height: 100rpx;
  879. padding: 0 24rpx;
  880. font-size: 28rpx;
  881. color: #333;
  882. text-align: center;
  883. &::placeholder {
  884. color: #999;
  885. }
  886. }
  887. }
  888. }
  889. .input-field {
  890. width: 100%;
  891. height: 100rpx;
  892. line-height: 100rpx;
  893. padding: 0 24rpx;
  894. font-size: 28rpx;
  895. color: #333;
  896. text-align: right;
  897. &::placeholder {
  898. color: #999;
  899. }
  900. }
  901. .time-value {
  902. display: flex;
  903. align-items: center;
  904. font-size: 28rpx;
  905. color: #333;
  906. line-height: 88rpx;
  907. height: 88rpx;
  908. .value {
  909. margin-right: 16rpx;
  910. }
  911. .arrow {
  912. margin-left: 32rpx;
  913. color: #999;
  914. font-size: 28rpx;
  915. font-weight: bold;
  916. }
  917. }
  918. .arrow {
  919. color: #999;
  920. }
  921. }
  922. .agreement-card {
  923. margin: 0rpx 20rpx;
  924. background-color: #fff;
  925. border-radius: 32rpx;
  926. padding: 30rpx 20rpx;
  927. margin-bottom: 20rpx;
  928. .agreement-item {
  929. display: flex;
  930. align-items: center;
  931. margin-bottom: 20rpx;
  932. .agreement-text {
  933. font-size: 28rpx;
  934. color: #333;
  935. margin-left: 10rpx;
  936. }
  937. }
  938. .price-notice {
  939. font-size: 24rpx;
  940. color: #ff7d00;
  941. text-align: center;
  942. }
  943. }
  944. .add-btn-container {
  945. width: 100%;
  946. position: fixed;
  947. bottom: 0rpx;
  948. padding: 32rpx;
  949. background-color: #fff;
  950. border-top: 1rpx solid #eee;
  951. box-sizing: border-box;
  952. /* 确定按钮 */
  953. .submit-btn {
  954. height: 88rpx;
  955. background: #1B64F0;
  956. border-radius: 44rpx;
  957. color: #fff;
  958. font-size: 32rpx;
  959. font-weight: 500;
  960. height: 88rpx;
  961. line-height: 88rpx;
  962. text-align: center
  963. }
  964. }
  965. .safe-area {
  966. height: 140rpx;
  967. }
  968. </style>