addAddress.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <template>
  2. <view class="content">
  3. <view class="inner">
  4. <view class="address-box">
  5. <textarea class="textarea" v-model="content"
  6. placeholder="请粘贴或输入文本,点击'识别'自动识别姓名、电话、地址,格式:深圳市龙岗区坂田街道长坑路西2巷2号202 黄大大 18888888888" />
  7. <view class="btns">
  8. <view class="btn parse" @click="parseAddress()">识别</view>
  9. </view>
  10. </view>
  11. <view class="form-box">
  12. <view class="form-item">
  13. <text class="label">联系人</text>
  14. <input type="text" v-model="form.realName" maxlength="10" placeholder="姓名" class="form-input" />
  15. </view>
  16. <view class="form-item">
  17. <text class="label">手机号</text>
  18. <input type="number" v-model="form.phone" maxlength="11" placeholder="手机号" class="form-input" />
  19. </view>
  20. <view class="form-item">
  21. <text class="label">所在地区</text>
  22. <picker :value="multiIndex" class="birth-picker" mode="multiSelector" range-key="n"
  23. :range="addressList" @change="pickerChange" @columnchange="pickerColumnchange">
  24. <view class="right-box">
  25. <view class="input-box">
  26. <input type="text" v-model="form.address" placeholder="请选择" class="form-input"
  27. disabled="disabled" />
  28. </view>
  29. <image class="arrow" src="/static/images/arrow_gray.png" mode=""></image>
  30. </view>
  31. </picker>
  32. </view>
  33. <view class="form-item">
  34. <text class="label">详细地址</text>
  35. <textarea class="form-textarea" v-model="form.detail" placeholder="请输入详细地址" />
  36. </view>
  37. </view>
  38. <!-- 设为默认地址 -->
  39. <view class="setting-box">
  40. <text class="label">设为默认地址</text>
  41. <evan-switch @change="logChange" v-model="isDefault" activeColor="#0bb3f2"
  42. inactiveColor="rgba(0, 0, 0, 0.1)"></evan-switch>
  43. </view>
  44. </view>
  45. <view class="btn-box">
  46. <view class="sub-btn" @click="submit()">保存地址</view>
  47. </view>
  48. </view>
  49. </template>
  50. <script>
  51. import {
  52. addAddress, // 添加地址
  53. editAddress, // 编辑地址
  54. getCitys, //获取省市区
  55. parseAddress, //地址解析
  56. getAddressById
  57. } from "@/api/order.js"
  58. // import {parseAddress,getCity,getCitys,getAddressById,addAddress,editAddress} from '@/api/address'
  59. import EvanSwitch from '@/components/evan-switch/evan-switch.vue'
  60. export default {
  61. components: {
  62. EvanSwitch
  63. },
  64. data() {
  65. return {
  66. content: null,
  67. type: null,
  68. addressId: null,
  69. // isDefault: false,
  70. multiIndex: [0, 0, 0],
  71. address: [],
  72. addressList: [
  73. [],
  74. [],
  75. []
  76. ],
  77. form: {
  78. realName: null,
  79. phone: null,
  80. addressId: null,
  81. province: null,
  82. city: null,
  83. district: null,
  84. // cityIds:null,
  85. // postCode:null,
  86. // longitude:null,
  87. // latitude:null,
  88. detail: null,
  89. // address: null,
  90. isDefault: 0,
  91. }
  92. }
  93. },
  94. onLoad(option) {
  95. console.log("option", option)
  96. this.type = option.type;
  97. if (this.type == 'edit') {
  98. uni.setNavigationBarTitle({
  99. title: "修改收货地址"
  100. })
  101. this.form.addressId = option.addressId;
  102. this.addressId = option.addressId;
  103. this.getAddressById();
  104. } else {
  105. uni.setNavigationBarTitle({
  106. title: "新增收货地址"
  107. })
  108. }
  109. this.getCitys()
  110. },
  111. methods: {
  112. logChange(value) {
  113. console.log('isDefault 新值:', value); // 验证值是否更新
  114. },
  115. parseAddress() {
  116. if (this.content == null || this.content == "") {
  117. uni.showToast({
  118. icon: 'none',
  119. title: '请输入地址信息'
  120. });
  121. return;
  122. }
  123. var data = {
  124. content: this.content
  125. };
  126. parseAddress(data).then(
  127. res => {
  128. if (res.code == 200) {
  129. this.form.realName = res.data.name
  130. this.form.phone = res.data.mobile
  131. this.form.address = res.data.provinceName + res.data.cityName + res.data.expAreaName
  132. this.form.province = res.data.provinceName
  133. this.form.city = res.data.cityName
  134. this.form.district = res.data.expAreaName
  135. this.form.detail = res.data.streetName + res.data.address
  136. } else {
  137. uni.showToast({
  138. icon: 'none',
  139. title: res.msg,
  140. });
  141. }
  142. },
  143. rej => {}
  144. );
  145. },
  146. getAddressById() {
  147. var data = {
  148. addressId: this.addressId
  149. };
  150. getAddressById(data).then(
  151. res => {
  152. if (res.code == 200) {
  153. this.form = res.data;
  154. console.log(" this.form", this.form)
  155. this.isDefault = this.form.isDefault == 1 ? true : false;
  156. this.form.address = this.form.province + this.form.city + this.form.district
  157. } else {
  158. uni.showToast({
  159. icon: 'none',
  160. title: res.msg,
  161. });
  162. }
  163. },
  164. rej => {}
  165. );
  166. },
  167. submit() {
  168. // 调试输出
  169. console.log('提交数据:', JSON.stringify(this.form));
  170. if (this.type == "add") {
  171. this.addAddress()
  172. } else if (this.type == "edit") {
  173. this.editAddress()
  174. }
  175. },
  176. editAddress() {
  177. this.form.isDefault=this.isDefault?1:0
  178. editAddress(submitData).then(
  179. res => {
  180. if (res.code == 200) {
  181. uni.showToast({
  182. icon: 'success',
  183. title: "操作成功",
  184. });
  185. setTimeout(function() {
  186. uni.$emit('refreshAddress');
  187. uni.navigateBack({
  188. delta: 1
  189. })
  190. }, 500);
  191. } else {
  192. uni.showToast({
  193. icon: 'none',
  194. title: res.msg,
  195. });
  196. }
  197. },
  198. rej => {}
  199. );
  200. },
  201. addAddress() {
  202. this.form.isDefault=this.isDefault?1:0
  203. addAddress(this.form).then(
  204. res => {
  205. if (res.code == 200) {
  206. uni.showToast({
  207. icon: 'success',
  208. title: "操作成功",
  209. });
  210. setTimeout(function() {
  211. uni.$emit('refreshAddress');
  212. uni.navigateBack({
  213. delta: 1
  214. })
  215. }, 500);
  216. } else {
  217. uni.showToast({
  218. icon: 'none',
  219. title: res.msg,
  220. });
  221. }
  222. },
  223. rej => {}
  224. );
  225. },
  226. // 地区选择
  227. pickerChange(e) {
  228. this.multiIndex = e.detail.value;
  229. // 数组内的下标
  230. // 获取一级类目
  231. // 获取二级类目
  232. // 获取三级类目
  233. this.form.address = this.addressList[0][this.multiIndex[0]].n + this.addressList[1][this.multiIndex[1]].n +
  234. this.addressList[2][this.multiIndex[2]].n
  235. this.form.province = this.addressList[0][this.multiIndex[0]].n
  236. this.form.city = this.addressList[1][this.multiIndex[1]].n
  237. this.form.district = this.addressList[2][this.multiIndex[2]].n
  238. this.form.cityId = this.addressList[1][this.multiIndex[1]].v;
  239. },
  240. pickerColumnchange(e) {
  241. // 第一列滑动
  242. if (e.detail.column === 0) {
  243. this.multiIndex[0] = e.detail.value
  244. // console.log('第一列滑动');
  245. this.addressList[1] = this.address[this.multiIndex[0]].c;
  246. this.addressList[2] = this.address[this.multiIndex[0]].c[0].c
  247. // 第一列滑动 第二列 和第三列 都变为第一个
  248. this.multiIndex.splice(1, 1, 0)
  249. this.multiIndex.splice(2, 1, 0)
  250. }
  251. // 第二列滑动
  252. if (e.detail.column === 1) {
  253. this.multiIndex[1] = e.detail.value
  254. // console.log('第二列滑动');
  255. // console.log(this.multiIndex)
  256. this.addressList[2] = this.address[this.multiIndex[0]].c[this.multiIndex[1]].c
  257. // 第二列 滑动 第三列 变成第一个
  258. this.multiIndex.splice(2, 1, 0)
  259. }
  260. // 第三列滑动
  261. if (e.detail.column === 2) {
  262. this.multiIndex[2] = e.detail.value
  263. }
  264. },
  265. getCitys() {
  266. getCitys().then(
  267. res => {
  268. if (res.code == 200) {
  269. this.address = res.data
  270. for (var i = 0; i < this.address.length; i++) {
  271. this.addressList[0].push(this.address[i])
  272. }
  273. for (var i = 0; i < this.address[0].c.length; i++) {
  274. this.addressList[1].push(this.address[0].c[i])
  275. }
  276. for (var i = 0; i < this.address[0].c[0].c.length; i++) {
  277. this.addressList[2].push(this.address[0].c[0].c[i])
  278. }
  279. } else {
  280. uni.showToast({
  281. icon: 'none',
  282. title: "请求失败",
  283. });
  284. }
  285. },
  286. rej => {}
  287. );
  288. }
  289. }
  290. }
  291. </script>
  292. <style lang="scss">
  293. page {
  294. height: 100%;
  295. }
  296. .content {
  297. height: 100%;
  298. display: flex;
  299. flex-direction: column;
  300. justify-content: space-between;
  301. .inner {
  302. height: calc(100% - 120upx);
  303. padding: 20upx;
  304. .address-box {
  305. padding: 30upx;
  306. background: #FFFFFF;
  307. border-radius: 16upx;
  308. margin-bottom: 20upx;
  309. position: relative;
  310. .textarea {
  311. width: 100%;
  312. height: 200upx;
  313. font-size: 30upx;
  314. color: #999999;
  315. padding-bottom: 100rpx;
  316. }
  317. .btns {
  318. right: 10rpx;
  319. bottom: 10rpx;
  320. position: absolute;
  321. .btn {
  322. width: 155upx;
  323. height: 64upx;
  324. line-height: 64upx;
  325. font-size: 26upx;
  326. font-family: PingFang SC;
  327. font-weight: 500;
  328. text-align: center;
  329. border-radius: 32upx;
  330. &.parse {
  331. background: #0bb3f2;
  332. color: #FFFFFF;
  333. }
  334. }
  335. }
  336. }
  337. .form-box {
  338. padding: 0 30upx;
  339. background: #FFFFFF;
  340. border-radius: 16upx;
  341. .form-item {
  342. padding: 30upx 0;
  343. display: flex;
  344. align-items: flex-start;
  345. border-bottom: 1px solid #F1F1F1;
  346. &:last-child {
  347. border-bottom: none;
  348. }
  349. .label {
  350. width: 180upx;
  351. text-align: left;
  352. font-size: 30upx;
  353. line-height: 44upx;
  354. font-family: PingFang SC;
  355. font-weight: 500;
  356. color: #222222;
  357. flex-shrink: 0;
  358. }
  359. input {
  360. text-align: left;
  361. }
  362. .form-input {
  363. font-size: 30upx;
  364. font-family: PingFang SC;
  365. font-weight: 500;
  366. color: #999999;
  367. text-align: left;
  368. }
  369. .form-textarea {
  370. font-size: 30upx;
  371. color: #999999;
  372. height: 100upx;
  373. padding: 4upx 0;
  374. }
  375. .birth-picker {
  376. flex: 1;
  377. display: flex;
  378. align-items: center;
  379. .right-box {
  380. width: 100%;
  381. display: flex;
  382. align-items: center;
  383. .input-box {
  384. width: 470upx;
  385. }
  386. .arrow {
  387. width: 13upx;
  388. height: 23upx;
  389. margin-left: 20upx;
  390. }
  391. }
  392. }
  393. }
  394. }
  395. .setting-box {
  396. height: 88upx;
  397. background: #FFFFFF;
  398. border-radius: 16upx;
  399. margin-top: 20upx;
  400. padding: 0 30upx;
  401. display: flex;
  402. align-items: center;
  403. justify-content: space-between;
  404. .label {
  405. font-size: 28upx;
  406. font-family: PingFang SC;
  407. font-weight: 500;
  408. color: #111111;
  409. }
  410. }
  411. }
  412. .btn-box {
  413. height: 120upx;
  414. padding: 0 30upx;
  415. display: flex;
  416. align-items: center;
  417. justify-content: center;
  418. background: #FFFFFF;
  419. .sub-btn {
  420. width: 100%;
  421. height: 88upx;
  422. line-height: 88upx;
  423. text-align: center;
  424. font-size: 30upx;
  425. font-family: PingFang SC;
  426. font-weight: bold;
  427. color: #FFFFFF;
  428. background: #0bb3f2;
  429. border-radius: 44upx;
  430. }
  431. }
  432. }
  433. </style>