permission.js 5.2 KB
Newer Older
潘琦 committed
1

潘琦 committed
2 3
import router from './router/index'
import store from '@/store'
潘琦 committed
4
// import {validatenull} from '@/util/validate'
潘琦 committed
5 6
import { getParamsByUrl, removeUrlParam } from '@/util/index'
import { getOauth, getUserByToken } from '@/api/auth/auth'
7
import baseUrl from '@/util/baseUrl'
潘琦 committed
8
import { Toast } from 'mint-ui'
9
// import { wxpermission } from '@/wxpermission'
潘琦 committed
10

11
const defaultUrl = baseUrl.authUrl
潘琦 committed
12 13
router.beforeEach((to, from, next) => {
  console.log('缓冲设置')
潘琦 committed
14
  console.log('code ===' + store.getters.code)
15 16 17 18 19 20 21 22 23 24 25 26

  // 判断链接是否被微信分享篡改,false需重定向
  if (redirectUrl()) {
    // store.dispatch('setToken', '')
    // store.dispatch('setUserId', '')
    // let accessToken = store.getters.access_token
    let storecode = store.getters.code
    if (!storecode || storecode === '') {
      let params = getParamsByUrl(window.location.href)
      if (params.code) {
        store.dispatch('setCode', params.code)
        getOauthFn(() => {
27
          // wxpermission(0, () => {})
28 29 30 31 32
          next()
        })
      } else {
        getWXOauth()
      }
潘琦 committed
33
    } else {
34
      // wxpermission(0, () => {})
35
      next()
潘琦 committed
36
    }
37 38 39 40
  } else {
    if (globalNewUrl !== '') {
      window.location.href = globalNewUrl
    }
潘琦 committed
41
  }
42
  // store.dispatch('setToken', '490cd9b7-dca7-4cc3-87e9-5eacbb5a5d77')
潘琦 committed
43 44 45 46 47 48 49 50 51 52 53
})

let getWXOauth = () => {
  console.log()
  let url = removeUrlParam('code')
  // store.dispatch('setCode', '')
  let authId = ''
  if (getParamsByUrl(window.location.href).auth_id) {
    authId = '&auth_id=' + getParamsByUrl(window.location.href).auth_id
    store.dispatch('setAuthId', getParamsByUrl(window.location.href).auth_id)
  }
54
  window.location.href = defaultUrl + '/wxoauth/route?redirect_uri=' + encodeURIComponent(url) + authId + '&auth_scene=voucher'
潘琦 committed
55 56 57
}

let getOauthFn = (callback) => {
58 59 60
  if (getParamsByUrl(window.location.href).auth_id) {
    store.dispatch('setAuthId', getParamsByUrl(window.location.href).auth_id)
  }
潘琦 committed
61 62 63 64 65 66 67 68 69 70 71 72
  let spoauthParams = {
    code: store.getters.code
  }
  getOauth(spoauthParams).then(function (res) {
    if (res.token && res.token !== '') {
      getUserByTokenFn(res.token, store.getters.code, callback)
    } else {
      store.dispatch('setCode', '')
      setTimeout(function () {
        getWXOauth()
      }, 3 * 1000)
      Toast({
73
        message: '微信token已失效!',
潘琦 committed
74 75 76
        position: 'center',
        duration: 3 * 1000
      })
潘琦 committed
77
    }
潘琦 committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  }).catch((error) => {
    console.log(error)
    Toast({
      message: '鉴权失败',
      position: 'center',
      duration: 3 * 1000
    })
    // setTimeout(function () {
    //   getWXOauth()
    // }, 3 * 1000)
  })
}

const getUserByTokenFn = (token, code, callback) => {
  let spoauthParams = {
    token: token
  }
  getUserByToken(spoauthParams).then(function (res) {
    let accessToken = ''
    let userId = ''
98 99 100 101
    let accountId = res.accountid
    if (accountId && accountId !== '') {
      store.dispatch('setAccountId', accountId)
    }
潘琦 committed
102 103 104 105 106 107 108 109 110 111 112
    if (res.ext && res.ext !== '') {
      let extOBJ = JSON.parse(res.ext)
      accessToken = extOBJ.access_token
      userId = extOBJ.user_id
    }
    if (accessToken !== '') {
      if (userId !== '') {
        store.dispatch('setToken', accessToken)
        store.dispatch('setUserId', userId)
        store.dispatch('setCode', code)
        callback()
潘琦 committed
113
      } else {
潘琦 committed
114 115 116
        setTimeout(function () {
          getWXOauth()
        }, 3 * 1000)
潘琦 committed
117
        Toast({
潘琦 committed
118
          message: 'user id不能为空!',
潘琦 committed
119
          position: 'center',
潘琦 committed
120
          duration: 3 * 1000
潘琦 committed
121 122
        })
      }
潘琦 committed
123 124 125 126
    } else {
      setTimeout(function () {
        getWXOauth()
      }, 3 * 1000)
潘琦 committed
127
      Toast({
潘琦 committed
128
        message: 'token不能为空!',
潘琦 committed
129
        position: 'center',
潘琦 committed
130
        duration: 3 * 1000
潘琦 committed
131
      })
潘琦 committed
132 133 134 135 136 137 138
    }
  }).catch((error) => {
    console.log(error)
    Toast({
      message: '鉴权失败',
      position: 'center',
      duration: 3 * 1000
潘琦 committed
139
    })
潘琦 committed
140 141 142 143 144 145
    setTimeout(function () {
      getWXOauth()
    }, 3 * 1000)
  })
}

146
let globalNewUrl = ''
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
// 判断链接是否被微信分享篡改
const redirectUrl = () => {
  const curUrl = window.location.href
  const curUrlArr = curUrl.split('?')
  // Toast({
  //   message: curUrl,
  //   position: 'center',
  //   duration: 2 * 1000
  // })
  // console.log(curUrlArr[1].indexOf('from=singlemessage'))
  if (curUrlArr.length === 3 && curUrlArr[1].indexOf('from=singlemessage') >= 0) {
    let newUrl = curUrlArr[0] + '#' + curUrlArr[1].split('#')[1] + '?' + curUrlArr[2]
    // Toast({
    //   message: curUrlArr[2],
    //   position: 'center',
    //   duration: 5 * 1000
    // })
164 165
    globalNewUrl = newUrl
    // window.location.href = newUrl
166
    return false
167 168
  } else if (curUrl.indexOf('?scene=0') >= 0) {
    let newUrl = curUrl.replace('?scene=0', '')
169
    globalNewUrl = newUrl
170
    return false
171
  } else if (curUrl.indexOf('/voucher/?') >= 0) {
172 173 174
    const urlArr = curUrl.split('/voucher/?')
    const locationStr = urlArr[0]
    const pathArr = urlArr[1].split('#')
175
    let newUrl = locationStr + '/voucher/#' + pathArr[1]
176
    globalNewUrl = newUrl
177
    return false
178 179 180 181 182
  } else {
    return true
  }
}

潘琦 committed
183 184 185 186
// const nextToPath = (to, from, next) => {
//   let toquery = to.query
//   if (from.query.code) {
//     toquery.code = from.query.code
潘琦 committed
187
//   }
潘琦 committed
188 189 190 191 192
//   next({
//     path: to.path,
//     query: toquery
//   })
// }