hookehuyr

授权功能模拟

1 <!-- 1 <!--
2 * @Date: 2022-09-19 14:11:06 2 * @Date: 2022-09-19 14:11:06
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2022-10-13 23:00:28 4 + * @LastEditTime: 2022-10-18 16:09:34
5 * @FilePath: /swx/src/pages/auth/index.vue 5 * @FilePath: /swx/src/pages/auth/index.vue
6 * @Description: 文件描述 6 * @Description: 文件描述
7 --> 7 -->
8 <template> 8 <template>
9 - <div></div> 9 + <div>
10 + <button wx:if="{{canIUse}}" open-type="getUserInfo" @getuserinfo="bindGetUserInfo">授权登录</button>
11 + <view @tap="auth">授权登陆</view>
12 + </div>
10 </template> 13 </template>
11 14
12 <script setup> 15 <script setup>
13 import Taro from '@tarojs/taro' 16 import Taro from '@tarojs/taro'
14 import { ref } from "vue"; 17 import { ref } from "vue";
15 -import request from '../../utils/request'; 18 +import request from '@/utils/request';
19 +import { userInfo } from 'os';
16 20
17 </script> 21 </script>
18 22
...@@ -45,6 +49,85 @@ export default { ...@@ -45,6 +49,85 @@ export default {
45 .catch(err => { 49 .catch(err => {
46 console.error(err); 50 console.error(err);
47 }); 51 });
52 + },
53 + data () {
54 + return {
55 + canIUse: wx.canIUse('button.open-type.getUserInfo')
56 + }
57 + },
58 + onLoad: function() {
59 + // 查看是否授权
60 + wx.getSetting({
61 + success (res){
62 + if (res.authSetting['scope.userInfo']) {
63 + // 已经授权,可以直接调用 getUserInfo 获取头像昵称
64 + wx.getUserInfo({
65 + success: function(res) {
66 + console.warn(res.userInfo)
67 + }
68 + })
69 + }
70 + }
71 + })
72 + },
73 + methods: {
74 + bindGetUserInfo (e) {
75 + console.warn(e.detail.userInfo)
76 + },
77 + // auth () {
78 + // Taro.getSetting({
79 + // success: function (res) {
80 + // if (!res.authSetting['scope.userInfo']) {
81 + // console.warn(0);
82 + // Taro.authorize({
83 + // scope: 'scope.userInfo',
84 + // success: function () {
85 + // Taro.getUserInfo({
86 + // success: function(res) {
87 + // var userInfo = res.userInfo
88 + // console.warn(userInfo);
89 + // }
90 + // })
91 + // },
92 + // fail: function (error) {
93 + // console.error(error)
94 + // }
95 + // })
96 + // }
97 + // }
98 + // })
99 + // }
100 + auth () {
101 + // wx.getSetting({
102 + // success (res){
103 + // if (res.authSetting['scope.userInfo']) {
104 + // // 已经授权,可以直接调用 getUserInfo 获取头像昵称
105 + // wx.getUserInfo({
106 + // success: function(res) {
107 + // console.warn(res.userInfo)
108 + // }
109 + // })
110 + // }
111 + // }
112 + // })
113 + wx.getSetting({
114 + success(res) {
115 + if (!res.authSetting['scope.userInfo']) {
116 + wx.authorize({
117 + scope: 'scope.userInfo',
118 + success () {
119 + // 已经授权,可以直接调用 getUserInfo 获取头像昵称
120 + wx.getUserInfo({
121 + success: function(res) {
122 + console.warn(res.userInfo)
123 + }
124 + })
125 + }
126 + })
127 + }
128 + }
129 + })
130 + }
48 } 131 }
49 }; 132 };
50 </script> 133 </script>
......