index.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <template>
  2. <view class="main">
  3. <view class="one">
  4. <scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
  5. <radio-group @change="addressChange">
  6. <view class="list" v-for="(item,index) in list" :key="index">
  7. <label class="radio">
  8. <radio :value="item._id" :checked="item._id == address_id" />
  9. </label>
  10. <view class="list_1">
  11. <view class="name">
  12. <text>{{item.name}}</text>
  13. <text>{{item.phone}}</text>
  14. </view>
  15. <view class="address">
  16. <text>{{item.province}}</text>
  17. <text>{{item.city}}</text>
  18. <text>{{item.area}}</text>
  19. <text>{{item.address}}</text>
  20. <text>{{item.number}}</text>
  21. </view>
  22. <view class="btn">
  23. <button class="btn_1" size="mini" @click="toDefa(item)"
  24. v-if="item.is_default==false">设为默认</button>
  25. <button class="btn_2" size="mini" @click="toCommon(item)">编辑</button>
  26. <button class="btn_3" size="mini" @click="toDel(item)">删除</button>
  27. </view>
  28. <view class="default" v-if="item.is_default==true">
  29. <text>默认</text>
  30. </view>
  31. </view>
  32. </view>
  33. <view class="is_bottom" v-if="is_bottom">
  34. <text>{{config.bottom_title}}</text>
  35. </view>
  36. </radio-group>
  37. </scroll-view>
  38. </view>
  39. <view class="two">
  40. <view class="two_1">
  41. <button type="default" @click="toWxaddress()">获取微信地址</button>
  42. </view>
  43. <view class="two_1">
  44. <button type="default" @click="toCommon()">新增收货地址</button>
  45. </view>
  46. </view>
  47. </view>
  48. </template>
  49. <script setup lang="ts">
  50. import { getCurrentInstance, ref } from 'vue';
  51. //该依赖已内置不需要单独安装
  52. import { onLoad, onHide, onShow, onPullDownRefresh } from "@dcloudio/uni-app";
  53. // 请求接口
  54. const $api = getCurrentInstance()?.appContext.config.globalProperties.$api;
  55. const $config = getCurrentInstance()?.appContext.config.globalProperties.$config;
  56. // 基本信息
  57. const config = ref({ logo: [] });
  58. const user = ref({});
  59. // id
  60. const address_id = ref('');
  61. // 列表
  62. const list = ref([]);
  63. const total = ref(0);
  64. const skip = ref(0);
  65. const limit = ref(6);
  66. const page = ref(0);
  67. // 数据是否触底
  68. const is_bottom = ref(false);
  69. const scrollTop = ref(0);
  70. onLoad(async (options) => {
  71. address_id.value = options && options.id
  72. })
  73. onShow(async () => {
  74. await searchConfig();
  75. await searchUser();
  76. await clearPage();
  77. await search();
  78. })
  79. onPullDownRefresh(async () => {
  80. await clearPage();
  81. await search();
  82. uni.stopPullDownRefresh();
  83. })
  84. onHide(async () => {
  85. await clearPage();
  86. })
  87. // config信息
  88. const searchConfig = async () => {
  89. config.value = uni.getStorageSync('config');
  90. };
  91. // 用户信息
  92. const searchUser = async () => {
  93. user.value = uni.getStorageSync('user');
  94. };
  95. // 查询
  96. const search = async () => {
  97. if (user.value._id) {
  98. const info = {
  99. skip: skip.value,
  100. limit: limit.value,
  101. user: user.value._id,
  102. is_use: '0'
  103. }
  104. const res = await $api('address', 'GET', info);
  105. if (res.errcode === 0) {
  106. list.value = list.value.concat(res.data)
  107. total.value = res.total
  108. } else {
  109. uni.showToast({
  110. title: res.errmsg || '',
  111. icon: 'error',
  112. });
  113. }
  114. } else {
  115. uni.navigateTo({
  116. url: `/pagesHome/login/index`
  117. })
  118. }
  119. };
  120. // 选择收货地址
  121. const addressChange = async (e) => {
  122. let data = list.value.find((i) => i._id == e.detail.value);
  123. if (data) {
  124. address_id.value == data._id;
  125. uni.$emit("id", e.detail.value);
  126. uni.navigateBack({
  127. delta: 1
  128. });
  129. }
  130. };
  131. // 设置默认
  132. const toDefa = async (e) => {
  133. uni.showModal({
  134. title: '提示',
  135. content: '确定设置该地址为默认地址吗?',
  136. success: async function (res) {
  137. if (res.confirm) {
  138. const arr = await $api(`address/toDefault/${e._id}`, `POST`);
  139. if (arr.errcode == '0') {
  140. uni.showToast({
  141. title: '添加默认成功',
  142. icon: 'none'
  143. })
  144. await clearPage()
  145. await search()
  146. } else {
  147. uni.showToast({
  148. title: arr.errmsg,
  149. icon: 'none'
  150. })
  151. }
  152. }
  153. }
  154. });
  155. };
  156. // 获取微信地址
  157. const toWxaddress = async () => {
  158. uni.chooseAddress({
  159. success: async function (add) {
  160. let params = {
  161. user: user.value._id,
  162. name: add.userName,
  163. phone: add.telNumber,
  164. province: add.provinceName,
  165. city: add.cityName,
  166. area: add.countyName,
  167. address: add.detailInfo
  168. }
  169. const arr = await $api(`address`, 'POST', params);
  170. if (arr.errcode == '0') {
  171. uni.showToast({
  172. title: '添加收货地址成功',
  173. icon: 'none'
  174. })
  175. clearPage();
  176. } else {
  177. uni.showToast({
  178. title: arr.errmsg,
  179. icon: 'none'
  180. })
  181. }
  182. },
  183. fail: function () {
  184. clearPage();
  185. }
  186. })
  187. };
  188. // 编辑
  189. const toCommon = async (e) => {
  190. uni.navigateTo({
  191. url: `/pagesHome/address/add?id=${e && e._id || ''}`
  192. })
  193. };
  194. // 删除
  195. const toDel = async (e) => {
  196. uni.showModal({
  197. title: '提示',
  198. content: '确定删除该地址吗?',
  199. success: async function (res) {
  200. if (res.confirm) {
  201. const arr = await $api(`address/${e._id}`, 'DELETE');
  202. if (arr.errcode == '0') {
  203. uni.showToast({
  204. title: '删除信息成功',
  205. icon: 'none'
  206. })
  207. if (address_id.value == e._id) uni.$emit("id", '')
  208. await clearPage()
  209. await search()
  210. } else {
  211. uni.showToast({
  212. title: arr.errmsg,
  213. icon: 'none'
  214. })
  215. }
  216. }
  217. }
  218. });
  219. };
  220. // 分页
  221. const toPage = () => {
  222. if (total.value > list.value.length) {
  223. uni.showLoading({
  224. title: '加载中',
  225. mask: true
  226. })
  227. page.value = page.value + 1;
  228. skip.value = page.value * limit.value;
  229. search();
  230. uni.hideLoading();
  231. } else is_bottom.value = true
  232. };
  233. // 清空列表
  234. const clearPage = () => {
  235. list.value = []
  236. skip.value = 0
  237. limit.value = 6
  238. page.value = 0
  239. };
  240. </script>
  241. <style lang="scss">
  242. .main {
  243. display: flex;
  244. flex-direction: column;
  245. width: 100vw;
  246. height: 100vh;
  247. background-color: var(--f1Color);
  248. .one {
  249. position: relative;
  250. flex-grow: 1;
  251. .list {
  252. display: flex;
  253. background: var(--fffColor);
  254. margin: 2vw 2vw 0 2vw;
  255. padding: 2vw;
  256. border-radius: 5px;
  257. position: relative;
  258. .list_1 {
  259. flex-grow: 1;
  260. .name {
  261. font-size: var(--font16Size);
  262. margin: 0 0 2vw 0;
  263. text {
  264. padding: 0 2vw 0 0;
  265. }
  266. }
  267. .address {
  268. font-size: var(--font14Size);
  269. margin: 0 0 1vw 0;
  270. text {
  271. padding: 0 2vw 0 0;
  272. }
  273. }
  274. .btn {
  275. text-align: center;
  276. button {
  277. margin: 0 2vw 2vw 2vw;
  278. background-color: var(--f35BColor);
  279. color: var(--fffColor);
  280. }
  281. .btn_2 {
  282. background-color: var(--fF0Color);
  283. }
  284. .btn_3 {
  285. background-color: var(--fFB1Color);
  286. }
  287. }
  288. .default {
  289. position: absolute;
  290. top: 0;
  291. right: 0;
  292. text {
  293. background: var(--fFB1Color);
  294. font-size: var(--font12Size);
  295. padding: 1vw;
  296. border-radius: 5px;
  297. }
  298. }
  299. }
  300. }
  301. }
  302. .two {
  303. display: flex;
  304. justify-content: space-around;
  305. .two_1 {
  306. margin: 0 0 2vw 0;
  307. button {
  308. font-size: var(--font14Size);
  309. color: var(--mainColor);
  310. background-color: var(--fFFColor);
  311. }
  312. }
  313. .two_1:nth-child(2) {
  314. button {
  315. background-color: var(--fFFColor);
  316. }
  317. }
  318. }
  319. }
  320. .scroll-view {
  321. position: absolute;
  322. top: 0;
  323. left: 0;
  324. right: 0;
  325. bottom: 0;
  326. .list-scroll-view {
  327. display: flex;
  328. flex-direction: column;
  329. }
  330. }
  331. .is_bottom {
  332. text-align: center;
  333. text {
  334. padding: 2vw 0;
  335. display: inline-block;
  336. color: #858585;
  337. font-size: 14px;
  338. }
  339. }
  340. </style>