edit.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <template>
  2. <view class="container">
  3. <!-- 物品信息 -->
  4. <view class="form-card">
  5. <!-- 姓名 + 电话 并排 -->
  6. <view class="form-item row">
  7. <input class="input-field flex-item" placeholder="姓名" placeholder-class="placeholder" maxlength="20"
  8. v-model="formData.contactName" />
  9. <input class="input-field flex-item" placeholder="电话" placeholder-class="placeholder" maxlength="11"
  10. v-model="formData.contactPhone" />
  11. </view>
  12. <!-- 省市区选择 -->
  13. <picker mode="region" @change="changeAddress">
  14. <view class="form-item">
  15. <text :class="['address-text', formData.provinceName ? '' : 'placeholder']">
  16. {{ formData.provinceName ? formData.provinceName + ' ' + formData.cityName + ' ' + formData.countyName : '省市区' }}
  17. </text>
  18. <u-icon class="arrow" name='arrow-right' size="18"></u-icon>
  19. </view>
  20. </picker>
  21. <!-- 详细地址 -->
  22. <view class="form-item">
  23. <input class="input-field full-width" placeholder="详细地址" placeholder-class="placeholder"
  24. v-model="formData.detailedAddress" />
  25. </view>
  26. <!-- 设置默认和清空 -->
  27. <view class="form-item actions">
  28. <view class="action-left">
  29. <switch :checked="formData.defaultFlag == 1" color="#007AFF" @change="onDefaultChange" />
  30. <text class="action-text">设为默认寄件地址</text>
  31. </view>
  32. <view class="action-right" @click="clearForm">
  33. <text class="clear-text">清空</text>
  34. </view>
  35. </view>
  36. </view>
  37. <view class="section-title" v-if="addressList.length > 0">最近使用地址</view>
  38. <!-- 最近使用地址 -->
  39. <view class="recent-address" v-if="addressList.length > 0">
  40. <view class="address-item" v-for="(item, index) in addressList" :key="item.id"
  41. @click="onSelectAddress(item)">
  42. <AddressInfo :address="item" />
  43. </view>
  44. </view>
  45. <!-- 确定按钮 -->
  46. <view class="submit-btn" @click="onConfirm">确定</view>
  47. <!-- 安全区域占位 -->
  48. <view class="safe-area"></view>
  49. </view>
  50. </template>
  51. <script setup>
  52. import {
  53. ref,
  54. reactive
  55. } from 'vue'
  56. import {
  57. onLoad
  58. } from '@dcloudio/uni-app'
  59. import AddressInfo from '@/components/AddressInfo.vue'
  60. import {
  61. addBook,
  62. updateBook,
  63. getBook,getLastAddress
  64. } from '@/api/address.js'
  65. const addType = ref('') // 'sender' 或 'receiver',表示从下单页面哪个字段进入
  66. const addressList = ref([])
  67. // 表单数据
  68. const formData = reactive({
  69. addressId:undefined,
  70. contactName: '',
  71. contactPhone: '',
  72. provinceName: '',
  73. cityName: '',
  74. countyName: '',
  75. addressId: null,
  76. detailedAddress: '',
  77. defaultFlag: 0
  78. })
  79. onLoad((option) => {
  80. if (option.id) {
  81. formData.addressId = option.id;
  82. getAddress();
  83. uni.setNavigationBarTitle({
  84. title: '编辑地址'
  85. });
  86. }
  87. addType.value = option.addType || '';
  88. getAddressList()
  89. });
  90. // 获取地址详情
  91. const getAddress = async () => {
  92. let res = await getBook(formData.addressId);
  93. let {
  94. addressId,
  95. contactName,
  96. contactPhone,
  97. countyName,
  98. defaultFlag,
  99. detailedAddress,
  100. provinceName,
  101. cityName
  102. } = res.data;
  103. Object.assign(formData, {
  104. addressId,
  105. contactName,
  106. contactPhone,
  107. cityName,
  108. countyName,
  109. defaultFlag,
  110. detailedAddress,
  111. provinceName
  112. });
  113. };
  114. // 选择省市区
  115. const changeAddress = (e) => {
  116. formData.provinceName = e.detail.value[0];
  117. formData.cityName = e.detail.value[1];
  118. formData.countyName = e.detail.value[2];
  119. };
  120. // 默认地址切换
  121. const onDefaultChange = (e) => {
  122. formData.defaultFlag = e.detail.value ? 1 : 0;
  123. };
  124. // 清空表单
  125. const clearForm = () => {
  126. formData.contactName = '';
  127. formData.contactPhone = '';
  128. formData.provinceName = '';
  129. formData.cityName = '';
  130. formData.countyName = '';
  131. formData.detailedAddress = '';
  132. formData.defaultFlag = 0;
  133. formData.addressId = null; // 清空ID,避免残留
  134. };
  135. // 校验手机号
  136. const isValidPhone = (phone) => /^1[3-9]\d{9}$/.test(phone);
  137. // 提交
  138. const onConfirm = async () => {
  139. // 姓名校验
  140. if (!formData.contactName || formData.contactName.trim() === '') {
  141. return uni.showToast({ title: '请输入姓名', icon: 'none' });
  142. }
  143. if (formData.contactName.length > 20) {
  144. return uni.showToast({ title: '姓名不能超过20个字符', icon: 'none' });
  145. }
  146. // 电话校验
  147. if (!formData.contactPhone) {
  148. return uni.showToast({ title: '请输入联系电话', icon: 'none' });
  149. }
  150. if (!isValidPhone(formData.contactPhone)) {
  151. return uni.showToast({ title: '请输入正确的手机号码', icon: 'none' });
  152. }
  153. // 省市区校验
  154. if (!formData.provinceName) {
  155. return uni.showToast({ title: '请选择省市区', icon: 'none' });
  156. }
  157. // 详细地址校验
  158. if (!formData.detailedAddress || formData.detailedAddress.trim() === '') {
  159. return uni.showToast({ title: '请输入详细地址', icon: 'none' });
  160. }
  161. if (formData.detailedAddress.length > 100) {
  162. return uni.showToast({ title: '详细地址不能超过100个字符', icon: 'none' });
  163. }
  164. uni.showLoading({
  165. title:'正在保存',
  166. mask: true
  167. })
  168. // 提交
  169. let api = formData.addressId ? updateBook(formData) : addBook(formData);
  170. let res = await api;
  171. if (res.code === 200) {
  172. uni.showToast({ title: '保存成功', icon: 'success' });
  173. debugger
  174. // 如果接口返回了新地址数据,更新到formData(特别是新增时的ID)
  175. if (!formData.addressId) {
  176. formData.addressId = res.data
  177. }
  178. debugger
  179. onSelectAddress(formData);
  180. uni.hideLoading()
  181. } else {
  182. uni.hideLoading()
  183. uni.showToast({ title: res.msg || '保存失败', icon: 'none' });
  184. }
  185. };
  186. // 选择地址(用于返回数据给上一页)
  187. const onSelectAddress = (address) => {
  188. if (addType.value !== 'sender' && addType.value !== 'receiver') {
  189. return;
  190. }
  191. uni.$emit('addressSelected', {
  192. type: addType.value,
  193. address
  194. });
  195. uni.navigateBack() // 返回上一页
  196. };
  197. // 获取地址列表
  198. const getAddressList = () => {
  199. getLastAddress().then(res => {
  200. addressList.value = res.data
  201. }, err => {
  202. })
  203. }
  204. </script>
  205. <style scoped lang="less">
  206. .container {
  207. background-color: #F5F7FA;
  208. min-height: 100vh;
  209. padding: 20rpx 20rpx 30rpx 20rpx;
  210. }
  211. .form-card {
  212. background-color: #fff;
  213. border-radius: 32rpx;
  214. padding: 0 20rpx;
  215. }
  216. .form-item {
  217. display: flex;
  218. align-items: center;
  219. min-height: 100rpx;
  220. border-bottom: 1rpx solid #F1F3F8;
  221. padding: 0 0;
  222. &.row {
  223. gap: 20rpx;
  224. }
  225. &:last-child {
  226. border-bottom: none;
  227. }
  228. }
  229. /* 输入框通用样式 */
  230. .input-field {
  231. height: 100rpx;
  232. line-height: 100rpx;
  233. font-size: 28rpx;
  234. color: #333;
  235. background: transparent;
  236. padding: 0;
  237. &.flex-item {
  238. flex: 1;
  239. width: auto;
  240. min-width: 0; // 防止溢出
  241. }
  242. &.full-width {
  243. width: 100%;
  244. }
  245. .placeholder {
  246. color: #999;
  247. }
  248. }
  249. /* 省市区文字样式 */
  250. .address-text {
  251. flex: 1;
  252. font-size: 28rpx;
  253. line-height: 100rpx;
  254. color: #333;
  255. &.placeholder {
  256. color: #999;
  257. }
  258. }
  259. .arrow {
  260. margin-left: 16rpx;
  261. color: #999;
  262. font-size: 28rpx;
  263. }
  264. /* 操作按钮行 */
  265. .actions {
  266. justify-content: space-between;
  267. padding: 20rpx 0;
  268. }
  269. .action-left {
  270. display: flex;
  271. align-items: center;
  272. }
  273. .action-text {
  274. font-size: 28rpx;
  275. color: #333;
  276. margin-left: 10rpx;
  277. }
  278. .clear-text {
  279. font-size: 28rpx;
  280. color: #1B64F0;
  281. line-height: 44rpx;
  282. }
  283. /* 最近地址(隐藏) */
  284. .section-title {
  285. height: 48rpx;
  286. font-size: 32rpx;
  287. color: #333333;
  288. line-height: 48rpx;
  289. margin: 20rpx 0;
  290. font-weight: bold;
  291. }
  292. .recent-address {
  293. background-color: #fff;
  294. padding: 0 30rpx;
  295. }
  296. .address-item {
  297. padding: 30rpx 0;
  298. border-bottom: 1rpx solid #eee;
  299. }
  300. /* 确定按钮 */
  301. .submit-btn {
  302. position: fixed;
  303. bottom: 40rpx;
  304. left: 30rpx;
  305. right: 30rpx;
  306. width: 686rpx;
  307. height: 88rpx;
  308. background: #1B64F0;
  309. border-radius: 44rpx;
  310. display: flex;
  311. justify-content: center;
  312. align-items: center;
  313. font-size: 32rpx;
  314. color: #FFFFFF;
  315. line-height: 88rpx;
  316. z-index: 10;
  317. }
  318. .safe-area {
  319. height: 140rpx;
  320. }
  321. </style>