login.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import {
  2. post,
  3. } from '../../utils/http';
  4. import {
  5. objToParam,
  6. } from '../../utils/util';
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. userInfo: null,
  13. prePage: 0
  14. },
  15. /**
  16. * 生命周期函数--监听页面加载
  17. */
  18. onLoad: function (options) {
  19. console.log(options)
  20. if(options.prePage) {
  21. this.setData({
  22. prePage: options.prePage
  23. })
  24. }
  25. },
  26. /**
  27. * 生命周期函数--监听页面初次渲染完成
  28. */
  29. onReady: function () {
  30. },
  31. /**
  32. * 生命周期函数--监听页面显示
  33. */
  34. onShow: function () {
  35. },
  36. /**
  37. * 生命周期函数--监听页面隐藏
  38. */
  39. onHide: function () {
  40. },
  41. /**
  42. * 生命周期函数--监听页面卸载
  43. */
  44. onUnload: function () {
  45. },
  46. /**
  47. * 页面相关事件处理函数--监听用户下拉动作
  48. */
  49. onPullDownRefresh: function () {
  50. },
  51. /**
  52. * 页面上拉触底事件的处理函数
  53. */
  54. onReachBottom: function () {
  55. },
  56. /**
  57. * 用户点击右上角分享
  58. */
  59. onShareAppMessage: function () {
  60. },
  61. /**
  62. * 获取用户信息
  63. */
  64. getUserInfo(e) {
  65. wx.getUserProfile({
  66. desc: 'desc',
  67. success: (res) => {
  68. this.setData({
  69. userInfo: JSON.parse(res.rawData)
  70. })
  71. console.log(JSON.parse(res.rawData))
  72. }
  73. })
  74. },
  75. /**
  76. * 获取用户手机号
  77. */
  78. getPhoneNumber(e) {
  79. let that = this;
  80. wx.login({
  81. success(res) {
  82. post('api/login', {
  83. js_code: res.code,
  84. avatar_url: that.data.userInfo.avatarUrl,
  85. nickname: that.data.userInfo.nickName,
  86. iv: e.detail.iv,
  87. encrypted_data: e.detail.encryptedData,
  88. wx_data: JSON.stringify(that.data.userInfo)
  89. }, (res) => {
  90. console.log(res)
  91. if(res.data.status == 0) {
  92. wx.showToast({
  93. title: '未绑定角色,请联系管理员处理!',
  94. icon: 'none'
  95. })
  96. return;
  97. }
  98. wx.setStorageSync('token', res.data.token);
  99. wx.setStorageSync('userInfo', res.data)
  100. wx.showToast({
  101. title: res.msg,
  102. icon: 'none'
  103. })
  104. setTimeout(() => {
  105. if(that.data.prePage == 1) {
  106. wx.navigateBack()
  107. } else {
  108. let sceneData = getApp().globalData.sceneData
  109. wx.reLaunch({
  110. url: `/${sceneData.path}?${objToParam(sceneData.query)}`,
  111. })
  112. // wx.switchTab({
  113. // url: '/pages/home/home',
  114. // })
  115. }
  116. }, 1500);
  117. })
  118. }
  119. })
  120. },
  121. /**
  122. * 跳转去用户协议
  123. */
  124. goToAgree(e) {
  125. wx.navigateTo({
  126. url: '/pages/agreement/agreement?agree=' + e.currentTarget.dataset.agree,
  127. })
  128. }
  129. })