addEditAddress.vue 10.0 KB

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