123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <template>
- <view></view>
- </template>
- <script>
- import { getUserInfo } from '@/api/login.js';
- import { mapMutations, mapState } from 'vuex';
- // #ifdef H5
- import { wechatAuth } from '@/api/wx';
- // #endif
- // #ifdef MP-WEIXIN
- import { wechatMpAuth } from '@/api/wx';
- // #endif
- export default {
- computed: {
- ...mapState(['urlFile'])
- },
- onLoad(option) {
- let obj = this;
- // 判断是否需要保存定向地址
- // #ifdef H5
- this.loadH5();
- // #endif
- // #ifdef MP-WEIXIN
- this.loadMp(option);
- // #endif
- },
- methods: {
- ...mapMutations('user', ['login', 'setUserInfo', 'userInfo']),
- // #ifdef H5
- loadH5() {
- let obj = this;
- let url = window.location.href;
- let code = url.match(/code=([0-9]|[a-z]|[A-Z])*/g)[0].replace('code=', '');
- let spread = uni.getStorageSync('spread') || '';
- wechatAuth({
- code: code,
- spread: spread
- })
- .then(({ data }) => {
- obj.wchatAuth(data);
- // 设置成功后修改当前页面
- history.replaceState({ name: '首页' }, '首页', obj.urlFile + '/pages/index/index');
- })
- .catch(e => {
- uni.showModal({
- title: '错误',
- content: JSON.stringify(e) + option.code,
- showCancel: false
- });
- });
- },
- // #endif
- // #ifdef MP-WEIXIN
- loadMp(option) {
- let obj = this;
- // 获取登录授权页数据
- let user = obj.$api.prePage().userInfo;
- // 获取推广人id
- let spread_spid = uni.getStorageSync('spread') || '';
- wechatMpAuth({
- code: option.code,
- iv: user.target.iv,
- encryptedData: user.target.encryptedData,
- spread_spid: spread_spid
- })
- .then(({ data }) => {
- obj.wchatAuth(data);
- })
- .catch(e => {
- uni.showModal({
- title: '错误',
- content: JSON.stringify(e) + option.code,
- showCancel: false
- });
- });
- },
- // #endif
- wchatAuth(data) {
- let obj = this;
- // 保存token
- uni.setStorageSync('token', data.token);
- // 获取用户基础信息
- getUserInfo({}).then(e => {
- console.log('获取用户信息');
- console.log(e);
- obj.login();
- // 保存返回用户数据
- obj.setUserInfo(e.data);
- let ur = uni.getStorageSync('present') || '/pages/index/index';
- // 用于处理缓存bug
- if (ur == 'pages/product/product') {
- ur = '/pages/index/index';
- }
- if (e.data.phone) {
- uni.switchTab({
- url: ur,
- fail(e) {
- console.log(e);
- uni.navigateTo({
- url: ur,
- fail(e) {
- console.log(e);
- uni.switchTab({
- url: '/pages/index/index'
- });
- }
- });
- }
- });
- } else {
- uni.showModal({
- title: '提示',
- content: '请先绑定手机号',
- showCancel: false,
- success: res => {
- uni.navigateTo({
- url: '/pages/set/phone'
- });
- },
- fail(e) {
- console.log(e);
- }
- });
- }
- }).catch((e) => {
- console.log(e);
- });
- }
- }
- };
- </script>
- <style></style>
|