index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <template>
  2. <view :style="viewColor">
  3. <form @submit="formSubmit" report-submit='true'>
  4. <view class='addAddress'>
  5. <view class='list'>
  6. <view class='item acea-row row-between-wrapper'>
  7. <view class='name'>姓名</view>
  8. <input type='text' placeholder='请输入姓名' name='real_name' :value="userAddress.real_name" placeholder-class='placeholder'></input>
  9. </view>
  10. <view class='item acea-row row-between-wrapper'>
  11. <view class='name'>联系电话</view>
  12. <input type='text' placeholder='请输入联系电话' name="phone" :value='userAddress.phone' placeholder-class='placeholder'></input>
  13. </view>
  14. <view class='item acea-row row-between-wrapper'>
  15. <view class='name'>所在地区</view>
  16. <view class="region">
  17. <view class="region_count" @click="changeRegion">
  18. <text v-if="!addressInfo.length" style="color:#cdcdcd;">请选择地址</text>
  19. <text v-else>{{addressText}}</text>
  20. <text class="iconfont icon-xiangyou"></text>
  21. </view>
  22. </view>
  23. </view>
  24. <view class='item acea-row row-between-wrapper'>
  25. <view class='name'>详细地址</view>
  26. <input type='text' class="location-input" placeholder='请填写具体地址' name='detail' placeholder-class='placeholder' v-model="userAddress.detail"></input>
  27. <view class="location" @click="selfLocation">
  28. <text class="iconfont icon-chakanditu"></text>
  29. <br>
  30. 定位
  31. </view>
  32. </view>
  33. </view>
  34. <view class='default acea-row row-middle'>
  35. <checkbox-group @change='ChangeIsDefault'>
  36. <checkbox :checked="userAddress.is_default ? true : false" />设置为默认地址</checkbox-group>
  37. </view>
  38. <button class='keepBnt' form-type="submit" :disabled="loading">立即保存</button>
  39. <!-- #ifdef MP -->
  40. <view class="wechatAddress" v-if="!address_id" @click="getWxAddress">导入微信地址</view>
  41. <!-- #endif -->
  42. <!-- #ifdef H5 -->
  43. <view class="wechatAddress" v-if="this.$wechat.isWeixin() && !address_id" @click="getAddress">导入微信地址</view>
  44. <!-- #endif -->
  45. </view>
  46. </form>
  47. <areaWindow ref="areaWindow" :display="display" :address="addressInfo"
  48. @submit="OnChangeAddress" @changeClose="changeClose"></areaWindow>
  49. </view>
  50. </template>
  51. <script>
  52. // +----------------------------------------------------------------------
  53. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  54. // +----------------------------------------------------------------------
  55. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  56. // +----------------------------------------------------------------------
  57. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  58. // +----------------------------------------------------------------------
  59. // | Author: CRMEB Team <admin@crmeb.com>
  60. // +----------------------------------------------------------------------
  61. import { editAddress, getAddressDetail } from '@/api/user.js';
  62. import { getCityV2, getCityList } from '@/api/api.js';
  63. import { mapGetters } from "vuex";
  64. import areaWindow from '@/components/areaWindow';
  65. import { getGeocoder } from '@/api/store.js';
  66. import { toLogin } from '@/libs/login.js';
  67. export default {
  68. components: {
  69. areaWindow,
  70. },
  71. data() {
  72. return {
  73. cartId: '', //购物车id
  74. pinkId: 0, //拼团id
  75. couponId: 0, //优惠券id
  76. address_id: 0, //地址id
  77. productType: 0,
  78. userAddress: {
  79. is_default: false
  80. }, //地址详情
  81. region: ['省', '市', '区'],
  82. valueRegion: [0, 0, 0],
  83. district: [],
  84. multiArray: [],
  85. multiIndex: [0, 0, 0],
  86. cityId: 0,
  87. display: false,
  88. addressInfo:[],
  89. latitude: '',
  90. longitude: '',
  91. loading: false
  92. };
  93. },
  94. computed: {...mapGetters(['isLogin','viewColor']),
  95. addressText(){
  96. return this.addressInfo.map(v=>v.name).join('/');
  97. }
  98. },
  99. onLoad(options) {
  100. if (this.isLogin) {
  101. this.cartId = options.cartId || '';
  102. this.pinkId = options.pinkId || 0;
  103. this.couponId = options.couponId || 0;
  104. this.address_id = options.id || 0;
  105. this.productType = options.product_type || 0;
  106. uni.setNavigationBarTitle({
  107. title: options.id ? '修改地址' : '添加地址'
  108. })
  109. this.getUserAddress();
  110. this.getCityList();
  111. } else {
  112. toLogin()
  113. }
  114. },
  115. methods: {
  116. OnChangeAddress(address){
  117. this.addressInfo = address;
  118. },
  119. // 地址数据
  120. getCityList: function() {
  121. let that = this;
  122. getCityV2(0).then(res => {
  123. this.district = res.data
  124. })
  125. },
  126. changeRegion(){
  127. this.display = true;
  128. },
  129. // 关闭地址弹窗;
  130. changeClose: function() {
  131. this.display = false;
  132. },
  133. getUserAddress: function() {
  134. if (!this.address_id) return false;
  135. let that = this;
  136. getAddressDetail(this.address_id).then(res => {
  137. let region = [res.data.province, res.data.city, res.data.district];
  138. that.$set(that, 'userAddress', res.data);
  139. that.$set(that, 'region', region);
  140. that.city_id = res.data.city_id
  141. that.addressInfo = res.data.area
  142. });
  143. },
  144. // 导入共享地址(小程序)
  145. getWxAddress: function() {
  146. let that = this;
  147. uni.authorize({
  148. scope: 'scope.address',
  149. success: function(res) {
  150. uni.chooseAddress({
  151. success: function(res) {
  152. getCityList(res.provinceName+'/'+res.cityName+'/'+res.countyName).then(res=>{
  153. that.addressInfo = res.data;
  154. })
  155. that.userAddress.real_name = res.userName;
  156. that.userAddress.phone = res.telNumber;
  157. that.userAddress.detail = res.detailInfo;
  158. },
  159. fail: function(res) {
  160. uni.showToast({
  161. title: res.errMsg,
  162. icon: 'none',
  163. duration: 1000
  164. });
  165. if (res.errMsg == 'chooseAddress:cancel') return that.$util.Tips({
  166. title: '取消选择'
  167. });
  168. },
  169. })
  170. },
  171. fail: function(res) {
  172. uni.showModal({
  173. title: '您已拒绝导入微信地址权限',
  174. content: '是否进入权限管理,调整授权?',
  175. success(res) {
  176. if (res.confirm) {
  177. uni.openSetting({
  178. success: function(res) {}
  179. });
  180. } else if (res.cancel) {
  181. return that.$util.Tips({
  182. title: '已取消!'
  183. });
  184. }
  185. }
  186. })
  187. },
  188. })
  189. },
  190. // 导入共享地址(微信);
  191. getAddress() {
  192. let that = this;
  193. that.$wechat.openAddress().then(res => {
  194. getCityList(res.provinceName+'/'+res.cityName+'/'+res.countryName).then(res=>{
  195. that.addressInfo = res.data;
  196. })
  197. that.userAddress.real_name = res.userName;
  198. that.userAddress.phone = res.telNumber;
  199. that.userAddress.detail = res.detailInfo;
  200. }).catch(err => {
  201. console.log(err);
  202. });
  203. },
  204. selfLocation() {
  205. let self = this
  206. uni.showLoading({
  207. title: '定位中',
  208. mask: true,
  209. });
  210. uni.getLocation({
  211. type: 'gcj02',
  212. success: (res) => {
  213. let latitude, longitude;
  214. latitude = res.latitude.toString();
  215. longitude = res.longitude.toString();
  216. self.latitude = res.latitude
  217. self.longitude = res.longitude
  218. getGeocoder({
  219. lat: latitude,
  220. long: longitude
  221. }).then(res => {
  222. const data = res.data;
  223. getCityList(data.address_component.province+'/'+data.address_component.city+'/'+data.address_component.district+'/'+(!data.address_reference.town ? '' : data.address_reference.town.title)).then(res=>{
  224. self.addressInfo = res.data;
  225. self.$set(self.userAddress, 'detail', data.formatted_addresses.recommend);
  226. uni.hideLoading();
  227. }).catch(e=>{
  228. uni.hideLoading();
  229. uni.showToast({
  230. title: '定位匹配失败,请手动输入地址',
  231. icon: 'none',
  232. duration: 1000
  233. });
  234. })
  235. }).catch(e=>{
  236. uni.hideLoading();
  237. uni.showToast({
  238. title: '定位获取失败,请手动输入地址',
  239. icon: 'none',
  240. duration: 1000
  241. });
  242. })
  243. },
  244. fail: (res) => {
  245. uni.hideLoading();
  246. uni.showToast({
  247. title: res.errMsg,
  248. icon: 'none',
  249. duration: 1000
  250. });
  251. }
  252. });
  253. },
  254. /**
  255. * 提交用户添加地址
  256. *
  257. */
  258. formSubmit: function(e) {
  259. let that = this,
  260. value = e.detail.value;
  261. if (!value.real_name) return that.$util.Tips({
  262. title: '请填写收货人姓名'
  263. });
  264. if (!value.phone) return that.$util.Tips({
  265. title: '请填写联系电话'
  266. });
  267. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(value.phone)) return that.$util.Tips({
  268. title: '请输入正确的手机号码'
  269. });
  270. if (!that.addressInfo.length) return that.$util.Tips({
  271. title: '请选择所在地区'
  272. });
  273. if (!value.detail) return that.$util.Tips({
  274. title: '请填写详细地址'
  275. });
  276. value.address_id = that.address_id;
  277. value.is_default = that.userAddress.is_default ? 1 : 0;
  278. value.area = that.addressInfo;
  279. uni.showLoading({
  280. title: '保存中',
  281. mask: true
  282. })
  283. that.loading = true;
  284. editAddress(value).then(res => {
  285. if (that.address_id)
  286. that.$util.Tips({
  287. title: '修改成功',
  288. icon: 'success'
  289. });
  290. else
  291. that.$util.Tips({
  292. title: '添加成功',
  293. icon: 'success'
  294. });
  295. setTimeout(function() {
  296. if (that.cartId) {
  297. let cartId = that.cartId;
  298. let pinkId = that.pinkId;
  299. let couponId = that.couponId;
  300. that.cartId = '';
  301. that.pinkId = '';
  302. that.couponId = '';
  303. uni.$emit('updataAddress')
  304. if(that.productType == 20){
  305. uni.navigateTo({
  306. url: '/pages/points_mall/integral_order?cartId=' + cartId + '&addressId=' + (that.id ? that.id : res.data
  307. .address_id) + '&pinkId=' + pinkId + '&couponId=' + couponId
  308. });
  309. }else{
  310. uni.navigateTo({
  311. url: '/pages/users/order_confirm/index?cartId=' + cartId + '&addressId=' + (that.id ? that.id : res.data
  312. .address_id) + '&pinkId=' + pinkId + '&couponId=' + couponId
  313. });
  314. }
  315. } else {
  316. // #ifdef H5
  317. return history.back();
  318. // #endif
  319. // #ifndef H5
  320. return uni.navigateBack({
  321. delta: 1,
  322. })
  323. // #endif
  324. }
  325. that.loading = false;
  326. }, 1000);
  327. }).catch(err => {
  328. that.loading = false;
  329. return that.$util.Tips({
  330. title: err
  331. });
  332. })
  333. },
  334. ChangeIsDefault: function(e) {
  335. this.$set(this.userAddress, 'is_default', !this.userAddress.is_default);
  336. }
  337. }
  338. }
  339. </script>
  340. <style scoped lang="scss">
  341. /deep/checkbox .uni-checkbox-input.uni-checkbox-input-checked,
  342. /deep/checkbox .wx-checkbox-input.wx-checkbox-input-checked {
  343. border: 1px solid var(--view-theme)!important;
  344. background-color: var(--view-theme)!important;
  345. color: #fff!important;
  346. }
  347. .addAddress .list {
  348. background-color: #fff;
  349. }
  350. .addAddress .list .item {
  351. padding: 30rpx;
  352. border-top: 1px solid #eee;
  353. position: relative;
  354. }
  355. .addAddress .list .item .location{
  356. position: absolute;
  357. right: 30rpx;
  358. top: 15rpx;
  359. text-align: center;
  360. font-size: 20rpx;
  361. .iconfont{
  362. color: var(--view-theme);
  363. }
  364. }
  365. .addAddress .list .item .name {
  366. width: 195rpx;
  367. font-size: 30rpx;
  368. color: #333;
  369. }
  370. .addAddress .list .item .address {
  371. flex: 1;
  372. margin-left: 20rpx;
  373. }
  374. .addAddress .list .item input,.region .region_count {
  375. width: 475rpx;
  376. font-size: 30rpx;
  377. box-sizing: border-box;
  378. }
  379. .region .region_count{
  380. height: 42rpx;
  381. line-height: 42rpx;
  382. .icon-xiangyou{
  383. float: right;
  384. }
  385. }
  386. .addAddress .list .location-input{
  387. padding-right: 70rpx;
  388. }
  389. .addAddress .list .item .placeholder {
  390. color: #ccc;
  391. }
  392. .addAddress .list .item picker {
  393. width: 475rpx;
  394. }
  395. .addAddress .list .item picker .picker {
  396. width: 410rpx;
  397. font-size: 30rpx;
  398. }
  399. .addAddress .list .item picker .iconfont {
  400. font-size: 43rpx;
  401. }
  402. .addAddress .default {
  403. padding: 0 30rpx;
  404. height: 90rpx;
  405. background-color: #fff;
  406. margin-top: 23rpx;
  407. }
  408. .addAddress .default checkbox {
  409. margin-right: 15rpx;
  410. }
  411. .addAddress .keepBnt {
  412. width: 690rpx;
  413. height: 86rpx;
  414. border-radius: 50rpx;
  415. text-align: center;
  416. line-height: 86rpx;
  417. margin: 50rpx auto;
  418. font-size: 32rpx;
  419. color: #fff;
  420. background-color: var(--view-theme);
  421. }
  422. .addAddress .keepBnt[disabled]{
  423. background: #bbb;
  424. }
  425. .addAddress .wechatAddress {
  426. width: 690rpx;
  427. height: 86rpx;
  428. border-radius: 50rpx;
  429. text-align: center;
  430. line-height: 86rpx;
  431. margin: 0 auto;
  432. font-size: 32rpx;
  433. color: var(--view-theme);
  434. border: 1px solid var(--view-theme);
  435. }
  436. </style>