login.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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('一键授权登录按钮被点击', e);
  53. wx.showLoading({
  54. title: '正在登录中...',
  55. })
  56. if (e.type == "getphonenumber") {
  57. console.log("添加手机信息",this.data.userInfo)
  58. var user = {
  59. UserName: this.data.userInfo.UserName,
  60. UserAvatarUrl: this.data.userInfo.UserAvatarUrl,
  61. UserPhoneNumber: '',
  62. Address: '',
  63. encryptedData: e.detail.encryptedData,
  64. iv: e.detail.iv
  65. };
  66. this.setData({
  67. userInfo: user
  68. })
  69. this.toLogin()
  70. }
  71. if (e.type == "tap") {
  72. console.log("添加用户信息",this.data.userInfo)
  73. // 获取用户信息授权
  74. const that = this;
  75. wx.getUserProfile({
  76. desc: '用于完善用户资料', // 声明获取用户信息的目的
  77. success(res) {
  78. console.log('用户信息:', res.userInfo);
  79. var user = {
  80. UserName: res.userInfo.nickName,
  81. UserAvatarUrl: res.userInfo.avatarUrl,
  82. UserPhoneNumber: '',
  83. Address: '',
  84. encryptedData: that.data.userInfo.encryptedData,
  85. iv: that.data.userInfo.iv
  86. };
  87. that.setData({
  88. userInfo: user
  89. })
  90. that.toLogin()
  91. // 将用户信息发送到服务器
  92. // 例如:wx.request({ url: 'https://example.com/userInfo', data: res.userInfo })
  93. },
  94. fail(err) {
  95. console.error('获取用户信息失败:', err);
  96. wx.hideLoading()
  97. },
  98. });
  99. }
  100. },
  101. toLogin() {
  102. if(!this.cheackUserInfo()) {
  103. console.log("userInfo 信息不全",this.data.userInfo)
  104. wx.hideLoading()
  105. return
  106. }
  107. // 调用微信登录接口
  108. var that = this;
  109. wx.login({
  110. success(res) {
  111. if (res.code) {
  112. console.log('登录成功,code:', res.code);
  113. api.request_Wechatlogin(res.code)
  114. .then(res => {
  115. console.log("获取wid", res.data.data)
  116. wx.setStorageSync('WachatID', res.data.data)
  117. console.log("传递的userInfo", that.data.userInfo)
  118. return api.request_AddMiniprogramUser(that.data.userInfo)
  119. }).then(res => {
  120. console.log("添加user", res)
  121. wx.hideLoading()
  122. wx.redirectTo({
  123. url: '../scan/scan',
  124. })
  125. }).catch(err => {
  126. console.log("报错", res)
  127. wx.hideLoading()
  128. });
  129. } else {
  130. wx.hideLoading()
  131. console.log('登录失败:', res.errMsg);
  132. }
  133. },
  134. fail(err) {
  135. wx.hideLoading()
  136. console.error('登录接口调用失败:', err);
  137. },
  138. });
  139. },
  140. cheackUserInfo(){
  141. var userInfo = this.data.userInfo;
  142. return userInfo.UserAvatarUrl != '' && userInfo.UserAvatarUrl != undefined && userInfo.UserName != '' && userInfo.UserName != undefined && userInfo.encryptedData != '' && userInfo.encryptedData != undefined && userInfo.iv != '' && userInfo.iv != undefined
  143. }
  144. })