edit.vue 12 KB

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