Skip to content

Commit 97ebff8

Browse files
committed
增加首页用户授权判断
1 parent cfe6057 commit 97ebff8

5 files changed

Lines changed: 144 additions & 3 deletions

File tree

app.wxss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777

7878
.common-list .list-item {
7979
position: relative;
80-
border-bottom: 1px solid #eee;
80+
border-bottom: 1px dashed #eee;
8181
padding: 24rpx 2rpx;
8282
min-height: 200rpx;
8383
overflow: hidden;

pages/index/index.js

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
var Api = require('../../utils/api.js');
1313
var util = require('../../utils/util.js');
14+
var auth = require('../../utils/auth.js');
1415
var WxParse = require('../../wxParse/wxParse.js');
1516
var wxApi = require('../../utils/wxApi.js')
1617
var wxRequest = require('../../utils/wxRequest.js')
@@ -43,7 +44,9 @@ Page({
4344
floatDisplay: "none",
4445
displayfirstSwiper: "none",
4546
currentIndex: 0,
46-
topNav: []
47+
topNav: [],
48+
userInfo: app.globalData.userInfo,
49+
isLoginPopup: false
4750

4851

4952
},
@@ -121,6 +124,15 @@ Page({
121124
// },
122125

123126
onLoad: function(options) {
127+
var self = this;
128+
if (!app.globalData.isGetOpenid) {
129+
//self.getUsreInfo();
130+
self.userAuthorization();
131+
} else {
132+
self.setData({
133+
userInfo: app.globalData.userInfo
134+
});
135+
}
124136
var that = this;
125137
wx.request({
126138
//url: 'https://www.yeehee.cn/wp-json/watch-life-net/v1/post/static',
@@ -507,6 +519,130 @@ Page({
507519
});
508520
},
509521

522+
userAuthorization: function () {
523+
var self = this;
524+
// 判断是否是第一次授权,非第一次授权且授权失败则进行提醒
525+
wx.getSetting({
526+
success: function success(res) {
527+
console.log(res.authSetting);
528+
var authSetting = res.authSetting;
529+
if (!('scope.userInfo' in authSetting)) {
530+
//if (util.isEmptyObject(authSetting)) {
531+
console.log('第一次授权');
532+
self.setData({
533+
isLoginPopup: true
534+
})
535+
536+
} else {
537+
console.log('不是第一次授权', authSetting);
538+
// 没有授权的提醒
539+
if (authSetting['scope.userInfo'] === false) {
540+
wx.showModal({
541+
title: '用户未授权',
542+
content: '如需正常使用评论、点赞、赞赏等功能需授权获取用户信息。是否在授权管理中选中“用户信息”?',
543+
showCancel: true,
544+
cancelColor: '#296fd0',
545+
confirmColor: '#296fd0',
546+
confirmText: '设置权限',
547+
success: function (res) {
548+
if (res.confirm) {
549+
console.log('用户点击确定')
550+
wx.openSetting({
551+
success: function success(res) {
552+
console.log('打开设置', res.authSetting);
553+
var scopeUserInfo = res.authSetting["scope.userInfo"];
554+
if (scopeUserInfo) {
555+
auth.getUsreInfo(null);
556+
}
557+
}
558+
});
559+
}
560+
}
561+
})
562+
} else {
563+
auth.getUsreInfo(null);
564+
}
565+
}
566+
}
567+
});
568+
},
569+
agreeGetUser: function (e) {
570+
var userInfo = e.detail.userInfo;
571+
var self = this;
572+
if (userInfo) {
573+
auth.getUsreInfo(e.detail);
574+
self.setData({
575+
userInfo: userInfo
576+
})
577+
}
578+
setTimeout(function () {
579+
self.setData({
580+
isLoginPopup: false
581+
})
582+
}, 1200);
583+
584+
},
585+
closeLoginPopup() {
586+
this.setData({
587+
isLoginPopup: false
588+
});
589+
},
590+
openLoginPopup() {
591+
this.setData({
592+
isLoginPopup: true
593+
});
594+
},
595+
confirm: function () {
596+
this.setData({
597+
'dialog.hidden': true,
598+
'dialog.title': '',
599+
'dialog.content': ''
600+
})
601+
},
602+
getUsreInfo: function () {
603+
var self = this;
604+
var wxLogin = wxApi.wxLogin();
605+
var jscode = '';
606+
wxLogin().then(response => {
607+
jscode = response.code
608+
var wxGetUserInfo = wxApi.wxGetUserInfo()
609+
return wxGetUserInfo()
610+
}).
611+
//获取用户信息
612+
then(response => {
613+
console.log(response.userInfo);
614+
console.log("成功获取用户信息(公开信息)");
615+
app.globalData.userInfo = response.userInfo;
616+
app.globalData.isGetUserInfo = true;
617+
self.setData({
618+
userInfo: response.userInfo
619+
});
620+
621+
var url = Api.getOpenidUrl();
622+
var data = {
623+
js_code: jscode,
624+
encryptedData: response.encryptedData,
625+
iv: response.iv,
626+
avatarUrl: response.userInfo.avatarUrl
627+
}
628+
var postOpenidRequest = wxRequest.postRequest(url, data);
629+
//获取openid
630+
postOpenidRequest.then(response => {
631+
if (response.data.status == '200') {
632+
//console.log(response.data.openid)
633+
console.log("openid 获取成功");
634+
app.globalData.openid = response.data.openid;
635+
app.globalData.isGetOpenid = true;
636+
} else {
637+
console.log(response.data.message);
638+
}
639+
})
640+
}).catch(function (error) {
641+
console.log('error: ' + error.errMsg);
642+
self.userAuthorization();
643+
})
644+
},
645+
510646
//设置首页咨询按钮点击事件:当用户点击咨询按钮时,自动推送一条消息给管理员
511647
notifyAdmin: function() {
512648
console.log('[*] 用户咨询,开始通知管理员。');

pages/index/index.wxml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
<import src="../../templates/common-list.wxml" />
1414
<import src="../../templates/copyright.wxml" />
15+
<import src="../../templates/login-popup.wxml" />
1516

1617
<form catchsubmit="formSubmit" catchreset="formReset" id="search-form">
1718
<view class="search-pancel">
@@ -22,6 +23,8 @@
2223
</view>
2324
</form>
2425

26+
<template is="login-popup" data="{{show: isLoginPopup, userInfo: userInfo}}"></template>
27+
2528
<!-- 首页幻灯片 -->
2629
<view class="index-header" style="display:{{displaySwiper}}">
2730
<swiper class="index-swiper" indicator-dots="" interval="10000" autoplay="true" indicator-color="rgba(255, 255, 255, .2)" indicator-active-color="rgba(255, 255, 255, .7)" previous-margin="30rpx" next-margin="50rpx" circular="true" bindchange="handleChange">

pages/topic/topic.wxss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
border-radius: 12rpx;
4242
margin-bottom: 60rpx;
4343
background: #fff;
44-
box-shadow:2rpx 4rpx 40rpx #c2c2c2;
44+
box-shadow:2rpx 4rpx 80rpx #c2c2c2;
4545

4646
}
4747

pages/usercenter/usercenter.wxml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
<text class="userinfo-nickname">{{userInfo.nickName}}</text>
2121
</view>
2222

23+
<template is="login-popup" data="{{show: isLoginPopup, userInfo: userInfo}}"></template>
24+
2325
<zan-panel title="" style="width:100vw;border-top:20rpx solid #eee;">
2426
<zan-cell-group>
2527
<zan-cell title="热门排行" is-link url="../hot/hot"></zan-cell>

0 commit comments

Comments
 (0)