addAddress.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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} 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. }else{
  144. uni.showToast({
  145. icon:'none',
  146. title: res.msg,
  147. });
  148. }
  149. },
  150. rej => {}
  151. );
  152. },
  153. getAddressById(){
  154. var data={id:this.id};
  155. getAddressById(data).then(
  156. res => {
  157. if(res.code==200){
  158. this.form=res.data;
  159. this.isDefault=this.form.isDefault==1?true:false;
  160. this.form.address=this.form.province+this.form.city+this.form.district
  161. }else{
  162. uni.showToast({
  163. icon:'none',
  164. title: res.msg,
  165. });
  166. }
  167. },
  168. rej => {}
  169. );
  170. },
  171. submit(){
  172. if(this.type=="add"){
  173. this.addAddress()
  174. }
  175. else if(this.type=="edit"){
  176. this.editAddress()
  177. }
  178. },
  179. editAddress(){
  180. this.form.isDefault=this.isDefault?1:0
  181. editAddress(this.form).then(
  182. res => {
  183. if(res.code==200){
  184. uni.showToast({
  185. icon:'success',
  186. title: "操作成功",
  187. });
  188. setTimeout(function() {
  189. uni.$emit('refreshAddress');
  190. uni.navigateBack({
  191. delta: 1
  192. })
  193. }, 500);
  194. }else{
  195. uni.showToast({
  196. icon:'none',
  197. title: res.msg,
  198. });
  199. }
  200. },
  201. rej => {}
  202. );
  203. },
  204. addAddress(){
  205. this.form.isDefault=this.isDefault?1:0
  206. addAddress(this.form).then(
  207. res => {
  208. if(res.code==200){
  209. uni.showToast({
  210. icon:'success',
  211. title: "操作成功",
  212. });
  213. setTimeout(function() {
  214. uni.$emit('refreshAddress');
  215. uni.navigateBack({
  216. delta: 1
  217. })
  218. }, 500);
  219. }else{
  220. uni.showToast({
  221. icon:'none',
  222. title: res.msg,
  223. });
  224. }
  225. },
  226. rej => {}
  227. );
  228. },
  229. // 地区选择
  230. pickerChange(e) {
  231. this.multiIndex = e.detail.value;
  232. // 数组内的下标
  233. // 获取一级类目
  234. // 获取二级类目
  235. // 获取三级类目
  236. this.form.address=this.addressList[0][this.multiIndex[0]].n+this.addressList[1][this.multiIndex[1]].n+this.addressList[2][this.multiIndex[2]].n
  237. this.form.province=this.addressList[0][this.multiIndex[0]].n
  238. this.form.city=this.addressList[1][this.multiIndex[1]].n
  239. this.form.district=this.addressList[2][this.multiIndex[2]].n
  240. this.form.cityId=this.addressList[1][this.multiIndex[1]].v;
  241. },
  242. pickerColumnchange(e){
  243. // 第一列滑动
  244. if(e.detail.column === 0){
  245. this.multiIndex[0] = e.detail.value
  246. // console.log('第一列滑动');
  247. this.addressList[1] = this.address[this.multiIndex[0]].c;
  248. this.addressList[2] = this.address[this.multiIndex[0]].c[0].c
  249. // 第一列滑动 第二列 和第三列 都变为第一个
  250. this.multiIndex.splice(1, 1, 0)
  251. this.multiIndex.splice(2, 1, 0)
  252. }
  253. // 第二列滑动
  254. if(e.detail.column === 1){
  255. this.multiIndex[1] = e.detail.value
  256. // console.log('第二列滑动');
  257. // console.log(this.multiIndex)
  258. this.addressList[2] = this.address[this.multiIndex[0]].c[this.multiIndex[1]].c
  259. // 第二列 滑动 第三列 变成第一个
  260. this.multiIndex.splice(2, 1, 0)
  261. }
  262. // 第三列滑动
  263. if(e.detail.column === 2){
  264. this.multiIndex[2] = e.detail.value
  265. }
  266. },
  267. getCitys(){
  268. getCitys().then(
  269. res => {
  270. if(res.code==200){
  271. this.address=res.data
  272. for(var i=0; i<this.address.length; i++){
  273. this.addressList[0].push(this.address[i])
  274. }
  275. for(var i=0; i<this.address[0].c.length; i++){
  276. this.addressList[1].push(this.address[0].c[i])
  277. }
  278. for(var i=0; i<this.address[0].c[0].c.length; i++){
  279. this.addressList[2].push(this.address[0].c[0].c[i])
  280. }
  281. }else{
  282. uni.showToast({
  283. icon:'none',
  284. title: "请求失败",
  285. });
  286. }
  287. },
  288. rej => {}
  289. );
  290. }
  291. }
  292. }
  293. </script>
  294. <style lang="scss">
  295. page{
  296. height: 100%;
  297. }
  298. .content{
  299. height: 100%;
  300. display: flex;
  301. flex-direction: column;
  302. justify-content: space-between;
  303. .inner{
  304. height: calc(100% - 120upx);
  305. padding: 20upx;
  306. .address-box{
  307. padding: 30upx;
  308. background: #FFFFFF;
  309. border-radius: 16upx;
  310. margin-bottom: 20upx;
  311. position: relative;
  312. .textarea{
  313. width: 100%;
  314. height: 200upx;
  315. font-size: 30upx;
  316. color: #999999;
  317. padding-bottom:100rpx;
  318. }
  319. .btns{
  320. right:10rpx;
  321. bottom:10rpx;
  322. position: absolute;
  323. .btn{
  324. width: 155upx;
  325. height: 64upx;
  326. line-height: 64upx;
  327. font-size: 26upx;
  328. font-family: PingFang SC;
  329. font-weight: 500;
  330. text-align: center;
  331. border-radius: 32upx;
  332. &.parse{
  333. background: #FF233C;
  334. color: #FFFFFF;
  335. }
  336. }
  337. }
  338. }
  339. .form-box{
  340. padding: 0 30upx;
  341. background: #FFFFFF;
  342. border-radius: 16upx;
  343. .form-item{
  344. padding: 30upx 0;
  345. display: flex;
  346. align-items: flex-start;
  347. border-bottom: 1px solid #F1F1F1;
  348. &:last-child{
  349. border-bottom: none;
  350. }
  351. .label{
  352. width: 180upx;
  353. text-align: left;
  354. font-size: 30upx;
  355. line-height: 44upx;
  356. font-family: PingFang SC;
  357. font-weight: 500;
  358. color: #222222;
  359. flex-shrink: 0;
  360. }
  361. input{
  362. text-align: left;
  363. }
  364. .form-input{
  365. font-size: 30upx;
  366. font-family: PingFang SC;
  367. font-weight: 500;
  368. color: #999999;
  369. text-align: left;
  370. }
  371. .form-textarea{
  372. font-size: 30upx;
  373. color: #999999;
  374. height: 100upx;
  375. padding: 4upx 0;
  376. }
  377. .birth-picker {
  378. flex: 1;
  379. display: flex;
  380. align-items: center;
  381. .right-box{
  382. width: 100%;
  383. display: flex;
  384. align-items: center;
  385. .input-box{
  386. width: 470upx;
  387. }
  388. .arrow{
  389. width: 13upx;
  390. height: 23upx;
  391. margin-left: 20upx;
  392. }
  393. }
  394. }
  395. }
  396. }
  397. .setting-box{
  398. height: 88upx;
  399. background: #FFFFFF;
  400. border-radius: 16upx;
  401. margin-top: 20upx;
  402. padding: 0 30upx;
  403. display: flex;
  404. align-items: center;
  405. justify-content: space-between;
  406. .label{
  407. font-size: 28upx;
  408. font-family: PingFang SC;
  409. font-weight: 500;
  410. color: #111111;
  411. }
  412. }
  413. }
  414. .btn-box{
  415. height: 120upx;
  416. padding: 0 30upx;
  417. display: flex;
  418. align-items: center;
  419. justify-content: center;
  420. background: #FFFFFF;
  421. .sub-btn{
  422. width: 100%;
  423. height: 88upx;
  424. line-height: 88upx;
  425. text-align: center;
  426. font-size: 30upx;
  427. font-family: PingFang SC;
  428. font-weight: bold;
  429. color: #FFFFFF;
  430. background: #FF233C;
  431. border-radius: 44upx;
  432. }
  433. }
  434. }
  435. .icon-btn {
  436. display: flex;
  437. flex-direction: column;
  438. align-items: center;
  439. justify-content: center;
  440. image {
  441. width: 55rpx;
  442. height: 55rpx;
  443. }
  444. text {
  445. font-size: 26rpx;
  446. color: #222222;
  447. }
  448. }
  449. </style>