login.vue 2.5 KB

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