Browse Source

自己手写用户授权弹窗前

master
蟑螂恶霸 2 years ago
parent
commit
2dd1426f40
  1. 31
      components/tabbar/tabbar.vue
  2. 1
      main.js
  3. 5
      manifest.json
  4. 6
      pages/index/index.vue
  5. 233
      pages/my/my.vue
  6. 33
      subpkg/login/login.vue

31
components/tabbar/tabbar.vue

@ -18,6 +18,7 @@
<script> <script>
import urlConfig from '@/utils/config.js' import urlConfig from '@/utils/config.js'
import { mapMutations, mapState } from 'vuex'
export default { export default {
props: { props: {
@ -55,6 +56,7 @@
this.listTabbar(); this.listTabbar();
}, },
methods: { methods: {
// ...mapMutations('m_user', ['token', 'updateUserInfo', 'updateToken', 'updateUserName','updateUserId','updateAvatarUrl', 'updateRedirectInfo']),
listTabbar() { listTabbar() {
this.$api.listTabbar({}) this.$api.listTabbar({})
.then(res => { .then(res => {
@ -73,6 +75,35 @@
}, },
//tabble //tabble
goPages(pageIndex) { goPages(pageIndex) {
// console.log("================== pageIndox ==================")
// if(pageIndex == "pages/my/my") {
// uni.getUserProfile({
// desc: '',
// success(res) {
// console.log("================== getUserProfile ==================")
// console.log(res);
// }, fail(err) {
// console.log(err);
// }
// });
// uni.authorize({
// scope: 'scope.userInfo',
// success () {
// console.log("================== scope.userInfo ==================")
// }, fail (error) {
// console.log(error);
// }
// });
// uni.getSetting({
// success(res) {
// console.log(res.authSetting);
// }, fail(err) {
// console.log(err);
// }
// });
// }
uni.switchTab({ uni.switchTab({
url: "/" + pageIndex url: "/" + pageIndex
}) })

1
main.js

@ -3,6 +3,7 @@
import Vue from 'vue' import Vue from 'vue'
import App from './App' import App from './App'
import store from '@/store/store.js' import store from '@/store/store.js'
import share from './utils/share.js'
//导入网络请求的包 //导入网络请求的包
import {$http} from '@escook/request-miniprogram' import {$http} from '@escook/request-miniprogram'

5
manifest.json

@ -61,9 +61,10 @@
/* */ /* */
"mp-weixin" : { "mp-weixin" : {
/* */ /* */
"appid" : "wx9cf00eac2731a25d", "appid" : "wx04d7c402ea59d415",
"setting" : { "setting" : {
"urlCheck" : false "urlCheck" : false,
"minified" : true
}, },
"usingComponents" : true, "usingComponents" : true,
"permission" : { "permission" : {

6
pages/index/index.vue

@ -80,8 +80,8 @@
...mapState('m_user', ['token']) ...mapState('m_user', ['token'])
}, },
onLoad(options) { onLoad(options) {
this.checkLogin(); // this.checkLogin();
if (this.token != "") { // if (this.token != "") {
this.getSwiperList(); this.getSwiperList();
this.getLocation(); this.getLocation();
this.getContent(); this.getContent();
@ -89,7 +89,7 @@
this.showTitle = false this.showTitle = false
clearTimeout(this.timer) clearTimeout(this.timer)
}, 5000) }, 5000)
} // }
}, },
onShow() { onShow() {
this.page= "home" this.page= "home"

233
pages/my/my.vue

@ -1,47 +1,66 @@
<template> <template>
<view class="wrap"> <view class="wrap">
<view class="stabar"></view> <view class="stabar"></view>
<view class="mainwrap"> <view class="mainwrap">
<view class="perinfo"> <span class="type-name"><img :src="avatar"></img></span> <view class="perinfo"> <span class="type-name"><img :src="avatar"></img></span>
<view class="dycd"> <span class="myname" @click="checkMyInfo()">{{name}}</span> <view class="dycd"> <span class="myname" @click="checkMyInfo()">{{name}}</span>
<view class="myitem"> <span @click="showMedal()">勋章{{medalNum}}<i :style="{'background-color':active == 0 ?'red':'none'}"></i></span> <span>挑战历史{{challengeNum}}</span> </view> <view class="myitem"> <span @click="showMedal()">勋章{{medalNum}}<i
</view> :style="{'background-color':active == 0 ?'red':'none'}"></i></span>
</view> <span>挑战历史{{challengeNum}}</span>
<view class="banbox_sec"> </view>
<swiper style="height: 100%;" :indicator-dots="false" :autoplay="true" :interval="3000" :duration="1000" :circular="true"> </view>
<swiper-item v-for="(item, i) in swiperList" :key="i"> </view>
<navigator style="height: 100%;" class="swiper-item"> <view class="banbox_sec">
<img :src="item.uploadPath"></img> <swiper style="height: 100%;" :indicator-dots="false" :autoplay="true" :interval="3000" :duration="1000"
</navigator> :circular="true">
</swiper-item> <swiper-item v-for="(item, i) in swiperList" :key="i">
</swiper> <navigator style="height: 100%;" class="swiper-item">
<img :src="item.uploadPath"></img>
</navigator>
</swiper-item>
</swiper>
</view>
<view class="mset">
<ul>
<li v-for="(item,i) in functionList" :key="i">
<span @click="navClickHandler(item)"
:style="'background-image:url('+item.uploadPath+');'">{{item.name}}</span>
</li>
<li>
<span @click="getUserInfoByWX()">测试</span>
</li>
</ul>
</view>
</view>
<view class="footbox">
<tabbar :page="page"></tabbar>
</view>
<view>
<!-- 普通弹窗 -->
<uni-popup ref="popup" background-color="#fff" @change="change">
<view class="popup-content" :class="{ 'popup-height': type === 'left' || type === 'right' }"><text
class="text">popup 内容</text></view>
</uni-popup>
</view> </view>
<view class="mset">
<ul>
<li v-for="(item,i) in functionList" :key="i">
<span @click="navClickHandler(item)" :style="'background-image:url('+item.uploadPath+');'">{{item.name}}</span>
</li>
</ul>
</view>
</view>
<view class="footbox">
<tabbar :page="page"></tabbar>
</view>
</view> </view>
</template> </template>
<script> <script>
import tabbar from "@/components/tabbar/tabbar.vue"; import tabbar from "@/components/tabbar/tabbar.vue";
import {mapState,mapMutations} from 'vuex' import {
mapState,
mapMutations
} from 'vuex'
export default { export default {
data() { data() {
return { return {
active:1, active: 1,
medalNum:0, medalNum: 0,
challengeNum:0, challengeNum: 0,
page: "", page: "",
name:"", name: "",
avatar: uni.$http.baseUrl + "/profile//2024/01/18/6147d64a3b94d7b3d3cc35aaf8cbfe06.png", avatar: uni.$http.baseUrl + "/profile//2024/01/18/6147d64a3b94d7b3d3cc35aaf8cbfe06.png",
// //
swiperList: [], swiperList: [],
@ -67,6 +86,7 @@
this.getMyMedalList() this.getMyMedalList()
this.getChallengeTotal() this.getChallengeTotal()
this.getStatus() this.getStatus()
this.checkLogin();
}, },
onPullDownRefresh() { onPullDownRefresh() {
this.name = this.userName; this.name = this.userName;
@ -78,10 +98,12 @@
this.getStatus() this.getStatus()
}, },
computed: { computed: {
...mapState('m_user', ['userinfo','userId','userName','avatarUrl']) ...mapState('m_user', ['token', 'userinfo', 'userId', 'userName', 'avatarUrl'])
}, },
methods: { methods: {
...mapMutations('m_user', ['updateAddress', 'updateUserInfo', 'updateToken']), ...mapMutations('m_user', ['updateAddress', 'updateUserInfo', 'updateToken', , 'updateUserName',
'updateUserId', 'updateAvatarUrl', 'updateRedirectInfo'
]),
async logout() { async logout() {
const [err, succ] = await uni.showModal({ const [err, succ] = await uni.showModal({
title: '提示', title: '提示',
@ -98,7 +120,9 @@
const { const {
data: res data: res
} = await uni.$http.post('/small/wechat/showMyMedal',{guid:this.userId}) } = await uni.$http.post('/small/wechat/showMyMedal', {
guid: this.userId
})
if (!res.success) return uni.$showMsg() if (!res.success) return uni.$showMsg()
this.medalNum = res.data.length; this.medalNum = res.data.length;
@ -108,9 +132,11 @@
const { const {
data: res data: res
} = await uni.$http.post('/small/wechat/getActiveStatus',{guid:this.userId}) } = await uni.$http.post('/small/wechat/getActiveStatus', {
guid: this.userId
})
if (!res.success) return uni.$showMsg() if (!res.success) return uni.$showMsg()
if(res.data!=null){ if (res.data != null) {
this.active = res.data.active; this.active = res.data.active;
} }
// uni.$showMsg('') // uni.$showMsg('')
@ -119,7 +145,9 @@
const { const {
data: res data: res
} = await uni.$http.post('/small/questionFre/listAll',{userId:this.userId}) } = await uni.$http.post('/small/questionFre/listAll', {
userId: this.userId
})
if (!res.success) return uni.$showMsg() if (!res.success) return uni.$showMsg()
this.challengeNum = res.data.length; this.challengeNum = res.data.length;
@ -153,30 +181,143 @@
this.functionList = res.data; this.functionList = res.data;
// uni.$showMsg('') // uni.$showMsg('')
}, },
async showMedal(){ async showMedal() {
if (this.active == 0){ if (this.active == 0) {
const { const {
data: res data: res
} = await uni.$http.post('/small/wechat/editActiveStatus',{guid:this.userId,active:1}) } = await uni.$http.post('/small/wechat/editActiveStatus', {
guid: this.userId,
active: 1
})
if (!res.success) return uni.$showMsg() if (!res.success) return uni.$showMsg()
this.active = 1; this.active = 1;
} }
uni.navigateTo({ uni.navigateTo({
url:'/subpkg/medal/medal?medalNum='+this.medalNum+'&avatarUrl='+this.avatar+'&name='+this.name url: '/subpkg/medal/medal?medalNum=' + this.medalNum + '&avatarUrl=' + this.avatar +
'&name=' + this.name
}) })
}, },
navClickHandler(item) { navClickHandler(item) {
if(item !== ""){ if (item !== "") {
uni.navigateTo({ uni.navigateTo({
url: item.pagePath url: item.pagePath
}) })
} }
}, },
checkMyInfo(){ checkMyInfo() {
uni.navigateTo({ uni.navigateTo({
url:'/subpkg/userInfo/userInfo?avatarUrl='+this.avatar+'&name='+this.name url: '/subpkg/userInfo/userInfo?avatarUrl=' + this.avatar + '&name=' + this.name
}) })
} },
checkLogin() {
// if(this.token === "") {
this.getUserInfoByWX();
// }
},
toggle(type) {
this.type = type
// open uni-popup type
this.$refs.popup.open(type)
},
getUserInfoByWX() {
console.log("================== getUserInfoByWX ==================")
let that = this;
this.toggle('bottom');
// uni.openSetting({
// success(res) {
// console.log(res.authSetting);
// },
// fail(err) {
// console.log(err);
// }
// });
//
uni.getUserInfo({
provider: 'weixin',
success: function(infoRes) {
console.log('用户昵称为:' + infoRes.userInfo.nickName);
that.getToken(infoRes);
},
fail(error) {
console.log('失败', error);
uni.switchTab({
url: '/pages/index/index'
})
}
});
},
getUserInfoByWX2ShowModal() {
let that = this;
uni.getSetting({
success(res) {
// .
uni.showModal({
title: '授权提示',
content: '因您首次进入!我们需要获取您的基础信息,请确认授权',
success: function(res) {
if (res.confirm) {
//
uni.getUserInfo({
provider: 'weixin',
success: function(infoRes) {
console.log('用户昵称为:' + infoRes.userInfo
.nickName);
that.getToken(infoRes);
},
fail(error) {
console.log('失败', error);
uni.switchTab({
url: '/pages/index/index'
})
}
});
} else if (res.cancel) {
//
console.log('用户拒绝授权');
uni.switchTab({
url: '/pages/index/index'
})
}
}
});
}
});
},
async getToken(info) {
// code
const [err, res] = await uni.login().catch(err => err)
if (err || res.errMsg !== 'login:ok') return uni.$showMsg('登录失败!')
//
const query = {
code: res.code,
encryptedData: info.encryptedData,
iv: info.iv,
rawData: info.rawData,
signature: info.signature
}
const {
data: loginResult
} = await uni.$http.post('/small/wechat/login', query)
if (loginResult.token === "") return uni.$showMsg('登录失败!')
// token vuex
this.updateToken(loginResult.token)
this.updateUserName(loginResult.userName)
this.updateUserId(loginResult.userId)
if (loginResult.avatarUrl != null && loginResult.avatarUrl.indexOf('http') == -1) {
loginResult.avatarUrl = uni.$http.baseUrl + loginResult.avatarUrl
}
this.name = loginResult.userName;
this.avatar = loginResult.avatarUrl;
this.updateAvatarUrl(loginResult.avatarUrl)
uni.switchTab({
url: '/pages/my/my'
})
//this.navigateBack()
},
}, },
components: { components: {
tabbar tabbar

33
subpkg/login/login.vue

@ -19,7 +19,7 @@
...mapState('m_user', ['redirectInfo']) ...mapState('m_user', ['redirectInfo'])
}, },
methods:{ methods:{
...mapMutations('m_user', ['updateUserInfo', 'updateToken', 'updateUserName','updateUserId','updateAvatarUrl', 'updateRedirectInfo']), ...mapMutations('m_user', ['token', 'updateUserInfo', 'updateToken', 'updateUserName','updateUserId','updateAvatarUrl', 'updateRedirectInfo']),
getUserInfo(e) { getUserInfo(e) {
if (e.detail.errMsg === 'getUserInfo:fail auth deny') return uni.$showMsg('您取消了登录授权!') if (e.detail.errMsg === 'getUserInfo:fail auth deny') return uni.$showMsg('您取消了登录授权!')
@ -32,6 +32,7 @@
this.updateUserInfo(e.detail.userInfo) this.updateUserInfo(e.detail.userInfo)
this.getToken(e.detail) this.getToken(e.detail)
// this.getUserInfoByWX(e.detail);
}, },
async getToken(info) { async getToken(info) {
// code // code
@ -74,6 +75,36 @@
} }
}) })
} }
},
//
getUserInfoByWX(info) {
let that = this;
uni.getSetting({
success(res) {
// .
uni.showModal({
title: '授权提示',
content: '我们需要获取你的基础用户信息,请确认授权',
success: function (res) {
if (res.confirm) {
//
uni.getUserInfo({
provider: 'weixin',
success: function (infoRes) {
console.log('用户昵称为:' + infoRes.userInfo.nickName);
that.getToken(info);
}, fail (error) {
console.log('失败', error);
}
});
} else if (res.cancel) {
//
console.log('用户拒绝授权');
}
}
});
}
});
} }
} }
} }

Loading…
Cancel
Save