activity.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. showSignInSuccessDlg: false,
  13. activity: {},
  14. signInNodes: [{ pos: 1, text: '星期一' },
  15. { pos: 2, text: '星期二' },
  16. { pos: 3, text: '星期三' },
  17. { pos: 4, text: '星期四' },
  18. { pos: 5, text: '星期五' },
  19. { pos: 6, text: '星期六' }],
  20. lastSignInNode: { pos: 7, text: '星期天' },
  21. todayIsSigned: false,
  22. isLogin: getMobileCache() != "",
  23. notUseNum: 0
  24. },
  25. /**
  26. * 生命周期函数--监听页面加载
  27. */
  28. onLoad: function (options) {
  29. this.data.dayOfWeek = this.getDayOfWeek()
  30. this.startLoadActivityData()
  31. this.setPageStyle()
  32. },
  33. startLoadActivityData() {
  34. SignIn.getActivityData(getMobileCache()).then(res => {
  35. if (res.code == 200) {
  36. this.setData({
  37. activity: res.data
  38. })
  39. this.mapToView(res.data)
  40. }
  41. }).catch(_ => {
  42. }).finally(_ => {
  43. this.setData({
  44. showPage: true
  45. })
  46. })
  47. if (this.data.isLogin) {
  48. SignIn.getUserAwardCouponNum({
  49. mobile: getMobileCache()
  50. }).then( res => {
  51. if (res.code == 200) {
  52. this.setData({
  53. notUseNum: res.data.notUseNum || 0
  54. })
  55. }
  56. console.log(res)
  57. }).catch(_ => {})
  58. }
  59. },
  60. setPageStyle() {
  61. let dwObj = wx.getMenuButtonBoundingClientRect()
  62. let navHeight_ = (dwObj.top + dwObj.height)
  63. let capsuleTop_ = dwObj.top
  64. let windowHeight = wx.getSystemInfoSync().windowHeight
  65. this.setData({
  66. navHeight: navHeight_,
  67. capsuleTop:capsuleTop_,
  68. capHeight: dwObj.height,
  69. bodyHeight: windowHeight - navHeight_,
  70. });
  71. },
  72. mapToView(data) {
  73. this.setColors(data.color)
  74. if (data.dayAwardList && data.dayAwardList.length > 0) {
  75. data.dayAwardList.forEach(item => {
  76. if (item.dayNo == 7) {
  77. this.preDealItemData(this.data.lastSignInNode, item);
  78. } else {
  79. if (this.data.signInNodes[item.dayNo - 1]) {
  80. this.preDealItemData(this.data.signInNodes[item.dayNo - 1], item)
  81. }
  82. }
  83. });
  84. this.setData({
  85. signInNodes: this.data.signInNodes,
  86. lastSignInNode: this.data.lastSignInNode
  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. backGroundStyle: "background: linear-gradient(" +color+", "+ newColor+ ", "+color+");",
  134. grayBackGroudStyle: "background: #c6c6c6"
  135. })
  136. },
  137. popMessage(message) {
  138. app.showToast(message, "none")
  139. },
  140. tapSignIn: function() {
  141. if (!this.data.isLogin) {
  142. return;
  143. }
  144. if (!this.data.activity.id) {
  145. this.popMessage("未找到签到活动")
  146. return;
  147. }
  148. if (this.isLocked) {
  149. return
  150. }
  151. this.isLocked = true
  152. SignIn.triggerSignIn(this.data.activity.id, getMobileCache()).then(res => {
  153. console.log(res)
  154. if (res.code == 200) {
  155. this.dealSignResult(res.data)
  156. this.startLoadActivityData()
  157. }
  158. }).catch(_ => {
  159. console.log(_)
  160. }).finally(_ => {
  161. this.isLocked = false
  162. })
  163. },
  164. /**
  165. * 处理签到结果
  166. */
  167. dealSignResult(data) {
  168. if (data.isHit == 0 || data.coupon == null) {
  169. // 没有配置奖品的情况下
  170. this.popMessage("签到成功!")
  171. return
  172. }
  173. const hitResult = data.coupon;
  174. if (hitResult.couponType == "C") {
  175. // 现金券
  176. if (hitResult.formatReduceCost.length >= 3) {
  177. hitResult._classSmallStyle = "_small"
  178. }
  179. } else if (hitResult.couponType == "D") {
  180. // 折扣券
  181. if (hitResult.formatReduceCost.length >= 3) {
  182. hitResult._classSmallStyle = "_small"
  183. }
  184. }
  185. if (hitResult.formatLeastCost == "0") {
  186. hitResult.formatLeastCostStr = "无门槛使用"
  187. } else {
  188. hitResult.formatLeastCostStr = "满" + hitResult.formatLeastCost + "元使用"
  189. }
  190. this.setData({
  191. hitResult: hitResult,
  192. showSignInSuccessDlg: true
  193. })
  194. },
  195. // 授权手机号
  196. getPhoneNumber(e) {
  197. getPhoneNumberSync(e, _ => {
  198. this.setData({ isLogin: true })
  199. this.startLoadActivityData()
  200. })
  201. },
  202. closeDlg() {
  203. this.setData({
  204. showSignInSuccessDlg: false
  205. })
  206. },
  207. toRecordDetailPage() {
  208. wx.navigateTo({
  209. url: '../record/record',
  210. })
  211. },
  212. /**
  213. * 生命周期函数--监听页面初次渲染完成
  214. */
  215. onReady: function () {
  216. },
  217. /**
  218. * 生命周期函数--监听页面显示
  219. */
  220. onShow: function () {
  221. },
  222. /**
  223. * 生命周期函数--监听页面隐藏
  224. */
  225. onHide: function () {
  226. },
  227. /**
  228. * 生命周期函数--监听页面卸载
  229. */
  230. onUnload: function () {
  231. },
  232. /**
  233. * 页面相关事件处理函数--监听用户下拉动作
  234. */
  235. onPullDownRefresh: function () {
  236. },
  237. /**
  238. * 页面上拉触底事件的处理函数
  239. */
  240. onReachBottom: function () {
  241. },
  242. /**
  243. * 用户点击右上角分享
  244. */
  245. onShareAppMessage: function () {
  246. }
  247. })