login.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. let that = this;
  54. wx.getUserProfile({
  55. desc: '获取用户信息',
  56. success: (res) => {
  57. console.log('res', res);
  58. // 调用微信登录接口
  59. wx.login({
  60. success: async (loginRes) => {
  61. console.log('数据:', loginRes)
  62. // 发送code到开发者服务器换取用户信息
  63. const result = await this.$request('post', '/system/appUser/miniLogin?tenantId=000000&code='+loginRes.code)
  64. console.log('登录成功:', result);
  65. if (result && result.data !== false) {
  66. // 保存token
  67. uni.setStorageSync("token", result.data.access_token);
  68. uni.setStorageSync("openId", result.data.open_id);
  69. //uni.setStorageSync("tenantId", result.data.tenantId);
  70. uni.setStorageSync("appUserId", result.data.app_user_id);
  71. let d = result.data;
  72. d.time = Date.now() / 1000;
  73. uni.setStorageSync('infoData', JSON.stringify(d))
  74. that.initdining()
  75. } else {
  76. // 处理错误情况
  77. }
  78. // 更新用户信息
  79. const id = uni.getStorageSync('appUserId');
  80. // console.log('id', id)
  81. uni.setStorageSync("userName", res.userInfo.nickName);
  82. const userInfo = {
  83. "userId": id,
  84. "nickName": res.userInfo.nickName,
  85. "profilePhoto": res.userInfo.avatarUrl
  86. }
  87. console.log('userInfo', userInfo)
  88. that.$request('post', '/system/user/editSysUser',
  89. userInfo, true)
  90. },
  91. fail: (error) => {
  92. console.error('登录失败:', error);
  93. }
  94. })
  95. },
  96. fail: (error) => {
  97. console.error('获取用户信息失败:', error);
  98. }
  99. })
  100. },
  101. //获取餐车信息
  102. async initdining() {
  103. const response = await this.$request('post', '/sale/diningCar/queryDiningCar', {
  104. "isAsc": "desc",
  105. "orderByColumn": "createTime"});
  106. if(response.code == 200) {
  107. let current;
  108. if(response.rows.length > 0){
  109. for(const item of response.rows){
  110. if(item.defaultState == '2'){
  111. current = item;
  112. break
  113. }
  114. }
  115. }
  116. if(current) {
  117. const carl = {
  118. id: current.id,
  119. customerPhone: current.customerPhone,
  120. name: current.name,
  121. openState: current.openState
  122. }
  123. uni.setStorageSync("carl", carl)
  124. // 跳转到下一个页面
  125. uni.switchTab({
  126. url:'/pages/home/home'
  127. })
  128. }else{
  129. uni.navigateTo({
  130. url: `/pages/diningList/diningList`
  131. });
  132. }
  133. }else{
  134. wx.showModal({
  135. title: '温馨提示',
  136. content: '系统异常!',
  137. })
  138. }
  139. }
  140. }
  141. }
  142. </script>
  143. <style lang="scss" scoped>
  144. .img {
  145. height: 375px;
  146. width: 100%;
  147. }
  148. .btn {
  149. display: flex;
  150. justify-content: center;
  151. align-items: center;
  152. margin-top: 20px;
  153. }
  154. </style>