activity.js 7.4 KB

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