Commit 7b808cbe by zhanghaozhe

微信登录redirect_url

parent cbe294db
...@@ -23,6 +23,7 @@ import './assets/font/iconfont.css'; ...@@ -23,6 +23,7 @@ import './assets/font/iconfont.css';
class App extends Component { class App extends Component {
previousLocation = this.props.location
componentDidMount() { componentDidMount() {
//平台信息 //平台信息
...@@ -34,6 +35,13 @@ class App extends Component { ...@@ -34,6 +35,13 @@ class App extends Component {
this.props.setCurrentUser(this.transformUser(res)) this.props.setCurrentUser(this.transformUser(res))
}) })
const {history} = this.props
history.listen(location => {
if (location.pathname.startsWith('/passport')) {
location.state = {from: this.previousLocation};
}
})
//微信 //微信
let code = getParam('code') let code = getParam('code')
...@@ -46,7 +54,6 @@ class App extends Component { ...@@ -46,7 +54,6 @@ class App extends Component {
if (data.errno == 200) { if (data.errno == 200) {
if (data.data['is_bind_mobile']) { if (data.data['is_bind_mobile']) {
window.location.assign(data.data.url) window.location.assign(data.data.url)
} else { } else {
let user = this.transformWxUser(res) let user = this.transformWxUser(res)
let {role, uid, token} = data.data let {role, uid, token} = data.data
...@@ -67,6 +74,13 @@ class App extends Component { ...@@ -67,6 +74,13 @@ class App extends Component {
} }
componentDidUpdate() {
const {location} = this.props
this.previousLocation = location.pathname.startsWith('/passport') ?
this.previousLocation.pathname === '/' ? location : this.previousLocation : location
}
transformUser = res => { transformUser = res => {
let payload let payload
if (res.data.code === 200) { if (res.data.code === 200) {
......
...@@ -77,7 +77,7 @@ const formikConfig = { ...@@ -77,7 +77,7 @@ const formikConfig = {
}).then(res => { }).then(res => {
if (!res.hasError) { if (!res.hasError) {
let {from} = props.location.state || {from: {pathname: '/', search: ''}} let {from} = props.location.state || {from: {pathname: '/', search: ''}}
history.replace({pathname: from.pathname, search: from.search}) history.replace({pathname: from.pathname, search: from.search || ''})
} else { } else {
Toast.info(res.msg, 2, null, false) Toast.info(res.msg, 2, null, false)
} }
......
...@@ -19,10 +19,13 @@ import wechat from './wechat.png' ...@@ -19,10 +19,13 @@ import wechat from './wechat.png'
class Passport extends Component { class Passport extends Component {
redirect_url = location.protocol + '//' + location.hostname redirectURL = '/'
constructor(props) { constructor(props) {
super(props); super(props);
const {location: {state: {from}}} = props
this.redirectURL = window.location.origin + from.pathname + from.search + from.hash
// console.log(props.location);
this.state = { this.state = {
loginWays: [ loginWays: [
{ {
...@@ -32,17 +35,17 @@ class Passport extends Component { ...@@ -32,17 +35,17 @@ class Passport extends Component {
{ {
logo: wechat, logo: wechat,
text: '微信', text: '微信',
url: 'https://www.baidu.com' url: ''
}, },
{ {
logo: qq, logo: qq,
text: 'QQ', text: 'QQ',
url: `${API["passport-api"]}/mob/qqlogin?redirect_url=https://m.julyedu.com` url: `${API["passport-api"]}/mob/qqlogin?redirect_url=${this.redirectURL}`
}, },
{ {
logo: sina, logo: sina,
text: '新浪', text: '新浪',
url: `${API['passport-api']}/mob/sinalogin?redirect_url=${this.redirect_url}` url: `${API['passport-api']}/mob/sinalogin?redirect_url=${this.redirectURL}`
}, },
] ]
} }
...@@ -51,11 +54,12 @@ class Passport extends Component { ...@@ -51,11 +54,12 @@ class Passport extends Component {
componentDidMount() { componentDidMount() {
this.routeWhenUserLoggedIn() this.routeWhenUserLoggedIn()
} }
componentDidUpdate() { componentDidUpdate() {
this.routeWhenUserLoggedIn() this.routeWhenUserLoggedIn()
// console.log(this.props.location);
} }
......
...@@ -32,9 +32,9 @@ class Login extends Component { ...@@ -32,9 +32,9 @@ class Login extends Component {
history.push('/passport/account-login', location.state) history.push('/passport/account-login', location.state)
break; break;
case '微信': case '微信':
let {from} = location.state || {from: {pathname: '/'}} let {from} = location.state || {from: {pathname: '/', search: '', hash: ''}}
const redirectURI = window.location.origin + from.pathname const redirectURI = window.location.origin + from.pathname + from.search + from.hash
window.location.assign(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx23dac6775ac82877&redirect_uri=${encodeURIComponent(redirectURI)}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`) window.location.assign(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx23dac6775ac82877&redirect_uri=${encodeURIComponent(redirectURI)}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`)
break; break;
......
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