login.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <view>
  3. <image :src="src" class="img"></image>
  4. <view class="btn">
  5. <view style="width: 80%;">
  6. <u-button v-if="canIUseGetUserProfile" shape="circle"
  7. color="linear-gradient(to right, #F54319, #FF6D20)" @click="getUserProfile">选择餐车</u-button>
  8. </view>
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. name: 'MyLogin',
  15. data() {
  16. return {
  17. src: '../../static/first.png',
  18. canIUseGetUserProfile: false,
  19. }
  20. },
  21. onLoad(e) {
  22. // 检查微信版本是否支持 getUserProfile
  23. if (wx.getUserProfile) {
  24. this.canIUseGetUserProfile = true;
  25. }
  26. uni.setStorageSync('dinnerId', '1806258588536201217')
  27. if(e.id){
  28. uni.setStorageSync('dinnerId', e.id)
  29. }
  30. let infoData = uni.getStorageSync('infoData');
  31. if(infoData){
  32. try{
  33. infoData = JSON.parse(infoData)
  34. // console.log('用户数据',infoData)
  35. if (infoData.expire_in && Date.now() / 1000 - infoData.time > infoData.expire_in) {
  36. uni.removeStorageSync('infoData')
  37. return null
  38. }
  39. if(infoData && infoData.access_token){
  40. uni.switchTab({
  41. url:'/pages/home/home'
  42. })
  43. }
  44. }catch(e){}
  45. }
  46. },
  47. methods: {
  48. getUserProfile() {
  49. wx.showModal({
  50. title: '温馨提示',
  51. content: '亲,授权微信登录后才能正常使用小程序功能',
  52. })
  53. wx.getUserProfile({
  54. desc: '获取用户信息',
  55. success: (res) => {
  56. console.log('res', res);
  57. // 调用微信登录接口
  58. wx.login({
  59. success: async (loginRes) => {
  60. console.log('数据:', loginRes)
  61. // 发送code到开发者服务器换取用户信息
  62. const result = await this.$request('post', '/auth/miniLogin', {
  63. tenantId: '000000',
  64. code: loginRes.code
  65. })
  66. console.log('登录成功:', result);
  67. if (result && result.data !== false) {
  68. // 保存token
  69. uni.setStorageSync("token", result.data.access_token);
  70. uni.setStorageSync("openId", result.data.open_id);
  71. //uni.setStorageSync("tenantId", result.data.tenantId);
  72. uni.setStorageSync("appUserId", result.data.app_user_id);
  73. let d = result.data;
  74. d.time = Date.now() / 1000;
  75. uni.setStorageSync('infoData', JSON.stringify(d))
  76. // 跳转到下一个页面
  77. uni.switchTab({
  78. url:'/pages/home/home'
  79. })
  80. } else {
  81. // 处理错误情况
  82. }
  83. // 更新用户信息
  84. const id = uni.getStorageSync('appUserId');
  85. console.log('id', id)
  86. uni.setStorageSync("userName", res.userInfo.nickName);
  87. const userInfo = {
  88. "userId": id,
  89. "nickName": res.userInfo.nickName,
  90. "profilePhoto": res.userInfo.avatarUrl
  91. }
  92. console.log('userInfo', userInfo)
  93. this.$request('post', '/system/user/editSysUser',
  94. userInfo, true)
  95. },
  96. fail: (error) => {
  97. console.error('登录失败:', error);
  98. }
  99. })
  100. },
  101. fail: (error) => {
  102. console.error('获取用户信息失败:', error);
  103. }
  104. })
  105. }
  106. }
  107. }
  108. </script>
  109. <style lang="scss" scoped>
  110. .img {
  111. height: 375px;
  112. width: 100%;
  113. }
  114. .btn {
  115. display: flex;
  116. justify-content: center;
  117. align-items: center;
  118. margin-top: 20px;
  119. }
  120. </style>