index.js 4.9 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. //报餐
  39. wx.switchTab({
  40. url: '/pages/reserve/index',
  41. });
  42. } else if (type == '2') {
  43. //余菜打包
  44. wx.showToast({
  45. title: '暂未开通',
  46. });
  47. } else {
  48. //卡路里计算
  49. wx.showToast({
  50. title: '暂未开通',
  51. });
  52. }
  53. },
  54. /**
  55. * 生命周期函数--监听页面加载
  56. */
  57. onLoad: async function (options) {
  58. await this.getMealCard();
  59. // 获取背景图片,站点信息
  60. const res = await app.$get('/config');
  61. const logo = `${res.data.logo[0].url || ''}`;
  62. this.setData({ background: logo });
  63. // wx.request({
  64. // url: `${app.globalData.publicUrl}/api/st/system/tenant/getTenant/${app.globalData.tenant}`,
  65. // method: 'get',
  66. // data: {},
  67. // success: (res) => {
  68. // const { data } = res.data;
  69. // this.setData({ background: `${app.globalData.fileUrl}` + data.img.home });
  70. // this.setData({ tenant: data.name });
  71. // },
  72. // error: (err) => {
  73. // wx.showToast({
  74. // title: err.msg,
  75. // icon: 'error',
  76. // });
  77. // },
  78. // });
  79. // TODO:获取微信运动信息
  80. // wx.getWeRunData({
  81. // success: (res) => {
  82. // const app = getApp();
  83. // const { encryptedData, iv } = res;
  84. // const session_key = app.globalData.wxInfo.session_key;
  85. // const data = { encryptedData, iv, session_key };
  86. // wx.request({
  87. // url: `${app.globalData.publicUrl}/st/api/weixin/decrypt`,
  88. // method: 'POST',
  89. // data,
  90. // success: (res) => {
  91. // const setpInfo = res.data?.data;
  92. // if (stepInfo) this.setData({ step: stepInfo.step });
  93. // },
  94. // });
  95. // },
  96. // });
  97. },
  98. /**
  99. * 生命周期函数--监听页面初次渲染完成
  100. */
  101. onReady: function () {},
  102. /**
  103. * 生命周期函数--监听页面显示
  104. */
  105. onShow: function () {
  106. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  107. this.getTabBar().setData({
  108. selected: 0,
  109. });
  110. }
  111. },
  112. /**
  113. * 生命周期函数--监听页面隐藏
  114. */
  115. onHide: function () {},
  116. /**
  117. * 生命周期函数--监听页面卸载
  118. */
  119. onUnload: function () {},
  120. /**
  121. * 页面相关事件处理函数--监听用户下拉动作
  122. */
  123. onPullDownRefresh: function () {},
  124. /**
  125. * 页面上拉触底事件的处理函数
  126. */
  127. onReachBottom: function () {},
  128. /**
  129. * 用户点击右上角分享
  130. */
  131. onShareAppMessage: function () {},
  132. // 获取用餐卡
  133. async getMealCard() {
  134. if(!app.globalData.wxInfo.openid) await app.toLogin();
  135. const openid = app.globalData.wxInfo.openid;
  136. if (!openid) return;
  137. const res = await app.$post(`/order/mealCard/${openid}`);
  138. if (res.data.errcode === 0) {
  139. const { data } = res.data;
  140. if (!data) {
  141. this.setData({
  142. thrList: [],
  143. });
  144. return;
  145. }
  146. this.initQrCode(data.data._id);
  147. this.setData({
  148. thrList: data.data.list || [],
  149. thrTitle: data.type,
  150. });
  151. }
  152. // wx.request({
  153. // url: `${app.globalData.publicUrl}/api/st/dining/order/mealCard/${openid}`,
  154. // method: 'POST',
  155. // success: (res) => {
  156. // if (res.data.errcode === 0) {
  157. // const { data } = res.data;
  158. // if (!data) {
  159. // this.setData({
  160. // thrList: [],
  161. // });
  162. // return;
  163. // }
  164. // this.initQrCode(data.data._id);
  165. // this.setData({
  166. // thrList: data.data.list || [],
  167. // thrTitle: data.type,
  168. // });
  169. // }
  170. // },
  171. // error: (err) => {
  172. // console.error(err.data.errmsg);
  173. // },
  174. // });
  175. },
  176. // 创建二维码
  177. initQrCode(id) {
  178. const url = `${app.globalData.publicUrl}/st/api/order/useMeal/${id}`;
  179. // const url = `${app.globalData.publicUrl}/api/st/dining/order/useMeal/${id}`;
  180. var qrcode = new QRCode(`myQrcode`, {
  181. text: url,
  182. width: 100,
  183. height: 100,
  184. padding: 3,
  185. colorDark: '#000000',
  186. colorLight: '#ffffff',
  187. correctLevel: QRCode.CorrectLevel.L,
  188. });
  189. },
  190. });