addEditAddress.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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="../../static/images/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="#C39A58" ></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:'success',
  175. title: "操作成功",
  176. });
  177. setTimeout(function() {
  178. uni.$emit('refreshAddress');
  179. uni.navigateBack({
  180. delta: 1
  181. })
  182. }, 500);
  183. }else{
  184. uni.showToast({
  185. icon:'none',
  186. title: res.msg,
  187. });
  188. }
  189. },
  190. rej => {}
  191. );
  192. },
  193. // 地区选择
  194. pickerChange(e) {
  195. this.multiIndex = e.detail.value;
  196. // 数组内的下标
  197. // 获取一级类目
  198. // 获取二级类目
  199. // 获取三级类目
  200. this.form.address=this.addressList[0][this.multiIndex[0]].n+this.addressList[1][this.multiIndex[1]].n+this.addressList[2][this.multiIndex[2]].n
  201. this.form.province=this.addressList[0][this.multiIndex[0]].n
  202. this.form.city=this.addressList[1][this.multiIndex[1]].n
  203. this.form.district=this.addressList[2][this.multiIndex[2]].n
  204. this.form.cityId=this.addressList[1][this.multiIndex[1]].v;
  205. },
  206. pickerColumnchange(e){
  207. // 第一列滑动
  208. if(e.detail.column === 0){
  209. this.multiIndex[0] = e.detail.value
  210. // console.log('第一列滑动');
  211. this.addressList[1] = this.address[this.multiIndex[0]].c;
  212. this.addressList[2] = this.address[this.multiIndex[0]].c[0].c
  213. // 第一列滑动 第二列 和第三列 都变为第一个
  214. this.multiIndex.splice(1, 1, 0)
  215. this.multiIndex.splice(2, 1, 0)
  216. }
  217. // 第二列滑动
  218. if(e.detail.column === 1){
  219. this.multiIndex[1] = e.detail.value
  220. // console.log('第二列滑动');
  221. // console.log(this.multiIndex)
  222. this.addressList[2] = this.address[this.multiIndex[0]].c[this.multiIndex[1]].c
  223. // 第二列 滑动 第三列 变成第一个
  224. this.multiIndex.splice(2, 1, 0)
  225. }
  226. // 第三列滑动
  227. if(e.detail.column === 2){
  228. this.multiIndex[2] = e.detail.value
  229. }
  230. },
  231. getCitys(){
  232. getCitys().then(
  233. res => {
  234. if(res.code==200){
  235. this.address=res.data
  236. for(var i=0; i<this.address.length; i++){
  237. this.addressList[0].push(this.address[i])
  238. }
  239. for(var i=0; i<this.address[0].c.length; i++){
  240. this.addressList[1].push(this.address[0].c[i])
  241. }
  242. for(var i=0; i<this.address[0].c[0].c.length; i++){
  243. this.addressList[2].push(this.address[0].c[0].c[i])
  244. }
  245. }else{
  246. uni.showToast({
  247. icon:'none',
  248. title: "请求失败",
  249. });
  250. }
  251. },
  252. rej => {}
  253. );
  254. }
  255. }
  256. }
  257. </script>
  258. <style lang="scss">
  259. page{
  260. height: 100%;
  261. }
  262. .content{
  263. height: 100%;
  264. display: flex;
  265. flex-direction: column;
  266. justify-content: space-between;
  267. .inner{
  268. height: calc(100% - 120upx);
  269. padding: 20upx;
  270. .address-box{
  271. margin-top: 20rpx;
  272. padding: 30upx;
  273. background: #FFFFFF;
  274. border-radius: 16upx;
  275. margin-bottom: 20upx;
  276. position: relative;
  277. .textarea{
  278. width: 100%;
  279. height: 200upx;
  280. font-size: 30upx;
  281. color: #999999;
  282. padding-bottom:100rpx;
  283. }
  284. .btns{
  285. right:10rpx;
  286. bottom:10rpx;
  287. position: absolute;
  288. .btn{
  289. width: 155upx;
  290. height: 64upx;
  291. line-height: 64upx;
  292. font-size: 26upx;
  293. font-family: PingFang SC;
  294. font-weight: 500;
  295. text-align: center;
  296. border-radius: 32upx;
  297. &.parse{
  298. background: #C39A58;
  299. color: #FFFFFF;
  300. }
  301. }
  302. }
  303. }
  304. .form-box{
  305. padding: 0 30upx;
  306. background: #FFFFFF;
  307. border-radius: 16upx;
  308. .form-item{
  309. height: 103upx;
  310. display: flex;
  311. align-items: center;
  312. justify-content: space-between;
  313. border-bottom: 1px solid #F1F1F1;
  314. &:last-child{
  315. border-bottom: none;
  316. }
  317. .label{
  318. min-width: 200upx;
  319. font-size: 30upx;
  320. font-family: PingFang SC;
  321. font-weight: 500;
  322. color: #222222;
  323. }
  324. .input-width{
  325. width: calc(100% - 200upx);
  326. text-align: right;
  327. }
  328. .form-input{
  329. font-size: 30upx;
  330. font-family: PingFang SC;
  331. font-weight: 500;
  332. color: #999999;
  333. text-align: right;
  334. }
  335. .birth-picker {
  336. display: flex;
  337. align-items: center;
  338. .right-box{
  339. width: 100%;
  340. display: flex;
  341. align-items: center;
  342. .arrow{
  343. width: 13upx;
  344. height: 23upx;
  345. margin-left: 15upx;
  346. }
  347. }
  348. }
  349. }
  350. }
  351. .setting-box{
  352. height: 88upx;
  353. background: #FFFFFF;
  354. border-radius: 16upx;
  355. margin-top: 20upx;
  356. padding: 0 30upx;
  357. display: flex;
  358. align-items: center;
  359. justify-content: space-between;
  360. .label{
  361. font-size: 28upx;
  362. font-family: PingFang SC;
  363. font-weight: 500;
  364. color: #111111;
  365. }
  366. }
  367. }
  368. .btn-box{
  369. height: 120upx;
  370. padding: 0 30upx;
  371. display: flex;
  372. align-items: center;
  373. justify-content: center;
  374. background: #FFFFFF;
  375. .sub-btn{
  376. width: 100%;
  377. height: 88upx;
  378. line-height: 88upx;
  379. text-align: center;
  380. font-size: 30upx;
  381. font-family: PingFang SC;
  382. font-weight: bold;
  383. color: #FFFFFF;
  384. background: #C39A58;
  385. border-radius: 44upx;
  386. }
  387. }
  388. }
  389. </style>