topMain.vue 7.4 KB

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