welfare.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. pageInfo.value.pageNo++
  269. getAllCouponPage()
  270. }
  271. function handleLogin () {
  272. if (!useUserStore.isLogin) {
  273. showAuthModal()
  274. return
  275. }
  276. }
  277. function toInfo () {
  278. uni.navigateTo({
  279. url: '/pagesA/info/index'
  280. })
  281. }
  282. async function onGetCoupon(id, canTake) {
  283. if (!canTake) {
  284. return
  285. }
  286. uni.showLoading({
  287. title: '领取中'
  288. })
  289. try {
  290. const {
  291. code,
  292. msg
  293. } = await takeCoupon(id)
  294. if (code !== 0) {
  295. uni.showToast({
  296. title: msg,
  297. icon: 'none'
  298. })
  299. return
  300. }
  301. uni.showToast({
  302. title: '领取成功',
  303. icon: 'success'
  304. })
  305. getMyCoupon()
  306. pageInfo.value.pageNo = 1
  307. items.value = []
  308. await getAllCouponPage()
  309. } catch (error) {
  310. } finally {
  311. uni.hideLoading()
  312. }
  313. }
  314. function handleTo (page) {
  315. uni.navigateTo({
  316. url: `/pagesA/${page}/index`
  317. })
  318. }
  319. </script>
  320. <style scoped lang="scss">
  321. $px: 20rpx;
  322. .box {
  323. overflow: hidden;
  324. height: 100vh;
  325. padding: $px 0 0 0;
  326. box-sizing: border-box;
  327. .scrollBox{
  328. height: 100%;
  329. padding-bottom: 120rpx;
  330. box-sizing: border-box;
  331. }
  332. .content {
  333. padding-bottom: 24rpx;
  334. box-sizing: border-box;
  335. }
  336. .wallet {
  337. // padding: 0 $px;
  338. box-sizing: border-box;
  339. height: 200rpx;
  340. margin-bottom: 20rpx;
  341. &-content {
  342. display: flex;
  343. height: 100%;
  344. // border: 2rpx solid #E5E5E5;
  345. border-radius: 10rpx;
  346. background: #FFF;
  347. &-member {
  348. width: 50%;
  349. display: flex;
  350. align-items: center;
  351. position: relative;
  352. &:after {
  353. content: '';
  354. position: absolute;
  355. width: 4rpx;
  356. height: 30%;
  357. right: 0;
  358. top: 35%;
  359. background: #00897B;
  360. }
  361. .iconBox {
  362. width: 100rpx;
  363. height: 100%;
  364. display: flex;
  365. align-items: center;
  366. justify-content: center;
  367. &-line {
  368. border: 4rpx solid #ffbc00;
  369. border-radius: 180rpx;
  370. padding: 10rpx;
  371. }
  372. }
  373. .name {
  374. flex: 1;
  375. .actvie {
  376. color: #ffbc00;
  377. }
  378. }
  379. }
  380. &-item {
  381. width: 16.6%;
  382. display: flex;
  383. flex-direction: column;
  384. align-items: center;
  385. justify-content: center;
  386. .title {
  387. font-size: .85em;
  388. }
  389. }
  390. &-integral {
  391. }
  392. &-cash {
  393. }
  394. }
  395. }
  396. .signIn {
  397. // padding: 0 $px;
  398. box-sizing: border-box;
  399. &-content {
  400. border-radius: 10rpx;
  401. background: #FFF;
  402. &-items {
  403. padding: 20rpx $px;
  404. display: flex;
  405. height: 220rpx;
  406. box-sizing: border-box;
  407. .item {
  408. height: 140rpx;
  409. width: 100%;
  410. align-items: center;
  411. justify-content: center;
  412. padding: 0 10rpx;
  413. .box {
  414. height: 100%;
  415. width: 100%;
  416. display: flex;
  417. flex-direction: column;
  418. background: #f2f4f7;
  419. border-radius: 10rpx;
  420. &.active {
  421. background: rgba(16,137,123, .66);
  422. }
  423. .text {
  424. font-size: .75em;
  425. text-align: center;
  426. }
  427. }
  428. .day {
  429. margin-top: 10rpx;
  430. height: 60rpx;
  431. text-align: center;
  432. font-size: .5em;
  433. }
  434. }
  435. }
  436. .tips {
  437. display: flex;
  438. align-items: center;
  439. justify-content: flex-end;
  440. height: 80rpx;
  441. font-size: .75em;
  442. color: #999;
  443. &-light {
  444. color: #00897B;
  445. padding: 0 10rpx;
  446. }
  447. button {
  448. width: 180rpx;
  449. height: 60rpx;
  450. font-size: 24rpx;
  451. margin: 0 10rpx;
  452. border: unset;
  453. background-color: #00897B;
  454. color: #FFF;
  455. &.disabled {
  456. background-color: #a7a7a7 !important;
  457. }
  458. }
  459. }
  460. }
  461. }
  462. .welfare {
  463. display: grid;
  464. grid-template-columns: repeat(2, 1fr);
  465. grid-gap: 20rpx;
  466. padding: 20rpx $px;
  467. .item {
  468. text-align: center;
  469. background: #FFF;
  470. padding: $px;
  471. font-size: .85em;
  472. &.disabled {
  473. position: relative;
  474. &::after {
  475. content: '已领取';
  476. display: flex;
  477. align-items: center;
  478. justify-content: center;
  479. position: absolute;
  480. top: 0;
  481. left: 0;
  482. width: 100%;
  483. height: 100%;
  484. background: rgba(255,255,255,.75);
  485. }
  486. }
  487. .img {
  488. image {
  489. width: 128rpx;
  490. height: 128rpx;
  491. }
  492. }
  493. }
  494. }
  495. }
  496. </style>