Commit 6686faff authored by ziyu's avatar ziyu

feat: 用户登录和授权

parent 9a71606c
...@@ -4,42 +4,29 @@ import eventHub from './common/eventHub'; ...@@ -4,42 +4,29 @@ import eventHub from './common/eventHub';
import vuex from '@wepy/x'; import vuex from '@wepy/x';
wepy.use(vuex); wepy.use(vuex);
// import store from './store';
wepy.app({ wepy.app({
hooks: { hooks: {
// App 级别 hook,对整个 App 生效 // App 级别 hook,对整个 App 生效
// 同时存在 Page hook 和 App hook 时,优先执行 Page hook,返回值再交由 App hook 处 // 同时存在 Page hook 和 App hook 时,优先执行 Page hook,返回值再交由 App hook 处
}, },
data: {
userInfo: null
},
onLaunch() { onLaunch() {
let that = this; wx.checkSession({
wx.getSetting({ success () {
success: res => { console.log('已登录')
if (res.authSetting['scope.userInfo']) { //session_key 未过期,并且在本生命周期一直有效
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框 },
wx.getUserInfo({ fail () {
success: res => { // session_key 已经失效,需要重新执行登录流程
// 可以将 res 发送给后台解码出 unionId wx.login({
// store.dispatch('setUserInfo',res.userInfo); success (res) {
if (res.code) {
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 //发起网络请求,告诉后端
// 所以此处加入 callback 以防止这种情况 wx.setStorageSync('wxCode',res.code);
if (that.userInfoReadyCallback) { } else {
that.userInfoReadyCallback(res => { console.log('登录失败!' + res.errMsg)
// store.dispatch('setUserInfo',res.userInfo); }
}) }
} })
}
})
}else{
// 未授权,跳转到授权页面
wx.reLaunch({
url: '/pages/auth'
});
}
} }
}); });
wx.setStorageSync('onNetworkStatusChange', true); wx.setStorageSync('onNetworkStatusChange', true);
......
...@@ -134,8 +134,31 @@ ...@@ -134,8 +134,31 @@
position: absolute; position: absolute;
z-index: 7; z-index: 7;
} }
.authmodal{
width:100%;
height:100%;
background-color: rgba(0,0,0,0.7);
position: fixed;
z-index:20;
.wrap {
width: 90%;
margin:0 auto;
position: relative;
top:200px;
padding: 50rpx 0;
background-color: #fff;
border-radius: 20rpx;
.text {
color:#333;
text-align: center;
}
button {
width:80%;
}
}
}
</style> </style>
<wxs module="tools" lang="babel"> <wxs module="tools">
function computClass (current,index,length) { function computClass (current,index,length) {
var allclass = ""; var allclass = "";
if(current==index) if(current==index)
...@@ -205,6 +228,12 @@ ...@@ -205,6 +228,12 @@
</swiper-item> </swiper-item>
</swiper> </swiper>
</view> </view>
<view class="authmodal" v-show="authmodal">
<view class="wrap">
<view class="text">来到lukii,再平凡的人,也有幸运的权利!</view>
<button open-type="getUserInfo" bindgetuserinfo="getUserInfo">授权并进入</button>
</view>
</view>
<!-- <network v-if="!isConnected"></network> --> <!-- <network v-if="!isConnected"></network> -->
<!-- <star-animation></star-animation> --> <!-- <star-animation></star-animation> -->
<!-- <open-animation></open-animation> --> <!-- <open-animation></open-animation> -->
...@@ -226,7 +255,7 @@ ...@@ -226,7 +255,7 @@
list:[], list:[],
adlist:[], adlist:[],
current:5, current:5,
userInfo: {}, // userInfo: {},
swiperItem:3, swiperItem:3,
videoList:[], videoList:[],
length: 4, length: 4,
...@@ -234,14 +263,15 @@ ...@@ -234,14 +263,15 @@
topPage:0, topPage:0,
lastPage:0, lastPage:0,
currentIndex:5, currentIndex:5,
authmodal:false,
}, },
computed: { computed: {
...mapState([ 'isConnected' ]) ...mapState([ 'isConnected', 'userInfo' ])
}, },
methods: { methods: {
...mapActions([ 'setIsConnected' ]), ...mapActions([ 'setIsConnected', 'setUserInfo' ]),
moveSwiper(e){ moveSwiper(e){
if(e.$wx.detail.source == 'touch') { if(e.$wx.detail.source == 'touch') {
let current = e.$wx.detail.current; let current = e.$wx.detail.current;
...@@ -278,7 +308,6 @@ ...@@ -278,7 +308,6 @@
if (callNow) func(); if (callNow) func();
} }
} }
console.log(current,this.list.length,222);
if(current <= 2) { if(current <= 2) {
debounce(setTimeout(function(){ debounce(setTimeout(function(){
that.topPage +=1; that.topPage +=1;
...@@ -357,9 +386,6 @@ ...@@ -357,9 +386,6 @@
} else { } else {
// console.log("手势未触发====="); // console.log("手势未触发=====");
} }
},
forbidMove() {
}, },
changeVideo(e) { changeVideo(e) {
this.setIsConnected(); this.setIsConnected();
...@@ -370,7 +396,7 @@ ...@@ -370,7 +396,7 @@
console.log('add video'); console.log('add video');
} }
}, },
deleteAnimation: function(current) { deleteAnimation (current) {
if (current && current.itemid != undefined) { if (current && current.itemid != undefined) {
let list = this.list; let list = this.list;
list = list.map((item, index) => { list = list.map((item, index) => {
...@@ -391,7 +417,15 @@ ...@@ -391,7 +417,15 @@
that.list = newlist; that.list = newlist;
}, 500); }, 500);
} }
} },
getUserInfo (e) {
if(e.$wx.detail.userInfo) {
this.setUserInfo(e.$wx.detail.userInfo);
// this.userInfo = e.$wx.detail.userInfo;
this.authmodal = false;
}
// store.dispatch('setUserInfo',e.$wx.detail.userInfo);
},
}, },
created () { created () {
...@@ -403,11 +437,6 @@ ...@@ -403,11 +437,6 @@
success (res) { success (res) {
self.userInfo = res.userInfo; self.userInfo = res.userInfo;
store.dispatch('setUserInfo', res.userInfo); store.dispatch('setUserInfo', res.userInfo);
},
fail () {
wx.reLaunch({
url: '/pages/auth'
});
} }
}); });
} }
...@@ -442,7 +471,35 @@ ...@@ -442,7 +471,35 @@
this.videoList = urls.map((url, index) => ({ id: index + 1, url })); this.videoList = urls.map((url, index) => ({ id: index + 1, url }));
}, },
onLoad () { onLoad () {
let that = this;
//如果已经存在store里就不用再获取用户信息了
if(this.userInfo) return;
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
// 可以将 res 发送给后台解码出 unionId
if(res.userInfo) {
that.setUserInfo(res.userInfo);
// that.userInfo = res.userInfo;
}
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
if (that.userInfoReadyCallback) {
that.userInfoReadyCallback(res => {
that.setUserInfo(res.userInfo||{});
// that.userInfo = res.userInfo||{};
})
}
}
})
}else{
that.authmodal = true;
}
}
});
}, },
}); });
</script> </script>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment