edit.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <template>
  2. <view class="container">
  3. <view class="form-card-textarea">
  4. <view class="form-item">
  5. <textarea class="input-field-textarea flex-item" placeholder="请输入文本,自动识别姓名、电话和地址"
  6. placeholder-class="placeholder" v-model="textArea" maxlength="250" />
  7. </view>
  8. <view class="form-item">
  9. <view class="search-btn" @click="handleSearch">
  10. 粘贴并识别
  11. </view>
  12. </view>
  13. </view>
  14. <!-- 物品信息 -->
  15. <view class="form-card" style="margin-top: 20rpx;">
  16. <!-- 姓名 + 电话 并排 -->
  17. <view class="form-item row">
  18. <input class="input-field flex-item" placeholder="姓名" placeholder-class="placeholder" maxlength="20"
  19. v-model="formData.contactName" />
  20. <input class="input-field flex-item" placeholder="电话" placeholder-class="placeholder" maxlength="11"
  21. v-model="formData.contactPhone" />
  22. </view>
  23. <!-- 省市区选择 -->
  24. <picker mode="region" @change="changeAddress">
  25. <view class="form-item">
  26. <text :class="['address-text', formData.provinceName ? '' : 'placeholder']">
  27. {{ formData.provinceName ? formData.provinceName + ' ' + formData.cityName + ' ' + formData.countyName : '省市区' }}
  28. </text>
  29. <u-icon class="arrow" name='arrow-right' size="18"></u-icon>
  30. </view>
  31. </picker>
  32. <!-- 详细地址 -->
  33. <view class="form-item">
  34. <input class="input-field full-width" placeholder="详细地址" placeholder-class="placeholder"
  35. v-model="formData.detailedAddress" />
  36. </view>
  37. <!-- 设置默认和清空 -->
  38. <view class="form-item actions">
  39. <view class="action-left">
  40. <switch :checked="formData.defaultFlag == 1" color="#007AFF" @change="onDefaultChange" />
  41. <text class="action-text">设为默认寄件地址</text>
  42. </view>
  43. <view class="action-right" @click="clearForm">
  44. <text class="clear-text">清空</text>
  45. </view>
  46. </view>
  47. </view>
  48. <view class="section-title" v-if="addressList.length > 0">最近使用地址</view>
  49. <!-- 最近使用地址 -->
  50. <view class="recent-address" v-if="addressList.length > 0">
  51. <view class="address-item" v-for="(item, index) in addressList" :key="item.id"
  52. @click="onSelectAddress(item)">
  53. <AddressInfo :address="item" />
  54. </view>
  55. </view>
  56. <!-- 确定按钮 -->
  57. <view class="add-btn-container">
  58. <view class="submit-btn" @click="onConfirm">确定</view>
  59. </view>
  60. <!-- 安全区域占位 -->
  61. <view class="safe-area"></view>
  62. </view>
  63. </template>
  64. <script setup>
  65. import {
  66. ref,
  67. reactive
  68. } from 'vue'
  69. import {
  70. onLoad
  71. } from '@dcloudio/uni-app'
  72. import AddressInfo from '@/components/AddressInfo.vue'
  73. import {
  74. addBook,
  75. updateBook,
  76. getBook,
  77. getLastAddress,
  78. getAddressInfo
  79. } from '@/api/address.js'
  80. const addType = ref('') // 'sender' 或 'receiver',表示从下单页面哪个字段进入
  81. const addressList = ref([])
  82. const textArea = ref('')
  83. // 表单数据
  84. const formData = reactive({
  85. addressId: undefined,
  86. contactName: '',
  87. contactPhone: '',
  88. provinceName: '',
  89. cityName: '',
  90. countyName: '',
  91. detailedAddress: '',
  92. defaultFlag: 0
  93. })
  94. onLoad((option) => {
  95. if (option.id) {
  96. formData.addressId = option.id;
  97. getAddress();
  98. uni.setNavigationBarTitle({
  99. title: '编辑地址'
  100. });
  101. }
  102. addType.value = option.addType || '';
  103. getAddressList()
  104. });
  105. // 粘贴并识别功能
  106. const handleSearch = async () => {
  107. try {
  108. // 1. 读取剪贴板内容
  109. const clipboardText = await new Promise((resolve, reject) => {
  110. uni.getClipboardData({
  111. success: res => resolve(res.data),
  112. fail: err => reject(err)
  113. });
  114. });
  115. if (!clipboardText) {
  116. uni.showToast({
  117. title: '剪贴板内容为空',
  118. icon: 'none'
  119. });
  120. return;
  121. }
  122. // 可选:将剪贴板内容显示到文本域中,方便用户查看
  123. textArea.value = clipboardText;
  124. uni.showLoading({
  125. title: '识别中...',
  126. mask: true
  127. });
  128. // 2. 调用接口解析地址
  129. const params = {
  130. param: clipboardText
  131. }; // 根据后端接口要求调整参数格式
  132. const res = await getAddressInfo(params);
  133. uni.hideLoading();
  134. if (res.code === 200 && res.data?.result?.length > 0) {
  135. const result = res.data.result[0];
  136. // 填充姓名
  137. if (result.name) {
  138. formData.contactName = result.name;
  139. }
  140. // 填充电话(取第一个手机号)
  141. if (result.mobile && result.mobile.length > 0) {
  142. formData.contactPhone = result.mobile[0];
  143. }
  144. // 填充省市区
  145. if (result.xzq) {
  146. // 从 fullName 中拆分省市区
  147. if (result.xzq.fullName) {
  148. const parts = result.xzq.fullName.split(',').map(s => s.trim());
  149. formData.provinceName = parts[0] || '';
  150. formData.cityName = parts[1] || '';
  151. formData.countyName = parts[2] || '';
  152. } else {
  153. // 兼容旧的字段
  154. formData.provinceName = result.xzq.province || '';
  155. formData.cityName = result.xzq.city || '';
  156. formData.countyName = result.xzq.district || '';
  157. }
  158. // 填充详细地址:优先使用 subArea,否则尝试从完整地址中去除省市区前缀
  159. if (result.xzq.subArea) {
  160. formData.detailedAddress = result.xzq.subArea;
  161. } else if (result.address) {
  162. const fullAddr = result.address;
  163. const prefix = `${formData.provinceName}${formData.cityName}${formData.countyName}`;
  164. if (fullAddr.startsWith(prefix)) {
  165. formData.detailedAddress = fullAddr.substring(prefix.length);
  166. } else {
  167. formData.detailedAddress = fullAddr; // 保底
  168. }
  169. }
  170. }
  171. uni.showToast({
  172. title: '识别成功',
  173. icon: 'success'
  174. });
  175. } else {
  176. uni.showToast({
  177. title: res.message || '识别失败,请重试',
  178. icon: 'none'
  179. });
  180. }
  181. } catch (error) {
  182. uni.hideLoading();
  183. uni.showToast({
  184. title: '操作失败',
  185. icon: 'none'
  186. });
  187. console.error('粘贴识别错误:', error);
  188. }
  189. };
  190. // 获取地址详情
  191. const getAddress = async () => {
  192. let res = await getBook(formData.addressId);
  193. let {
  194. addressId,
  195. contactName,
  196. contactPhone,
  197. countyName,
  198. defaultFlag,
  199. detailedAddress,
  200. provinceName,
  201. cityName
  202. } = res.data;
  203. Object.assign(formData, {
  204. addressId,
  205. contactName,
  206. contactPhone,
  207. cityName,
  208. countyName,
  209. defaultFlag,
  210. detailedAddress,
  211. provinceName
  212. });
  213. };
  214. // 选择省市区
  215. const changeAddress = (e) => {
  216. formData.provinceName = e.detail.value[0];
  217. formData.cityName = e.detail.value[1];
  218. formData.countyName = e.detail.value[2];
  219. };
  220. // 默认地址切换
  221. const onDefaultChange = (e) => {
  222. formData.defaultFlag = e.detail.value ? 1 : 0;
  223. };
  224. // 清空表单
  225. const clearForm = () => {
  226. formData.contactName = '';
  227. formData.contactPhone = '';
  228. formData.provinceName = '';
  229. formData.cityName = '';
  230. formData.countyName = '';
  231. formData.detailedAddress = '';
  232. formData.defaultFlag = 0;
  233. formData.addressId = null; // 清空ID,避免残留
  234. };
  235. // 校验手机号
  236. const isValidPhone = (phone) => /^1[3-9]\d{9}$/.test(phone);
  237. // 提交
  238. const onConfirm = async () => {
  239. // 姓名校验
  240. if (!formData.contactName || formData.contactName.trim() === '') {
  241. return uni.showToast({
  242. title: '请输入姓名',
  243. icon: 'none'
  244. });
  245. }
  246. if (formData.contactName.length > 20) {
  247. return uni.showToast({
  248. title: '姓名不能超过20个字符',
  249. icon: 'none'
  250. });
  251. }
  252. // 电话校验
  253. if (!formData.contactPhone) {
  254. return uni.showToast({
  255. title: '请输入联系电话',
  256. icon: 'none'
  257. });
  258. }
  259. if (!isValidPhone(formData.contactPhone)) {
  260. return uni.showToast({
  261. title: '请输入正确的手机号码',
  262. icon: 'none'
  263. });
  264. }
  265. // 省市区校验
  266. if (!formData.provinceName) {
  267. return uni.showToast({
  268. title: '请选择省市区',
  269. icon: 'none'
  270. });
  271. }
  272. // 详细地址校验
  273. if (!formData.detailedAddress || formData.detailedAddress.trim() === '') {
  274. return uni.showToast({
  275. title: '请输入详细地址',
  276. icon: 'none'
  277. });
  278. }
  279. if (formData.detailedAddress.length > 100) {
  280. return uni.showToast({
  281. title: '详细地址不能超过100个字符',
  282. icon: 'none'
  283. });
  284. }
  285. uni.showLoading({
  286. title: '正在保存',
  287. mask: true
  288. })
  289. // 提交
  290. let api = formData.addressId ? updateBook(formData) : addBook(formData);
  291. let res = await api;
  292. if (res.code === 200) {
  293. uni.showToast({
  294. title: '保存成功',
  295. icon: 'success'
  296. });
  297. // 如果接口返回了新地址数据,更新到formData(特别是新增时的ID)
  298. if (!formData.addressId) {
  299. formData.addressId = res.data
  300. }
  301. if (addType.value !== 'sender' && addType.value !== 'receiver') {
  302. uni.navigateBack() // 返回上一页
  303. return;
  304. }
  305. onSelectAddress(formData);
  306. uni.hideLoading()
  307. } else {
  308. uni.hideLoading()
  309. uni.showToast({
  310. title: res.msg || '保存失败',
  311. icon: 'none'
  312. });
  313. }
  314. };
  315. // 选择地址(用于返回数据给上一页)
  316. const onSelectAddress = (address) => {
  317. if (addType.value !== 'sender' && addType.value !== 'receiver') {
  318. return;
  319. }
  320. uni.$emit('addressSelected', {
  321. type: addType.value,
  322. address
  323. });
  324. uni.navigateBack() // 返回上一页
  325. };
  326. // 获取地址列表
  327. const getAddressList = () => {
  328. getLastAddress().then(res => {
  329. addressList.value = res.data
  330. }, err => {})
  331. }
  332. </script>
  333. <style scoped lang="less">
  334. .container {
  335. background-color: #F5F7FA;
  336. min-height: 100vh;
  337. padding: 20rpx 0rpx 30rpx 0rpx;
  338. }
  339. .form-card {
  340. margin: 0rpx 20rpx;
  341. background-color: #fff;
  342. border-radius: 32rpx;
  343. padding: 0 20rpx;
  344. }
  345. .form-card-textarea {
  346. margin: 0rpx 20rpx;
  347. background-color: #fff;
  348. border-radius: 32rpx;
  349. padding: 20rpx 20rpx;
  350. .search-btn {
  351. width: 204rpx;
  352. height: 72rpx;
  353. background: #1B64F0;
  354. border-radius: 38rpx;
  355. font-weight: 400;
  356. font-size: 32rpx;
  357. color: #FFFFFF;
  358. line-height: 72rpx;
  359. text-align: center;
  360. font-style: normal;
  361. position: absolute;
  362. right: 40rpx;
  363. }
  364. }
  365. .form-item {
  366. display: flex;
  367. align-items: center;
  368. min-height: 100rpx;
  369. border-bottom: 1rpx solid #F1F3F8;
  370. padding: 0 0;
  371. &.row {
  372. gap: 20rpx;
  373. }
  374. &.column {
  375. flex-direction: column;
  376. }
  377. &:last-child {
  378. border-bottom: none;
  379. }
  380. }
  381. .input-field-textarea {
  382. height: 200rpx;
  383. line-height: 48rpx;
  384. font-size: 28rpx;
  385. color: #333;
  386. background: transparent;
  387. padding: 20rxp 0rpx;
  388. &.flex-item {
  389. flex: 1;
  390. width: auto;
  391. min-width: 0; // 防止溢出
  392. }
  393. &.full-width {
  394. width: 100%;
  395. }
  396. .placeholder {
  397. color: #999;
  398. }
  399. }
  400. /* 输入框通用样式 */
  401. .input-field {
  402. height: 100rpx;
  403. line-height: 100rpx;
  404. font-size: 28rpx;
  405. color: #333;
  406. background: transparent;
  407. padding: 0;
  408. &.flex-item {
  409. flex: 1;
  410. width: auto;
  411. min-width: 0; // 防止溢出
  412. }
  413. &.full-width {
  414. width: 100%;
  415. }
  416. .placeholder {
  417. color: #999;
  418. }
  419. }
  420. /* 省市区文字样式 */
  421. .address-text {
  422. flex: 1;
  423. font-size: 28rpx;
  424. line-height: 100rpx;
  425. color: #333;
  426. &.placeholder {
  427. color: #999;
  428. }
  429. }
  430. .arrow {
  431. margin-left: 16rpx;
  432. color: #999;
  433. font-size: 28rpx;
  434. }
  435. /* 操作按钮行 */
  436. .actions {
  437. justify-content: space-between;
  438. padding: 20rpx 0;
  439. }
  440. .action-left {
  441. display: flex;
  442. align-items: center;
  443. }
  444. .action-text {
  445. font-size: 28rpx;
  446. color: #333;
  447. margin-left: 10rpx;
  448. }
  449. .clear-text {
  450. font-size: 28rpx;
  451. color: #1B64F0;
  452. line-height: 44rpx;
  453. }
  454. /* 最近地址(隐藏) */
  455. .section-title {
  456. height: 48rpx;
  457. font-size: 32rpx;
  458. color: #333333;
  459. line-height: 48rpx;
  460. margin: 20rpx;
  461. font-weight: bold;
  462. }
  463. .recent-address {
  464. margin: 0rpx 20rpx;
  465. background-color: #fff;
  466. padding: 0 30rpx;
  467. }
  468. .address-item {
  469. padding: 30rpx 0;
  470. border-bottom: 1rpx solid #eee;
  471. }
  472. .add-btn-container {
  473. width: 100%;
  474. position: fixed;
  475. bottom: 0rpx;
  476. padding: 32rpx;
  477. background-color: #fff;
  478. border-top: 1rpx solid #eee;
  479. box-sizing: border-box;
  480. /* 确定按钮 */
  481. .submit-btn {
  482. height: 88rpx;
  483. background: #1B64F0;
  484. border-radius: 44rpx;
  485. color: #fff;
  486. font-size: 32rpx;
  487. font-weight: 500;
  488. height: 88rpx;
  489. line-height: 88rpx;
  490. text-align: center
  491. }
  492. }
  493. .safe-area {
  494. height: 140rpx;
  495. }
  496. </style>