Explorar el Código

tron,eth登录绑定

baichun hace 5 años
padre
commit
9359f017d6

+ 17 - 0
_src/api/modules/user.js

@@ -69,6 +69,23 @@ export default {
     })
   },
   /**
+   * Tron登录
+   */
+  tronLogin (params) {
+    return axios.post('user/tronLogin', params)
+  },
+  /**
+   * Tron绑定
+   */
+  tronBind (data) {
+    return axios.request({
+      url: 'user/tronBind',
+      method: 'post',
+      needLogin: true,
+      data
+    })
+  },
+  /**
    * 解绑账号
    */
   unBind (data) {

BIN
_src/components/login/img/icon-eth.png


+ 2 - 2
_src/components/login/loginBox.vue

@@ -9,13 +9,13 @@
 
 <script>
 import { mapState } from 'vuex'
+// import meeAccount from '@/components/login/meeAccount'
 import otherAccount from '@/components/login/otherAccount'
 
-// Vue.component('mee-account', () => import('@/components/login/meeAccount'))
-
 export default {
   name: 'loginBox',
   components: {
+    // meeAccount,
     otherAccount
   },
   created () {

+ 27 - 9
_src/components/login/otherAccount.vue

@@ -8,14 +8,14 @@
             <i class="icon icon-meetone"></i>
             <p>Meetone</p>
         </li>
-        <!-- <li @click="loginSimpleWallet">
-            <i class="icon icon-meetone"></i>
-            <p>SimpleWallet</p>
-        </li> -->
-        <!-- <li>
+        <li @click="loginEth" v-if="isShowEth">
+            <i class="icon icon-eth"></i>
+            <p>ETH</p>
+        </li>
+        <li @click="loginTron" v-if="isShowTron">
             <i class="icon icon-tron"></i>
             <p>TRON</p>
-        </li> -->
+        </li>
         <li @click="loginTelegram" v-if="!inWallet">
             <i class="icon icon-tele"></i>
             <p>Telegram</p>
@@ -60,6 +60,21 @@ export default {
 </script>
 
 <style lang="scss" scoped>
+.f-row{
+  width: 212px;
+  padding-left: 85px;
+  border-bottom: 1px solid rgba(#000,0.1);
+  margin: 20px auto 0;
+  input{
+    width: 212px;
+    height: 54px;
+    line-height: 54px;
+    border: 0;
+    font-size: 14px;
+    outline: none;
+    background: transparent;
+  }
+}
 .sign-list{
   display: flex;
   // width: 400px;
@@ -78,14 +93,17 @@ export default {
   p{
     color: #333333;
     font-size: 12px;
-    margin-top: 20px;
+    margin-top: 16px;
   }
   .icon{
     display: inline-block;
-    width: 60px;
-    height: 60px;
+    width: 45px;
+    height: 45px;
     border-radius: 50%;
   }
+  .icon-eth{
+    background: url(./img/icon-eth.png) center/60% #ffffff no-repeat;
+  }
   .icon-eos{
     background: url(./img/icon-eos.png) center #ffffff no-repeat;
   }

+ 5 - 0
_src/components/login/package.json

@@ -0,0 +1,5 @@
+{
+  "dependencies": {
+    "ethjs": "^0.4.0"
+  }
+}

+ 1 - 3
_src/components/popup/avatar/index.vue

@@ -49,12 +49,10 @@ export default {
   computed: {
     uploadUrl () {
       let host = ''
-      let userId = localStorage.getItem('user_id')
-      let token = localStorage.getItem('token')
       if (window.location.port === '8080') {
         host = '//test.mee.chat/'
       }
-      return this.isMe ? `${host}user/changePhoto?user_id=${userId}&token=${token}` : `${host}group/changeCover?user_id=${userId}&token=${token}`
+      return this.isMe ? `${host}user/changePhoto` : `${host}group/changeCover`
     },
     uploadData () {
       let authData = {

+ 7 - 1
_src/components/popup/userInfo/index.vue

@@ -49,7 +49,7 @@
                 </div>
               </div>
               <p v-if="item.account" class="key">{{item.account}}</p>
-              <el-button @click="bindAccount(item.type)" :disabled="item.type == 'eth' || item.type == 'tron'" v-else>{{$t('userinfo.bind')}}</el-button>
+              <el-button @click="bindAccount(item.type)" v-else>{{$t('userinfo.bind')}}</el-button>
             </div>
           </div>
           <!-- <button class="send-msg-btn">发消息</button> -->
@@ -112,6 +112,12 @@ export default {
         case 'tg':
           this.bindTg()
           break
+        case 'eth':
+          this.bindEth()
+          break
+        case 'tron':
+          this.bindTron()
+          break
         case 'meetone':
           this.bindEos('meetone')
           break

+ 109 - 19
_src/mixins/login.js

@@ -2,10 +2,11 @@ import api from '@/api'
 import { showError, getUrlParam, getMeechatType, openBlankWindow, getUserOpt, setUserOpt } from '@/util/util'
 import { mapActions, mapState, mapMutations } from 'vuex'
 import ScatterJS from 'scatter-js/dist/scatter.esm'
+import Web3 from 'web3'
 import { Message } from 'element-ui'
 import PostMessager from '@/util/postMessager.js'
 
-let _timeoutHandler = 0
+let _timeoutHandler = 0 // 轮询定时器
 
 // 账号登录
 export const accountLoginMixin = {
@@ -15,7 +16,9 @@ export const accountLoginMixin = {
       winHandler: null, // 新开窗口
       winTimer: null, // 检测窗口定时器
       meechatType: getMeechatType(),
-      from: decodeURIComponent(getUrlParam('from'))
+      from: decodeURIComponent(getUrlParam('from')),
+      isShowEth: true, // 是否显示eth登录
+      isShowTron: true // 是否显示Tron登录
     }
   },
   computed: {
@@ -24,12 +27,10 @@ export const accountLoginMixin = {
   methods: {
     ...mapMutations(['setLogining']),
     ...mapActions([
-      'getUserInfo',
       'setScatter',
       'setAccount',
       'doScatterLogin',
       'doContractLogin',
-      'doScatterLogout',
       'initSocket'
     ]),
     /**
@@ -58,9 +59,10 @@ export const accountLoginMixin = {
           await this.loginEosMeetoneCommon(this.curLoginType)
           this.setLogining(false)
         } else {
-          this.$store.commit('setUserId', userId)
-          this.$store.commit('setToken', token)
-          this.loginSuccCallBack()
+          this.loginSuccCallBack({
+            userId: userId,
+            token: token
+          })
         }
 
         return true
@@ -160,8 +162,11 @@ export const accountLoginMixin = {
 
               // 签名登录
               try {
-                await this.doContractLogin(2)
-                this.loginSuccCallBack()
+                let ctLoginRes = await this.doContractLogin(2)
+                this.loginSuccCallBack({
+                  userId: ctLoginRes.data.user_id,
+                  token: ctLoginRes.data.token
+                })
               } catch (e) {
                 reject(e)
                 let err = e
@@ -202,6 +207,77 @@ export const accountLoginMixin = {
       })
     },
     /**
+     * @des Eth登录 MetaMask
+     */
+    async loginEth () {
+      this.curLoginType = 'eth'
+      this.setLogining(true)
+      if (window.ethereum) {
+        window.web3 = new Web3(window.ethereum)
+        try {
+          await window.ethereum.enable()
+          this.getEthAccount()
+        } catch (error) {
+        }
+      } else if (window.web3) {
+        window.web3 = new Web3(window.web3.currentProvider)
+        this.getEthAccount()
+      }
+    },
+    /**
+     * @des Eth登录 MetaMask-获取当前账号
+     */
+    async getEthAccount () {
+      let accounts = await window.web3.eth.getAccounts()
+      let account = accounts[0]
+
+      if (account) {
+        let randomRes = await api.user.getRandom2({ account: account })
+        let str = window.web3.utils.sha3(randomRes.data.data.random)
+
+        window.web3.eth.sign(str, account, async (err, rs) => {
+          let resutlSign = rs
+
+          let { data } = await api.user.ethLogin({
+            account: account,
+            sign: resutlSign
+          })
+          this.loginSuccCallBack({
+            userId: data.data.user_id,
+            token: data.data.token
+          })
+          console.log(err)
+        })
+      }
+    },
+    /**
+     * @des Tron登录 TronLink
+     */
+    async loginTron () {
+      this.curLoginType = 'tron'
+      this.setLogining(true)
+      let account = window.tronWeb.defaultAddress.base58
+      let randomRes = await api.user.getRandom2({ account: account })
+      let s = window.tronWeb.sha3(randomRes.data.data.random)
+      let resutlSign = await window.tronWeb.trx.signMessage(s)
+
+      let rs = await window.tronWeb.trx.verifyMessage(s, resutlSign, account)
+
+      if (rs) {
+        let { data } = await api.user.tronLogin({
+          account: account,
+          sign: resutlSign
+        })
+
+        this.loginSuccCallBack({
+          userId: data.data.user_id,
+          token: data.data.token
+        })
+      } else {
+        this.setLogining(false)
+      }
+    },
+    /**
      * @des SimpleWallet登录
     */
     loginSimpleWallet () {
@@ -267,15 +343,10 @@ export const accountLoginMixin = {
       }
 
       if (isLoginSuccess) {
-        let userId = res.data.data.user_id
-        let token = res.data.data.token
-
-        localStorage.setItem('user_id', userId)
-        localStorage.setItem('token', token)
-
-        this.$store.commit('setUserId', userId)
-        this.$store.commit('setToken', token)
-        this.loginSuccCallBack()
+        this.loginSuccCallBack({
+          userId: res.data.data.user_id,
+          token: res.data.data.token
+        })
 
         if (this.winHandler != null) {
           this.winHandler.close()
@@ -289,9 +360,18 @@ export const accountLoginMixin = {
         }, 1000)
       }
     },
-    async loginSuccCallBack () {
+    async loginSuccCallBack ({ userId = null, token = null }) {
       this.setLogining(false)
+
       setUserOpt('loginType', this.curLoginType)
+      if (userId) {
+        this.$store.commit('setUserId', userId)
+        localStorage.setItem('user_id', userId)
+      }
+      if (token) {
+        this.$store.commit('setToken', token)
+        localStorage.setItem('token', token)
+      }
 
       switch (this.meechatType) {
         // 内嵌版
@@ -329,5 +409,15 @@ export const accountLoginMixin = {
       this.postMessager = new PostMessager('*', { callback })
       window.postMessager = this.postMessager
     }
+
+    // eth初始化
+    if (!window.ethereum || !window.web3) {
+      this.isShowEth = false
+    }
+
+    // tron初始化
+    if (!window.tronWeb) {
+      this.isShowTron = false
+    }
   }
 }

+ 70 - 0
_src/mixins/user.js

@@ -3,6 +3,7 @@ import API from '@/api'
 import User from '@/store/db/User.js'
 import { setUserOpt, confirmPopup, openBlankWindow, getMeechatType, showError } from '@/util/util'
 import ScatterJS from 'scatter-js/dist/scatter.esm'
+import Web3 from 'web3'
 
 export const otherInfoMixins = {
   data () {
@@ -98,6 +99,7 @@ export const bindAccountMixins = {
       'doScatterBind',
       'getUserInfo'
     ]),
+    // eos/meetone绑定
     async bindEos (eosType) {
       setUserOpt('eosType', eosType)
       this.isLoading = true
@@ -122,6 +124,7 @@ export const bindAccountMixins = {
         }
       })
     },
+    // telegram绑定
     async bindTg () {
       this.winHandler = openBlankWindow('')
       let { data } = await API.user.tgCSRF({
@@ -138,6 +141,73 @@ export const bindAccountMixins = {
       }, 800)
       this.bindCheck(data.data.csrf_token)
     },
+    // eth绑定
+    async bindEth () {
+      if (!window.ethereum || !window.web3) {
+        console.log('你还没有安装eth插件')
+        return
+      }
+
+      if (window.ethereum) {
+        window.web3 = new Web3(window.ethereum)
+        try {
+          await window.ethereum.enable()
+          this.getEthAccount()
+        } catch (error) {
+        }
+      } else if (window.web3) {
+        window.web3 = new Web3(window.web3.currentProvider)
+        this.getEthAccount()
+      }
+    },
+    /**
+     * @des Eth登录 MetaMask-获取当前账号
+     */
+    async getEthAccount () {
+      let accounts = await window.web3.eth.getAccounts()
+      let account = accounts[0]
+
+      if (account) {
+        let randomRes = await API.user.getRandom2({ account: account })
+        let str = window.web3.utils.sha3(randomRes.data.data.random)
+
+        window.web3.eth.sign(str, account, async (err, rs) => {
+          let resutlSign = rs
+
+          await API.user.ethBind({
+            account: account,
+            sign: resutlSign
+          })
+          this.getUserInfo()
+          console.log(err)
+        })
+      }
+    },
+    // tron绑定
+    async bindTron () {
+      if (!window.tronWeb) {
+        console.log('你还没有安装tron插件')
+        return
+      }
+
+      let account = window.tronWeb.defaultAddress.base58
+      let randomRes = await API.user.getRandom2({ account: account })
+      let s = window.tronWeb.sha3(randomRes.data.data.random)
+      let resutlSign = await window.tronWeb.trx.signMessage(s)
+
+      let rs = await window.tronWeb.trx.verifyMessage(s, resutlSign, account)
+
+      if (rs) {
+        await API.user.tronBind({
+          account: account,
+          sign: resutlSign
+        })
+
+        this.getUserInfo()
+      } else {
+        this.setLogining(false)
+      }
+    },
     async bindCheck (uuID, times) {
       this.isLoading = true
       if (times === 0) {

+ 5 - 0
_src/package.json

@@ -0,0 +1,5 @@
+{
+  "dependencies": {
+    "ethjs": "^0.4.0"
+  }
+}

+ 1 - 1
_src/pages/h5/view/aboutMe.vue

@@ -25,7 +25,7 @@
             <p>{{item.account || $t('h5.notBind')}}</p>
           </div>
           <div class="btn-unbind" v-if="item.account" @click.stop="unbindAccount(item.type)">{{$t('userinfo.unbind')}}</div>
-          <el-button @click="bindAccount(item.type)" :disabled="item.type == 'eth' || item.type == 'tron'" v-else>{{$t('userinfo.bind')}}</el-button>
+          <el-button @click="bindAccount(item.type)" v-else>{{$t('userinfo.bind')}}</el-button>
           <div class="opetate" v-if="item.account" @click.stop="hanldeChange(item)">
             <i :class="item.is_visible === 0 ? 'unopen' : 'open'"></i>
             <p>{{item.is_visible === 0 ? $t('userinfo.private') : $t('userinfo.public')}}</p>

+ 1 - 0
_src/pages/h5/view/login.vue

@@ -37,6 +37,7 @@ export default {
     }
   },
   mounted () {
+    this.checkLocalLogin()
     if (localStorage.getItem('user_id') && localStorage.getItem('token')) {
       this.$router.replace({ path: '/' })
     } else {

+ 3 - 0
_src/store/mutations.js

@@ -76,5 +76,8 @@ export const mutations = {
       // 更新人数
       data.isJoin > 0 ? ++state.hotList[index].member_num : --state.hotList[index].member_num
     }
+  },
+  changeUserBinds () {
+
   }
 }