addEditAddress.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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="#FF233C" ></u-switch>
  41. </view>
  42. </view>
  43. <view class="btn-box">
  44. <view class="sub-btn flex-1" @click="submit()">保存地址</view>
  45. <view v-if="type=='add'" class="icon-btn" style="width: 15%;" @click="openShare">
  46. <image src="/static/images/icon_wx.png" mode="aspectFill"></image>
  47. <text>分享</text>
  48. </view>
  49. </view>
  50. </view>
  51. </template>
  52. <script>
  53. import {getCitys} from '@/api/common.js'
  54. import {parseAddress,getAddressById,addAddress,editAddress} from '@/api/userAddress.js'
  55. export default {
  56. components: {
  57. },
  58. data() {
  59. return {
  60. content:null,
  61. type:null,
  62. addressId:null,
  63. isDefault: false,
  64. addressList:[[],[],[]],
  65. multiIndex:[0,0,0],
  66. address:[],
  67. form: {
  68. realName:null,
  69. phone:"",
  70. detail:null,
  71. address: null,
  72. isDefault: 0,
  73. }
  74. }
  75. },
  76. onLoad(option) {
  77. this.type=option.type;
  78. if(this.type=='edit'){
  79. uni.setNavigationBarTitle({
  80.   title:"修改收货地址"
  81. })
  82. this.addressId=option.addressId;
  83. this.getAddressById();
  84. }
  85. else{
  86. uni.setNavigationBarTitle({
  87.   title:"新增收货地址"
  88. })
  89. }
  90. this.getCitys()
  91. },
  92. methods: {
  93. openShare() {
  94. //#ifdef H5
  95. uni.showToast({
  96. title: '请使用APP操作',
  97. icon: 'none'
  98. });
  99. //#endif
  100. //#ifdef APP-PLUS
  101. uni.share({
  102. provider: "weixin",
  103. scene: 'WXSceneSession',
  104. type: 5,
  105. imageUrl: 'https://bjzmky-1323137866.cos.ap-chongqing.myqcloud.com/app/app-address.jpg', //分享封面图片
  106. title: '乐享韶华-邀请你填写地址,下单更方便', // 分享标题
  107. miniProgram: {
  108. id: getApp().globalData.miniprogamId,
  109. path: '/pages_user/user/addAddress?type=add',
  110. type: 0, //0-正式版; 1-测试版; 2-体验版。 默认值为0。
  111. webUrl: "http://uniapp.dcloud.io"
  112. },
  113. success: function(res) {
  114. uni.showToast({
  115. title: "分享成功",
  116. icon: 'none'
  117. });
  118. },
  119. fail: function(err) {
  120. // 此处是调起微信分享失败的回调
  121. },
  122. complete: (e) => {
  123. console.log("WXSceneSession", e)
  124. }
  125. });
  126. //#endif
  127. },
  128. parseAddress(){
  129. if(this.content==null||this.content==""){
  130. uni.showToast({
  131. icon:'none',
  132. title: '请输入地址信息',
  133. });
  134. return;
  135. }
  136. var data={content:this.content};
  137. parseAddress(data).then(
  138. res => {
  139. if(res.code==200){
  140. this.form.realName=res.data.name
  141. this.form.phone=res.data.mobile
  142. this.form.address=res.data.provinceName+res.data.cityName+res.data.expAreaName
  143. this.form.province=res.data.provinceName
  144. this.form.city=res.data.cityName
  145. this.form.district=res.data.expAreaName
  146. this.form.detail=res.data.streetName+res.data.address
  147. this.form.detail=this.form.detail.replace(/\s+/g,"");
  148. }else{
  149. uni.showToast({
  150. icon:'none',
  151. title: res.msg,
  152. });
  153. }
  154. },
  155. rej => {}
  156. );
  157. },
  158. getAddressById(){
  159. var data={addressId:this.addressId};
  160. getAddressById(data).then(
  161. res => {
  162. if(res.code==200){
  163. this.form=res.data;
  164. this.isDefault=this.form.isDefault==1?true:false;
  165. this.form.address=this.form.province+this.form.city+this.form.district
  166. }else{
  167. uni.showToast({
  168. icon:'none',
  169. title: res.msg,
  170. });
  171. }
  172. },
  173. rej => {}
  174. );
  175. },
  176. submit(){
  177. if(this.type=="add"){
  178. this.addAddress()
  179. }
  180. else if(this.type=="edit"){
  181. this.editAddress()
  182. }
  183. },
  184. editAddress(){
  185. this.form.isDefault=this.isDefault?1:0
  186. editAddress(this.form).then(
  187. res => {
  188. if(res.code==200){
  189. uni.showToast({
  190. icon:'success',
  191. title: "操作成功",
  192. });
  193. setTimeout(function() {
  194. uni.$emit('refreshAddress');
  195. uni.navigateBack({
  196. delta: 1
  197. })
  198. }, 500);
  199. }else{
  200. uni.showToast({
  201. icon:'none',
  202. title: res.msg,
  203. });
  204. }
  205. },
  206. rej => {}
  207. );
  208. },
  209. addAddress(){
  210. this.form.isDefault=this.isDefault?1:0
  211. addAddress(this.form).then(
  212. res => {
  213. if(res.code==200){
  214. uni.showToast({
  215. icon:'success',
  216. title: "操作成功",
  217. });
  218. setTimeout(function() {
  219. uni.$emit('refreshAddress');
  220. uni.navigateBack({
  221. delta: 1
  222. })
  223. }, 500);
  224. }else{
  225. uni.showToast({
  226. icon:'none',
  227. title: res.msg,
  228. });
  229. }
  230. },
  231. rej => {}
  232. );
  233. },
  234. // 地区选择
  235. pickerChange(e) {
  236. this.multiIndex = e.detail.value;
  237. // 数组内的下标
  238. // 获取一级类目
  239. // 获取二级类目
  240. // 获取三级类目
  241. this.form.address=this.addressList[0][this.multiIndex[0]].n+this.addressList[1][this.multiIndex[1]].n+this.addressList[2][this.multiIndex[2]].n
  242. this.form.province=this.addressList[0][this.multiIndex[0]].n
  243. this.form.city=this.addressList[1][this.multiIndex[1]].n
  244. this.form.district=this.addressList[2][this.multiIndex[2]].n
  245. this.form.cityId=this.addressList[1][this.multiIndex[1]].v;
  246. },
  247. pickerColumnchange(e){
  248. // 第一列滑动
  249. if(e.detail.column === 0){
  250. this.multiIndex[0] = e.detail.value
  251. // console.log('第一列滑动');
  252. this.addressList[1] = this.address[this.multiIndex[0]].c;
  253. this.addressList[2] = this.address[this.multiIndex[0]].c[0].c
  254. // 第一列滑动 第二列 和第三列 都变为第一个
  255. this.multiIndex.splice(1, 1, 0)
  256. this.multiIndex.splice(2, 1, 0)
  257. }
  258. // 第二列滑动
  259. if(e.detail.column === 1){
  260. this.multiIndex[1] = e.detail.value
  261. // console.log('第二列滑动');
  262. // console.log(this.multiIndex)
  263. this.addressList[2] = this.address[this.multiIndex[0]].c[this.multiIndex[1]].c
  264. // 第二列 滑动 第三列 变成第一个
  265. this.multiIndex.splice(2, 1, 0)
  266. }
  267. // 第三列滑动
  268. if(e.detail.column === 2){
  269. this.multiIndex[2] = e.detail.value
  270. }
  271. },
  272. getCitys(){
  273. getCitys().then(
  274. res => {
  275. if(res.code==200){
  276. this.address=res.data
  277. for(var i=0; i<this.address.length; i++){
  278. this.addressList[0].push(this.address[i])
  279. }
  280. for(var i=0; i<this.address[0].c.length; i++){
  281. this.addressList[1].push(this.address[0].c[i])
  282. }
  283. for(var i=0; i<this.address[0].c[0].c.length; i++){
  284. this.addressList[2].push(this.address[0].c[0].c[i])
  285. }
  286. }else{
  287. uni.showToast({
  288. icon:'none',
  289. title: "请求失败",
  290. });
  291. }
  292. },
  293. rej => {}
  294. );
  295. }
  296. }
  297. }
  298. </script>
  299. <style lang="scss">
  300. page{
  301. height: 100%;
  302. }
  303. .content{
  304. height: 100%;
  305. display: flex;
  306. flex-direction: column;
  307. justify-content: space-between;
  308. .inner{
  309. height: calc(100% - 120upx);
  310. padding: 20upx;
  311. .address-box{
  312. margin-top: 20rpx;
  313. padding: 30upx;
  314. background: #FFFFFF;
  315. border-radius: 16upx;
  316. margin-bottom: 20upx;
  317. position: relative;
  318. .textarea{
  319. width: 100%;
  320. height: 200upx;
  321. font-size: 30upx;
  322. color: #999999;
  323. padding-bottom:100rpx;
  324. }
  325. .btns{
  326. right:10rpx;
  327. bottom:10rpx;
  328. position: absolute;
  329. .btn{
  330. width: 155upx;
  331. height: 64upx;
  332. line-height: 64upx;
  333. font-size: 26upx;
  334. font-family: PingFang SC;
  335. font-weight: 500;
  336. text-align: center;
  337. border-radius: 32upx;
  338. &.parse{
  339. background: #FF233C;
  340. color: #FFFFFF;
  341. }
  342. }
  343. }
  344. }
  345. .form-box{
  346. padding: 0 30upx;
  347. background: #FFFFFF;
  348. border-radius: 16upx;
  349. .form-item{
  350. height: 103upx;
  351. display: flex;
  352. align-items: center;
  353. justify-content: space-between;
  354. border-bottom: 1px solid #F1F1F1;
  355. &:last-child{
  356. border-bottom: none;
  357. }
  358. .label{
  359. min-width: 200upx;
  360. font-size: 30upx;
  361. font-family: PingFang SC;
  362. font-weight: 500;
  363. color: #222222;
  364. }
  365. .input-width{
  366. width: calc(100% - 200upx);
  367. text-align: right;
  368. }
  369. .form-input{
  370. font-size: 30upx;
  371. font-family: PingFang SC;
  372. font-weight: 500;
  373. color: #999999;
  374. text-align: right;
  375. }
  376. .birth-picker {
  377. display: flex;
  378. align-items: center;
  379. height: 44px;
  380. .right-box{
  381. width: 100%;
  382. display: flex;
  383. align-items: center;
  384. height: 44px;
  385. flex: 1;
  386. .arrow{
  387. width: 13upx;
  388. height: 23upx;
  389. margin-left: 15upx;
  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: #FF233C;
  429. border-radius: 44upx;
  430. }
  431. }
  432. }
  433. .icon-btn {
  434. display: flex;
  435. flex-direction: column;
  436. align-items: center;
  437. justify-content: center;
  438. image {
  439. width: 55rpx;
  440. height: 55rpx;
  441. }
  442. text {
  443. font-size: 26rpx;
  444. color: #222222;
  445. }
  446. }
  447. </style>