edit.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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" @tap="selectAddress(0)">
  47. <AddressInfo v-if="formData.id" :address="addressSend" />
  48. <view class="address-info">
  49. <view class="address-name">袁添昊 13344642161</view>
  50. <view class="address-detail">湖北省荆州市新石南路747号</view>
  51. </view>
  52. <!-- <view class="address-select">
  53. </view> -->
  54. </view>
  55. <!-- 地址项2 -->
  56. <view class="address-item" @tap="selectAddress(1)">
  57. <view class="address-info">
  58. <view class="address-name">袁添昊 13344642161</view>
  59. <view class="address-detail">湖北省荆州市新石南路747号</view>
  60. </view>
  61. <!-- <view class="address-select">
  62. </view> -->
  63. </view>
  64. </view>
  65. <!-- 下单按钮 -->
  66. <view class="submit-btn" @click="onConfirm">
  67. 确定
  68. </view>
  69. <!-- 安全区域占位 -->
  70. <view class="safe-area"></view>
  71. </view>
  72. </template>
  73. <script setup>
  74. import {
  75. ref,
  76. reactive
  77. } from 'vue'
  78. import {
  79. onLoad
  80. } from '@dcloudio/uni-app'
  81. import AddressInfo from '@/components/AddressInfo.vue'
  82. import {
  83. addBook,
  84. updateBook,
  85. getBook
  86. } from '@/api/address.js'
  87. const addType = ref('1') //1 表示从创建订单过来 2 表示编辑 3表示创建
  88. // 表单数据
  89. const formData = reactive({
  90. contactName: '', //姓名
  91. contactPhone: '', //电话
  92. provinceName: '', //省
  93. cityName: '', //市
  94. countyName: '', //区
  95. addressId: null, //地址ID
  96. detailedAddress: '', //详细地址
  97. defaultFlag: 0 //是否默认 0非默认1默认
  98. })
  99. // 最近地址列表
  100. const recentAddresses = ref([{
  101. name: '袁添昊',
  102. phone: '13344642161',
  103. address: '湖北省荆州市新石南路747号'
  104. },
  105. {
  106. name: '袁添昊',
  107. phone: '13344642161',
  108. address: '湖北省荆州市新石南路747号'
  109. }
  110. ])
  111. onLoad((option) => {
  112. if (option.id) {
  113. formData.addressId = option.id;
  114. getAddress()
  115. uni.setNavigationBarTitle({
  116. title:'编辑地址'
  117. })
  118. }
  119. })
  120. const getAddress = async () => {
  121. let res = await getBook(formData.addressId);
  122. console.log(res.data, formData);
  123. let {
  124. addressId,
  125. contactName,
  126. contactPhone,
  127. countyName,
  128. defaultFlag,
  129. detailedAddress,
  130. provinceName,
  131. cityName
  132. } = res.data;
  133. // formData = {addressId,contactName,contactPhone,countyName,defaultFlag,detailedAddress,provinceName}
  134. Object.assign(formData, {
  135. addressId,
  136. contactName,
  137. contactPhone,
  138. cityName,
  139. countyName,
  140. defaultFlag,
  141. detailedAddress,
  142. provinceName
  143. });
  144. }
  145. // 选择地址
  146. const changeAddress = (e) => {
  147. formData.provinceName = e.detail.value[0]
  148. formData.cityName = e.detail.value[1]
  149. formData.countyName = e.detail.value[2]
  150. }
  151. // 省市区选择
  152. const onRegionChange = (e) => {
  153. formData.region = e.detail.value
  154. }
  155. // 默认地址切换
  156. const onDefaultChange = (e) => {
  157. formData.defaultFlag = e.detail.value ? 1 : 0
  158. }
  159. // 清空表单
  160. const clearForm = () => {
  161. formData.contactName = ''
  162. formData.contactPhone = ''
  163. formData.provinceName = ''
  164. formData.cityName = ''
  165. formData.countyName = ''
  166. formData.detailedAddress = ''
  167. formData.defaultFlag = 0
  168. }
  169. // 确定提交
  170. const onConfirm = async () => {
  171. // 这里可以添加表单验证
  172. if (!formData.contactName || !formData.contactPhone || !formData.detailedAddress) {
  173. uni.showToast({
  174. title: '请填写完整信息',
  175. icon: 'none'
  176. })
  177. return
  178. }
  179. if (!formData.provinceName) return uni.showToast({
  180. title: '请选择省市区',
  181. icon: 'none'
  182. })
  183. // 提交逻辑
  184. let api = formData.addressId ? updateBook(formData) : addBook(formData)
  185. let res = await api;
  186. console.log('提交数据:', formData)
  187. uni.showToast({
  188. title: '提交成功',
  189. icon: 'success'
  190. })
  191. setTimeout(() => {
  192. // 返回上一页或执行其他操作
  193. uni.navigateBack()
  194. }, 800)
  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>