address.vue 6.3 KB

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