activity.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // pages/signIn/index.js
  2. import SignIn from '../../../api/signIn'
  3. import { isEmpty } from '../../../utils/util'
  4. import { getMobileCache, getPhoneNumber as getPhoneNumberSync } from '../../../utils/user'
  5. const app = getApp();
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. showPage: false,
  12. showTaskCard: false,
  13. showSignInSuccessDlg: false,
  14. activity: {},
  15. signInNodes: [{ pos: 1, text: '星期一' },
  16. { pos: 2, text: '星期二' },
  17. { pos: 3, text: '星期三' },
  18. { pos: 4, text: '星期四' },
  19. { pos: 5, text: '星期五' },
  20. { pos: 6, text: '星期六' }],
  21. lastSignInNode: { pos: 7, text: '星期天' },
  22. todayIsSigned: false,
  23. notUseNum: 0,
  24. },
  25. /**
  26. * 生命周期函数--监听页面加载
  27. */
  28. onLoad: function (options) {
  29. },
  30. /**
  31. * 生命周期函数--监听页面显示
  32. */
  33. onShow: function () {
  34. this.setData({
  35. isLogin: getMobileCache() != "",
  36. })
  37. this.data.dayOfWeek = this.getDayOfWeek()
  38. this.startLoadActivityData()
  39. },
  40. startLoadActivityData() {
  41. SignIn.getActivityData(getMobileCache()).then(res => {
  42. if (res.code == 200) {
  43. this.setData({
  44. activity: res.data
  45. })
  46. this.mapToView(res.data)
  47. }
  48. }).catch(_ => {
  49. }).finally(_ => {
  50. this.setData({
  51. showPage: true
  52. })
  53. })
  54. if (this.data.isLogin) {
  55. SignIn.getUserAwardCouponNum({
  56. mobile: getMobileCache()
  57. }).then( res => {
  58. if (res.code == 200) {
  59. this.setData({
  60. notUseNum: res.data.notUseNum || 0
  61. })
  62. }
  63. console.log(res)
  64. }).catch(_ => {})
  65. }
  66. },
  67. mapToView(data) {
  68. this.setColors(data.color)
  69. if (data.dayAwardList && data.dayAwardList.length > 0) {
  70. data.dayAwardList.forEach(item => {
  71. if (item.dayNo == 7) {
  72. this.preDealItemData(this.data.lastSignInNode, item);
  73. } else {
  74. if (this.data.signInNodes[item.dayNo - 1]) {
  75. this.preDealItemData(this.data.signInNodes[item.dayNo - 1], item)
  76. }
  77. }
  78. });
  79. let showTaskCard = false
  80. if (data.taskAwardList && data.taskAwardList.length > 0) {
  81. showTaskCard = true
  82. }
  83. this.setData({
  84. signInNodes: this.data.signInNodes,
  85. lastSignInNode: this.data.lastSignInNode,
  86. showTaskCard,
  87. })
  88. }
  89. },
  90. preDealItemData(target, item) {
  91. // 未签到的情况下,计算是否属于过签
  92. Object.assign(target, item);
  93. if (target.isSignIn == 0 && target.pos < this.data.dayOfWeek) {
  94. target.isSignIn = -1;
  95. target.textBgGround = this.data.grayBackGroudStyle
  96. } else {
  97. target.textBgGround = this.data.backGroundStyle
  98. }
  99. if (target.pos == this.data.dayOfWeek) {
  100. this.setData({
  101. todayIsSigned: target.isSignIn == 1
  102. })
  103. }
  104. },
  105. getDayOfWeek(date) {
  106. var now = date || new Date();
  107. var day = now.getDay();
  108. if (day == 0) {
  109. return 7
  110. }
  111. return day;
  112. },
  113. setColors(color) {
  114. if (isEmpty(color)) {
  115. color = "#EE5A5A"
  116. }
  117. if (color.length == 4) {
  118. color = color.replace(/#(.)(.)(.)$/, "#$1$1$2$2$3$3")
  119. }
  120. let newColor = "#";
  121. for (let i = 1; i <= 6; i++) {
  122. if (color.charAt(i) == 'a' || color.charAt(i) == 'A') {
  123. newColor += "9"
  124. } else if (color.charAt(i) == '0') {
  125. newColor += "0"
  126. } else {
  127. newColor += String.fromCharCode(color.charCodeAt(i) - 1)
  128. }
  129. }
  130. this.setData({
  131. mainColor: color,
  132. secColor: newColor,
  133. thirdColor: "rgb(" + this.toRGB(color) + ", 0.3)",
  134. backGroundStyle: "background: linear-gradient(" +color+", "+ newColor+ ", "+color+");",
  135. grayBackGroudStyle: "background: #c6c6c6"
  136. })
  137. },
  138. toRGB(hex) {
  139. const list = [hex.substr(1,2), hex.substr(3,2), hex.substr(5,2)]
  140. list[0] = parseInt(list[0] , 16)
  141. list[1] = parseInt(list[1] , 16)
  142. list[2] = parseInt(list[2] , 16)
  143. return list.join(',')
  144. },
  145. popMessage(message) {
  146. app.showToast(message, "none")
  147. },
  148. tapSignIn: function() {
  149. if (!this.data.isLogin) {
  150. return;
  151. }
  152. if (!this.data.activity.id) {
  153. this.popMessage("未找到签到活动")
  154. return;
  155. }
  156. if (this.isLocked) {
  157. return
  158. }
  159. this.isLocked = true
  160. SignIn.triggerSignIn(this.data.activity.id, getMobileCache()).then(res => {
  161. console.log(res)
  162. if (res.code == 200) {
  163. this.dealSignResult(res.data)
  164. this.startLoadActivityData()
  165. }
  166. }).catch(_ => {
  167. console.log(_)
  168. }).finally(_ => {
  169. this.isLocked = false
  170. })
  171. },
  172. /**
  173. * 处理签到结果
  174. */
  175. dealSignResult(data) {
  176. if (data.isHit == 0 || data.coupon == null) {
  177. // 没有配置奖品的情况下
  178. this.popMessage("签到成功!")
  179. return
  180. }
  181. const hitResult = data.coupon;
  182. if (hitResult.couponType == "C") {
  183. // 现金券
  184. if (hitResult.formatReduceCost.length >= 3) {
  185. hitResult._classSmallStyle = "_small"
  186. }
  187. } else if (hitResult.couponType == "D") {
  188. // 折扣券
  189. if (hitResult.formatDiscount.length >= 3) {
  190. hitResult._classSmallStyle = "_small"
  191. }
  192. }
  193. if (hitResult.formatLeastCost == "0") {
  194. hitResult.formatLeastCostStr = "无门槛使用"
  195. } else {
  196. hitResult.formatLeastCostStr = "满" + hitResult.formatLeastCost + "元使用"
  197. }
  198. this.setData({
  199. hitResult: hitResult,
  200. showSignInSuccessDlg: true
  201. })
  202. },
  203. // 授权手机号
  204. getPhoneNumber(e) {
  205. getPhoneNumberSync(e, _ => {
  206. this.setData({ isLogin: true })
  207. this.startLoadActivityData()
  208. })
  209. },
  210. closeDlg() {
  211. this.setData({
  212. showSignInSuccessDlg: false
  213. })
  214. },
  215. toRecordDetailPage() {
  216. wx.navigateTo({
  217. url: '../record/record',
  218. })
  219. },
  220. /**
  221. * 生命周期函数--监听页面初次渲染完成
  222. */
  223. onReady: function () {
  224. },
  225. /**
  226. * 生命周期函数--监听页面隐藏
  227. */
  228. onHide: function () {
  229. },
  230. /**
  231. * 生命周期函数--监听页面卸载
  232. */
  233. onUnload: function () {
  234. },
  235. /**
  236. * 页面相关事件处理函数--监听用户下拉动作
  237. */
  238. onPullDownRefresh: function () {
  239. },
  240. /**
  241. * 页面上拉触底事件的处理函数
  242. */
  243. onReachBottom: function () {
  244. },
  245. /**
  246. * 用户点击右上角分享
  247. */
  248. onShareAppMessage: function () {
  249. },
  250. handlePageSizes(e) {
  251. // console.log(e)
  252. }
  253. })