activity.js 8.2 KB

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