123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <template>
- <view>
- <view class="login-main">
- <image class="user" src="/static/images/userimg.png"></image>
- <view class="name">您好 </view>
- <view class="tip">为了您的最佳体验,请登录</view>
- </view>
- <button class="lgin-btn" open-type="getUserInfo" lang="zh_CN" withCredentials="true" @getuserinfo="mpWxLogin">
- <image src="/static/images/wxa.png"></image>
- <view class="text">微信一键登录</view>
- </button>
- </view>
- </template>
- <script>
- import {
- request
- } from '@/utils/request'
- import { USER_INFO } from '../../constant'
- export default {
- name: 'Login',
- data() {
- return {
- }
- },
- methods: {
- async mpWxLogin(userInfoData) {
- console.log('登陆')
- await this.mpWxGetUserInfo(userInfoData)
- this.$showToast('登陆成功')
- uni.navigateTo({
- url: '/pages/index'
- })
- },
- // 获取用户信息
- async mpWxGetUserInfo(userInfoData) {
- return new Promise(async (resolve, reject) => {
- if (!userInfoData.detail.userInfo) {
- reject(new Error('未拿到用户信息'))
- }
- try {
- uni.showLoading({
- mask: true,
- title: '请稍候...'
- })
- const [loginErr, loginData] = await uni.login({ provider: 'weixin' })
- const [err, userData] = await uni.getUserInfo()
- const res = await request('uni-card', 'loginByWx', {
- code: loginData.code,
- encryptedData: userData.encryptedData,
- iv: userData.iv
- }, {
- showLoading: true
- })
- console.log('res', res)
- uni.hideLoading()
- resolve(res)
- } catch (e) {
- uni.hideLoading()
- reject(e)
- }
- }).then(async (userInfo) => {
- this.$store.dispatch('setUserData', userInfo)
- await uni.setStorageSync(USER_INFO, userInfo)
- return userInfo
- })
- }
- }
- }
- </script>
- <style>
- page {
- background: #fff;
- }
- </style>
- <style scoped>
- .login-main {
- width: 100%;
- padding-top: 200rpx;
- }
- .login-main .user {
- width: 180rpx;
- height: 180rpx;
- margin: 0 auto;
- display: block;
- }
- .login-main .name {
- text-align: center;
- font-size: 40rpx;
- }
- .login-main .name text {
- padding: 0 0 0 20rpx;
- }
- .login-main .tip {
- color: #999;
- padding: .9em 0;
- text-align: center;
- padding-bottom: 50rpx;
- }
- .lgin-btn {
- width: 80%;
- margin: 0 auto;
- display: flex;
- align-items: center;
- justify-content: center;
- background: #DBA871;
- border-radius: 10rpx;
- padding: 15rpx 0;
- }
- .lgin-btn image {
- width: 50rpx;
- height: 50rpx;
- display: block;
- }
- .lgin-btn .text {
- color: #fff;
- font-size: 32rpx;
- padding-left: 10rpx;
- }
- .lgin-tip {
- color: #DBA871;
- text-align: center;
- padding: 20rpx 0;
- font-size: 28rpx;
- }
- </style>
|