login.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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" color="linear-gradient(to right, #F54319, #FF6D20)"
  7. @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. infoData: {},
  18. src: '../../static/first.png',
  19. userInfo: {},
  20. hasUserInfo: false,
  21. canIUseGetUserProfile: wx.getUserProfile ? true : false,
  22. }
  23. },
  24. methods: {
  25. getUserProfile() {
  26. wx.login({
  27. success: async (e) => {
  28. console.log('数据:', e)
  29. try {
  30. const result = await this.$request('post', '/auth/miniLogin', {
  31. tenantId: '000000',
  32. clientId: '8871d05eacc4406083d3bb0a085b6999',
  33. code: e.code
  34. })
  35. console.log('登录成功:', result);
  36. if (result && result.result !== false) {
  37. var token = result.data.accessToken;
  38. var haveOpenid = result.data.openId;
  39. var tenantId = result.data.tenantId
  40. uni.setStorageSync("token", token);
  41. wx.getUserProfile({
  42. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  43. success: (res) => {
  44. this.setData({
  45. userInfo: res.userInfo,
  46. hasUserInfo: true
  47. })
  48. }
  49. })
  50. console.log('userInfo', this.userInfo)
  51. uni.navigateTo({
  52. url: `/pages/diningList/diningList`
  53. });
  54. } else {
  55. // 处理错误情况
  56. }
  57. } catch (error) {
  58. // 处理请求失败的情况
  59. console.error('登录失败:', error);
  60. }
  61. }
  62. })
  63. // let that = this;
  64. // wx.login({
  65. // success: function(e) {
  66. // console.log('数据:', e)
  67. // uni.request({
  68. // url: 'http://36.137.224.81:8030/auth/miniLogin?tenantId=000000&code=' + e
  69. // .code,
  70. // header: {
  71. // "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8", //
  72. // // "Content-Type": "application/json;charset=UTF-8", //
  73. // },
  74. // method: 'post',
  75. // success: res => {
  76. // console.log('登录成功:', res)
  77. // uni.hideLoading()
  78. // var token = res.data.data.accessToken;
  79. // console.log('token:', token)
  80. // uni.setStorageSync("token", token);
  81. // uni.navigateTo({
  82. // url: `/pages/classify/classify`
  83. // })
  84. // // if (res.data.code == 200) {
  85. // // that.infoData = res.data.data
  86. // // }
  87. // },
  88. // fail: err => {
  89. // uni.hideLoading()
  90. // }
  91. // })
  92. // }
  93. // })
  94. }
  95. }
  96. }
  97. </script>
  98. <style lang="scss" scoped>
  99. .img {
  100. height: 375px;
  101. width: 100%;
  102. }
  103. .btn {
  104. display: flex;
  105. justify-content: center;
  106. align-items: center;
  107. margin-top: 20px;
  108. }
  109. </style> -->
  110. <template>
  111. <view>
  112. <image :src="src" class="img"></image>
  113. <view class="btn">
  114. <view style="width: 80%;">
  115. <u-button v-if="canIUseGetUserProfile" shape="circle"
  116. color="linear-gradient(to right, #F54319, #FF6D20)" @click="getUserProfile">选择餐车</u-button>
  117. </view>
  118. </view>
  119. </view>
  120. </template>
  121. <script>
  122. export default {
  123. name: 'MyLogin',
  124. data() {
  125. return {
  126. src: '../../static/first.png',
  127. canIUseGetUserProfile: false,
  128. }
  129. },
  130. onLoad() {
  131. // 检查微信版本是否支持 getUserProfile
  132. if (wx.getUserProfile) {
  133. this.canIUseGetUserProfile = true;
  134. }
  135. },
  136. methods: {
  137. getUserProfile() {
  138. wx.showModal({
  139. title: '温馨提示',
  140. content: '亲,授权微信登录后才能正常使用小程序功能',
  141. })
  142. wx.getUserProfile({
  143. desc: '获取用户信息',
  144. success: (res) => {
  145. console.log('res', res);
  146. // 调用微信登录接口
  147. wx.login({
  148. success: async (loginRes) => {
  149. console.log('数据:', loginRes)
  150. // 发送code到开发者服务器换取用户信息
  151. const result = await this.$request('post', '/auth/miniLogin', {
  152. tenantId: '000000',
  153. // clientId: 'e5cd7e4891bf95d1d19206ce24a7b32e',
  154. code: loginRes.code
  155. })
  156. // headers: {
  157. // isToken: false,
  158. // repeatSubmit: false
  159. // },
  160. console.log('登录成功:', result);
  161. if (result && result.data !== false) {
  162. // 保存token
  163. uni.setStorageSync("token", result.data.access_token);
  164. uni.setStorageSync("openId", result.data.open_id);
  165. //uni.setStorageSync("tenantId", result.data.tenantId);
  166. uni.setStorageSync("appUserId", result.data.app_user_id);
  167. // 跳转到下一个页面
  168. uni.navigateTo({
  169. url: `/pages/diningList/diningList`
  170. });
  171. } else {
  172. // 处理错误情况
  173. }
  174. // 更新用户信息
  175. const id = uni.getStorageSync('appUserId');
  176. console.log('id', id)
  177. uni.setStorageSync("userName", res.userInfo.nickName);
  178. const userInfo = {
  179. "userId": id,
  180. "nickName": res.userInfo.nickName,
  181. "profilePhoto": res.userInfo.avatarUrl
  182. }
  183. console.log('userInfo', userInfo)
  184. this.$request('post', '/system/user/editSysUser',
  185. userInfo, true)
  186. },
  187. fail: (error) => {
  188. console.error('登录失败:', error);
  189. }
  190. })
  191. },
  192. fail: (error) => {
  193. console.error('获取用户信息失败:', error);
  194. }
  195. })
  196. }
  197. }
  198. }
  199. </script>
  200. <style lang="scss" scoped>
  201. .img {
  202. height: 375px;
  203. width: 100%;
  204. }
  205. .btn {
  206. display: flex;
  207. justify-content: center;
  208. align-items: center;
  209. margin-top: 20px;
  210. }
  211. </style>