1.导入web3 和 crypto-js
package.json 中dependencies 下新增:
"dependencies": {
"node-xlsx": "^0.17.2",
"web3": "^1.10.0",
"crypto-js": "^4.0.0"
}
ts 文件中使用:
import Web3min from "web3/dist/web3.min.js";
const ethereum = (window as any).ethereum
const web3 = new Web3min(ethereum)
调用方法:
web3.eth.sendTransaction
由于cocos的限制只通过min.js的方法引入的,所以不可自动联想代码,可以通过
new Web3().eth.sendTransaction 先看到函数参数,再换成web3即可
crypto-js使用
import CryptoJS from 'crypto-js'
private encrypt(data: string) {
return CryptoJS.AES.encrypt(data, SECRET_KEY).toString()
}
private decrypt(data: string) {
const bytes = CryptoJS.AES.decrypt(data, SECRET_KEY)
return bytes.toString(CryptoJS.enc.Utf8)
}
2.导入axios
由于axios在cocos 中也会出现导入问题,所以直接采用拷贝axios.min.js的方式
先在package.json 中dependencies 中下载下来
"dependencies": {
"axios": "^0.19"
}
导入有2种方式:
方式一 直接导入
import axios from "axios/dist/axios.min.js";
const as = axios.create({
如果不行可使用方式二:
下载下来以后在node_modules中即可找到axios.min.js,只拷贝这个文件到项目中script文件夹即可
使用方式:
import axios from "../axios.min.js";
const as = axios.create({
baseURL: 'http://xxx',
timeout: 10000,
headers: {
'Content-Type': 'application/json',
}
})
//拦截器:
as.interceptors
//请求:
as.get
as.post
...