activity.js 6.9 KB

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