index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // pages/home/index.js
  2. const app = getApp()
  3. import QRCode from '../../utils/weapp-qrcode'
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. height: app.globalData.height * 2 + 25,
  10. navbarData: {
  11. name: '节俭会'
  12. },
  13. // 背景图片
  14. background: '',
  15. // 热量占比
  16. heat: 100,
  17. heatColor: { '0%': '#E1FFFF', '25%': '#00FF7F', '50%': '#191970', '75%': '#ee0a24', '100%': '#ff0000', },
  18. // 微信运动
  19. step: 0,
  20. // 用餐卡
  21. // 餐类别
  22. thrTitle: '',
  23. // 站点信息
  24. tenant: '',
  25. // 餐列表
  26. thrList: [],
  27. },
  28. // 查看本周菜谱
  29. reserveBtn: function () {
  30. wx.redirectTo({
  31. url: '/pages/food/index',
  32. })
  33. },
  34. // 报餐:1,余菜打包:2,卡路里计算:3
  35. twoBtn: function (e) {
  36. let type = e.currentTarget.dataset.smile
  37. if (type == '1') {//报餐
  38. wx.switchTab({
  39. url: '/pages/reserve/index',
  40. })
  41. } else if (type == '2') {//余菜打包
  42. wx.showToast({
  43. title: '暂未开通',
  44. })
  45. } else {//卡路里计算
  46. wx.showToast({
  47. title: '暂未开通',
  48. })
  49. }
  50. },
  51. /**
  52. * 生命周期函数--监听页面加载
  53. */
  54. onLoad: function (options) {
  55. // 登录,获取用户openid,sessonkey
  56. app.toLogin().then(res => {
  57. this.getMealCard();
  58. });
  59. // 获取背景图片,站点信息
  60. wx.request({
  61. url: `${app.globalData.publicUrl}/api/st/system/tenant/getTenant/${app.globalData.tenant}`,
  62. method: "get",
  63. header: { 'x-tenant': app.globalData.tenant },
  64. data: {},
  65. success: res => {
  66. const { data } = res.data;
  67. this.setData({ background: `${app.globalData.fileUrl}` + data.img.home })
  68. this.setData({ tenant: data.name })
  69. },
  70. error: err => {
  71. wx.showToast({
  72. title: err.msg,
  73. icon: 'error'
  74. })
  75. }
  76. })
  77. // 获取微信运动信息
  78. wx.getWeRunData({
  79. success: (res) => {
  80. const app = getApp()
  81. const { encryptedData, iv } = res
  82. const session_key = app.globalData.wxInfo.session_key;
  83. const data = { encryptedData, iv, session_key }
  84. wx.request({
  85. url: `${app.globalData.publicUrl}/api/st/system/weixin/decrypt`,
  86. method: "POST",
  87. data,
  88. header: {
  89. 'x-tenant': app.globalData.tenant
  90. },
  91. success: (res) => {
  92. const { stepInfo } = res.data.data;
  93. if (stepInfo) this.setData({ step: stepInfo.step })
  94. },
  95. })
  96. }
  97. })
  98. },
  99. /**
  100. * 生命周期函数--监听页面初次渲染完成
  101. */
  102. onReady: function () {
  103. },
  104. /**
  105. * 生命周期函数--监听页面显示
  106. */
  107. onShow: function () {
  108. if (typeof this.getTabBar === 'function' &&
  109. this.getTabBar()) {
  110. this.getTabBar().setData({
  111. selected: 0
  112. })
  113. }
  114. },
  115. /**
  116. * 生命周期函数--监听页面隐藏
  117. */
  118. onHide: function () {
  119. },
  120. /**
  121. * 生命周期函数--监听页面卸载
  122. */
  123. onUnload: function () {
  124. },
  125. /**
  126. * 页面相关事件处理函数--监听用户下拉动作
  127. */
  128. onPullDownRefresh: function () {
  129. },
  130. /**
  131. * 页面上拉触底事件的处理函数
  132. */
  133. onReachBottom: function () {
  134. },
  135. /**
  136. * 用户点击右上角分享
  137. */
  138. onShareAppMessage: function () {
  139. },
  140. // 获取用餐卡
  141. getMealCard() {
  142. const openid = app.globalData.wxInfo.openid;
  143. if (!openid) return;
  144. wx.request({
  145. url: `${app.globalData.publicUrl}/api/st/dining/order/mealCard/${openid}`,
  146. method: "POST",
  147. header: {
  148. 'x-tenant': app.globalData.tenant
  149. },
  150. success: (res) => {
  151. if (res.data.errcode === 0) {
  152. const { data } = res.data
  153. if (!data) {
  154. this.setData({
  155. thrList: [],
  156. })
  157. return;
  158. }
  159. this.initQrCode(data.data._id)
  160. this.setData({
  161. thrList: data.data.list || [],
  162. thrTitle: data.type
  163. })
  164. }
  165. },
  166. error: (err) => {
  167. console.error(err.data.errmsg)
  168. }
  169. })
  170. },
  171. // 创建二维码
  172. initQrCode(id) {
  173. const url = `${app.globalData.publicUrl}/api/st/dining/order/useMeal/${id}?_tenant=${app.globalData.tenant}`;
  174. var qrcode = new QRCode(`myQrcode`, {
  175. text: url,
  176. width: 100,
  177. height: 100,
  178. padding: 3,
  179. colorDark: "#000000",
  180. colorLight: "#ffffff",
  181. correctLevel: QRCode.CorrectLevel.L,
  182. });
  183. }
  184. })