addEditAddress.vue 9.8 KB

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