index.js 4.5 KB

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