permission.js 6.12 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
  let storecode = store.getters.code
  if (!storecode || storecode === '') {
    let params = getParamsByUrl(window.location.href)
18
    if (params.code && params.code !== 'undefined') {
19 20 21 22
      store.dispatch('setCode', params.code)
      getOauthFn(() => {
        next()
      })
潘琦 committed
23
    } else {
24
      getWXOauth()
潘琦 committed
25
    }
26
  } else {
27
    next()
潘琦 committed
28
  }
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  // 判断链接是否被微信分享篡改,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(() => {
  //         // wxpermission(0, () => {})
  //         next()
  //       })
  //     } else {
  //       getWXOauth()
  //     }
  //   } else {
  //     // wxpermission(0, () => {})
  //     next()
  //   }
  // } else {
  //   if (globalNewUrl !== '') {
  //     window.location.href = globalNewUrl
  //   }
  // }
55
  // store.dispatch('setToken', '490cd9b7-dca7-4cc3-87e9-5eacbb5a5d77')
潘琦 committed
56 57
})

58
let getWXOauth = () => {
潘琦 committed
59 60 61 62 63 64 65
  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)
  }
66
  // window.location.reload(defaultUrl + '/wxoauth/route?redirect_uri=' + encodeURIComponent(url) + authId + '&auth_scene=voucher&v=' + timeStamp)
67
  window.location.href = defaultUrl + '/wxoauth/route?redirect_uri=' + encodeURIComponent(url) + authId + '&auth_scene=voucher'
潘琦 committed
68 69 70
}

let getOauthFn = (callback) => {
71 72 73
  if (getParamsByUrl(window.location.href).auth_id) {
    store.dispatch('setAuthId', getParamsByUrl(window.location.href).auth_id)
  }
潘琦 committed
74 75 76 77 78 79 80 81 82
  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 () {
83 84 85
        getWXOauth(() => {
          callback()
        })
潘琦 committed
86 87
      }, 3 * 1000)
      Toast({
88
        message: '微信token已失效!',
潘琦 committed
89 90 91
        position: 'center',
        duration: 3 * 1000
      })
潘琦 committed
92
    }
潘琦 committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  }).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 = ''
113 114 115
    let accountId = res.accountid
    if (accountId && accountId !== '') {
      store.dispatch('setAccountId', accountId)
116 117 118 119 120 121
    } else {
      Toast({
        message: 'account id不能为空!',
        position: 'center',
        duration: 3 * 1000
      })
122
    }
潘琦 committed
123 124 125 126 127 128 129 130 131 132 133
    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
134
      } else {
潘琦 committed
135
        setTimeout(function () {
136 137 138
          getWXOauth(() => {
            callback()
          })
潘琦 committed
139
        }, 3 * 1000)
潘琦 committed
140
        Toast({
潘琦 committed
141
          message: 'user id不能为空!',
潘琦 committed
142
          position: 'center',
潘琦 committed
143
          duration: 3 * 1000
潘琦 committed
144 145
        })
      }
潘琦 committed
146 147
    } else {
      setTimeout(function () {
148 149 150
        getWXOauth(() => {
          callback()
        })
潘琦 committed
151
      }, 3 * 1000)
潘琦 committed
152
      Toast({
潘琦 committed
153
        message: 'token不能为空!',
潘琦 committed
154
        position: 'center',
潘琦 committed
155
        duration: 3 * 1000
潘琦 committed
156
      })
潘琦 committed
157 158 159 160 161 162 163
    }
  }).catch((error) => {
    console.log(error)
    Toast({
      message: '鉴权失败',
      position: 'center',
      duration: 3 * 1000
潘琦 committed
164
    })
潘琦 committed
165
    setTimeout(function () {
166 167 168
      getWXOauth(() => {
        callback()
      })
潘琦 committed
169 170 171 172
    }, 3 * 1000)
  })
}

173
// let globalNewUrl = ''
174
// 判断链接是否被微信分享篡改
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
// 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
//     // })
//     globalNewUrl = newUrl
//     // window.location.href = newUrl
//     return false
//   } else if (curUrl.indexOf('?scene=0') >= 0) {
//     let newUrl = curUrl.replace('?scene=0', '')
//     globalNewUrl = newUrl
//     return false
//   } else if (curUrl.indexOf('/voucher/?') >= 0) {
//     const urlArr = curUrl.split('/voucher/?')
//     const locationStr = urlArr[0]
//     const pathArr = urlArr[1].split('#')
//     let newUrl = locationStr + '/voucher/#' + pathArr[1]
//     globalNewUrl = newUrl
//     return false
//   } else {
//     return true
//   }
// }
209

潘琦 committed
210 211 212 213
// const nextToPath = (to, from, next) => {
//   let toquery = to.query
//   if (from.query.code) {
//     toquery.code = from.query.code
潘琦 committed
214
//   }
潘琦 committed
215 216 217 218 219
//   next({
//     path: to.path,
//     query: toquery
//   })
// }