address.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <template>
  2. <view class="container">
  3. <view class="top">
  4. <view class="dz">当前地址:
  5. <text style="color: #333;font-weight: bold;">{{ addressName }}</text>
  6. </view>
  7. <view class="shuaxin">
  8. <image src="/static/images/address/shuaxin.png" class="shuaxin-icon"></image>
  9. <view class="sx" @tap="shaxin">点击刷新</view>
  10. </view>
  11. </view>
  12. <view class="xzwz" v-if="isShowServiceAddress">请选择你的服务地址</view>
  13. <view class="address-list" v-if="addressList.length > 0">
  14. <view class="item" v-for="(item, index) in addressList" :key="index">
  15. <view class="l">
  16. <image src="/static/images/address/dingwei.png"></image>
  17. </view>
  18. <view class="c" @tap="addressAddOrUpdate" :data-address-tel="item.tel" :data-address-name="item.name"
  19. :data-address-id="item.id" :data-address-detail="item.addressDetail">
  20. <!-- <view class="address" v-if="item.addressName">{{ item.addressName }}{{ item.detail }}</view> -->
  21. <view class="address" v-else-if="">{{ item.province }} {{ item.city }} {{ item.county }}
  22. {{ item.addressDetail }}
  23. </view>
  24. <view class="mobile">{{ item.name }} {{ item.tel }}</view>
  25. <view class="default" v-if="item.isDefault">默认</view>
  26. </view>
  27. <view class="r">
  28. <image src="/static/images/address/xiugai.png" :data-address-id="item.id" @tap="updateAddress"
  29. class="update" />
  30. </view>
  31. </view>
  32. </view>
  33. <view class="empty-view" v-if="addressList.length <= 0">
  34. <text class="text">收货地址还没有~~~</text>
  35. </view>
  36. <view class="add-address" @tap="addressAddOrUpdate" data-address-id="0">新建</view>
  37. </view>
  38. </template>
  39. <script>
  40. var util = require('../../../utils/util.js');
  41. var api = require('../../../config/api.js');
  42. var map = require('../../../utils/map.js');
  43. var app = getApp();
  44. export default {
  45. data() {
  46. return {
  47. addressList: [],
  48. total: 0,
  49. addressName: '',
  50. isShowServiceAddress: false
  51. };
  52. },
  53. onLoad: function(options) {
  54. // 页面初始化 options为页面跳转所带来的参数
  55. var pages = getCurrentPages();
  56. var prevPage = pages[pages.length - 2];
  57. if (prevPage.route == 'pages/index/index') {
  58. this.setData({
  59. isShowServiceAddress: true
  60. });
  61. }
  62. this.getUserLocation();
  63. },
  64. onReady: function() {
  65. // 页面渲染完成
  66. },
  67. onShow: function() {
  68. // 页面显示
  69. this.getAddressList();
  70. },
  71. onHide: function() {
  72. // 页面隐藏
  73. },
  74. onUnload: function() {
  75. // 页面关闭
  76. },
  77. methods: {
  78. shaxin() {
  79. this.getUserLocation();
  80. },
  81. getUserLocation: function() {
  82. map.getUserLocation();
  83. let that = this;
  84. setTimeout(function() {
  85. that.setData({
  86. addressName: app.globalData.currentLocationName
  87. });
  88. }, 2000);
  89. },
  90. getAddressList() {
  91. let that = this;
  92. util.request(api.AddressList).then(function(res) {
  93. if (res.errno === 0) {
  94. that.setData({
  95. addressList: res.data.list,
  96. total: res.data.total
  97. });
  98. }
  99. });
  100. },
  101. updateAddress(event) {
  102. let addressId = event.currentTarget.dataset.addressId;
  103. uni.navigateTo({
  104. url: '/pages/ucenter/addressAdd/addressAdd?id=' + addressId
  105. });
  106. },
  107. async updateAddressData(address) {
  108. await util.request(api.AddressSave, address, 'POST').then(function(res) {
  109. if (res.errno === 0) {
  110. var city = res.data.city.indexOf('市辖') != -1 ? res.data.province : res.data.city;
  111. var addresInfo = {
  112. city: city,
  113. lng: res.data.lng,
  114. lat: res.data.lat,
  115. shopId: res.data.shopId,
  116. address: res.data.addressDetail,
  117. addressId: res.data.id,
  118. name: res.data.name,
  119. tel: res.data.tel
  120. };
  121. uni.setStorageSync('addressInfo', addresInfo);
  122. }
  123. });
  124. },
  125. addressAddOrUpdate(event) {
  126. // console.log(event)
  127. let addressId = event.currentTarget.dataset.addressId;
  128. let name = event.currentTarget.dataset.addressName;
  129. let tel = event.currentTarget.dataset.addressTel;
  130. let addressDetail = event.currentTarget.dataset.addressDetail;
  131. let address = null;
  132. this.addressList.forEach((item) => {
  133. if (addressId == item.id) {
  134. address = item;
  135. }
  136. });
  137. console.info(address);
  138. //返回之前,先取出上一页对象,并设置addressId
  139. var pages = getCurrentPages();
  140. console.log(pages);
  141. var prevPage = pages[pages.length - 2];
  142. if (null != address && (prevPage.route == 'pages/index/index' || prevPage.route ==
  143. 'pages/ucenter/serviceDetail/serviceDetail')) {
  144. // console.log("市辖区".indexOf("市"));
  145. var city = address.city.indexOf('市辖') != -1 ? address.province : address.city;
  146. //通过jz_shop的shop_no关联服务力分配的门店id,拿实际的jz_shop的门店id
  147. //这个是拿的服务力分配的门店,如果不转要改后台查询商品,要用shopNo,这里的shopId是shopNo
  148. // let distribution_unit=address.shopId;
  149. if (address.shopId == null) {
  150. this.updateAddressData(address);
  151. } else {
  152. var addresInfo = {
  153. city: city,
  154. lng: address.lng,
  155. lat: address.lat,
  156. shopId: address.shopId,
  157. address: address.addressDetail,
  158. shopName: address.shopName,
  159. name: name,
  160. tel: tel,
  161. addressId: address.id
  162. };
  163. uni.setStorageSync('addressInfo', addresInfo);
  164. }
  165. setTimeout(() => {
  166. uni.navigateBack();
  167. }, 300);
  168. } else if (prevPage.route == 'pages/checkout/checkout') {
  169. try {
  170. uni.setStorageSync('addressId', event.currentTarget.dataset.addressId);
  171. } catch (e) {}
  172. if (addressId && addressId != 0) {
  173. uni.navigateBack();
  174. } else {
  175. uni.navigateTo({
  176. url: '/pages/ucenter/addressAdd/addressAdd?id=' + addressId
  177. });
  178. }
  179. } else if (prevPage.route == 'pages/goods/infoCollect/infoCollect') {
  180. if (addressId && addressId != 0) {
  181. app.globalData.infoAddress = {
  182. addressId: addressId,
  183. addressDetail: addressDetail
  184. };
  185. uni.navigateBack();
  186. } else {
  187. uni.navigateTo({
  188. url: '/pages/ucenter/addressAdd/addressAdd?id=' + addressId
  189. });
  190. }
  191. } else {
  192. uni.navigateTo({
  193. url: '/pages/ucenter/addressAdd/addressAdd?id=' + event.currentTarget.dataset.addressId
  194. });
  195. }
  196. },
  197. deleteAddress(event) {
  198. console.log(event.target);
  199. let that = this;
  200. uni.showModal({
  201. title: '',
  202. content: '确定要删除地址?',
  203. success: function(res) {
  204. if (res.confirm) {
  205. let addressId = event.target.dataset.addressId;
  206. util.request(
  207. api.AddressDelete, {
  208. id: addressId
  209. },
  210. 'POST'
  211. ).then(function(res) {
  212. if (res.errno === 0) {
  213. that.getAddressList();
  214. uni.removeStorage({
  215. key: 'addressId',
  216. success: function(res) {}
  217. });
  218. }
  219. });
  220. console.log('用户点击确定');
  221. }
  222. }
  223. });
  224. return false;
  225. }
  226. }
  227. };
  228. </script>
  229. <style>
  230. page {
  231. height: 100%;
  232. width: 100%;
  233. background: #f4f4f4;
  234. }
  235. .xzwz {
  236. text-indent: 20rpx;
  237. color: #90a4ae;
  238. }
  239. .update {
  240. width: 35rpx;
  241. height: 35rpx;
  242. }
  243. .container {
  244. width: 100%;
  245. padding-bottom: 160rpx;
  246. }
  247. .address-list {
  248. padding-left: 31.25rpx;
  249. background: #fff;
  250. background-size: auto 10.5rpx;
  251. }
  252. .address-list .item {
  253. padding: 20rpx 0;
  254. align-items: center;
  255. display: flex;
  256. border-bottom: 1rpx solid #dcd9d9;
  257. }
  258. .address-list .l {
  259. width: 50rpx;
  260. height: 80rpx;
  261. overflow: hidden;
  262. }
  263. .address-list .l image {
  264. width: 40rpx;
  265. height: 50rpx;
  266. }
  267. .address-list .name {
  268. width: 125rpx;
  269. height: 43rpx;
  270. font-size: 29rpx;
  271. color: #333;
  272. margin-bottom: 5.2rpx;
  273. text-overflow: ellipsis;
  274. white-space: nowrap;
  275. overflow: hidden;
  276. }
  277. .address-list .default {
  278. float: right;
  279. margin-right: 40rpx;
  280. border: 1px solid #d77958;
  281. color: #d77958;
  282. border-radius: 20rpx;
  283. line-height: 20px;
  284. width: 100rpx;
  285. height: 40rpx;
  286. text-align: center;
  287. font-size: 20rpx;
  288. visibility: visible;
  289. }
  290. .address-list .c {
  291. flex: 1;
  292. height: auto;
  293. overflow: hidden;
  294. }
  295. .address-list .mobile {
  296. height: 29rpx;
  297. font-size: 29rpx;
  298. line-height: 29rpx;
  299. overflow: hidden;
  300. color: #666;
  301. margin-bottom: 6.25rpx;
  302. margin-top: 10rpx;
  303. width: 400rpx;
  304. float: left;
  305. }
  306. .address-list .address {
  307. font-size: 30rpx;
  308. overflow: hidden;
  309. color: black;
  310. }
  311. .address-list .r {
  312. width: 52rpx;
  313. height: auto;
  314. overflow: hidden;
  315. margin-right: 16.5rpx;
  316. }
  317. .address-list .del {
  318. display: block;
  319. width: 52rpx;
  320. height: 52rpx;
  321. }
  322. .add-address {
  323. border: none;
  324. right: 0;
  325. display: flex;
  326. justify-content: center;
  327. align-items: center;
  328. width: 90%;
  329. height: 90rpx;
  330. line-height: 98rpx;
  331. position: fixed;
  332. bottom: 25rpx;
  333. left: 0;
  334. border-radius: 0;
  335. padding: 0;
  336. margin: 0 0 0 5%;
  337. text-align: center;
  338. font-size: 25rpx;
  339. color: #f4f4f4;
  340. border-top-left-radius: 50rpx;
  341. border-bottom-left-radius: 50rpx;
  342. border-top-right-radius: 50rpx;
  343. border-bottom-right-radius: 50rpx;
  344. letter-spacing: 3rpx;
  345. background-color: #d77958;
  346. }
  347. .empty-view {
  348. height: 100%;
  349. width: 100%;
  350. display: flex;
  351. flex-direction: column;
  352. align-items: center;
  353. justify-content: center;
  354. }
  355. .empty-view .text {
  356. width: auto;
  357. font-size: 28rpx;
  358. line-height: 35rpx;
  359. color: #999;
  360. }
  361. .dz {
  362. width: 500rpx;
  363. height: 36rpx;
  364. font-size: 25rpx;
  365. color: #333333;
  366. line-height: 36rpx;
  367. font-weight: bold;
  368. display: inline-block;
  369. }
  370. .sx {
  371. width: 101rpx;
  372. height: 36rpx;
  373. font-weight: 500;
  374. font-size: 25rpx;
  375. color: #C47046;
  376. line-height: 36rpx;
  377. display: inline-block;
  378. }
  379. .top {
  380. background: #EED5CD;
  381. overflow: hidden;
  382. height: 62rpx;
  383. display: flex;
  384. padding: 16rpx 23rpx;
  385. justify-content: space-between;
  386. line-height: 62rpx;
  387. }
  388. .shuaxin{
  389. height: 36rpx;
  390. line-height: 36rpx;
  391. }
  392. .shuaxin-icon {
  393. width: 29rpx;
  394. height: 21rpx;
  395. margin-right: 8rpx;
  396. }
  397. </style>