addAddress.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <template>
  2. <view class="content">
  3. <view class="inner">
  4. <view class="address-box">
  5. <textarea class="textarea" v-model="content" placeholder="请粘贴或输入文本,点击'识别'自动识别姓名、电话、地址,格式:深圳市龙岗区坂田街道长坑路西2巷2号202 黄大大 18888888888" />
  6. <view class="btns" >
  7. <view class="btn parse" @click="parseAddress()">识别</view>
  8. </view>
  9. </view>
  10. <view class="form-box">
  11. <view class="form-item">
  12. <text class="label">联系人</text>
  13. <input type="text" v-model="form.realName" maxlength="10" placeholder="姓名" class="form-input" />
  14. </view>
  15. <view class="form-item">
  16. <text class="label">手机号</text>
  17. <input type="number" v-model="form.phone" maxlength="11" placeholder="手机号" class="form-input" />
  18. </view>
  19. <view class="form-item">
  20. <text class="label">所在地区</text>
  21. <picker :value="multiIndex" class="birth-picker" mode="multiSelector" range-key="n" :range="addressList" @change="pickerChange" @columnchange="pickerColumnchange">
  22. <view class="right-box">
  23. <view class="input-box">
  24. <input type="text" v-model="form.address" placeholder="请选择" class="form-input" disabled="disabled" />
  25. </view>
  26. <image class="arrow" src="https://zkzh-2025.oss-cn-beijing.aliyuncs.com/shop/images/arrow_gray.png" mode=""></image>
  27. </view>
  28. </picker>
  29. </view>
  30. <view class="form-item">
  31. <text class="label">详细地址</text>
  32. <textarea class="form-textarea" v-model="form.detail" placeholder="请输入详细地址" />
  33. </view>
  34. </view>
  35. <!-- 设为默认地址 -->
  36. <view class="setting-box">
  37. <text class="label">设为默认地址</text>
  38. <evan-switch v-model="isDefault" activeColor="#FF233C" inactiveColor="rgba(0, 0, 0, 0.1)"></evan-switch>
  39. </view>
  40. </view>
  41. <view class="btn-box">
  42. <view class="sub-btn" @click="submit()">保存地址</view>
  43. <view v-if="type=='add'" class="icon-btn" style="width: 15%;" @click="openShare">
  44. <image src="/static/images/icon_wx.png" mode="aspectFill"></image>
  45. <text>分享</text>
  46. </view>
  47. </view>
  48. </view>
  49. </template>
  50. <script>
  51. import {parseAddress,getCity,getCitys,getAddressById,addAddress,editAddress,getCityId} from '@/api/address'
  52. import EvanSwitch from '@/components/evan-switch/evan-switch.vue'
  53. export default {
  54. components: {
  55. EvanSwitch
  56. },
  57. data() {
  58. return {
  59. content:null,
  60. type:null,
  61. id:null,
  62. isDefault: false,
  63. addressList:[[],[],[]],
  64. multiIndex:[0,0,0],
  65. address:[],
  66. form: {
  67. realName:null,
  68. phone:null,
  69. detail:null,
  70. address: null,
  71. isDefault: 0,
  72. }
  73. }
  74. },
  75. onLoad(option) {
  76. this.type=option.type;
  77. if(this.type=='edit'){
  78. uni.setNavigationBarTitle({
  79.   title:"修改收货地址"
  80. })
  81. this.id=option.id;
  82. this.getAddressById();
  83. }
  84. else{
  85. uni.setNavigationBarTitle({
  86.   title:"新增收货地址"
  87. })
  88. }
  89. this.getCitys()
  90. },
  91. methods: {
  92. openShare() {
  93. //#ifdef H5
  94. uni.showToast({
  95. title: '请使用APP操作',
  96. icon: 'none'
  97. });
  98. //#endif
  99. //#ifdef APP-PLUS
  100. uni.share({
  101. provider: "weixin",
  102. scene: 'WXSceneSession',
  103. type: 5,
  104. imageUrl: 'https://bjzmky-1323137866.cos.ap-chongqing.myqcloud.com/app/app-address.jpg', //分享封面图片
  105. title: '乐享韶华-邀请你填写地址,下单更方便', // 分享标题
  106. miniProgram: {
  107. id: getApp().globalData.miniprogamId,
  108. path: '/pages_user/user/addAddress?type=add',
  109. type: 0, //0-正式版; 1-测试版; 2-体验版。 默认值为0。
  110. webUrl: "http://uniapp.dcloud.io"
  111. },
  112. success: function(res) {
  113. uni.showToast({
  114. title: "分享成功",
  115. icon: 'none'
  116. });
  117. },
  118. fail: function(err) {
  119. // 此处是调起微信分享失败的回调
  120. },
  121. complete: (e) => {
  122. console.log("WXSceneSession", e)
  123. }
  124. });
  125. //#endif
  126. },
  127. parseAddress(){
  128. if(this.content==null||this.content==""){
  129. uni.showToast({ icon:'none',title: '请输入地址信息'});
  130. return;
  131. }
  132. var data={content:this.content};
  133. parseAddress(data).then(
  134. res => {
  135. if(res.code==200){
  136. this.form.realName=res.data.name
  137. this.form.phone=res.data.mobile
  138. this.form.address=res.data.provinceName+res.data.cityName+res.data.expAreaName
  139. this.form.province=res.data.provinceName
  140. this.form.city=res.data.cityName
  141. this.form.district=res.data.expAreaName
  142. this.form.detail=res.data.streetName+res.data.address
  143. this.getCityId(this.form.province,this.form.city,this.form.district)
  144. }else{
  145. uni.showToast({
  146. icon:'none',
  147. title: res.msg,
  148. });
  149. }
  150. },
  151. rej => {}
  152. );
  153. },
  154. // 获得cityId
  155. getCityId(province, city, district) {
  156. getCityId({
  157. province: province,
  158. city: city,
  159. district: district
  160. }).then(res => {
  161. if (res.code == 200) {
  162. this.form.cityId = res.cityId
  163. } else {
  164. uni.showToast({
  165. title: res.msg,
  166. icon: 'none'
  167. });
  168. }
  169. },
  170. rej => {}
  171. );
  172. },
  173. getAddressById(){
  174. var data={id:this.id};
  175. getAddressById(data).then(
  176. res => {
  177. if(res.code==200){
  178. this.form=res.data;
  179. this.isDefault=this.form.isDefault==1?true:false;
  180. this.form.address=this.form.province+this.form.city+this.form.district
  181. }else{
  182. uni.showToast({
  183. icon:'none',
  184. title: res.msg,
  185. });
  186. }
  187. },
  188. rej => {}
  189. );
  190. },
  191. submit(){
  192. if(this.type=="add"){
  193. this.addAddress()
  194. }
  195. else if(this.type=="edit"){
  196. this.editAddress()
  197. }
  198. },
  199. editAddress(){
  200. this.form.isDefault=this.isDefault?1:0
  201. editAddress(this.form).then(
  202. res => {
  203. if(res.code==200){
  204. uni.showToast({
  205. icon:'success',
  206. title: "操作成功",
  207. });
  208. setTimeout(function() {
  209. uni.$emit('refreshAddress');
  210. uni.navigateBack({
  211. delta: 1
  212. })
  213. }, 500);
  214. }else{
  215. uni.showToast({
  216. icon:'none',
  217. title: res.msg,
  218. });
  219. }
  220. },
  221. rej => {}
  222. );
  223. },
  224. addAddress(){
  225. this.form.isDefault=this.isDefault?1:0
  226. addAddress(this.form).then(
  227. res => {
  228. if(res.code==200){
  229. uni.showToast({
  230. icon:'success',
  231. title: "操作成功",
  232. });
  233. setTimeout(function() {
  234. uni.$emit('refreshAddress');
  235. uni.navigateBack({
  236. delta: 1
  237. })
  238. }, 500);
  239. }else{
  240. uni.showToast({
  241. icon:'none',
  242. title: res.msg,
  243. });
  244. }
  245. },
  246. rej => {}
  247. );
  248. },
  249. // 地区选择
  250. pickerChange(e) {
  251. this.multiIndex = e.detail.value;
  252. // 数组内的下标
  253. // 获取一级类目
  254. // 获取二级类目
  255. // 获取三级类目
  256. this.form.address=this.addressList[0][this.multiIndex[0]].n+this.addressList[1][this.multiIndex[1]].n+this.addressList[2][this.multiIndex[2]].n
  257. this.form.province=this.addressList[0][this.multiIndex[0]].n
  258. this.form.city=this.addressList[1][this.multiIndex[1]].n
  259. this.form.district=this.addressList[2][this.multiIndex[2]].n
  260. this.form.cityId=this.addressList[1][this.multiIndex[1]].v;
  261. },
  262. pickerColumnchange(e){
  263. // 第一列滑动
  264. if(e.detail.column === 0){
  265. this.multiIndex[0] = e.detail.value
  266. // console.log('第一列滑动');
  267. this.addressList[1] = this.address[this.multiIndex[0]].c;
  268. this.addressList[2] = this.address[this.multiIndex[0]].c[0].c
  269. // 第一列滑动 第二列 和第三列 都变为第一个
  270. this.multiIndex.splice(1, 1, 0)
  271. this.multiIndex.splice(2, 1, 0)
  272. }
  273. // 第二列滑动
  274. if(e.detail.column === 1){
  275. this.multiIndex[1] = e.detail.value
  276. // console.log('第二列滑动');
  277. // console.log(this.multiIndex)
  278. this.addressList[2] = this.address[this.multiIndex[0]].c[this.multiIndex[1]].c
  279. // 第二列 滑动 第三列 变成第一个
  280. this.multiIndex.splice(2, 1, 0)
  281. }
  282. // 第三列滑动
  283. if(e.detail.column === 2){
  284. this.multiIndex[2] = e.detail.value
  285. }
  286. },
  287. getCitys(){
  288. getCitys().then(
  289. res => {
  290. if(res.code==200){
  291. this.address=res.data
  292. for(var i=0; i<this.address.length; i++){
  293. this.addressList[0].push(this.address[i])
  294. }
  295. for(var i=0; i<this.address[0].c.length; i++){
  296. this.addressList[1].push(this.address[0].c[i])
  297. }
  298. for(var i=0; i<this.address[0].c[0].c.length; i++){
  299. this.addressList[2].push(this.address[0].c[0].c[i])
  300. }
  301. }else{
  302. uni.showToast({
  303. icon:'none',
  304. title: "请求失败",
  305. });
  306. }
  307. },
  308. rej => {}
  309. );
  310. }
  311. }
  312. }
  313. </script>
  314. <style lang="scss">
  315. page{
  316. height: 100%;
  317. }
  318. .content{
  319. height: 100%;
  320. display: flex;
  321. flex-direction: column;
  322. justify-content: space-between;
  323. .inner{
  324. height: calc(100% - 120upx);
  325. padding: 20upx;
  326. .address-box{
  327. padding: 30upx;
  328. background: #FFFFFF;
  329. border-radius: 16upx;
  330. margin-bottom: 20upx;
  331. position: relative;
  332. .textarea{
  333. width: 100%;
  334. height: 200upx;
  335. font-size: 30upx;
  336. color: #999999;
  337. padding-bottom:100rpx;
  338. }
  339. .btns{
  340. right:10rpx;
  341. bottom:10rpx;
  342. position: absolute;
  343. .btn{
  344. width: 155upx;
  345. height: 64upx;
  346. line-height: 64upx;
  347. font-size: 26upx;
  348. font-family: PingFang SC;
  349. font-weight: 500;
  350. text-align: center;
  351. border-radius: 32upx;
  352. &.parse{
  353. background: #FF233C;
  354. color: #FFFFFF;
  355. }
  356. }
  357. }
  358. }
  359. .form-box{
  360. padding: 0 30upx;
  361. background: #FFFFFF;
  362. border-radius: 16upx;
  363. .form-item{
  364. padding: 30upx 0;
  365. display: flex;
  366. align-items: flex-start;
  367. border-bottom: 1px solid #F1F1F1;
  368. &:last-child{
  369. border-bottom: none;
  370. }
  371. .label{
  372. width: 180upx;
  373. text-align: left;
  374. font-size: 30upx;
  375. line-height: 44upx;
  376. font-family: PingFang SC;
  377. font-weight: 500;
  378. color: #222222;
  379. flex-shrink: 0;
  380. }
  381. input{
  382. text-align: left;
  383. }
  384. .form-input{
  385. font-size: 30upx;
  386. font-family: PingFang SC;
  387. font-weight: 500;
  388. color: #999999;
  389. text-align: left;
  390. }
  391. .form-textarea{
  392. font-size: 30upx;
  393. color: #999999;
  394. height: 100upx;
  395. padding: 4upx 0;
  396. }
  397. .birth-picker {
  398. flex: 1;
  399. display: flex;
  400. align-items: center;
  401. .right-box{
  402. width: 100%;
  403. display: flex;
  404. align-items: center;
  405. .input-box{
  406. width: 470upx;
  407. }
  408. .arrow{
  409. width: 13upx;
  410. height: 23upx;
  411. margin-left: 20upx;
  412. }
  413. }
  414. }
  415. }
  416. }
  417. .setting-box{
  418. height: 88upx;
  419. background: #FFFFFF;
  420. border-radius: 16upx;
  421. margin-top: 20upx;
  422. padding: 0 30upx;
  423. display: flex;
  424. align-items: center;
  425. justify-content: space-between;
  426. .label{
  427. font-size: 28upx;
  428. font-family: PingFang SC;
  429. font-weight: 500;
  430. color: #111111;
  431. }
  432. }
  433. }
  434. .btn-box{
  435. height: 120upx;
  436. padding: 0 30upx;
  437. display: flex;
  438. align-items: center;
  439. justify-content: center;
  440. background: #FFFFFF;
  441. .sub-btn{
  442. width: 100%;
  443. height: 88upx;
  444. line-height: 88upx;
  445. text-align: center;
  446. font-size: 30upx;
  447. font-family: PingFang SC;
  448. font-weight: bold;
  449. color: #FFFFFF;
  450. background: #FF233C;
  451. border-radius: 44upx;
  452. }
  453. }
  454. }
  455. .icon-btn {
  456. display: flex;
  457. flex-direction: column;
  458. align-items: center;
  459. justify-content: center;
  460. image {
  461. width: 55rpx;
  462. height: 55rpx;
  463. }
  464. text {
  465. font-size: 26rpx;
  466. color: #222222;
  467. }
  468. }
  469. </style>