topMain.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <template>
  2. <view class="header-content flex-column">
  3. <view class="head-title flex-left justify-between">
  4. <view style="width: 94rpx">
  5. <u-icon
  6. v-if="false"
  7. name="arrow-left"
  8. color="#000E08"
  9. size="42rpx"
  10. @click="goBack"
  11. bold
  12. ></u-icon>
  13. <view v-else></view>
  14. </view>
  15. <view>{{ title }}</view>
  16. <image
  17. src="@/static/img/icon-switch.svg"
  18. style="width: 90rpx; height: 90rpx"
  19. ></image>
  20. </view>
  21. <view
  22. class="flex-row items-center"
  23. :style="back ? 'padding: 0 28rpx;' : ''"
  24. >
  25. <view style="padding-right: 20rpx" @click="goMy">
  26. <image
  27. :class="back ? 'max-head' : 'min-head'"
  28. src="@/static/img/head.png"
  29. ></image>
  30. </view>
  31. <!-- <view class="user-box">{{ address }}</view> -->
  32. <!-- <view class="wallet-box" @click="modal.open()">连接钱包</view> -->
  33. <view class="user-box" @click="address ? copyToClipboard() : modal.open()">
  34. {{ address ? formattedAddress : '连接钱包' }}
  35. </view>
  36. <view class="wallet-box" @click="address ? disconnect() : null" :class="{ disabled: !address }">
  37. {{ address ? '断开钱包' : '未链接钱包或未成为VIP只可观看3分钟' }}
  38. </view>
  39. </view>
  40. </view>
  41. </template>
  42. <script setup>
  43. import { ref,computed,onMounted } from "vue";
  44. // const { address } = useAccount();
  45. onMounted(() => {
  46. disconnect();
  47. });
  48. const formattedAddress = computed(() => {
  49. if (!address.value) return '';
  50. return `${address.value.slice(0, 4)}****${address.value.slice(-4)}`;
  51. });
  52. // 添加 copyToClipboard 方法
  53. const copyToClipboard = () => {
  54. const textToCopy = address.value;
  55. navigator.clipboard.writeText(textToCopy).then(() => {
  56. uni.showToast({
  57. title: '复制成功',
  58. icon: 'success'
  59. });
  60. }).catch(err => {
  61. console.error("Failed to copy: ", err);
  62. uni.showToast({
  63. title: '复制失败',
  64. icon: 'none'
  65. });
  66. });
  67. };
  68. const props = defineProps({
  69. back: {
  70. type: Boolean,
  71. default: false,
  72. },
  73. title: {
  74. type: Boolean,
  75. default: "首页",
  76. },
  77. });
  78. // 点击头像后跳转个人中心
  79. const goMy = () => {
  80. if (!props.back) {
  81. uni.navigateTo({
  82. url: "/pages/my/index",
  83. });
  84. }
  85. };
  86. const goBack = () => {
  87. uni.navigateBack();
  88. };
  89. //wallet
  90. import {
  91. arbitrum,
  92. mainnet
  93. } from '@wagmi/core/chains'
  94. import {
  95. createWeb3Modal,
  96. defaultWagmiConfig,
  97. useWeb3Modal,
  98. useWeb3ModalEvents,
  99. useWeb3ModalState,
  100. useWeb3ModalTheme
  101. } from '@web3modal/wagmi/vue'
  102. import {
  103. chains,
  104. projectId,
  105. wagmiConfig
  106. } from "../utils/config.js";
  107. // 3. Create modal
  108. createWeb3Modal({
  109. wagmiConfig,
  110. projectId,
  111. chains,
  112. themeMode: 'light'
  113. })
  114. import {
  115. useAccount,
  116. useBalance,
  117. useDisconnect,
  118. useReadContract,
  119. useSignMessage,
  120. useWriteContract
  121. } from "@wagmi/vue";
  122. import ERC20 from '../contract/abi/ERC20.json'
  123. import {
  124. ChainId,
  125. USDT_ADDRESS
  126. } from "@/contract/address";
  127. import {
  128. formatUnits,
  129. parseUnits
  130. } from "viem";
  131. const {
  132. signMessageAsync
  133. } = useSignMessage()
  134. const {
  135. disconnect
  136. } = useDisconnect()
  137. // 4. Use modal composable 。
  138. // 注意在使用“useWeb3Mode”compositable之前,请调用“createWeb3Model”
  139. const modal = useWeb3Modal()
  140. const {
  141. address,
  142. chainId,
  143. isConnected
  144. } = useAccount()
  145. //签名
  146. async function sign() {
  147. const signature = await signMessageAsync({
  148. message: 'gm wagmi frens'
  149. })
  150. console.log(signature)
  151. }
  152. // function getBalance(){
  153. // console.log(isConnected.value,chainId.value,address.value)
  154. // // 读取合约数据
  155. // const usdtBalance = useReadContract({
  156. // abi:ERC20,
  157. // address: USDT_ADDRESS[chainId.value],
  158. // functionName: 'balanceOf',
  159. // args: [address.value],
  160. // })
  161. // console.log(usdtBalance)
  162. // }
  163. // 获取原生代币余额
  164. const {
  165. data: balance
  166. } = useBalance({
  167. address
  168. })
  169. // 获取 USDT 余额
  170. const {
  171. data: usdtBalance
  172. } = useReadContract({
  173. abi: ERC20,
  174. address: USDT_ADDRESS[ChainId.BSC],
  175. functionName: 'balanceOf',
  176. args: [address],
  177. })
  178. // 获取 USDT授权某个合约额度 直接转账transfer不需要授权 调用合约扣除自己代币的需要先授权
  179. // 授权方法 approve 参数 合约地址 数量(一般MaxUint256)具体看 abi erc20
  180. // export const MaxUint256: bigint = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
  181. const {
  182. data: allowance
  183. } = useReadContract({
  184. abi: ERC20,
  185. address: USDT_ADDRESS[ChainId.BSC],
  186. functionName: 'allowance',
  187. //自己地址 合约地址
  188. args: [address, address],
  189. })
  190. const {
  191. writeContractAsync
  192. } = useWriteContract()
  193. defineExpose({
  194. transfer
  195. })
  196. async function transfer(tox) {
  197. console.log('tox====',tox);
  198. if (!address.value) {
  199. uni.showToast({
  200. title: '请先连接钱包',
  201. icon: 'none',
  202. duration: 2000
  203. });
  204. return;
  205. }
  206. //转给谁
  207. // const to = "0x6A6374400CBf4A30fEA48AE3DD12F7b9562288fd"
  208. const to = "0x9e4caaf0525eeb2F92Ab985AacC14B9C8BcfDB82"//小明的钱包
  209. //转多少 这里以 1 个 USDT 举例 因为区块链没有小数 USDT 的代币精度是 18 1个 USDT 在区块链上表示为 1*10**18
  210. //const value = parseUnits("1", 18) //库封装了方法
  211. const value = parseUnits(tox, 18) //库封装了方法
  212. const res = await writeContractAsync({
  213. abi: ERC20,
  214. address: USDT_ADDRESS[ChainId.MATCH],
  215. functionName: 'transfer',
  216. args: [
  217. to,
  218. value
  219. ],
  220. })
  221. console.log(res)
  222. }
  223. </script>
  224. <style lang="less" scoped>
  225. .header-content {
  226. .user-box {
  227. width: 276rpx;
  228. height: 50rpx;
  229. display: flex;
  230. align-items: center;
  231. justify-content: center;
  232. border-radius: 34rpx;
  233. font-weight: bold;
  234. font-size: 30rpx;
  235. color: #1f1f1f;
  236. background-color: #6ae199;
  237. // text-align: center;
  238. // padding: 10rpx; /* 增加内边距以更好地显示文本 */
  239. }
  240. .wallet-box {
  241. width: 200rpx; /* 调整总长度 */
  242. height: auto; /* 让高度自适应内容 */
  243. display: flex;
  244. align-items: center;
  245. justify-content: center;
  246. border-radius: 34rpx;
  247. font-weight: bold;
  248. font-size: 26rpx;
  249. color: #6c6c6c;
  250. background-color: #dadada;
  251. text-align: center;
  252. margin-left: 36rpx;
  253. padding: 10rpx; /* 增加内边距以更好地显示文本 */
  254. word-break: break-word; /* 允许换行 */
  255. white-space: normal; /* 确保文本正常换行 */
  256. }
  257. .wallet-box.disabled {
  258. pointer-events: none; /* 禁止点击 */
  259. opacity: 0.6; /* 使不可点击的按钮看起来像禁用的 */
  260. }
  261. }
  262. .min-head {
  263. width: 120rpx;
  264. height: 120rpx;
  265. }
  266. .max-head {
  267. width: 160rpx;
  268. height: 160rpx;
  269. }
  270. </style>