login.vue 2.6 KB

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