activity.js 7.8 KB

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