新版本的MetaMask 出于隐私目的,需求请求用户授权,代码如下, 关键代码如下:
        if (window.ethereum) {
            // 请求用户授权
            window.ethereum.enable(function(){
                web3.eth.getAccounts((error, accounts) => {
                    web3.eth.defaultAccount = accounts[0];
                    console.log(accounts)
                })
            })
        } else  if (typeof web3 !== 'undefined') {
            web3 = new Web3(web3.currentProvider);
            web3.eth.defaultAccount = web3.eth.accounts[0];
            console.log(web3.eth.defaultAccount);
        } else {
            // set the provider you want from Web3.providers
            web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
        }