edit.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. <template>
  2. <view class="container">
  3. <!-- 物品信息 -->
  4. <view class="form-card">
  5. <view class="form-item">
  6. <view class="item-label">
  7. <input class="input-field" placeholder="姓名" placeholder-class="placeholder" maxlength="10"
  8. v-model="formData.contactName" />
  9. </view>
  10. <view class="item-control">
  11. <input class="input-field" placeholder="电话" placeholder-class="placeholder" maxlength="11"
  12. v-model="formData.contactPhone" />
  13. </view>
  14. </view>
  15. <picker mode="region" @change="changeAddress">
  16. <view class="form-item form-items">
  17. <text
  18. class="time-value placeholder">{{ formData.provinceName ? formData.provinceName + formData.cityName + formData.countyName : '省市区' }}</text>
  19. <text class="time-value value">{{ selectedTime }}</text>
  20. <u-icon class="arrow" name='arrow-right' size="18"></u-icon>
  21. </view>
  22. </picker>
  23. <view class="form-item">
  24. <view class="item-control">
  25. <input class="input-field" placeholder="详细地址" placeholder-class="placeholder"
  26. v-model="formData.detailedAddress" />
  27. </view>
  28. </view>
  29. <view class="form-item">
  30. <!-- 设置默认和清空 -->
  31. <view class="form-actions">
  32. <view class="action-left">
  33. <switch :checked="formData.defaultFlag == 1" color="#007AFF" @change="onDefaultChange" />
  34. <text class="action-text">设为默认寄件地址</text>
  35. </view>
  36. <view class="action-right" @click="clearForm">
  37. <text class="clear-text">清空</text>
  38. </view>
  39. </view>
  40. </view>
  41. </view>
  42. <view class="section-title" v-if="false">最近使用地址</view>
  43. <!-- 最近使用地址 -->
  44. <view class="recent-address" v-if="false">
  45. <!-- 地址项1 -->
  46. <view class="address-item" @click="onSelectAddress(addressItem)">
  47. <AddressInfo v-if="formData.id" :address="addressSend" />
  48. </view>
  49. </view>
  50. <!-- 下单按钮 -->
  51. <view class="submit-btn" @click="onConfirm">
  52. 确定
  53. </view>
  54. <!-- 安全区域占位 -->
  55. <view class="safe-area"></view>
  56. </view>
  57. </template>
  58. <script setup>
  59. import {
  60. ref,
  61. reactive
  62. } from 'vue'
  63. import {
  64. onLoad
  65. } from '@dcloudio/uni-app'
  66. import AddressInfo from '@/components/AddressInfo.vue'
  67. import {
  68. addBook,
  69. updateBook,
  70. getBook
  71. } from '@/api/address.js'
  72. const addType = ref('') //1 表示从创建订单过来 2 表示编辑 3表示创建
  73. // 表单数据
  74. const formData = reactive({
  75. contactName: '', //姓名
  76. contactPhone: '', //电话
  77. provinceName: '', //省
  78. cityName: '', //市
  79. countyName: '', //区
  80. addressId: null, //地址ID
  81. detailedAddress: '', //详细地址
  82. defaultFlag: 0 //是否默认 0非默认1默认
  83. })
  84. // 最近地址列表
  85. const recentAddresses = ref([{
  86. name: '袁添昊',
  87. phone: '13344642161',
  88. address: '湖北省荆州市新石南路747号'
  89. },
  90. {
  91. name: '袁添昊',
  92. phone: '13344642161',
  93. address: '湖北省荆州市新石南路747号'
  94. }
  95. ])
  96. onLoad((option) => {
  97. if (option.id) {
  98. formData.addressId = option.id;
  99. getAddress()
  100. uni.setNavigationBarTitle({
  101. title:'编辑地址'
  102. })
  103. }
  104. addType.value = option.addType
  105. })
  106. const getAddress = async () => {
  107. let res = await getBook(formData.addressId);
  108. console.log(res.data, formData);
  109. let {
  110. addressId,
  111. contactName,
  112. contactPhone,
  113. countyName,
  114. defaultFlag,
  115. detailedAddress,
  116. provinceName,
  117. cityName
  118. } = res.data;
  119. // formData = {addressId,contactName,contactPhone,countyName,defaultFlag,detailedAddress,provinceName}
  120. Object.assign(formData, {
  121. addressId,
  122. contactName,
  123. contactPhone,
  124. cityName,
  125. countyName,
  126. defaultFlag,
  127. detailedAddress,
  128. provinceName
  129. });
  130. }
  131. // 选择地址
  132. const changeAddress = (e) => {
  133. formData.provinceName = e.detail.value[0]
  134. formData.cityName = e.detail.value[1]
  135. formData.countyName = e.detail.value[2]
  136. }
  137. // 省市区选择
  138. const onRegionChange = (e) => {
  139. formData.region = e.detail.value
  140. }
  141. // 默认地址切换
  142. const onDefaultChange = (e) => {
  143. formData.defaultFlag = e.detail.value ? 1 : 0
  144. }
  145. // 清空表单
  146. const clearForm = () => {
  147. formData.contactName = ''
  148. formData.contactPhone = ''
  149. formData.provinceName = ''
  150. formData.cityName = ''
  151. formData.countyName = ''
  152. formData.detailedAddress = ''
  153. formData.defaultFlag = 0
  154. }
  155. // 确定提交
  156. const onConfirm = async () => {
  157. // 这里可以添加表单验证
  158. if (!formData.contactName || !formData.contactPhone || !formData.detailedAddress) {
  159. uni.showToast({
  160. title: '请填写完整信息',
  161. icon: 'none'
  162. })
  163. return
  164. }
  165. if (!formData.provinceName) return uni.showToast({
  166. title: '请选择省市区',
  167. icon: 'none'
  168. })
  169. // 提交逻辑
  170. let api = formData.addressId ? updateBook(formData) : addBook(formData)
  171. let res = await api;
  172. console.log('提交数据:', formData)
  173. uni.showToast({
  174. title: '提交成功',
  175. icon: 'success'
  176. })
  177. if(res.code == 200){
  178. setTimeout(() => {
  179. // 返回上一页或执行其他操作
  180. formData.addressId = '-1'
  181. onSelectAddress(formData)
  182. uni.navigateBack() // 返回上一页
  183. }, 400)
  184. }
  185. }
  186. const onSelectAddress = (address) => {
  187. console.log('-------address---',address)
  188. if (addType.value != 'sender' && addType.value != 'receiver') {
  189. return
  190. }
  191. uni.$emit('addressSelected', {
  192. type:addType.value, // 'sender' 或 'receiver'
  193. address
  194. })
  195. }
  196. </script>
  197. <style scoped lang="less">
  198. .container {
  199. background-color: #F5F7FA;
  200. min-height: 100vh;
  201. padding: 20rpx 20rpx 30rpx 20rpx;
  202. }
  203. .form-card {
  204. background-color: #fff;
  205. border-radius: 32rpx;
  206. padding: 0rpx 20rpx;
  207. }
  208. .form-title {
  209. font-size: 32rpx;
  210. font-weight: 600;
  211. color: #333;
  212. margin-bottom: 32rpx;
  213. }
  214. .form-item {
  215. display: flex;
  216. justify-content: space-between;
  217. align-items: center;
  218. height: 100rpx;
  219. border-bottom: 1rpx solid #F1F3F8;
  220. &:last-child {
  221. border-bottom: none;
  222. margin-bottom: 0;
  223. }
  224. .item-label {
  225. font-size: 28rpx;
  226. color: #666;
  227. font-weight: 400;
  228. &.required::before {
  229. content: '*';
  230. color: #ff4444;
  231. margin-right: 8rpx;
  232. }
  233. }
  234. .item-control {
  235. display: flex;
  236. align-items: center;
  237. &.btn {
  238. background: #fff;
  239. border: 1rpx solid #dcdfe6;
  240. border-radius: 5rpx;
  241. height: 59rpx;
  242. width: 266rpx;
  243. box-sizing: border-box;
  244. }
  245. }
  246. .input-field {
  247. width: 100%;
  248. height: 100rpx;
  249. line-height: 100rpx;
  250. padding: 0 24rpx;
  251. font-size: 28rpx;
  252. color: #333;
  253. text-align: left;
  254. &::placeholder {
  255. color: #999;
  256. }
  257. }
  258. .time-value {
  259. display: flex;
  260. align-items: center;
  261. font-size: 28rpx;
  262. color: #333;
  263. line-height: 88rpx;
  264. height: 88rpx;
  265. padding: 0 24rpx;
  266. &.placeholder {
  267. color: #999;
  268. }
  269. .value {
  270. margin-right: 16rpx;
  271. }
  272. .arrow {
  273. margin-left: 32rpx;
  274. color: #999;
  275. font-size: 28rpx;
  276. font-weight: bold;
  277. }
  278. }
  279. }
  280. .form-items {
  281. &:last-child {
  282. border-bottom: 1rpx solid #F1F3F8;
  283. }
  284. }
  285. /* 寄件标识 */
  286. .send-tag {
  287. padding: 40rpx 30rpx 20rpx;
  288. background-color: #fff;
  289. }
  290. .send-circle {
  291. width: 60rpx;
  292. height: 60rpx;
  293. border-radius: 50%;
  294. background-color: #007AFF;
  295. color: #fff;
  296. display: flex;
  297. align-items: center;
  298. justify-content: center;
  299. font-size: 28rpx;
  300. font-weight: bold;
  301. }
  302. /* 表单区域 */
  303. .form-container {
  304. background-color: #fff;
  305. padding: 0 30rpx;
  306. margin-bottom: 20rpx;
  307. }
  308. .form-title {
  309. font-size: 18px;
  310. font-weight: 500;
  311. color: #333;
  312. padding: 30rpx 0 20rpx;
  313. border-bottom: 1rpx solid #eee;
  314. }
  315. .form-item {
  316. width: 100%;
  317. padding: 30rpx 0;
  318. border-bottom: 1rpx solid #eee;
  319. }
  320. .form-input {
  321. font-size: 16px;
  322. height: 40rpx;
  323. line-height: 40rpx;
  324. color: #333;
  325. }
  326. .form-input::placeholder {
  327. color: #999;
  328. }
  329. .picker-placeholder {
  330. font-size: 16px;
  331. height: 40rpx;
  332. line-height: 40rpx;
  333. color: #999;
  334. }
  335. .picker-placeholder.has-value {
  336. color: #333;
  337. }
  338. /* 表单操作 */
  339. .form-actions {
  340. width: 100%;
  341. display: flex;
  342. justify-content: space-between;
  343. align-items: center;
  344. padding: 30rpx 0;
  345. }
  346. .action-left {
  347. display: flex;
  348. align-items: center;
  349. }
  350. .action-text {
  351. font-size: 28rpx;
  352. color: #333;
  353. margin-left: 10rpx;
  354. }
  355. .action-right {
  356. padding: 10rpx 20rpx;
  357. }
  358. .clear-text {
  359. height: 44rpx;
  360. font-weight: 400;
  361. font-size: 28rpx;
  362. color: #1B64F0;
  363. line-height: 44rpx;
  364. }
  365. /* 最近地址 */
  366. .recent-address {
  367. background-color: #fff;
  368. padding: 0 30rpx;
  369. }
  370. .section-title {
  371. height: 48rpx;
  372. font-size: 32rpx;
  373. color: #333333;
  374. line-height: 48rpx;
  375. margin: 20rpx 0rpx;
  376. font-weight: bold;
  377. }
  378. .address-item {
  379. display: flex;
  380. justify-content: space-between;
  381. align-items: center;
  382. padding: 30rpx 0;
  383. border-bottom: 1rpx solid #eee;
  384. }
  385. .address-info {
  386. flex: 1;
  387. }
  388. .address-name {
  389. font-size: 16px;
  390. color: #333;
  391. margin-bottom: 10rpx;
  392. }
  393. .address-detail {
  394. font-size: 14px;
  395. color: #666;
  396. line-height: 1.4;
  397. }
  398. .address-select {
  399. width: 40rpx;
  400. height: 40rpx;
  401. border: 1rpx solid #007AFF;
  402. border-radius: 50%;
  403. margin-left: 20rpx;
  404. }
  405. .safe-area {
  406. height: 140rpx;
  407. }
  408. .submit-btn {
  409. position: fixed;
  410. bottom: 40rpx;
  411. left: 30rpx;
  412. right: 30rpx;
  413. width: 686rpx;
  414. height: 88rpx;
  415. background: #1B64F0;
  416. border-radius: 44rpx;
  417. display: flex;
  418. justify-content: center;
  419. align-items: center;
  420. font-size: 32rpx;
  421. color: #FFFFFF;
  422. line-height: 88rpx;
  423. text-align: center;
  424. z-index: 10;
  425. }
  426. </style>