addAddress.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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://bjczwh.oss-cn-beijing.aliyuncs.com/app/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="#2BC7B9" inactiveColor="rgba(0, 0, 0, 0.1)"></evan-switch>
  39. </view>
  40. <button @tap="getAddress" class="getwxaddress">一键获取微信地址</button>
  41. </view>
  42. <view class="btn-box">
  43. <view class="sub-btn" @click="submit()">保存地址</view>
  44. </view>
  45. </view>
  46. </template>
  47. <script>
  48. import {parseAddress,getCity,getCitys,getAddressById,addAddress,editAddress} from '@/api/address'
  49. import EvanSwitch from '@/components/evan-switch/evan-switch.vue'
  50. export default {
  51. components: {
  52. EvanSwitch
  53. },
  54. data() {
  55. return {
  56. content:null,
  57. type:null,
  58. id:null,
  59. isDefault: false,
  60. addressList:[[],[],[]],
  61. multiIndex:[0,0,0],
  62. address:[],
  63. form: {
  64. realName:null,
  65. phone:null,
  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.id=option.id;
  79. this.getAddressById();
  80. }
  81. else{
  82. uni.setNavigationBarTitle({
  83.   title:"新增收货地址"
  84. })
  85. }
  86. this.getCitys()
  87. },
  88. methods: {
  89. async getAddress(){
  90. // 调用微信小程序的chooseAddress接口
  91. uni.chooseAddress({
  92. success: (res) => {
  93. console.log('获取地址成功', res);
  94. this.form.realName = res.userName;
  95. this.form.phone = res.telNumber;
  96. this.form.address =res.cityName+res.countyName;
  97. this.form.detail = res.detailInfo;
  98. // 可以在这里将地址信息保存到本地或发送到服务器
  99. uni.setStorageSync('userAddress', res);
  100. },
  101. fail: (err) => {
  102. console.error('获取地址失败', err);
  103. // 判断是否是用户拒绝授权
  104. if (err.errMsg.indexOf('auth deny') > -1 || err.errMsg.indexOf('用户拒绝') > -1) {
  105. uni.showModal({
  106. title: '授权提示',
  107. content: '请允许获取地址信息,否则无法完成操作',
  108. confirmText: '去设置',
  109. success: (modalRes) => {
  110. if (modalRes.confirm) {
  111. // 打开设置页面,让用户手动授权
  112. uni.openSetting({
  113. success: (settingRes) => {
  114. console.log('设置页面返回', settingRes);
  115. }
  116. });
  117. }
  118. }
  119. });
  120. } else {
  121. uni.showToast({
  122. title: '获取地址失败,请重试',
  123. icon: 'none'
  124. });
  125. }
  126. }
  127. });
  128. },
  129. parseAddress(){
  130. if(this.content==null||this.content==""){
  131. uni.showToast({ icon:'none',title: '请输入地址信息'});
  132. return;
  133. }
  134. var data={content:this.content};
  135. parseAddress(data).then(
  136. res => {
  137. if(res.code==200){
  138. this.form.realName=res.data.name
  139. this.form.phone=res.data.mobile
  140. this.form.address=res.data.provinceName+res.data.cityName+res.data.expAreaName
  141. this.form.province=res.data.provinceName
  142. this.form.city=res.data.cityName
  143. this.form.district=res.data.expAreaName
  144. this.form.detail=res.data.streetName+res.data.address
  145. }else{
  146. uni.showToast({
  147. icon:'none',
  148. title: res.msg,
  149. });
  150. }
  151. },
  152. rej => {}
  153. );
  154. },
  155. getAddressById(){
  156. var data={id:this.id};
  157. getAddressById(data).then(
  158. res => {
  159. if(res.code==200){
  160. this.form=res.data;
  161. this.isDefault=this.form.isDefault==1?true:false;
  162. this.form.address=this.form.province+this.form.city+this.form.district
  163. }else{
  164. uni.showToast({
  165. icon:'none',
  166. title: res.msg,
  167. });
  168. }
  169. },
  170. rej => {}
  171. );
  172. },
  173. submit(){
  174. if(this.type=="add"){
  175. this.addAddress()
  176. }
  177. else if(this.type=="edit"){
  178. this.editAddress()
  179. }
  180. },
  181. editAddress(){
  182. this.form.isDefault=this.isDefault?1:0
  183. editAddress(this.form).then(
  184. res => {
  185. if(res.code==200){
  186. uni.showToast({
  187. icon:'success',
  188. title: "操作成功",
  189. });
  190. setTimeout(function() {
  191. uni.$emit('refreshAddress');
  192. uni.navigateBack({
  193. delta: 1
  194. })
  195. }, 500);
  196. }else{
  197. uni.showToast({
  198. icon:'none',
  199. title: res.msg,
  200. });
  201. }
  202. },
  203. rej => {}
  204. );
  205. },
  206. addAddress(){
  207. this.form.isDefault=this.isDefault?1:0
  208. addAddress(this.form).then(
  209. res => {
  210. if(res.code==200){
  211. uni.showToast({
  212. icon:'success',
  213. title: "操作成功",
  214. });
  215. setTimeout(function() {
  216. uni.$emit('refreshAddress');
  217. uni.navigateBack({
  218. delta: 1
  219. })
  220. }, 500);
  221. }else{
  222. uni.showToast({
  223. icon:'none',
  224. title: res.msg,
  225. });
  226. }
  227. },
  228. rej => {}
  229. );
  230. },
  231. // 地区选择
  232. pickerChange(e) {
  233. this.multiIndex = e.detail.value;
  234. // 数组内的下标
  235. // 获取一级类目
  236. // 获取二级类目
  237. // 获取三级类目
  238. this.form.address=this.addressList[0][this.multiIndex[0]].n+this.addressList[1][this.multiIndex[1]].n+this.addressList[2][this.multiIndex[2]].n
  239. this.form.province=this.addressList[0][this.multiIndex[0]].n
  240. this.form.city=this.addressList[1][this.multiIndex[1]].n
  241. this.form.district=this.addressList[2][this.multiIndex[2]].n
  242. this.form.cityId=this.addressList[1][this.multiIndex[1]].v;
  243. },
  244. pickerColumnchange(e){
  245. // 第一列滑动
  246. if(e.detail.column === 0){
  247. this.multiIndex[0] = e.detail.value
  248. // console.log('第一列滑动');
  249. this.addressList[1] = this.address[this.multiIndex[0]].c;
  250. this.addressList[2] = this.address[this.multiIndex[0]].c[0].c
  251. // 第一列滑动 第二列 和第三列 都变为第一个
  252. this.multiIndex.splice(1, 1, 0)
  253. this.multiIndex.splice(2, 1, 0)
  254. }
  255. // 第二列滑动
  256. if(e.detail.column === 1){
  257. this.multiIndex[1] = e.detail.value
  258. // console.log('第二列滑动');
  259. // console.log(this.multiIndex)
  260. this.addressList[2] = this.address[this.multiIndex[0]].c[this.multiIndex[1]].c
  261. // 第二列 滑动 第三列 变成第一个
  262. this.multiIndex.splice(2, 1, 0)
  263. }
  264. // 第三列滑动
  265. if(e.detail.column === 2){
  266. this.multiIndex[2] = e.detail.value
  267. }
  268. },
  269. getCitys(){
  270. getCitys().then(
  271. res => {
  272. if(res.code==200){
  273. this.address=res.data
  274. for(var i=0; i<this.address.length; i++){
  275. this.addressList[0].push(this.address[i])
  276. }
  277. for(var i=0; i<this.address[0].c.length; i++){
  278. this.addressList[1].push(this.address[0].c[i])
  279. }
  280. for(var i=0; i<this.address[0].c[0].c.length; i++){
  281. this.addressList[2].push(this.address[0].c[0].c[i])
  282. }
  283. }else{
  284. uni.showToast({
  285. icon:'none',
  286. title: "请求失败",
  287. });
  288. }
  289. },
  290. rej => {}
  291. );
  292. }
  293. }
  294. }
  295. </script>
  296. <style lang="scss">
  297. page{
  298. height: 100%;
  299. }
  300. .getwxaddress{
  301. border: none;
  302. background-color: #3BA737;
  303. color: #FFFFFF;
  304. margin-top: 20rpx;
  305. border-radius: 20rpx;
  306. width: 70%;
  307. }
  308. .content{
  309. height: 100%;
  310. display: flex;
  311. flex-direction: column;
  312. justify-content: space-between;
  313. .inner{
  314. height: calc(100% - 120upx);
  315. padding: 20upx;
  316. .address-box{
  317. padding: 30upx;
  318. background: #FFFFFF;
  319. border-radius: 16upx;
  320. margin-bottom: 20upx;
  321. position: relative;
  322. .textarea{
  323. width: 100%;
  324. height: 200upx;
  325. font-size: 30upx;
  326. color: #999999;
  327. padding-bottom:100rpx;
  328. }
  329. .btns{
  330. right:10rpx;
  331. bottom:10rpx;
  332. position: absolute;
  333. .btn{
  334. width: 155upx;
  335. height: 64upx;
  336. line-height: 64upx;
  337. font-size: 26upx;
  338. font-family: PingFang SC;
  339. font-weight: 500;
  340. text-align: center;
  341. border-radius: 32upx;
  342. &.parse{
  343. background: #3BA737;
  344. color: #FFFFFF;
  345. }
  346. }
  347. }
  348. }
  349. .form-box{
  350. padding: 0 30upx;
  351. background: #FFFFFF;
  352. border-radius: 16upx;
  353. .form-item{
  354. padding: 30upx 0;
  355. display: flex;
  356. align-items: flex-start;
  357. border-bottom: 1px solid #F1F1F1;
  358. &:last-child{
  359. border-bottom: none;
  360. }
  361. .label{
  362. width: 180upx;
  363. text-align: left;
  364. font-size: 30upx;
  365. line-height: 44upx;
  366. font-family: PingFang SC;
  367. font-weight: 500;
  368. color: #222222;
  369. flex-shrink: 0;
  370. }
  371. input{
  372. text-align: left;
  373. }
  374. .form-input{
  375. font-size: 30upx;
  376. font-family: PingFang SC;
  377. font-weight: 500;
  378. color: #999999;
  379. text-align: left;
  380. }
  381. .form-textarea{
  382. font-size: 30upx;
  383. color: #999999;
  384. height: 100upx;
  385. padding: 4upx 0;
  386. }
  387. .birth-picker {
  388. flex: 1;
  389. display: flex;
  390. align-items: center;
  391. .right-box{
  392. width: 100%;
  393. display: flex;
  394. align-items: center;
  395. .input-box{
  396. width: 470upx;
  397. }
  398. .arrow{
  399. width: 13upx;
  400. height: 23upx;
  401. margin-left: 20upx;
  402. }
  403. }
  404. }
  405. }
  406. }
  407. .setting-box{
  408. height: 88upx;
  409. background: #FFFFFF;
  410. border-radius: 16upx;
  411. margin-top: 20upx;
  412. padding: 0 30upx;
  413. display: flex;
  414. align-items: center;
  415. justify-content: space-between;
  416. .label{
  417. font-size: 28upx;
  418. font-family: PingFang SC;
  419. font-weight: 500;
  420. color: #111111;
  421. }
  422. }
  423. }
  424. .btn-box{
  425. height: 120upx;
  426. padding: 0 30upx;
  427. display: flex;
  428. align-items: center;
  429. justify-content: center;
  430. background: #FFFFFF;
  431. .sub-btn{
  432. width: 100%;
  433. height: 88upx;
  434. line-height: 88upx;
  435. text-align: center;
  436. font-size: 30upx;
  437. font-family: PingFang SC;
  438. font-weight: bold;
  439. color: #FFFFFF;
  440. background: #3BA737;
  441. border-radius: 44upx;
  442. }
  443. }
  444. }
  445. </style>