index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. console.log(res)
  92. const setpInfo = 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. onShow: function () {
  107. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  108. this.getTabBar().setData({
  109. selected: 0,
  110. });
  111. }
  112. },
  113. /**
  114. * 生命周期函数--监听页面隐藏
  115. */
  116. onHide: function () {},
  117. /**
  118. * 生命周期函数--监听页面卸载
  119. */
  120. onUnload: function () {},
  121. /**
  122. * 页面相关事件处理函数--监听用户下拉动作
  123. */
  124. onPullDownRefresh: function () {},
  125. /**
  126. * 页面上拉触底事件的处理函数
  127. */
  128. onReachBottom: function () {},
  129. /**
  130. * 用户点击右上角分享
  131. */
  132. onShareAppMessage: function () {},
  133. // 获取用餐卡
  134. async getMealCard() {
  135. if(!app.globalData.wxInfo.openid) await app.toLogin();
  136. const openid = app.globalData.wxInfo.openid;
  137. if (!openid) return;
  138. const res = await app.$post(`/order/mealCard/${openid}`);
  139. if (res.data.errcode === 0) {
  140. const { data } = res.data;
  141. if (!data) {
  142. this.setData({
  143. thrList: [],
  144. });
  145. return;
  146. }
  147. this.initQrCode(data.data._id);
  148. this.setData({
  149. thrList: data.data.list || [],
  150. thrTitle: data.type,
  151. });
  152. }
  153. // wx.request({
  154. // url: `${app.globalData.publicUrl}/api/st/dining/order/mealCard/${openid}`,
  155. // method: 'POST',
  156. // success: (res) => {
  157. // if (res.data.errcode === 0) {
  158. // const { data } = res.data;
  159. // if (!data) {
  160. // this.setData({
  161. // thrList: [],
  162. // });
  163. // return;
  164. // }
  165. // this.initQrCode(data.data._id);
  166. // this.setData({
  167. // thrList: data.data.list || [],
  168. // thrTitle: data.type,
  169. // });
  170. // }
  171. // },
  172. // error: (err) => {
  173. // console.error(err.data.errmsg);
  174. // },
  175. // });
  176. },
  177. // 创建二维码
  178. initQrCode(id) {
  179. const url = `${app.globalData.publicUrl}/st/api/order/useMeal/${id}`;
  180. // const url = `${app.globalData.publicUrl}/api/st/dining/order/useMeal/${id}`;
  181. var qrcode = new QRCode(`myQrcode`, {
  182. text: url,
  183. width: 100,
  184. height: 100,
  185. padding: 3,
  186. colorDark: '#000000',
  187. colorLight: '#ffffff',
  188. correctLevel: QRCode.CorrectLevel.L,
  189. });
  190. },
  191. });