index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // pages/login_page/index.js
  2. const {
  3. request,
  4. serverName
  5. } = require('../../utils/services');
  6. // const WxParse = require('../../../common/component/wxParse/wxParse.js');
  7. const {
  8. Toast,
  9. } = require('../../utils/util.js');
  10. const app = getApp();
  11. Page({
  12. /**
  13. * 页面的初始数据
  14. */
  15. data: {
  16. login_img: "../../imgs/testImg/needLogin.jpg",
  17. login_logo: "../../imgs/testImg/logo.png"
  18. },
  19. _getUserInfoToLogin: function(e) {
  20. console.log(e);
  21. Toast.showToast2('loading');
  22. if (e.detail.encryptedData) {
  23. // 可以将 res 发送给后台解码出 unionId
  24. // app.globalData.userInfo = res.userInfo;
  25. // app.globalData.encryptedData = res.encryptedData;
  26. // app.globalData.iv = res.iv;
  27. let {
  28. encryptedData,
  29. iv,
  30. userInfo
  31. } = e.detail;
  32. app.globalData.userInfo = userInfo;
  33. wx.setStorageSync("userInfo", userInfo)
  34. wx.login({
  35. success: res => {
  36. let {
  37. code
  38. } = res;
  39. if (code) {
  40. wx.request({
  41. url: serverName + '/wx/api/user/getLoginSessionKey',
  42. data: {
  43. encryptedData,
  44. iv,
  45. code,
  46. },
  47. header: {
  48. 'content-type': 'application/x-www-form-urlencoded'
  49. },
  50. method: 'post',
  51. // dataType: 'json',
  52. // responseType: 'text',
  53. success: res => {
  54. if (res.data.code > -1) {
  55. let {
  56. loginSessionKey
  57. } = res.data.data;
  58. wx.setStorageSync('token', loginSessionKey)
  59. Toast.showToast('tip', '登录成功', success => {
  60. wx.navigateBack({
  61. })
  62. });
  63. } else {
  64. Toast.showToast('warn', '登录失败,请重试');
  65. }
  66. },
  67. fail: res => {
  68. Toast.showToast('warn', '登录失败,请重试');
  69. },
  70. complete: res => {
  71. Toast.hideLoading();
  72. },
  73. })
  74. }
  75. }
  76. })
  77. }
  78. },
  79. /**
  80. * 生命周期函数--监听页面加载
  81. */
  82. onLoad: function(options) {
  83. },
  84. /**
  85. * 生命周期函数--监听页面初次渲染完成
  86. */
  87. onReady: function() {
  88. },
  89. /**
  90. * 生命周期函数--监听页面显示
  91. */
  92. onShow: function() {
  93. },
  94. /**
  95. * 生命周期函数--监听页面隐藏
  96. */
  97. onHide: function() {
  98. },
  99. /**
  100. * 生命周期函数--监听页面卸载
  101. */
  102. onUnload: function() {
  103. },
  104. /**
  105. * 页面相关事件处理函数--监听用户下拉动作
  106. */
  107. /**
  108. * 页面上拉触底事件的处理函数
  109. */
  110. onReachBottom: function() {
  111. },
  112. /**
  113. * 用户点击右上角分享
  114. */
  115. onShareAppMessage: function() {
  116. }
  117. })