index.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <!-- 签到 -->
  2. <template>
  3. <view class="sign-wrap">
  4. <view class="calendar">
  5. <!-- 每日签到 -->
  6. <view class="sign-everyday u-flex u-row-between u-p-30">
  7. <text class="sign-everyday-title">每日签到</text>
  8. <view class="sign-num-box">
  9. 已连续签到
  10. <text class="sign-num">{{ cuntinueDays }}</text>
  11. </view>
  12. </view>
  13. <!-- 切换年月 -->
  14. <view class="bar u-flex u-row-center">
  15. <view class="previous" @tap="handleCalendar(0)">
  16. <button class="u-reset-button bar-btn u-p-r-30"><view class="u-iconfont uicon-arrow-left" style="color:#c4c4c4 ;font-size:28rpx ;"></view></button>
  17. </view>
  18. <view class="date">{{ cur_year || '--' }} 年 {{ cur_month || '--' }} 月</view>
  19. <view class="next" @tap="handleCalendar(1)">
  20. <button class="u-reset-button bar-btn u-p-l-30"><view class="u-iconfont uicon-arrow-right" style="color:#c4c4c4 ;font-size:28rpx ;"></view></button>
  21. </view>
  22. </view>
  23. <!-- 显示星期 -->
  24. <view class="week u-flex">
  25. <view class="week-item u-flex u-row-center" v-for="(item, index) in weeks_ch" :key="index">{{ item }}</view>
  26. </view>
  27. <!-- 日历表 -->
  28. <view class="myDateTable">
  29. <view v-for="(item, j) in days" :key="j" class="dateCell u-flex u-row-center u-col-center">
  30. <!-- 空格 -->
  31. <view v-if="!item.date" class="cell"><text :decode="true">&nbsp;&nbsp;</text></view>
  32. <view v-else>
  33. <!-- 已签到日期 -->
  34. <view v-if="item.is_sign" class="cell is-sign">
  35. <text>{{ item.day }}</text>
  36. </view>
  37. <!-- 未签到日期 -->
  38. <view class="cell" v-else>
  39. <text>{{ item.day }}</text>
  40. </view>
  41. </view>
  42. </view>
  43. <!-- 签到按钮 -->
  44. <view class="u-flex u-col-center u-row-center sign-box">
  45. <button :disabled="isPresentMonth || isSign" @tap="onSign" class="u-reset-button sign-btn">{{ isSign ? '已签到' : '签到' }}</button>
  46. </view>
  47. </view>
  48. </view>
  49. <view class="cu-modal" :class="{ show: showSign }" @tap="showSign = false">
  50. <view class="sign-modal-box cu-dialog" style="width: 610rpx;">
  51. <view class="modal-head u-flex-col u-col-center">
  52. <image class="modal-bg" :src="$IMG_URL + '/imgs/modal/sign_modal_bg.jpg'" mode=""></image>
  53. <image class="sign-tag" :src="$IMG_URL + '/imgs/modal/sign_modal_succeed.png'" mode=""></image>
  54. <view class="sign-num-box">
  55. 已连续打卡
  56. <text class="sign-num">{{ cuntinueDays }}</text>
  57. </view>
  58. </view>
  59. <view class="modal-content u-flex-col u-col-center">
  60. <view class="sign-success">签到成功</view>
  61. <view class="sign-score">恭喜您获得{{ score || '0' }}积分</view>
  62. </view>
  63. <view class="modal-bottom u-flex u-row-center"><button class="u-reset-button confirem-btn" @tap="showSign = false">确认</button></view>
  64. </view>
  65. </view>
  66. </view>
  67. </template>
  68. <script>
  69. import { mapMutations, mapActions, mapState } from 'vuex';
  70. export default {
  71. components: {},
  72. data() {
  73. return {
  74. days: [], //日历
  75. cuntinueDays: 0, //签到天数
  76. score: '', //签到获得的积分
  77. cur_year: 0, //当前选的年
  78. cur_month: 0, //当前选的月
  79. cur_day: 0, //当前选择的天
  80. today: parseInt(new Date().getDate()), //本日
  81. toMonth: parseInt(new Date().getMonth() + 1), //本月
  82. toYear: parseInt(new Date().getFullYear()), //本年
  83. weeks_ch: ['日', '一', '二', '三', '四', '五', '六'], //星期
  84. isPresentMonth: false, //是否可签到
  85. isSign: false,
  86. showSign: false
  87. };
  88. },
  89. computed: {},
  90. onLoad() {
  91. this.cur_year = this.toYear;
  92. this.cur_month = this.toMonth;
  93. this.cur_day = this.today;
  94. this.getSignList();
  95. },
  96. methods: {
  97. ...mapActions(['getUserInfo']),
  98. // 当前签到记录
  99. getSignList() {
  100. let that = this;
  101. let month = that.cur_month < 10 ? '0' + that.cur_month : that.cur_month;
  102. let query = `${that.cur_year}-${month}`;
  103. that.$http('user.signList', {
  104. month: query
  105. }).then(res => {
  106. if (res.code == 1) {
  107. let emptyDays = that.calculateEmptyGrids();
  108. that.cuntinueDays = that.cuntinueDays === 0 ? res.data.cuntinue_days : that.cuntinueDays;
  109. that.days = [...emptyDays, ...res.data.days];
  110. that.selSign();
  111. }
  112. });
  113. },
  114. // 选中日期
  115. selSign() {
  116. let that = this;
  117. let selToday = `${that.toYear}-${that.toMonth.toString().padStart(2, '0')}-${that.today.toString().padStart(2, '0')}`;
  118. let newDay = `${that.cur_year}-${that.cur_month.toString().padStart(2, '0')}-${that.cur_day.toString().padStart(2, '0')}`;
  119. if (selToday === newDay) {
  120. let day = that.days.find(item => {
  121. return item.date === selToday;
  122. });
  123. that.isSign = day.is_sign;
  124. }
  125. },
  126. // 计算当月1号前空了几个格子
  127. calculateEmptyGrids() {
  128. let that = this;
  129. let emptyDays = [];
  130. const firstDayOfWeek = new Date(Date.UTC(that.cur_year, that.cur_month - 1, 1)).getDay();
  131. if (firstDayOfWeek > 0) {
  132. for (let i = 0; i < firstDayOfWeek; i++) {
  133. let obj = {
  134. day: null,
  135. is_sign: false
  136. };
  137. emptyDays.push(obj);
  138. }
  139. }
  140. return emptyDays;
  141. },
  142. //签到
  143. onSign() {
  144. let that = this;
  145. let month = that.cur_month.toString().padStart(2, '0');
  146. let query = `${that.cur_year}-${month}`;
  147. that.$http(
  148. 'user.sign',
  149. {
  150. month: query
  151. },
  152. '签到中...'
  153. ).then(res => {
  154. if (res.code == 1) {
  155. that.getSignList();
  156. that.score = res.data.score;
  157. that.getUserInfo();
  158. that.showSign = true;
  159. }
  160. });
  161. },
  162. // 切换控制年月,上一个月,下一个月
  163. handleCalendar(type) {
  164. const cur_year = parseInt(this.cur_year);
  165. const cur_month = parseInt(this.cur_month);
  166. let newMonth = cur_month;
  167. let newYear = cur_year;
  168. switch (type) {
  169. case 0:
  170. //上个月
  171. this.isPresentMonth = true;
  172. newMonth = cur_month - 1;
  173. if (newMonth < 1) {
  174. newYear = cur_year - 1;
  175. newMonth = 12;
  176. }
  177. if (newYear < this.toYear || (newYear === this.toYear && newMonth <= this.toMonth)) {
  178. this.isPresentMonth = false;
  179. }
  180. break;
  181. case 1:
  182. newMonth = cur_month + 1;
  183. if (newMonth > 12) {
  184. newYear = cur_year + 1;
  185. newMonth = 1;
  186. }
  187. if (newYear > this.toYear || (newYear === this.toYear && newMonth > this.toMonth)) {
  188. this.isPresentMonth = true;
  189. }
  190. break;
  191. default:
  192. break;
  193. }
  194. this.cur_year = newYear;
  195. this.cur_month = newMonth;
  196. this.getSignList();
  197. }
  198. }
  199. };
  200. </script>
  201. <style lang="scss">
  202. // 日历
  203. .calendar {
  204. background: #fff;
  205. .sign-everyday {
  206. height: 100rpx;
  207. background: rgba(255, 255, 255, 1);
  208. border: 1rpx solid rgba(223, 223, 223, 0.4);
  209. .sign-everyday-title {
  210. font-size: 32rpx;
  211. font-weight: 500;
  212. color: rgba(51, 51, 51, 1);
  213. }
  214. .sign-num-box {
  215. font-size: 26rpx;
  216. font-weight: 500;
  217. color: rgba(153, 153, 153, 1);
  218. .sign-num {
  219. font-weight: 600;
  220. color: #e3ad5b;
  221. padding: 0 10rpx;
  222. }
  223. }
  224. }
  225. // 年月日
  226. .bar {
  227. height: 100rpx;
  228. .date {
  229. font-weight: 600;
  230. }
  231. }
  232. // 星期
  233. .week {
  234. .week-item {
  235. font-size: 24rpx;
  236. font-weight: 500;
  237. color: rgba(153, 153, 153, 1);
  238. flex: 1;
  239. }
  240. }
  241. // 日历表
  242. .myDateTable {
  243. display: flex;
  244. flex-wrap: wrap;
  245. .dateCell {
  246. width: (750rpx/7);
  247. height: 80rpx;
  248. font-size: 26rpx;
  249. font-weight: 400;
  250. color: rgba(51, 51, 51, 1);
  251. .cell {
  252. display: flex;
  253. justify-content: center;
  254. align-items: center;
  255. }
  256. }
  257. }
  258. }
  259. .is-sign {
  260. width: 40rpx;
  261. height: 40rpx;
  262. background: linear-gradient(132deg, rgba(243, 223, 177, 1), rgba(243, 223, 177, 1), rgba(236, 190, 96, 1));
  263. border-radius: 50%;
  264. box-shadow: 0 0 4rpx 4rpx rgba(#e3ad5b, 0.4);
  265. color: #fff;
  266. }
  267. // 签到按钮
  268. .sign-box {
  269. height: 140rpx;
  270. width: 100%;
  271. .sign-btn {
  272. width: 219rpx;
  273. line-height: 70rpx;
  274. background: linear-gradient(90deg, rgba(233, 180, 97, 1), rgba(238, 204, 137, 1));
  275. border-radius: 35rpx;
  276. font-size: 28rpx;
  277. font-weight: 500;
  278. color: rgba(255, 255, 255, 0.9);
  279. }
  280. }
  281. .sign-modal-box {
  282. width: 520rpx;
  283. background: #fff;
  284. border-radius: 20rpx;
  285. position: relative;
  286. .modal-head {
  287. height: 320rpx;
  288. align-items: center;
  289. justify-content: center;
  290. position: relative;
  291. .modal-bg {
  292. width: 100%;
  293. height: 100%;
  294. }
  295. .sign-tag {
  296. width: 70rpx;
  297. height: 70rpx;
  298. position: absolute;
  299. top: 50rpx;
  300. left: 50%;
  301. transform: translateX(-50%);
  302. }
  303. .sign-num-box {
  304. font-size: 30rpx;
  305. color: #fff;
  306. position: absolute;
  307. top: 150rpx;
  308. left: 50%;
  309. transform: translateX(-50%);
  310. .sign-num {
  311. font-size: 36rpx;
  312. font-weight: 600;
  313. padding: 0 10rpx;
  314. }
  315. }
  316. }
  317. .modal-content {
  318. padding: 30rpx 0;
  319. .sign-success {
  320. font-size: 34rpx;
  321. font-weight: bold;
  322. color: rgba(227, 173, 91, 1);
  323. }
  324. .sign-score {
  325. font-size: 26rpx;
  326. font-weight: 500;
  327. color: rgba(153, 153, 153, 1);
  328. padding-top: 16rpx;
  329. }
  330. }
  331. .modal-bottom {
  332. padding-bottom: 47rpx;
  333. .confirem-btn {
  334. width: 300rpx;
  335. line-height: 70rpx;
  336. background: linear-gradient(90deg, rgba(233, 180, 97, 1), rgba(238, 204, 137, 1));
  337. border-radius: 35rpx;
  338. padding: 0;
  339. font-size: 28rpx;
  340. font-weight: 500;
  341. color: rgba(255, 255, 255, 0.9);
  342. }
  343. }
  344. }
  345. </style>