activity.js 7.4 KB

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