address.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <template>
  2. <view class="content b-t">
  3. <!-- #ifdef H5 -->
  4. <empty src="../../static/error/emptyAddress.png" v-if="addressList.length === 0"></empty>
  5. <!-- #endif -->
  6. <!-- #ifndef H5 -->
  7. <empty src="../static/error/emptyAddress.png" v-if="addressList.length === 0"></empty>
  8. <!-- #endif -->
  9. <view class="list" v-for="(item, index) in addressList" :key="index" @click="checkAddress(item)">
  10. <view class="wrapper">
  11. <view class="address-box">
  12. <text class="name">{{ item.real_name }}</text>
  13. <text class="mobile">{{ item.phone }}</text>
  14. </view>
  15. <view class="u-box">
  16. <text class="address">{{ item.province + item.city + item.district }} {{ item.detail }}</text>
  17. </view>
  18. </view>
  19. <view class="buttom">
  20. <view class="default-buttom" @click.stop="defaultUp(item,index)">
  21. <image class="checkedImg" v-if="item.is_default == 1" src="../../static/icon/addressIconXz.png"
  22. mode="widthFix"></image>
  23. <view v-else class="checkbox">
  24. </view>
  25. <text class="text">设为默认地址</text>
  26. </view>
  27. <view class="operation">
  28. <view @click.stop="addAddress('edit', item)" class="flex">
  29. <image class="icon" src="../../static/icon/addressIcon1.png" mode="widthFix"></image>
  30. <text class="text">编辑</text>
  31. </view>
  32. <view class="blank"></view>
  33. <view @click.stop="delAddress(item)" class="flex">
  34. <image class="icon" src="../../static/icon/addressIcon2.png" mode="widthFix"></image>
  35. <text class="text">删除</text>
  36. </view>
  37. </view>
  38. </view>
  39. </view>
  40. <!-- #ifdef H5 || MP -->
  41. <view class="add-btn two base-buttom" @click="addWxAddress">获取微信地址</view>
  42. <!-- #endif -->
  43. <view class="add-btn one base-buttom" @click="addAddress('add')">新增地址</view>
  44. </view>
  45. </template>
  46. <script>
  47. import {
  48. getAddressList,
  49. setAddressDefault,
  50. addressDel,
  51. addressEdit
  52. } from '@/api/user.js';
  53. // #ifdef H5
  54. import weixinObj from "@/plugin/jweixin-module/index.js";
  55. import {
  56. weixindata
  57. } from "../../utils/wxAuthorized.js"
  58. // #endif
  59. export default {
  60. data() {
  61. return {
  62. source: 0,
  63. addressList: []
  64. };
  65. },
  66. onLoad(option) {
  67. this.source = option.source || 0
  68. this.loadAddress();
  69. },
  70. methods: {
  71. // 加载地址
  72. loadAddress() {
  73. getAddressList({
  74. page: 1,
  75. limit: 100
  76. }).then(({
  77. data
  78. }) => {
  79. this.addressList = data;
  80. });
  81. },
  82. // 设为默认地址
  83. defaultUp(data, ind) {
  84. this.addressList = this.addressList.map((e) => {
  85. e.is_default = 0
  86. return e
  87. })
  88. this.addressList[ind].is_default = 1
  89. setAddressDefault({
  90. id: data.id
  91. }).then(({
  92. data
  93. }) => {
  94. this.loadAddress();
  95. }).catch((e) => {
  96. console.log(e);
  97. });
  98. },
  99. //删除地址
  100. delAddress(item) {
  101. addressDel({
  102. id: item.id
  103. }).then(({
  104. data
  105. }) => {
  106. this.$api.msg('删除成功')
  107. })
  108. let s = this.addressList.indexOf(item);
  109. this.addressList.splice(s, 1);
  110. },
  111. //选择地址
  112. checkAddress(item) {
  113. if (this.source == 1) {
  114. //this.$api.prePage()获取上一页实例,在App.vue定义
  115. this.$api.prePage().addressData = item;
  116. uni.navigateBack();
  117. }
  118. },
  119. // 添加地址
  120. addAddress(type, item) {
  121. uni.navigateTo({
  122. url: `/pages/set/addressManage?type=${type}&data=${JSON.stringify(item)}`
  123. });
  124. },
  125. //添加或修改成功之后回调
  126. refreshList() {
  127. // 重新加载地址
  128. this.loadAddress()
  129. },
  130. // #ifdef H5
  131. // 公众号获取地址
  132. addWxAddress() {
  133. let that = this;
  134. weixindata().then(() => {
  135. weixinObj.openAddress({
  136. success: function(data) {
  137. that.addAddressOn(data)
  138. },fail(e){
  139. console.log(e,'授权错误');
  140. }
  141. });
  142. })
  143. },
  144. // #endif
  145. // #ifdef MP
  146. addWxAddress() {
  147. let that = this;
  148. wx.chooseAddress({
  149. success: function(data) {
  150. console.log('获取地址',data);
  151. that.addAddressOn(data)
  152. },fail(e){
  153. console.log(e);
  154. }
  155. });
  156. },
  157. // #endif
  158. // #ifdef H5 || MP
  159. addAddressOn(data) {
  160. uni.showModal({
  161. title: '返回',
  162. content: JSON.stringify(data),
  163. success: res => {},
  164. fail: () => {},
  165. complete: () => {}
  166. });
  167. let that = this;
  168. addressEdit({
  169. real_name: data.userName,
  170. phone: data.telNumber,
  171. address: {
  172. province: data.provinceName,
  173. city: data.cityName,
  174. district: data.countyName
  175. },
  176. detail: data.detailInfo,
  177. longitude: data.longitude, //经度
  178. latitude:data.latitude, //纬度
  179. type: 1
  180. }).then(function(e) {
  181. uni.showToast({
  182. title: '提交成功',
  183. duration: 2000
  184. });
  185. setTimeout(function() {
  186. that.loadAddress();
  187. }, 800);
  188. }).catch((err)=>{
  189. console.log(err)
  190. });
  191. }
  192. // #endif
  193. }
  194. };
  195. </script>
  196. <style lang="scss">
  197. page {
  198. padding-bottom: 120rpx;
  199. padding-top: 20rpx;
  200. background-color: $page-color-base;
  201. }
  202. .content {
  203. position: relative;
  204. }
  205. .list {
  206. align-items: center;
  207. padding: 20rpx 30rpx;
  208. background: #fff;
  209. margin: 20rpx;
  210. margin-top: 0;
  211. .buttom {
  212. display: flex;
  213. align-items: center;
  214. justify-content: space-between;
  215. padding-top: 10rpx;
  216. .checkbox,
  217. .checkedImg {
  218. height: 36rpx;
  219. width: 36rpx;
  220. border-radius: 100rpx;
  221. }
  222. .checkbox {
  223. border: 1px solid $font-color-light;
  224. }
  225. .checkbox.checked {
  226. color: $base-color;
  227. }
  228. .default-buttom {
  229. display: flex;
  230. align-items: center;
  231. }
  232. .operation {
  233. display: flex;
  234. align-items: center;
  235. .blank {
  236. width: 30rpx;
  237. }
  238. .icon {
  239. width: 32rpx;
  240. }
  241. }
  242. .text {
  243. padding-left: 10rpx;
  244. font-size: 24rpx;
  245. color: #666666;
  246. }
  247. }
  248. }
  249. .wrapper {
  250. display: flex;
  251. flex-direction: column;
  252. flex: 1;
  253. border-bottom: 1px solid #f0f0f0;
  254. padding-bottom: 20rpx;
  255. }
  256. .address-box {
  257. display: flex;
  258. align-items: center;
  259. justify-content: space-between;
  260. .address {
  261. font-size: $font-base + 2rpx;
  262. color: $font-color-dark;
  263. }
  264. .mobile {
  265. font-size: $font-base;
  266. color: rgba(51, 51, 51, 1);
  267. }
  268. }
  269. .u-box {
  270. font-size: $font-base;
  271. color: $font-color-light;
  272. margin-top: 16rpx;
  273. .name {
  274. margin-right: 30rpx;
  275. }
  276. }
  277. .icon-bianji {
  278. display: flex;
  279. align-items: center;
  280. height: 80rpx;
  281. font-size: 40rpx;
  282. color: $font-color-light;
  283. padding-left: 30rpx;
  284. }
  285. .add-btn {
  286. position: fixed;
  287. z-index: 95;
  288. left: 30rpx;
  289. right: 30rpx;
  290. &.one {
  291. bottom: 16rpx;
  292. }
  293. &.two {
  294. bottom: 136rpx;
  295. background-color: $font-color-disabled;
  296. }
  297. }
  298. </style>