login.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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', '/system/appUser/miniLogin?tenantId=000000&code='+loginRes.code)
  63. console.log('登录成功:', result);
  64. if (result && result.data !== false) {
  65. // 保存token
  66. uni.setStorageSync("token", result.data.access_token);
  67. uni.setStorageSync("openId", result.data.open_id);
  68. //uni.setStorageSync("tenantId", result.data.tenantId);
  69. uni.setStorageSync("appUserId", result.data.app_user_id);
  70. let d = result.data;
  71. d.time = Date.now() / 1000;
  72. uni.setStorageSync('infoData', JSON.stringify(d))
  73. // 跳转到下一个页面
  74. uni.switchTab({
  75. url:'/pages/home/home'
  76. })
  77. } else {
  78. // 处理错误情况
  79. }
  80. // 更新用户信息
  81. const id = uni.getStorageSync('appUserId');
  82. // console.log('id', id)
  83. uni.setStorageSync("userName", res.userInfo.nickName);
  84. const userInfo = {
  85. "userId": id,
  86. "nickName": res.userInfo.nickName,
  87. "profilePhoto": res.userInfo.avatarUrl
  88. }
  89. console.log('userInfo', userInfo)
  90. this.$request('post', '/system/user/editSysUser',
  91. userInfo, true)
  92. },
  93. fail: (error) => {
  94. console.error('登录失败:', error);
  95. }
  96. })
  97. },
  98. fail: (error) => {
  99. console.error('获取用户信息失败:', error);
  100. }
  101. })
  102. }
  103. }
  104. }
  105. </script>
  106. <style lang="scss" scoped>
  107. .img {
  108. height: 375px;
  109. width: 100%;
  110. }
  111. .btn {
  112. display: flex;
  113. justify-content: center;
  114. align-items: center;
  115. margin-top: 20px;
  116. }
  117. </style>