address.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. let that = this;
  161. addressEdit({
  162. real_name: data.userName,
  163. phone: data.telNumber,
  164. address: {
  165. province: data.provinceName,
  166. city: data.cityName,
  167. district: data.countyName
  168. },
  169. detail: data.detailInfo,
  170. type: 1
  171. }).then(function(e) {
  172. uni.showToast({
  173. title: '提交成功',
  174. duration: 2000
  175. });
  176. setTimeout(function() {
  177. that.loadAddress();
  178. }, 800);
  179. });
  180. }
  181. // #endif
  182. }
  183. };
  184. </script>
  185. <style lang="scss">
  186. page {
  187. padding-bottom: 120rpx;
  188. padding-top: 20rpx;
  189. background-color: $page-color-base;
  190. }
  191. .content {
  192. position: relative;
  193. }
  194. .list {
  195. align-items: center;
  196. padding: 20rpx 30rpx;
  197. background: #fff;
  198. margin: 20rpx;
  199. margin-top: 0;
  200. .buttom {
  201. display: flex;
  202. align-items: center;
  203. justify-content: space-between;
  204. padding-top: 10rpx;
  205. .checkbox,
  206. .checkedImg {
  207. height: 36rpx;
  208. width: 36rpx;
  209. border-radius: 100rpx;
  210. }
  211. .checkbox {
  212. border: 1px solid $font-color-light;
  213. }
  214. .checkbox.checked {
  215. color: $base-color;
  216. }
  217. .default-buttom {
  218. display: flex;
  219. align-items: center;
  220. }
  221. .operation {
  222. display: flex;
  223. align-items: center;
  224. .blank {
  225. width: 30rpx;
  226. }
  227. .icon {
  228. width: 32rpx;
  229. }
  230. }
  231. .text {
  232. padding-left: 10rpx;
  233. font-size: 24rpx;
  234. color: #666666;
  235. }
  236. }
  237. }
  238. .wrapper {
  239. display: flex;
  240. flex-direction: column;
  241. flex: 1;
  242. border-bottom: 1px solid #f0f0f0;
  243. padding-bottom: 20rpx;
  244. }
  245. .address-box {
  246. display: flex;
  247. align-items: center;
  248. justify-content: space-between;
  249. .address {
  250. font-size: $font-base + 2rpx;
  251. color: $font-color-dark;
  252. }
  253. .mobile {
  254. font-size: $font-base;
  255. color: rgba(51, 51, 51, 1);
  256. }
  257. }
  258. .u-box {
  259. font-size: $font-base;
  260. color: $font-color-light;
  261. margin-top: 16rpx;
  262. .name {
  263. margin-right: 30rpx;
  264. }
  265. }
  266. .icon-bianji {
  267. display: flex;
  268. align-items: center;
  269. height: 80rpx;
  270. font-size: 40rpx;
  271. color: $font-color-light;
  272. padding-left: 30rpx;
  273. }
  274. .add-btn {
  275. position: fixed;
  276. z-index: 95;
  277. left: 30rpx;
  278. right: 30rpx;
  279. &.one {
  280. bottom: 16rpx;
  281. }
  282. &.two {
  283. bottom: 136rpx;
  284. background-color: $font-color-disabled;
  285. }
  286. }
  287. </style>