addAddress.vue 10 KB

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