login.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //const app = require('../../js/api');
  2. import api from '../../js/api'
  3. // pages/login/login.js
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. userInfo: {}
  10. },
  11. /**
  12. * 生命周期函数--监听页面加载
  13. */
  14. onLoad(options) {
  15. },
  16. /**
  17. * 生命周期函数--监听页面初次渲染完成
  18. */
  19. onReady() {
  20. },
  21. /**
  22. * 生命周期函数--监听页面显示
  23. */
  24. onShow() {
  25. },
  26. /**
  27. * 生命周期函数--监听页面隐藏
  28. */
  29. onHide() {
  30. },
  31. /**
  32. * 生命周期函数--监听页面卸载
  33. */
  34. onUnload() {
  35. },
  36. /**
  37. * 页面相关事件处理函数--监听用户下拉动作
  38. */
  39. onPullDownRefresh() {
  40. },
  41. /**
  42. * 页面上拉触底事件的处理函数
  43. */
  44. onReachBottom() {
  45. },
  46. /**
  47. * 用户点击右上角分享
  48. */
  49. onShareAppMessage() {
  50. },
  51. handleAuthLogin(e) {
  52. console.log('一键授权登录按钮被点击');
  53. wx.showLoading({
  54. title: '正在登录中...',
  55. })
  56. // if(e.detail.userInfo) {
  57. // wx.getUserProfile({
  58. // desc: '用于完善用户信息',
  59. // success:(res) => {
  60. // console.log(res.userInfo)
  61. // },
  62. // fail:(err) => {
  63. // console.log("获取用户信息失败",err)
  64. // }
  65. // })
  66. // }
  67. // 获取用户信息授权
  68. const that = this;
  69. wx.getUserProfile({
  70. desc: '用于完善用户资料', // 声明获取用户信息的目的
  71. success(res) {
  72. console.log('用户信息:', res.userInfo);
  73. var user = {
  74. UserName: res.userInfo.nickName,
  75. UserAvatarUrl: res.userInfo.avatarUrl,
  76. UserPhoneNumber: '',
  77. Address: ''
  78. };
  79. that.setData({
  80. userInfo:user
  81. })
  82. that.toLogin()
  83. // 将用户信息发送到服务器
  84. // 例如:wx.request({ url: 'https://example.com/userInfo', data: res.userInfo })
  85. },
  86. fail(err) {
  87. console.error('获取用户信息失败:', err);
  88. wx.hideLoading()
  89. },
  90. });
  91. },
  92. toLogin(){
  93. // 调用微信登录接口
  94. var that = this;
  95. wx.login({
  96. success(res) {
  97. if (res.code) {
  98. console.log('登录成功,code:', res.code);
  99. api.request_Wechatlogin(res.code)
  100. .then(res => {
  101. console.log("获取wid",res.data.data)
  102. wx.setStorageSync('WachatID', res.data.data)
  103. return api.request_AddMiniprogramUser(that.data.userInfo)
  104. }).then(res => {
  105. console.log("添加user",res)
  106. wx.hideLoading()
  107. wx.redirectTo({
  108. url: '../scan/scan',
  109. })
  110. }).catch(err => {
  111. console.log("报错",res)
  112. wx.hideLoading()
  113. });
  114. } else {
  115. wx.hideLoading()
  116. console.log('登录失败:', res.errMsg);
  117. }
  118. },
  119. fail(err) {
  120. wx.hideLoading()
  121. console.error('登录接口调用失败:', err);
  122. },
  123. });
  124. }
  125. })