activity.js 8.4 KB

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