index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. //打卡日历页面
  2. const Parse = getApp().Parse;
  3. const dateF = require('../../../utils/date.js')
  4. const company = getApp().globalData.company;
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. days: [], //存放一个月的天数数组
  11. signUp: [], //用户判断当天是否已打卡 可废除下方模拟打卡数组直接采用此数组
  12. //用于判断
  13. cur_year: 0, //年
  14. cur_month: 0, //月
  15. count: 0, //累计打卡的数量
  16. continuous_daka_count: 0, //连续打卡次数
  17. task: null
  18. },
  19. /**
  20. * 生命周期函数--监听页面加载
  21. */
  22. onLoad: async function (options) {
  23. //获取当前年月
  24. const date = new Date();
  25. let _cur_year = date.getFullYear();
  26. let _cur_month = date.getMonth() + 1;
  27. let _weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
  28. this.setData({
  29. cur_year: _cur_year,
  30. cur_month: _cur_month,
  31. weeks_ch: _weeks_ch,
  32. })
  33. await this.getTask(),//查询积分表赋值给credit
  34. await this.calculateEmptyGrids(this.data.cur_year, this.data.cur_month); // 计算当月1号前空了几个格子,把它填充在days数组的前面
  35. await this.calculateDays(this.data.cur_year, this.data.cur_month); // 绘制当月天数占的格子,并把它放到days数组中
  36. await this.onGetSignUp();//获取当前用户当前任务的人签到状态
  37. },
  38. //查询积分表赋值给credit
  39. async getTask() {
  40. let Task = new Parse.Query('Task');
  41. Task.equalTo('company', company);
  42. Task.equalTo('isOpen', true);
  43. Task.equalTo('taskType', 'daily-check');
  44. let task = await Task.first();
  45. if (task && task.id) {
  46. let taskJSON = task.toJSON();
  47. this.setData({
  48. task: taskJSON
  49. });
  50. await this.setSignUp()
  51. } else {
  52. wx.showToast({
  53. title: '未设置签到规则',
  54. icon: 'none',
  55. })
  56. setTimeout(() => {
  57. wx.navigateBack({
  58. delta: 1
  59. });
  60. }, 1500)
  61. }
  62. },
  63. ///查询数据库签到了多少天 并复制给signUp数组
  64. async setSignUp() {
  65. let uid = Parse.User.current().id;
  66. let TaskLog = new Parse.Query("TaskLog");
  67. TaskLog.equalTo('company', company);
  68. TaskLog.equalTo('user', uid);
  69. TaskLog.equalTo('task', this.data.task.objectId);
  70. let taskLog = await TaskLog.find();
  71. let parsetsJSON = [];
  72. if (taskLog && taskLog.length > 0) {
  73. taskLog.forEach(parse => {
  74. let b = parse.toJSON();
  75. parsetsJSON.push(b);
  76. })
  77. }
  78. console.log(parsetsJSON)
  79. //这个用来装数据库的时间
  80. let count = new Array();
  81. for (let i = 0; i < taskLog.length; i++) {
  82. let sum = "";
  83. let dateSubString = "";
  84. sum = parsetsJSON[i].updatedAt;
  85. dateSubString = sum.substring(0, 10);
  86. count.push(dateSubString)
  87. }
  88. this.setData({
  89. signUp: count
  90. })
  91. this.onGetSignUp();
  92. },
  93. //获取当前用户该任务的签到数组
  94. onGetSignUp: function () {
  95. let that = this;
  96. let _arr = [];
  97. that.data.signUp.map(item => {
  98. _arr.push(item);
  99. })
  100. that.setData({
  101. count: _arr.length
  102. });
  103. //获取后就判断签到情况
  104. that.onJudgeSign();//匹配判断当月与当月哪些日子签到打卡
  105. },
  106. //匹配判断当月与当月哪些日子签到打卡
  107. onJudgeSign() {
  108. let that = this;
  109. let signs = that.data.signUp;
  110. let daysArr = that.data.days;
  111. for (let i = 0; i < signs.length; i++) {
  112. let current = new Date(signs[i].replace(/-/g, "/"));
  113. let year = current.getFullYear();
  114. let month = current.getMonth() + 1;
  115. let day = current.getDate();
  116. day = parseInt(day);
  117. for (let j = 0; j < daysArr.length; j++) {
  118. //年月日相同并且已打卡
  119. if (year == that.data.cur_year && month == that.data.cur_month && daysArr[j].date == day) {
  120. daysArr[j].isSign = true;
  121. }
  122. }
  123. }
  124. that.setData({
  125. days: daysArr
  126. });
  127. that.onJudgeContinuousClock();
  128. },
  129. //判断连续打卡次数
  130. onJudgeContinuousClock() {
  131. let that = this;
  132. let _count = 0;
  133. let arr = that.data.signUp
  134. // 再实际数据中,dateList 是不需要咱们手动排序的,我这边主要是用于测试,所以一些校验并未完善
  135. arr = arr.sort((a, b) => {
  136. return Date.parse(`${a.year}/${a.month}/${a.date}`) - Date.parse(`${b.year}/${b.month}/${b.date}`)
  137. })
  138. for (let i = 0; i < arr.length; i++) {
  139. //把时间转换为时间戳
  140. if (i != 0) {
  141. var newDate_ = Date.parse(arr[i]); //当天
  142. var theOriginalTime_ = Date.parse(arr[i - 1]);
  143. //计算天
  144. let _day = parseInt(newDate_ - theOriginalTime_) / (1000 * 60 * 60);
  145. if (_day <= 24) {
  146. _count += 1;
  147. } else {
  148. _count = 0;
  149. }
  150. }
  151. }
  152. that.setData({
  153. continuous_daka_count: _count != 0 ? _count + 1 : 0,
  154. })
  155. },
  156. //查询有没有签到
  157. async onBindTap() {
  158. //获取今天零点时间
  159. let beforeDawn = new Date(new Date().toLocaleDateString()).getTime(); //获取当天凌晨的时间
  160. let date = new Date(beforeDawn);
  161. let uid = Parse.User.current().id;
  162. let TaskLog = new Parse.Query('TaskLog');
  163. TaskLog.equalTo('company', company);
  164. TaskLog.equalTo('user', uid);
  165. TaskLog.equalTo('task', this.data.task.objectId);
  166. TaskLog.greaterThan('createdAt', date);
  167. let taskLog = await TaskLog.first();
  168. if (taskLog && taskLog.id) {
  169. wx.showToast({
  170. title: '今日已签到',
  171. icon: 'none'
  172. })
  173. return
  174. } else {
  175. let DailyCheck = Parse.Object.extend('DailyCheck')
  176. let dailyCheck = new DailyCheck()
  177. dailyCheck.set("user", {
  178. __type: "Pointer",
  179. className: "_User",
  180. objectId: uid
  181. })
  182. dailyCheck.set("company", {
  183. __type: "Pointer",
  184. className: "Company",
  185. objectId: company
  186. })
  187. let check = await dailyCheck.save();
  188. if (check && check.id) {
  189. wx.showToast({
  190. title: '签到成功',
  191. icon: 'success'
  192. })
  193. this.setSignUp();
  194. this.inCredit();
  195. }
  196. }
  197. },
  198. async check() {
  199. let userid = Parse.User.current().id
  200. let start = new Date(new Date(new Date().toLocaleDateString()).getTime())
  201. let TaskLog = new Parse.Query('TaskLog')
  202. TaskLog.equalTo('user', userid)
  203. TaskLog.equalTo('task', this.data.task.objectId)
  204. TaskLog.equalTo('company', company)
  205. TaskLog.greaterThan('createdAt', start)
  206. let log = await TaskLog.first()
  207. if (log && log.id) {
  208. return
  209. } else {
  210. let credit = this.data.task.credit ? this.data.task.credit : 1
  211. let maxLength = this.data.task.increLength - 1
  212. let increasing = this.data.task.increasing
  213. if (increasing && !maxLength) {
  214. credit = credit + (increasing * this.data.continuous_daka_count)
  215. }
  216. if (increasing && maxLength) {
  217. if (this.data.continuous_daka_count < maxLength) {
  218. credit = credit + (increasing * this.data.continuous_daka_count)
  219. } else {
  220. credit = credit + (increasing * maxLength)
  221. }
  222. }
  223. this.createdLog(userid, this.data.task.objectId, credit)
  224. }
  225. },
  226. async createdLog(uid, tid, credit) {
  227. let TaskLog = Parse.Object.extend('TaskLog')
  228. let tasklog = new TaskLog()
  229. tasklog.set('user', {
  230. __type: "Pointer",
  231. className: '_User',
  232. objectId: uid
  233. })
  234. tasklog.set('task', {
  235. __type: "Pointer",
  236. className: 'Task',
  237. objectId: tid
  238. })
  239. tasklog.set('company', {
  240. __type: "Pointer",
  241. className: 'Company',
  242. objectId: company
  243. })
  244. tasklog.set('isReceive', false)
  245. tasklog.set('credit', credit)
  246. await tasklog.save()
  247. wx.showToast({
  248. title: '签到成功',
  249. icon: 'success'
  250. })
  251. await this.setSignUp()
  252. await this.inCredit(credit)
  253. },
  254. //签到给用户添加5积分
  255. async inCredit(credit) {
  256. let uid = Parse.User.current().id
  257. let Profile = new Parse.Query('Profile')
  258. Profile.equalTo('user', uid)
  259. Profile.equalTo('company', company)
  260. let profile = await Profile.first()
  261. let profileJSON = profile.toJSON()
  262. let Account = new Parse.Query('Account')
  263. Account.equalTo('company', company)
  264. Account.equalTo('user', uid)
  265. let account = await Account.first()
  266. let accountJSON = account.toJSON()
  267. let targetAccount = accountJSON.objectId
  268. let AccountLog = Parse.Object.extend('AccountLog')
  269. let accountLog = new AccountLog()
  270. accountLog.set('assetCount', credit)
  271. accountLog.set('targetAccount', { __type: 'Pointer', 'className': 'Account', objectId: targetAccount })
  272. accountLog.set('company', { __type: 'Pointer', className: 'Company', objectId: company })
  273. accountLog.set('fromAccountName', '南昌水业工会')
  274. accountLog.set('assetType', 'credit')
  275. if (credit == 1) {
  276. accountLog.set('desc', '签到获得积分')
  277. }
  278. if (credit == 2) {
  279. accountLog.set('desc', '连续签到2天获得积分')
  280. }
  281. if (credit >= 3) {
  282. accountLog.set('desc', '连续签到3天获得积分')
  283. }
  284. accountLog.set('profile', { __type: 'Pointer', className: 'Profile', objectId: profileJSON.objectId })
  285. accountLog.set('isVerified', false)
  286. accountLog.save().then((res) => {
  287. res.set('isVerified', true)
  288. res.save()
  289. })
  290. },
  291. // 获取当月共多少天
  292. getThisMonthDays: function (year, month) {
  293. return new Date(year, month, 0).getDate()
  294. },
  295. // 获取当月第一天星期几
  296. getFirstDayOfWeek: function (year, month) {
  297. return new Date(Date.UTC(year, month - 1, 1)).getDay();
  298. },
  299. // 计算当月1号前空了几个格子,把它填充在days数组的前面
  300. calculateEmptyGrids: function (year, month) {
  301. let that = this;
  302. //计算每个月时要清零
  303. that.setData({
  304. days: [],
  305. });
  306. const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
  307. if (firstDayOfWeek > 0) {
  308. for (let i = 0; i < firstDayOfWeek; i++) {
  309. let obj = {
  310. date: null,
  311. isSign: false //拒绝
  312. }
  313. that.data.days.push(obj);
  314. }
  315. this.setData({
  316. days: that.data.days,
  317. });
  318. //清空
  319. } else {
  320. this.setData({
  321. days: []
  322. });
  323. }
  324. },
  325. // 绘制当月天数占的格子,并把它放到days数组中
  326. calculateDays: function (year, month) {
  327. let that = this;
  328. const thisMonthDays = this.getThisMonthDays(year, month);
  329. for (let i = 1; i <= thisMonthDays; i++) {
  330. let obj = {
  331. date: i,
  332. isSign: false
  333. }
  334. that.data.days.push(obj);
  335. }
  336. this.setData({
  337. days: that.data.days
  338. });
  339. },
  340. // 切换控制年月,上一个月,下一个月
  341. handleCalendar: function (e) {
  342. let that = this;
  343. const handle = e.currentTarget.dataset.handle;
  344. const cur_year = that.data.cur_year;
  345. const cur_month = that.data.cur_month;
  346. if (handle === 'prev') {
  347. let newMonth = cur_month - 1;
  348. let newYear = cur_year;
  349. if (newMonth < 1) {
  350. newYear = cur_year - 1;
  351. newMonth = 12;
  352. }
  353. this.setData({
  354. cur_year: newYear,
  355. cur_month: newMonth
  356. })
  357. this.calculateEmptyGrids(newYear, newMonth);
  358. this.calculateDays(newYear, newMonth);
  359. this.onGetSignUp();
  360. } else {
  361. let newMonth = cur_month + 1;
  362. let newYear = cur_year;
  363. if (newMonth > 12) {
  364. newYear = cur_year + 1;
  365. newMonth = 1;
  366. }
  367. this.setData({
  368. cur_year: newYear,
  369. cur_month: newMonth
  370. })
  371. this.calculateEmptyGrids(newYear, newMonth);
  372. this.calculateDays(newYear, newMonth);
  373. this.onGetSignUp();
  374. }
  375. },
  376. /**
  377. * 生命周期函数--监听页面初次渲染完成
  378. */
  379. onReady: function () {
  380. },
  381. /**
  382. * 生命周期函数--监听页面显示
  383. */
  384. onShow: function (options) {
  385. },
  386. /**
  387. * 生命周期函数--监听页面隐藏
  388. */
  389. onHide: function () {
  390. },
  391. /**
  392. * 生命周期函数--监听页面卸载
  393. */
  394. onUnload: function () {
  395. },
  396. /**
  397. * 页面相关事件处理函数--监听用户下拉动作
  398. */
  399. onPullDownRefresh: function () {
  400. },
  401. /**
  402. * 页面上拉触底事件的处理函数
  403. */
  404. onReachBottom: function () {
  405. },
  406. /**
  407. * 用户点击右上角分享
  408. */
  409. onShareAppMessage: function () {
  410. },
  411. })