welfare.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. <template>
  2. <layout-page>
  3. <view class="box defaultBgc">
  4. <scroll-view class="scrollBox" scroll-y="true" @scrolltolower="loadingMore">
  5. <view class="content">
  6. <!-- 钱包 -->
  7. <view class="wallet">
  8. <view class="wallet-content">
  9. <view class="wallet-content-member">
  10. <view class="iconBox">
  11. <view class="iconBox-line">
  12. <uni-icons type="vip-filled" size="20" color="#ffbc00"></uni-icons>
  13. </view>
  14. </view>
  15. <view class="name">
  16. <text v-if="useUserStore.isLogin" @tap="toInfo" class="active">
  17. {{ useUserStore?.baseInfo?.name || useUserStore?.userInfo?.phone }}
  18. </text>
  19. <text v-else @tap="handleLogin">点击登录</text>
  20. </view>
  21. </view>
  22. <view class="wallet-content-integral wallet-content-item" @tap="handleTo('integral')">
  23. <text>{{ balance.point || 0 }}</text>
  24. <text class="title">积分</text>
  25. </view>
  26. <view class="wallet-content-cash wallet-content-item" @tap="handleTo('balance')">
  27. <text>{{ balance.balance > 0 ? (balance?.balance / 100.0).toFixed(2) : 0 }}</text>
  28. <text class="title">余额</text>
  29. </view>
  30. <view class="wallet-content-coupon wallet-content-item" @tap="handleTo('coupon')">
  31. <text>{{ myCoupon }}</text>
  32. <text class="title">优惠券</text>
  33. </view>
  34. </view>
  35. </view>
  36. <!-- 签到 -->
  37. <view class="signIn">
  38. <view class="signIn-content">
  39. <view class="signIn-content-items">
  40. <view class="item" v-for="(sign, i) in SignItems" :key="sign.day">
  41. <view class="box" :class="{ active: i < continuousDay }">
  42. <uni-icons type="vip" size="30" color="#ffbc00" style="text-align: center;"></uni-icons>
  43. <text class="text">+{{ sign.point }}</text>
  44. </view>
  45. <view class="day">
  46. <text>{{ i === todayNumber ? '今' : '第' + sign.day }}天</text>
  47. </view>
  48. </view>
  49. </view>
  50. <view class="tips">
  51. <button
  52. :disabled="todaySignIn"
  53. :class="{ disabled: todaySignIn }"
  54. @tap="handleSignIn"
  55. >
  56. {{ todaySignIn ? '已签到' : '签到'}}
  57. </button>
  58. </view>
  59. </view>
  60. </view>
  61. <!-- 福利 -->
  62. <view class="welfare">
  63. <view
  64. v-for="item in items"
  65. :key="item.id"
  66. class="item"
  67. :class="{ disabled: !item.canTake }"
  68. @tap="onGetCoupon(item.id, item.canTake)"
  69. >
  70. <view class="img">
  71. <image
  72. src="/static/img/ticket.png"
  73. mode="scaleToFill"
  74. />
  75. </view>
  76. <text>{{ item.name }}</text>
  77. </view>
  78. </view>
  79. <uni-load-more :status="more" />
  80. </view>
  81. </scroll-view>
  82. </view>
  83. </layout-page>
  84. </template>
  85. <script setup>
  86. import { ref, watch } from 'vue'
  87. import layoutPage from '@/layout'
  88. import {
  89. getRewardSignInConfigList,
  90. getRewardSignInRecordSummary,
  91. createRewardSignInRecord,
  92. getAccountBalance,
  93. getUserAccount,
  94. getDiyTemplateUsed,
  95. getDiyTemplate,
  96. getCouponTemplatePage,
  97. takeCoupon,
  98. getCouponPage
  99. } from '@/api/sign'
  100. import { onShow, onLoad } from '@dcloudio/uni-app'
  101. import { userStore } from '@/store/user'
  102. import { showAuthModal } from '@/hooks/useModal'
  103. const useUserStore = userStore()
  104. // 设置自定义tabBar选中值
  105. const SignItems = ref([])
  106. // 连续签到天数
  107. const continuousDay = ref(0)
  108. // 今天有无签到
  109. const todaySignIn = ref(false)
  110. // 今天
  111. const todayNumber = ref()
  112. // 加载
  113. const signLoading = ref(false)
  114. const balance = ref({})
  115. const pageInfo = ref({
  116. pageNo: 1,
  117. pageSize: 20
  118. })
  119. const total = ref(0)
  120. const items = ref([])
  121. const more = ref('more')
  122. const myCoupon = ref(0)
  123. watch(() => useUserStore.isLogin, () => {
  124. if (useUserStore.isLogin) {
  125. init()
  126. }
  127. })
  128. watch(() => useUserStore.refreshToken, () => {
  129. init()
  130. })
  131. onShow(() => {
  132. const currentPage = getCurrentPages()[0]; // 获取当前页面实例
  133. const currentTabBar = currentPage?.getTabBar?.();
  134. // 设置当前tab页的下标index
  135. currentTabBar?.setData({ selected: 2 });
  136. init()
  137. })
  138. function init () {
  139. // 获取签到列表
  140. getSignIn()
  141. // 获取签到信息
  142. getSummary()
  143. // 获取余额积分
  144. getBalance()
  145. pageInfo.value.pageNo = 1
  146. items.value = []
  147. // 获取模板
  148. getAllCouponPage()
  149. getMyCoupon()
  150. }
  151. // 获取积分余额
  152. async function getBalance() {
  153. const { data } = await getAccountBalance()
  154. const { data: _data } = await getUserAccount()
  155. balance.value = {
  156. ...data,
  157. point: _data.point
  158. }
  159. }
  160. // 获取个人签到统计
  161. async function getSummary() {
  162. const { data } = await getRewardSignInRecordSummary()
  163. if (!data) return
  164. continuousDay.value = data.continuousDay // 连续签到第n天
  165. todaySignIn.value = data.todaySignIn // 今天有无签到
  166. todayNumber.value = todaySignIn.value ? continuousDay.value - 1 : continuousDay.value
  167. }
  168. // 获取签到列表
  169. async function getSignIn () {
  170. try {
  171. const { data } = await getRewardSignInConfigList()
  172. SignItems.value = data
  173. } catch (error) {
  174. }
  175. }
  176. // 签到
  177. async function handleSignIn () {
  178. if (signLoading.value) {
  179. return
  180. }
  181. try {
  182. signLoading.value = true
  183. await createRewardSignInRecord()
  184. setTimeout(async () => {
  185. await getSummary()
  186. uni.showToast({
  187. title: '签到成功',
  188. icon: 'success',
  189. mask: true
  190. })
  191. // 更新积分
  192. getBalance()
  193. }, 1000)
  194. } catch (error) {
  195. } finally {
  196. signLoading.value = false
  197. }
  198. }
  199. // 获取优惠券分页
  200. async function getAllCouponPage () {
  201. try {
  202. const { data } = await getCouponTemplatePage({ ...pageInfo.value })
  203. console.log(data)
  204. if (!data || !data.list || !data.list.length) {
  205. if (pageInfo.value.pageNo === 1) {
  206. more.value = 'more'
  207. return
  208. }
  209. pageInfo.value.pageNo--
  210. more.value = 'more'
  211. return
  212. }
  213. items.value.push(...data.list)
  214. total.value = +data.total
  215. more.value = total.value === items.value.length ? 'noMore' : 'more'
  216. } catch (error) {
  217. if (pageInfo.value.pageNo === 1) {
  218. more.value = 'more'
  219. return
  220. }
  221. pageInfo.value.pageNo--
  222. more.value = 'more'
  223. }
  224. }
  225. async function getMyCoupon () {
  226. const { data } = await getCouponPage({ pageNo:1, pageSize: 1, status: 1 })
  227. if (data) {
  228. myCoupon.value = +data.total
  229. }
  230. }
  231. // 优惠券列表
  232. async function handleGetTmpUsed () {
  233. try {
  234. const { data } = await getDiyTemplateUsed()
  235. if (!data?.home?.components) {
  236. uni.showToast({
  237. title: '暂无优惠券',
  238. icon: 'none',
  239. mask: true
  240. })
  241. return
  242. }
  243. const idsItem = data.home.components.find(e => e.id === 'CouponCard')
  244. if (!idsItem) {
  245. uni.showToast({
  246. title: '暂无优惠券',
  247. icon: 'none',
  248. mask: true
  249. })
  250. return
  251. }
  252. const ids = idsItem?.property?.couponIds
  253. if (!ids) {
  254. uni.showToast({
  255. title: '暂无优惠券',
  256. icon: 'none',
  257. mask: true
  258. })
  259. return
  260. }
  261. const { data: _data } = await getDiyTemplate(ids.join(','))
  262. items.value = _data
  263. more.value = 'noMore'
  264. } catch (error) {
  265. }
  266. }
  267. async function loadingMore () {
  268. if (more.value === 'noMore') {
  269. return
  270. }
  271. more.value = 'loading'
  272. pageInfo.value.pageNo++
  273. getAllCouponPage()
  274. }
  275. function handleLogin () {
  276. if (!useUserStore.isLogin) {
  277. showAuthModal()
  278. return
  279. }
  280. }
  281. function toInfo () {
  282. uni.navigateTo({
  283. url: '/pagesA/info/index'
  284. })
  285. }
  286. async function onGetCoupon(id, canTake) {
  287. if (!canTake) {
  288. return
  289. }
  290. uni.showLoading({
  291. title: '领取中'
  292. })
  293. try {
  294. const {
  295. code,
  296. msg
  297. } = await takeCoupon(id)
  298. if (code !== 0) {
  299. uni.showToast({
  300. title: msg,
  301. icon: 'none'
  302. })
  303. return
  304. }
  305. uni.showToast({
  306. title: '领取成功',
  307. icon: 'success'
  308. })
  309. getMyCoupon()
  310. pageInfo.value.pageNo = 1
  311. items.value = []
  312. await getAllCouponPage()
  313. } catch (error) {
  314. } finally {
  315. uni.hideLoading()
  316. }
  317. }
  318. function handleTo (page) {
  319. uni.navigateTo({
  320. url: `/pagesA/${page}/index`
  321. })
  322. }
  323. </script>
  324. <style scoped lang="scss">
  325. $px: 20rpx;
  326. .box {
  327. overflow: hidden;
  328. height: 100vh;
  329. padding: $px 0 0 0;
  330. box-sizing: border-box;
  331. .scrollBox{
  332. height: 100%;
  333. padding-bottom: 120rpx;
  334. box-sizing: border-box;
  335. }
  336. .content {
  337. padding-bottom: 24rpx;
  338. box-sizing: border-box;
  339. }
  340. .wallet {
  341. // padding: 0 $px;
  342. box-sizing: border-box;
  343. height: 200rpx;
  344. margin-bottom: 20rpx;
  345. &-content {
  346. display: flex;
  347. height: 100%;
  348. // border: 2rpx solid #E5E5E5;
  349. border-radius: 10rpx;
  350. background: #FFF;
  351. &-member {
  352. width: 50%;
  353. display: flex;
  354. align-items: center;
  355. position: relative;
  356. &:after {
  357. content: '';
  358. position: absolute;
  359. width: 4rpx;
  360. height: 30%;
  361. right: 0;
  362. top: 35%;
  363. background: #00897B;
  364. }
  365. .iconBox {
  366. width: 100rpx;
  367. height: 100%;
  368. display: flex;
  369. align-items: center;
  370. justify-content: center;
  371. &-line {
  372. border: 4rpx solid #ffbc00;
  373. border-radius: 180rpx;
  374. padding: 10rpx;
  375. }
  376. }
  377. .name {
  378. flex: 1;
  379. .actvie {
  380. color: #ffbc00;
  381. }
  382. }
  383. }
  384. &-item {
  385. width: 16.6%;
  386. display: flex;
  387. flex-direction: column;
  388. align-items: center;
  389. justify-content: center;
  390. .title {
  391. font-size: .85em;
  392. }
  393. }
  394. &-integral {
  395. }
  396. &-cash {
  397. }
  398. }
  399. }
  400. .signIn {
  401. // padding: 0 $px;
  402. box-sizing: border-box;
  403. &-content {
  404. border-radius: 10rpx;
  405. background: #FFF;
  406. &-items {
  407. padding: 20rpx $px;
  408. display: flex;
  409. height: 220rpx;
  410. box-sizing: border-box;
  411. .item {
  412. height: 140rpx;
  413. width: 100%;
  414. align-items: center;
  415. justify-content: center;
  416. padding: 0 10rpx;
  417. .box {
  418. height: 100%;
  419. width: 100%;
  420. display: flex;
  421. flex-direction: column;
  422. background: #f2f4f7;
  423. border-radius: 10rpx;
  424. &.active {
  425. background: rgba(16,137,123, .66);
  426. }
  427. .text {
  428. font-size: .75em;
  429. text-align: center;
  430. }
  431. }
  432. .day {
  433. margin-top: 10rpx;
  434. height: 60rpx;
  435. text-align: center;
  436. font-size: .5em;
  437. }
  438. }
  439. }
  440. .tips {
  441. display: flex;
  442. align-items: center;
  443. justify-content: flex-end;
  444. height: 80rpx;
  445. font-size: .75em;
  446. color: #999;
  447. &-light {
  448. color: #00897B;
  449. padding: 0 10rpx;
  450. }
  451. button {
  452. width: 180rpx;
  453. height: 60rpx;
  454. font-size: 24rpx;
  455. margin: 0 10rpx;
  456. border: unset;
  457. background-color: #00897B;
  458. color: #FFF;
  459. &.disabled {
  460. background-color: #a7a7a7 !important;
  461. }
  462. }
  463. }
  464. }
  465. }
  466. .welfare {
  467. display: grid;
  468. grid-template-columns: repeat(2, 1fr);
  469. grid-gap: 20rpx;
  470. padding: 20rpx $px;
  471. .item {
  472. text-align: center;
  473. background: #FFF;
  474. padding: $px;
  475. font-size: .85em;
  476. &.disabled {
  477. position: relative;
  478. &::after {
  479. content: '已领取';
  480. display: flex;
  481. align-items: center;
  482. justify-content: center;
  483. position: absolute;
  484. top: 0;
  485. left: 0;
  486. width: 100%;
  487. height: 100%;
  488. background: rgba(255,255,255,.75);
  489. }
  490. }
  491. .img {
  492. image {
  493. width: 128rpx;
  494. height: 128rpx;
  495. }
  496. }
  497. }
  498. }
  499. }
  500. </style>