123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- <template>
- <view class="quick-login-box">
- <view class="item" v-for="(item,index) in servicesList" :key="index"
- @click="item.path?to(item.path):login_before(item.id,false)">
- <image class="logo" :src="item.logo" mode="widthFix"></image>
- <text class="login-title">{{item.text}}</text>
- </view>
- </view>
- </template>
- <script>
- import {
- mapGetters,
- mapMutations
- } from 'vuex';
-
- import loginSuccess from '@/pages/ucenter/login-page/common/loginSuccess.js';
- export default {
- computed: {
- loginConfig() {
- return getApp().globalData.config.router.login
- },
- agreements() {
- return getApp().globalData.config.about.agreements || []
- }
- },
- data() {
- return {
- servicesList: [{
- "id": "username",
- "text": "账号登录",
- "logo": "/static/uni-quick-login/user.png",
- "path": "/pages/ucenter/login-page/pwd-login/pwd-login"
- },
- {
- "id": "smsCode",
- "text": "短信验证码",
- "logo": "/static/uni-quick-login/sms.png",
- "path": "/pages/ucenter/login-page/index/index"
- }
- ],
- oauthServices: [],
- config: {
- "weixin": {
- "text": "微信登录",
- "logo": "/static/uni-quick-login/wechat.png",
- },
- "apple": {
- "text": "苹果登录",
- "logo": "/static/uni-quick-login/apple.png",
- },
- "univerify": {
- "text": "一键登录",
- "logo": "/static/uni-quick-login/univerify.png",
- },
- "qq": {
- "text": "QQ登录",
- "logo": "/static/uni-quick-login/univerify.png",
- },
- "xiaomi": {
- "text": "小米登录",
- "logo": "/static/uni-quick-login/univerify.png",
- },
- "sinaweibo": {
- "text": "微博登录",
- "logo": "/static/uni-quick-login/univerify.png",
- }
- },
- univerifyStyle:{
- "fullScreen": true,
- "backgroundColor": "#ffffff",
- "buttons": {
- "iconWidth": "45px",
- "list": []
- },
- "privacyTerms": {
- "defaultCheckBoxState": false,
- "textColor": "#BBBBBB",
- "termsColor": "#5496E3",
- "prefix": "我已阅读并同意",
- "suffix": "并使用本机号码登录",
- "privacyItems": []
- }
- }
- }
- },
- watch: {
- agree(agree) {
- this.univerifyStyle.privacyTerms.defaultCheckBoxState = agree
- }
- },
- props: {
- agree: {
- type: Boolean,
- default () {
- return false
- }
- }
- },
- async created() {
- this.univerifyStyle.privacyTerms.privacyItems = this.agreements
-
- let servicesList = this.servicesList
-
-
- let id = 'weixin'
- if (this.loginConfig.includes(id)) {
- servicesList.push({
- ...this.config[id],
- id
- })
- }
-
-
- this.oauthServices = await new Promise((callBack)=>{
- plus.oauth.getServices(oauthServices => {
- callBack(oauthServices.filter(({nativeClient,id})=>nativeClient&&this.loginConfig.includes(id)))
-
- }, err => {
- callBack([])
- uni.hideLoading()
- uni.showModal({
- title: '获取服务供应商失败:' + JSON.stringify(err),
- showCancel: false,
- confirmText: '知道了'
- });
- console.error('获取服务供应商失败:' + JSON.stringify(err));
- })
- })
-
-
- servicesList = servicesList.concat(this.oauthServices.map( ({id})=>{
- return {...this.config[id],id}
- }))
-
- servicesList.forEach(({id,logo})=>{
- if(id != 'univerify'){
- this.univerifyStyle.buttons.list.push({"iconPath":logo,"provider":id})
- }
- })
-
- this.servicesList = servicesList.filter(item=>{
- return item.path != this.getRoute(1)
- })
-
- },
- methods: {
- ...mapMutations({
- setUserInfo: 'user/login'
- }),
- getRoute(n = 0) {
- let pages = getCurrentPages();
-
- if (n > pages.length) {
- return ''
- }
- return '/' + pages[pages.length - n].route
- },
- to(path) {
-
- if (this.getRoute(2) == path) {
- uni.navigateBack();
- } else {
- uni.navigateTo({
- url: path
- })
- }
- },
- login_before(type, navigateBack = true) {
- if (!this.agree&&type!='univerify') {
- return uni.showToast({
- title: '你未同意隐私政策协议',
- icon: 'none'
- });
- }
- uni.showLoading({
- mask: true
- })
-
- let oauthService = this.oauthServices.find((service) => service.id == type)
-
-
-
- if (type == 'weixin') {
- return oauthService.authorize(({
- code
- }) => {
-
- this.login({
- code
- }, type)
- },
- err => {
- uni.hideLoading()
- console.log(err);
- uni.showModal({
- content: JSON.stringify(err),
- showCancel: false
- });
- })
- }
-
- uni.login({
- "provider": type,
- "univerifyStyle": this.univerifyStyle,
- complete: (e) => {
- console.log(e);
- },
- success: async e => {
- console.log(e);
- if (type == 'apple') {
- let res = await this.getUserInfo({
- provider: "apple"
- })
- uni.hideLoading()
- Object.assign(e.authResult, res.userInfo)
- }
-
- if (type == 'weixin') {
- return this.login({
- code: e.code
- }, type)
- }
-
- this.login(e.authResult, type)
- },
- fail: (err) => {
- uni.hideLoading()
- console.log(err);
- if (type == 'univerify') {
- if (err.metadata && err.metadata.error_data) {
- uni.showToast({
- title: "一键登录:" + err.metadata.error_data,
- icon: 'none'
- });
- }
- if (err.errMsg) {
- uni.showToast({
- title: "一键登录:" + err.errMsg,
- icon: 'none'
- });
- }
- switch (err.errCode) {
- case 30002:
- console.log('在一键登录界面,点击其他登录方式');
- break;
- case 30003:
- console.log('关闭了登录');
- if (navigateBack) {
- uni.navigateBack()
- }
- break;
- case 30006:
- uni.showModal({
- title: "登录服务初始化错误",
- content: err.metadata.error_data,
- showCancel: false,
- confirmText: '知道了',
- });
- break;
- case "30008":
- uni.showToast({
- title: '点击了第三方登陆',
- icon: 'none'
- });
- console.log('点击了第三方登陆,provider:',err.provider);
- let {path} = this.servicesList.find(item=>item.id==err.provider)||{}
- console.log('path',path);
- if(path&&path!=this.getRoute(1)){
- this.to(path)
- }else{
- this.login_before(err.provider)
- }
- break;
- default:
- console.log(9527, err);
- break;
- }
- }
- }
- })
- },
- login(params, type) {
- console.log({
- params,
- type
- });
- let action = 'loginBy' + type.trim().toLowerCase().replace(type[0], type[0].toUpperCase())
- uniCloud.callFunction({
- name: 'uni-id-cf',
- data: {
- action,
- params
- },
- success: ({
- result
- }) => {
- console.log(result);
- if (result.code === 0) {
- if (type == 'univerify') {
- uni.closeAuthView()
- }
- uni.hideLoading()
- loginSuccess(result)
- delete result.userInfo.token
- this.setUserInfo(result.userInfo)
- } else {
- uni.showModal({
- content: result.msg,
- showCancel: false
- });
- }
- },
- complete: () => {
- uni.hideLoading()
- }
- })
- },
- async getUserInfo(e) {
- return new Promise((resolve, reject) => {
- uni.getUserInfo({
- ...e,
- success: (res) => {
- resolve(res);
- },
- fail: (err) => {
- uni.showModal({
- content: JSON.stringify(err),
- showCancel: false
- });
- reject(err);
- }
- })
- })
- }
- }
- }
- </script>
- <style scoped>
- .quick-login-box {
- flex-direction: row;
- width: 750rpx;
- justify-content: space-around;
- position: fixed;
- bottom: 10rpx;
- left: 0;
- }
- .item {
- flex-direction: column;
- justify-content: center;
- align-items: center;
- height: 200rpx;
- }
- .logo {
- width: 60rpx;
- height: 60rpx;
- }
- .login-title {
- margin-top: 4px;
- font-size: 26rpx;
- }
- </style>
|