index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. const app = getApp()
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. frameStyle: { useTop: true, name: '系统首页', leftArrow: false, useBar: false },
  8. // 主体高度
  9. infoHeight: '',
  10. },
  11. toLogin: function (e) {
  12. let value = e.currentTarget.dataset.type;
  13. wx.redirectTo({ url: `/pages/${value}/index`, })
  14. },
  15. /**
  16. * 生命周期函数--监听页面加载
  17. */
  18. onLoad: function (options) {
  19. // 监听用户是否登录
  20. // this.watchLogin();
  21. // 计算高度
  22. this.searchHeight();
  23. },
  24. // 监听用户是否登录
  25. watchLogin: function () {
  26. // 获取openid
  27. wx.login({
  28. success: res => {
  29. const { code: js_code } = res
  30. wx.request({
  31. url: `${app.globalData.publicUrl}/api/hc/wx/openid?js_code=` + js_code,
  32. method: "get",
  33. success: res => {
  34. const { errcode, openid } = res.data;
  35. if (errcode === 0) app.globalData.wxInfo = { openid };
  36. wx.getStorage({
  37. key: 'user',
  38. success: res => {
  39. // 已经登录,去home页面
  40. if (res.data) wx.redirectTo({ url: '/pages/home/index', })
  41. },
  42. fail: res => {
  43. // 没有登录,使用openid登录,登录成功去home页面,登录失败去login页面
  44. wx.request({
  45. url: app.globalData.publicUrl + `/api/hc/user/login`,
  46. method: "post",
  47. data: { openid: openid },
  48. header: {},
  49. success: (res) => {
  50. console.log(res);
  51. if (res.data.errcode == '0') {
  52. // 登录成功,去home页面
  53. wx.setStorage({ key: "user", data: res.data.data });
  54. app.globalData.userInfo = res.data.data;
  55. wx.navigateTo({ url: `/pages/home/index` })
  56. } else {
  57. // openid登录没有用户,去登录
  58. wx.navigateTo({ url: `/pages/login/index` })
  59. }
  60. },
  61. })
  62. }
  63. })
  64. },
  65. error: err => { }
  66. })
  67. }
  68. })
  69. },
  70. // 计算高度
  71. searchHeight: function () {
  72. let frameStyle = this.data.frameStyle;
  73. let client = app.globalData.client;
  74. // 减去状态栏
  75. let infoHeight = client.windowHeight - (client.statusBarHeight + client.getMenu.height + (client.getMenu.top - client.statusBarHeight) * 2);
  76. // 是否减去底部菜单
  77. if (frameStyle.useBar) infoHeight = infoHeight - 50;
  78. if (infoHeight) this.setData({ infoHeight: infoHeight })
  79. },
  80. /**
  81. * 生命周期函数--监听页面初次渲染完成
  82. */
  83. onReady: function () {
  84. },
  85. /**
  86. * 生命周期函数--监听页面显示
  87. */
  88. onShow: function () {
  89. },
  90. /**
  91. * 生命周期函数--监听页面隐藏
  92. */
  93. onHide: function () {
  94. },
  95. /**
  96. * 生命周期函数--监听页面卸载
  97. */
  98. onUnload: function () {
  99. },
  100. /**
  101. * 页面相关事件处理函数--监听用户下拉动作
  102. */
  103. onPullDownRefresh: function () {
  104. },
  105. /**
  106. * 页面上拉触底事件的处理函数
  107. */
  108. onReachBottom: function () {
  109. },
  110. /**
  111. * 用户点击右上角分享
  112. */
  113. onShareAppMessage: function () {
  114. }
  115. })