Commit 6686faff authored by ziyu's avatar ziyu

feat: 用户登录和授权

parent 9a71606c
......@@ -4,42 +4,29 @@ import eventHub from './common/eventHub';
import vuex from '@wepy/x';
wepy.use(vuex);
// import store from './store';
wepy.app({
hooks: {
// App 级别 hook,对整个 App 生效
// 同时存在 Page hook 和 App hook 时,优先执行 Page hook,返回值再交由 App hook 处
},
data: {
userInfo: null
},
onLaunch() {
let that = this;
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
// 可以将 res 发送给后台解码出 unionId
// store.dispatch('setUserInfo',res.userInfo);
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
if (that.userInfoReadyCallback) {
that.userInfoReadyCallback(res => {
// store.dispatch('setUserInfo',res.userInfo);
})
wx.checkSession({
success () {
console.log('已登录')
//session_key 未过期,并且在本生命周期一直有效
},
fail () {
// session_key 已经失效,需要重新执行登录流程
wx.login({
success (res) {
if (res.code) {
//发起网络请求,告诉后端
wx.setStorageSync('wxCode',res.code);
} else {
console.log('登录失败!' + res.errMsg)
}
}
})
}else{
// 未授权,跳转到授权页面
wx.reLaunch({
url: '/pages/auth'
});
}
}
});
wx.setStorageSync('onNetworkStatusChange', true);
......
......@@ -134,8 +134,31 @@
position: absolute;
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>
<wxs module="tools" lang="babel">
<wxs module="tools">
function computClass (current,index,length) {
var allclass = "";
if(current==index)
......@@ -205,6 +228,12 @@
</swiper-item>
</swiper>
</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> -->
<!-- <star-animation></star-animation> -->
<!-- <open-animation></open-animation> -->
......@@ -226,7 +255,7 @@
list:[],
adlist:[],
current:5,
userInfo: {},
// userInfo: {},
swiperItem:3,
videoList:[],
length: 4,
......@@ -234,14 +263,15 @@
topPage:0,
lastPage:0,
currentIndex:5,
authmodal:false,
},
computed: {
...mapState([ 'isConnected' ])
...mapState([ 'isConnected', 'userInfo' ])
},
methods: {
...mapActions([ 'setIsConnected' ]),
...mapActions([ 'setIsConnected', 'setUserInfo' ]),
moveSwiper(e){
if(e.$wx.detail.source == 'touch') {
let current = e.$wx.detail.current;
......@@ -278,7 +308,6 @@
if (callNow) func();
}
}
console.log(current,this.list.length,222);
if(current <= 2) {
debounce(setTimeout(function(){
that.topPage +=1;
......@@ -357,9 +386,6 @@
} else {
// console.log("手势未触发=====");
}
},
forbidMove() {
},
changeVideo(e) {
this.setIsConnected();
......@@ -370,7 +396,7 @@
console.log('add video');
}
},
deleteAnimation: function(current) {
deleteAnimation (current) {
if (current && current.itemid != undefined) {
let list = this.list;
list = list.map((item, index) => {
......@@ -391,7 +417,15 @@
that.list = newlist;
}, 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 () {
......@@ -403,11 +437,6 @@
success (res) {
self.userInfo = res.userInfo;
store.dispatch('setUserInfo', res.userInfo);
},
fail () {
wx.reLaunch({
url: '/pages/auth'
});
}
});
}
......@@ -442,7 +471,35 @@
this.videoList = urls.map((url, index) => ({ id: index + 1, url }));
},
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>
......
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