login.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <view>
  3. <view class="login-main">
  4. <image class="user" src="/static/images/userimg.png"></image>
  5. <view class="name">您好 </view>
  6. <view class="tip">为了您的最佳体验,请登录</view>
  7. </view>
  8. <button class="lgin-btn" open-type="getUserInfo" lang="zh_CN" withCredentials="true" @getuserinfo="mpWxLogin">
  9. <image src="/static/images/wxa.png"></image>
  10. <view class="text">微信一键登录</view>
  11. </button>
  12. </view>
  13. </template>
  14. <script>
  15. import {
  16. request
  17. } from '@/utils/request'
  18. import { USER_INFO } from '../../constant'
  19. export default {
  20. name: 'Login',
  21. data() {
  22. return {
  23. }
  24. },
  25. methods: {
  26. async mpWxLogin(userInfoData) {
  27. console.log('登陆')
  28. await this.mpWxGetUserInfo(userInfoData)
  29. this.$showToast('登陆成功')
  30. uni.navigateTo({
  31. url: '/pages/index'
  32. })
  33. },
  34. // 获取用户信息
  35. async mpWxGetUserInfo(userInfoData) {
  36. return new Promise(async (resolve, reject) => {
  37. if (!userInfoData.detail.userInfo) {
  38. reject(new Error('未拿到用户信息'))
  39. }
  40. try {
  41. uni.showLoading({
  42. mask: true,
  43. title: '请稍候...'
  44. })
  45. const [loginErr, loginData] = await uni.login({ provider: 'weixin' })
  46. const [err, userData] = await uni.getUserInfo()
  47. const res = await request('uni-card', 'loginByWx', {
  48. code: loginData.code,
  49. encryptedData: userData.encryptedData,
  50. iv: userData.iv
  51. }, {
  52. showLoading: true
  53. })
  54. console.log('res', res)
  55. uni.hideLoading()
  56. resolve(res)
  57. } catch (e) {
  58. uni.hideLoading()
  59. reject(e)
  60. }
  61. }).then(async (userInfo) => {
  62. this.$store.dispatch('setUserData', userInfo)
  63. await uni.setStorageSync(USER_INFO, userInfo)
  64. return userInfo
  65. })
  66. }
  67. }
  68. }
  69. </script>
  70. <style>
  71. page {
  72. background: #fff;
  73. }
  74. </style>
  75. <style scoped>
  76. .login-main {
  77. width: 100%;
  78. padding-top: 200rpx;
  79. }
  80. .login-main .user {
  81. width: 180rpx;
  82. height: 180rpx;
  83. margin: 0 auto;
  84. display: block;
  85. }
  86. .login-main .name {
  87. text-align: center;
  88. font-size: 40rpx;
  89. }
  90. .login-main .name text {
  91. padding: 0 0 0 20rpx;
  92. }
  93. .login-main .tip {
  94. color: #999;
  95. padding: .9em 0;
  96. text-align: center;
  97. padding-bottom: 50rpx;
  98. }
  99. .lgin-btn {
  100. width: 80%;
  101. margin: 0 auto;
  102. display: flex;
  103. align-items: center;
  104. justify-content: center;
  105. background: #DBA871;
  106. border-radius: 10rpx;
  107. padding: 15rpx 0;
  108. }
  109. .lgin-btn image {
  110. width: 50rpx;
  111. height: 50rpx;
  112. display: block;
  113. }
  114. .lgin-btn .text {
  115. color: #fff;
  116. font-size: 32rpx;
  117. padding-left: 10rpx;
  118. }
  119. .lgin-tip {
  120. color: #DBA871;
  121. text-align: center;
  122. padding: 20rpx 0;
  123. font-size: 28rpx;
  124. }
  125. </style>