activity.js 8.3 KB

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