page.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import React from 'react';
  2. import './index.less';
  3. import Page from '@src/containers/Page';
  4. import { asyncSMessage } from '@src/services/AsyncTools';
  5. import { WechatH5AppId, H5Url } from '../../../../Constant';
  6. import { User } from '../../../stores/user';
  7. export default class extends Page {
  8. init() {
  9. const { code, state = '' } = this.props.core.query;
  10. console.log(this.state);
  11. const [scope, jump] = state.split('|');
  12. if (code) {
  13. User.loginWechat(code, !!scope).then((info) => {
  14. if (!info.id && !scope) {
  15. this.redirect('snsapi_userinfo', `1|${jump || '/'}`);
  16. } else if (info.bindMobile) {
  17. toLink(jump || '/');
  18. } else {
  19. replaceLink(`/bind?url=${encodeURIComponent(jump)}`);
  20. }
  21. })
  22. .catch(e => {
  23. asyncSMessage(e.message, 'error');
  24. });
  25. } else {
  26. this.redirect('snsapi_base', `|${this.state.search.url || '/'}`);
  27. }
  28. }
  29. redirect(scope, state) {
  30. const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${WechatH5AppId}&redirect_uri=${encodeURIComponent(
  31. `${H5Url}/login`,
  32. )}&response_type=code&scope=${scope}&state=${encodeURIComponent(state)}#wechat_redirect`;
  33. window.location.href = url;
  34. }
  35. renderView() {
  36. return <div />;
  37. }
  38. }