Browse Source

登录逻辑优化

dmy 5 years ago
parent
commit
45696a81c3
3 changed files with 46 additions and 56 deletions
  1. 1 5
      _src/api/http.js
  2. 3 39
      _src/components/chatMini/chatMini.vue
  3. 42 12
      _src/store/actions.js

+ 1 - 5
_src/api/http.js

@@ -1,7 +1,6 @@
 import axios from 'axios'
 import Qs from 'qs'
 import { MessageBox } from 'element-ui'
-// import { Cookie } from '../util/util'
 import $store from '@/store'
 
 let host = ''
@@ -51,10 +50,7 @@ ax.interceptors.response.use(
       return Promise.resolve(response)
     } else {
       if (response.data.code === -5) { // code为-5是登录态失效
-        $store.dispatch('resetGameLogin').then(() => {
-          // 重新登录成功话刷新页面
-          location.reload()
-        })
+        $store.dispatch('resetGameLogin')
       } else {
         // 请求出错提示错误
         MessageBox.confirm(response.data.msg, 'Error', {

+ 3 - 39
_src/components/chatMini/chatMini.vue

@@ -158,7 +158,6 @@ import chatAt from '@/components/chatAt'
 import atMe from '@/components/chatAt/atme'
 import chatPin from '@/components/chatPin'
 import { mapActions, mapState, mapMutations } from 'vuex'
-import API from '@/api'
 import { getMiniWsUrl } from '@/util/contract.js'
 import { isMobile, scrollMsgIntoView } from '@/util/util.js'
 import WsManager from '@/util/wsManager.js'
@@ -355,7 +354,7 @@ export default {
       'getHistoryMsg',
       'doSendMsg',
       'doSendFile',
-      'doParentLogin'
+      'doContractLogin'
     ]),
     handleMoreClick () {
       this.showEmoji = false
@@ -547,19 +546,9 @@ export default {
         // scatter登录
         await this.getParentIdentity()
         if (!this.account) return
-
         // 合约登录
-        let { data } = await API.user.getRandom2({ account: this.account.name })
-        let random = data.data.random
-        let accessToken = data.data.accessToken
-
-        let trxId = await this.doParentLogin([this.account.name, random])
-        let param = {
-          account: this.account.name,
-          access_token: accessToken,
-          trx_id: trxId
-        }
-        this.eosLogin(param)
+        await this.doContractLogin()
+        location.replace(location.href.replace('show=false', 'show=true'))
       } catch (msg) {
         if (msg.type) {
           // scatter报错的情况
@@ -574,31 +563,6 @@ export default {
         this.showLoginBtn = true
       }
     },
-    async eosLogin (param) {
-      let { data } = await API.user.eosLogin2(param)
-      if (data.result === 1) {
-        let userId = data.data.user_id
-        let token = data.data.token
-        localStorage.setItem('user_id', userId)
-        localStorage.setItem('token', token)
-
-        location.replace(location.href.replace('show=false', 'show=true'))
-        //
-        //        this.setUserId(userId)
-        //        this.setToken(token)
-        //
-        //        // 设置groupId
-        //        this.initGroup({
-        //          userId,
-        //          groupId: this.groupId,
-        //          useCache: false
-        //        })
-        //
-        //        this.showLoginBtn = false
-        //
-        //        await this.getUserInfo()
-      }
-    },
     /**
      * 聊天窗体滚动到底部
      */

+ 42 - 12
_src/store/actions.js

@@ -188,24 +188,54 @@ export const actions = {
   resetGameLogin ({ dispatch, commit, state }, params) {
     localStorage.removeItem('user_id')
     localStorage.removeItem('token')
-    dispatch('doGameLogin')
+    dispatch('doContractLogin')
   },
   /**
-   * 内嵌版本调起合约登录
+   * 合约登录, 生成业务登录态token
 	 * @param {Array} params
    */
-  async doParentLogin ({ dispatch, commit, state }, params) {
+  async doContractLogin ({ commit, state }, params) {
     try {
+      // 合约登录
+      let { data } = await API.user.getRandom2({ account: state.account.name })
+      let random = data.data.random
+      let accessToken = data.data.accessToken
       let identity = { authorization: [`${state.account.name}@${state.account.authority}`] }
-      let trx = await window.postMessager.send({
-        action: 'meechat:eosAction',
-        data: {
-          contract: 'meechatadmin',
-          action: 'login',
-          param: params.concat(identity)
-        }
-      })
-      return Promise.resolve(trx.transaction_id)
+
+      let trx = null
+      if (self !== top) {
+        trx = await window.postMessager.send({
+          action: 'meechat:eosAction',
+          data: {
+            contract: 'meechatadmin',
+            action: 'login',
+            param: [state.account.name, random, identity]
+          }
+        })
+      } else {
+        let contract = await window.EOS.contract('meechatadmin')
+        trx = await contract.login(state.account.name, random, identity)
+      }
+
+      let param = {
+        account: state.account.name,
+        access_token: accessToken,
+        trx_id: trx.transaction_id
+      }
+
+      let res = await API.user.eosLogin2(param)
+      if (res.data.result === 1) {
+        let userId = res.data.data.user_id
+        let token = res.data.data.token
+
+        localStorage.setItem('user_id', userId)
+        localStorage.setItem('token', token)
+
+        commit('setUserId', userId)
+        commit('setToken', token)
+
+        return Promise.resolve(res.data)
+      }
     } catch (error) {
       return Promise.reject(error)
     }