|
|
<template> <view id="login-type"> <view class="phone__warning"> <image src="https://cdn.guojiang.club/app_guojiang_logo.png"></image> <!--<i class="iconfont icon-anquanjinggao"></i>--> </view> <!-- #ifdef MP-WEIXIN --> <button class="wechat-phoneBtn phone__btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"> <i class="iconfont icon-hudong"></i> <view class="btn__text">微信登录</view> </button> <!-- #endif --> <!-- #ifdef APP-PLUS --> <button class="wechat-phoneBtn phone__btn" @click="appLogin"> <i class="iconfont icon-hudong"></i> <view class="btn__text">微信登录</view> </button> <!-- #endif --> <!-- #ifdef MP-WEIXIN--> <view class="phone_login" @tap="jumpLogin"> 手机号登录 </view> <!-- #endif --> <!-- #ifdef APP-PLUS --> <view class="phone_login" @tap="jumploginType"> 手机号登录 </view> <!-- #endif --> <view class="select" @tap="chageAgreement"> <text> 注册/登录即代表同意《果酱使用协议》</text> </view>
<view class="mask" :hidden="!showAgreement">
</view> <view class="greement-box" :hidden="!showAgreement"> <view class="title mx-1px-bottom"> 用户协议 </view> <view class="content"> <!-- <u-parse :content="agreement.user_agreement" /> --> </view> <view class="btn mx-1px-top" @tap="chageAgreement"> 确定 </view> </view> </view></template><script> import { pageLogin, getUrl, config, wechat, GetUrl } from '@/common/js/utils.js'; import uParse from '@/components/gaoyia-parse/parse.vue';
export default {
components: { uParse },
data() { return { code: '', url: '', logo: '', author: config.PACKAGES.author, config: '', showAgreement: false, agreement: '', open_id: '', h5_url: '' }; },
onShow() { var token = this.$cookieStorage.get('user_token'); // this.setData({
// token: token
// });
this.token = token;
var initInfo = this.$cookieStorage.get('init');
if (initInfo && initInfo.shop_show_logo) { this.setData({ logo: initInfo.shop_show_logo }); }
if (token) { wx.switchTab({ url: '/pages/user/personal/personal' }); } else { // #ifdef MP-WEIXIN
this.wxLogin(); // #endif
} },
onLoad(e) { // 第三方平台配置颜色
var bgConfig = this.$cookieStorage.get('globalConfig') || ''; this.setData({ config: bgConfig });
if (e.url) { // #ifdef APP-PLUS
console.log(e.url) this.url = e.url // #endif
// #ifdef MP-WEIXIN || H5
this.url = decodeURIComponent(e.url); console.log('this.url',this.url) // #endif
}
if (e.shop_id) { this.$cookieStorage.set('shop_id', e.shop_id); }
this.getGreementInfo();
// #ifdef APP-PLUS
var appid = plus.runtime.appid; // #endif
// #ifdef H5
var env = { isIPad: /ipad/i.test(window.navigator.userAgent), isIphone: /iphone|ipad|ipod/i.test(window.navigator.userAgent), isWechat: /MicroMessenger/i.test(window.navigator.userAgent) } if(env.isWechat){ if(e.openid){ this.$cookieStorage.set('openid',e.openid) } var openid = this.$cookieStorage.get('openid') || e.openid; if(openid){ this.postNewQuickLogin(openid); } else{ let origin = window.location.origin; var url = origin+'/pages/user/register/register'; window.location.href = this.$config.GLOBAL.baseUrl+'oauth/wxOauth?redirect_url='+encodeURIComponent(url); } } else{ let url = this.url; wx.navigateTo({ url:'/pages/user/loginType/loginType?url='+url }) } // #endif
}, methods: { jumploginType(){ var url = this.url; if(url){ wx.navigateTo({ url:'/pages/user/loginType/loginType?url='+encodeURIComponent(JSON.stringify(this.url))+'&is_app_bind=app' }) } else{ wx.navigateTo({ url:'/pages/user/loginType/loginType?is_app_bind=app' }) } }, //h5调用微信登陆接口
postNewQuickLogin(open_id){ wx.showLoading({ title: '正在登录', mask: true }); this.$http.post({ api:'api/oauth/newQuickLogin', data:{ open_id:open_id } }).then(res=>{ res = res.data; if(res.status){ if(res.data.access_token){ var access_token = res.data.token_type + ' ' + res.data.access_token; var expires_in = res.data.expires_in || 315360000; this.$cookieStorage.set("user_token", access_token, expires_in); //如果用户没有绑定手机号
if(res.data.has_bind_mobile == 0){ wx.reLaunch({ url:'/pages/user/bindingphone/bindingphone' }) } else{ var url = this.url; if(url){ wx.redirectTo({ url:'/'+ url, fail:res=>{ wx.switchTab({ url:'/'+ url }) } }) } else{ wx.switchTab({ url:'/pages/user/personal/personal' }) } } } else{ wx.showModal({ content: res.message || '请求失败,请重试', showCancel: false }); } } else{ wx.showModal({ content: res.message || '请求失败,请重试', showCancel: false }); } wx.hideLoading() }).catch(rej=>{ wx.showModal({ content: rej.message || '请求失败,请重试', showCancel: false }); wx.hideLoading() }) }, // app调用登录接口
appLogin() { var that = this; uni.getProvider({ service: 'oauth', success: function(res) { uni.login({ provider: "weixin", success: function(loginRes) { uni.getUserInfo({ provider: 'weixin', success: function (infoRes) { var app_info = { nickName:infoRes.userInfo.nickName, avatarUrl:infoRes.userInfo.avatarUrl } that.$cookieStorage.set('app_user_info',app_info) } }); console.log('loginRes',loginRes); that.postUnionidLogin(loginRes) } })
} })
}, // app登录请求的接口
postUnionidLogin(data) { var newobj = { openid: data.authResult.openid, unionid: data.authResult.unionid } this.$http.post({ api: 'api/oauth/app/unionid/login', data: { openid: data.authResult.openid, unionid: data.authResult.unionid } }).then(res => { res = res.data; if (res.data) { if (res.data.access_token) { var access_token = res.data.token_type + ' ' + res.data.access_token; var expires_in = res.data.expires_in || 315360000; this.$cookieStorage.set("user_token", access_token, expires_in); if (this.url) { wx.redirectTo({ url: "/" + this.url, fail: res => { wx.switchTab({ url: "/" + this.url }); } });
} else { wx.switchTab({ url: '/pages/user/personal/personal' });
} } else { var open_id = res.data.open_id; wx.showToast({ title: '需先绑定手机号', duration: 3000, success: res => { var mobileurl = '/pages/user/loginType/loginType?url=' + encodeURIComponent(JSON.stringify(this.url))+ '&open_id=' +encodeURIComponent(JSON.stringify(open_id)) +'&is_app_weixin=appweixin' wx.redirectTo({ url: mobileurl }) } })
} } else { wx.showModal({ content: res.message || '请求失败,请重试', showCancel: false });
} }) }, chageAgreement() { this.setData({ showAgreement: !this.showAgreement }); },
// 获取用户协议
getGreementInfo() { this.$http.get({ api: 'api/user/agreement' }).then(res => { if (res.statusCode == 200) { res = res.data;
if (res.status) { this.setData({ agreement: res.data }); } } }); },
wxLogin() {
//H5判断是否是微信环境
//#ifdef H5
/* if (!wechat.isWechat()) { this.jumpLogin(); return; } else { var url = this.$route.query.url;
var obj_url = GetUrl(url);
if (obj_url == undefined || obj_url.openid == undefined) { this.officialAccountLogin(); } else { this.open_id = obj_url.openid; this.url = url; this.quickLoginByOpenId(url) } */ // }
//#endif
// #ifdef APP-PLUS
this.jumpLogin(); return; // #endif
//#ifdef MP-WEIXIN
wx.showLoading({ title: '正在自动登录', mask: true }); wx.login({ success: res => { if (res.code) { this.autoLogin(res.code); } else { wx.showToast({ title: '获取code失败', icon: 'error' }); } }, }); // #endif
},
quickLoginByOpenId(url) { var token = this.$cookieStorage.get('user_token'); if (wechat.isWechat() && !token) { this.$http.post({ api: 'api/oauth/official-account/quick-login', data: { open_id: this.open_id } }).then(res => { if (res.statusCode == 200) { res = res.data; if (res.status) { if (res.data.open_id != '') {
var url_ = '/pages/user/loginType/loginType?url=' + decodeURIComponent(this.url) + '&open_id=' + this.open_id; wx.redirectTo({ url: url_ }); return; }
if (res.data.access_token) { var access_token = res.data.token_type + ' ' + res.data.access_token; var expires_in = res.data.expires_in || 315360000; this.$cookieStorage.set("user_token", access_token, expires_in); }
if (this.url) { wx.redirectTo({ url: "/" + this.url, fail: rej => { wx.switchTab({ url: "/" + this.url }); } });
} else { wx.switchTab({ url: '/pages/user/personal/personal' }); } } else { wx.showModal({ content: res.message || '请求失败,请重试', showCancel: false }); } } else { wx.showModal({ title: '', content: res.message || '请求失败,请重试', showCancel: false }); } wx.hideLoading();
}, err => { wx.hideLoading();
}); } },
officialAccountLogin() {
var url = this.url ? encodeURIComponent(this.url) : 'pages/user/personal/personal'
var redirect_url = config.GLOBAL.mobile_url + 'pages/user/register/register?url=' + url;
console.log(redirect_url);
this.$http.get({ api: 'api/oauth/official-account/get-redirect-url', data: { redirect_url: redirect_url } }).then(res => { if (res.statusCode == 200) { res = res.data; if (res.status) { if (res.data.url) { window.location.href = res.data.url; }
} else { wx.showModal({ title: '', content: '请求失败', showCancel: false }); } } else { wx.showModal({ title: '', content: '请求失败', showCancel: false }); } wx.hideLoading();
}, err => { wx.hideLoading();
});
},
jumpLogin() { if (this.url) { wx.navigateTo({ url: '/pages/user/loginType/loginType?url=' + encodeURIComponent(this.url) + '&open_id=' + this.open_id }); } else { wx.navigateTo({ url: '/pages/user/loginType/loginType?open_id=' + this.open_id }); } },
getPhoneNumber(e) { if (e.detail.encryptedData) { wx.login({ success: res => { if (res.code) { this.setData({ code: res.code }); this.phone(e); } else { wx.showModal({ content: " 获取code失败", showCancel: false }); } } }); return; } else { this.jumpLogin(); } },
phone(e) { wx.showLoading({ title: '正在登录', mask: true }); this.$http.post({ api: 'api/oauth/miniprogram/mobile', data: { open_type: 'miniprogram', code: this.code, encryptedData: e.detail.encryptedData, iv: e.detail.iv, open_id: this.open_id, shop_id: this.$cookieStorage.get('shop_id') || '', agent_code: this.$cookieStorage.get('coupon_agent_code') || this.$cookieStorage.get('agent_code') || '', clerk_id: this.$cookieStorage.get('clerk_id') || '', agent_code_time: this.$cookieStorage.get('agent_code_time') || '', shop_id_time: this.$cookieStorage.get('shop_id_time') || '' } }).then(res => { if (res.statusCode == 200) { res = res.data;
if (res.data.access_token) { var access_token = res.data.token_type + ' ' + res.data.access_token; var expires_in = res.data.expires_in || 315360000; // debugger;
this.$cookieStorage.set("user_token", access_token, expires_in); // this.$cookieStorage.set("user_token",access_token,expires_in);
// wx.setStorageSync("user_token",access_token);
if (this.url) { var path = ['pages/entity/store/store', 'pages/index/index/index', 'pages/index/classification/classification', 'pages/store/tabCart/tabCart', 'pages/user/personal/personal', 'pages/travels/index/index', 'pages/user/collar/collar' ]; var pathIndex = path.indexOf(this.url);
if (pathIndex == -1) { wx.redirectTo({ url: "/" + this.url }); } else { wx.switchTab({ url: "/" + this.url }); } } else { wx.switchTab({ url: '/pages/user/personal/personal' }); } } else { wx.showModal({ content: res.message || '请求失败,请重试', showCancel: false }); } } else { wx.showModal({ content: '请求失败,请重试', showCancel: false }); }
wx.hideLoading(); }).catch(rej => { wx.hideLoading(); wx.showModal({ content: '请求失败,请重试', showCancel: false }); }); },
autoLogin(code) { this.$http.post({ api: 'api/oauth/miniprogram/login', data: { code: code, open_type: 'miniprogram', shop_id: this.$cookieStorage.get('shop_id') || '', agent_code: this.$cookieStorage.get('coupon_agent_code') || this.$cookieStorage.get('agent_code') || '', clerk_id: this.$cookieStorage.get('clerk_id') || '', agent_code_time: this.$cookieStorage.get('agent_code_time') || '', shop_id_time: this.$cookieStorage.get('shop_id_time') || '' } }).then(res => { res = res.data; if (res.data && res.data.open_id) { this.setData({ open_id: res.data.open_id }); } // 如果接口返回token就直接登录,如果没有则弹出授权
if (res.data.access_token) { console.log('已经返回给我了token'); wx.hideLoading(); var access_token = res.data.token_type + ' ' + res.data.access_token; var expires_in = res.data.expires_in || 315360000; this.$cookieStorage.set("user_token", access_token, expires_in); if (this.url) { wx.redirectTo({ url: "/" + this.url, fail: () => { wx.switchTab({ url: "/" + this.url }); } })
} else { wx.switchTab({ url: '/pages/user/personal/personal' }); } } else { wx.hideLoading(); } }).catch(rej => { wx.hideLoading(); wx.showModal({ content: '请求失败,请重试', showCancel: false, success: res => { if (res.confirm || !res.cancel && !res.confirm) { this.wxLogin(); } } }); }); },
setData: function(obj) { let that = this; let keys = []; let val, data; Object.keys(obj).forEach(function(key) { keys = key.split('.'); val = obj[key]; data = that.$data; keys.forEach(function(key2, index) { if (index + 1 == keys.length) { that.$set(data, key2, val); } else { if (!data[key2]) { that.$set(data, key2, {}); } }
data = data[key2]; }); }); } }, computed: {}, watch: {} };</script>
<style rel="stylesheet/less" lang="less"> @import "register";</style>
|