user_address.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <template>
  2. <view class="user-address">
  3. <view class="no-address flex-col col-center" v-if="!hasAddress">
  4. <image class="img-null mt20" src="/static/images/address_null.png"></image>
  5. <view class="sm muted">暂无添加地址,请添加~</view>
  6. </view>
  7. <view class="address-list" v-else>
  8. <u-radio-group v-model="currentId" class="radio-group" @change="radioChange" :active-color="colorConfig.primary">
  9. <view v-for="(item, index) in addressList" :key="index" class="item bg-white m-b-20" @tap="onSelect(item.id)">
  10. <view class="address">
  11. <view class="consignee md bold">
  12. {{item.contact}}
  13. <text class="phone m-l-10">{{item.telephone}}</text>
  14. </view>
  15. <view class="lighter sm m-t-10">
  16. {{item.province}} {{item.city}} {{item.district}} {{item.address}}
  17. </view>
  18. </view>
  19. <view class="operation flex row-between">
  20. <view @tap.stop="">
  21. <u-radio class="radio flex" :name="item.id">
  22. <text class="xs">{{currentId == item.id ? '默认' : '设为默认'}}</text>
  23. </u-radio>
  24. </view>
  25. <view class="flex row-center">
  26. <router-link :to="{path: '/bundle/pages/address_edit/address_edit', query: {id: item.id}}">
  27. <view class="flex m-r-20">
  28. <image class="icon-md m-r-10" src="/static/images/icon_edit.png"></image>
  29. 编辑
  30. </view>
  31. </router-link>
  32. <view class="flex m-l-20" @tap.stop="showSurePop(item.id)">
  33. <image class="icon-md m-r-10" src="/static/images/icon_del_1.png"></image>
  34. 删除
  35. </view>
  36. </view>
  37. </view>
  38. </view>
  39. </u-radio-group>
  40. </view>
  41. <u-modal
  42. id="delete-dialog"
  43. v-model="deleteSure"
  44. :show-cancel-button="true"
  45. confirm-text="狠心删除"
  46. :confirm-color="colorConfig.primary"
  47. :show-title="false"
  48. @confirm="delAddressFun">
  49. <view class="flex-col col-center tips-dialog p-t-40">
  50. <image class="icon-lg" src="/static/images/icon_warning.png"></image>
  51. <view style="margin-top:30rpx">确认删除该地址吗?</view>
  52. </view>
  53. </u-modal>
  54. <view class="footer flex row-between fixed bg-white">
  55. <!-- #ifdef H5 || MP-WEIXIN -->
  56. <view class="btn flex row-center bg-gray br60 m-r-20" @click="getWxAddressFun" v-if="isWeixin">
  57. <image class="icon-lg m-r-10" src="/static/images/icon_wechat.png"></image>
  58. <text class="md">微信导入</text>
  59. </view>
  60. <!-- #endif -->
  61. <router-link class="flex-1" :to="{path: '/bundle/pages/address_edit/address_edit'}">
  62. <view class="btn bg-primary white md flex row-center br60">新增收货地址</view>
  63. </router-link>
  64. </view>
  65. </view>
  66. </template>
  67. <script>
  68. import { getAddressLists, delAddress, setDefaultAddress } from '@/api/user';
  69. import wechath5 from '@/utils/wechath5'
  70. import {isWeixinClient} from '@/utils/tools'
  71. export default {
  72. data() {
  73. return {
  74. addressList: [],
  75. hasAddress: true,
  76. deleteSure: false,
  77. currentId: 0,
  78. isWeixin: true
  79. };
  80. },
  81. onLoad (options) {
  82. this.type = this.$Route.query.type;
  83. //#ifdef H5
  84. this.isWeixin = isWeixinClient()
  85. //#endif
  86. },
  87. onShow() {
  88. this.getAddressListsFun();
  89. },
  90. methods: {
  91. onSelect(id) {
  92. if (this.type) {
  93. uni.$emit('selectaddress', {
  94. id
  95. });
  96. uni.navigateBack();
  97. }
  98. },
  99. getAddressListsFun() {
  100. getAddressLists().then(res => {
  101. if (res.code == 1) {
  102. if (res.data.length) {
  103. this.addressList = res.data;
  104. const defaultA = res.data.find((item) => item.is_default)
  105. this.currentId = defaultA ? defaultA.id : 0
  106. this.hasAddress = true;
  107. } else {
  108. this.hasAddress = false;
  109. }
  110. } else {
  111. this.hasAddress = false
  112. }
  113. });
  114. },
  115. async radioChange(id) {
  116. const {code, msg} = await setDefaultAddress(id)
  117. if (code == 1) {
  118. this.$toast({
  119. title: msg
  120. })
  121. this.getAddressListsFun();
  122. }
  123. },
  124. onLoadFun() {
  125. this.getAddressListsFun();
  126. },
  127. delAddressFun() {
  128. delAddress(this.delectId).then(res => {
  129. if (res.code == 1) {
  130. this.$toast({
  131. title: res.msg
  132. });
  133. this.deleteSure = false
  134. this.getAddressListsFun();
  135. }
  136. });
  137. },
  138. getWxAddressFun() {
  139. // #ifdef H5
  140. wechath5.getWxAddress().then((res) => {
  141. uni.setStorageSync('wxAddress', JSON.stringify(res));
  142. setTimeout(() => {
  143. this.$Router.push({
  144. path: '/bundle/pages/address_edit/address_edit'
  145. })
  146. }, 200);
  147. })
  148. // #endif
  149. // #ifdef MP-WEIXIN
  150. uni.authorize({
  151. scope: 'scope.address',
  152. success: (res) => {
  153. uni.chooseAddress({
  154. success: (res) => {
  155. uni.setStorageSync('wxAddress', JSON.stringify(res));
  156. setTimeout(() => {
  157. this.$Router.push({
  158. path: '/bundle/pages/address_edit/address_edit'
  159. })
  160. }, 200);
  161. },
  162. fail: (res) => {
  163. if (res.errMsg == 'chooseAddress:cancel') return this.$toast({
  164. title: '取消选择'
  165. });
  166. }
  167. });
  168. },
  169. fail: (res) => {
  170. uni.showModal({
  171. title: '您已拒绝导入微信地址权限',
  172. content: '是否进入权限管理,调整授权?',
  173. success:(res) => {
  174. if (res.confirm) {
  175. uni.openSetting({
  176. success: (res) => {}
  177. });
  178. } else if (res.cancel) {
  179. return this.$toast({
  180. title: '已取消!'
  181. });
  182. }
  183. }
  184. });
  185. }
  186. });
  187. // #endif
  188. },
  189. showSurePop(id) {
  190. this.deleteSure = true;
  191. this.delectId = id;
  192. },
  193. }
  194. };
  195. </script>
  196. <style lang="scss">
  197. .user-address {
  198. padding-bottom: calc(140rpx + env(safe-area-inset-bottom));
  199. .no-address {
  200. padding-top: 300rpx;
  201. text-align: center;
  202. }
  203. .address-list {
  204. padding: 10rpx 0;
  205. .item {
  206. padding: 0 30rpx;
  207. .address {
  208. padding: 20rpx 0;
  209. border-bottom: $-solid-border;
  210. }
  211. .operation {
  212. height: 80rpx;
  213. }
  214. }
  215. .u-radio-group {
  216. display: block;
  217. }
  218. }
  219. .footer {
  220. position: fixed;
  221. left: 0;
  222. right: 0;
  223. bottom: 0;
  224. height: 118rpx;
  225. padding: 0 30rpx;
  226. box-sizing: content-box;
  227. padding-bottom: env(safe-area-inset-bottom);
  228. .btn {
  229. flex: 1;
  230. height: 80rpx;
  231. }
  232. }
  233. }
  234. .tips-dialog {
  235. height: 230rpx;
  236. width: 100%;
  237. }
  238. </style>