header.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <template>
  2. <div class="header">
  3. <img class="logo" src="../static/img/logo.svg" alt="DeNet" />
  4. <div class="operation">
  5. <div class="login" @click="login" v-if="loginStatus">
  6. <img class="add" src="../static/img/header-add.svg" alt="" />
  7. <span>Create NFTs</span>
  8. </div>
  9. <div class="login disabled" v-else>
  10. <img class="add loading" src="../static/img/header-loading.svg" alt="" />
  11. <span>Create NFTs</span>
  12. </div>
  13. <div class="down" @click="install">
  14. <div class="text">Install DeNet</div>
  15. </div>
  16. </div>
  17. </div>
  18. <div class="header-place"></div>
  19. </template>
  20. <script lang="ts" setup>
  21. import Api from '../static/http/api'
  22. import storage from 'good-storage'
  23. import { postRequest } from '../static/http'
  24. import { getOauthUrl, createWindow, callBackUrl } from '../static/utils'
  25. import { getStorage, removeStorage, setStorage, storageKey } from '../static/utils/storage'
  26. import { ref } from 'vue'
  27. import { ElMessage } from 'element-plus'
  28. import { Report } from '../static/report'
  29. import { businessType, pageSource, objectType } from '../static/report/enum'
  30. const timer = ref(0)
  31. const loginStatus = ref(true)
  32. const install = () => {
  33. window.open(`https://chrome.google.com/webstore/detail/denet/inlfbeejfdgkknpiodhemfcokbdgofja`);
  34. // Report
  35. Report({
  36. baseInfo: {
  37. pageSource: pageSource.homePage,
  38. },
  39. params: {
  40. eventData: {
  41. businessType: businessType.buttonClick,
  42. objectType: objectType.installDenetButton,
  43. }
  44. }
  45. })
  46. }
  47. const checkInstall = () => {
  48. return new Promise((resolve, reject) => {
  49. // chrome-extension://inlfbeejfdgkknpiodhemfcokbdgofja/img/icon-denet-logo.svg
  50. let dom = document.querySelector('#denet_message');
  51. if (dom) {
  52. resolve(true)
  53. } else {
  54. reject(false)
  55. }
  56. })
  57. }
  58. const login = () => {
  59. loginStatus.value = false;
  60. checkInstall().then(() => {
  61. let userInfo = getStorage(storageKey.userInfo);
  62. if (userInfo) {
  63. location.href = `/nft/list`
  64. } else {
  65. twitterAuth()
  66. }
  67. }).catch(() => {
  68. loginStatus.value = true;
  69. install()
  70. })
  71. // Report
  72. Report({
  73. baseInfo: {
  74. pageSource: pageSource.homePage,
  75. },
  76. params: {
  77. eventData: {
  78. businessType: businessType.buttonClick,
  79. objectType: objectType.createNftsButton,
  80. }
  81. }
  82. })
  83. }
  84. const twitterAuth = () => {
  85. postRequest(Api.twitterRequestToken, {
  86. params: {
  87. oauthCallback: callBackUrl
  88. }
  89. }).then(res => {
  90. let { code, data, msg } = res;
  91. if ( code === 0 ) {
  92. let url = getOauthUrl(data.authToken);
  93. let win = createWindow(url);
  94. timer.value = setInterval(() => {
  95. console.log(5555)
  96. if (win && win.closed) {
  97. clearInterval(timer.value);
  98. twitterLogin(data);
  99. }
  100. }, 500)
  101. } else {
  102. ElMessage({
  103. type: 'error',
  104. message: msg,
  105. })
  106. loginStatus.value = true;
  107. }
  108. })
  109. }
  110. const twitterLogin = (data: { authToken: string, consumerKey: string }) => {
  111. console.log(6666)
  112. let verifier = storage.get(storageKey.verifier)
  113. console.log(7777, verifier)
  114. if (verifier) {
  115. postRequest(Api.twitterLogin, {
  116. params: {
  117. consumerKey: data.consumerKey,
  118. oauthToken: data.authToken,
  119. oauthVerifier: verifier
  120. }
  121. }).then(res => {
  122. let { code, data, msg } = res;
  123. if ( code === 0 ) {
  124. setStorage(storageKey.userInfo, data);
  125. location.href = `/nft/list`
  126. } else {
  127. ElMessage({
  128. type: 'error',
  129. message: msg,
  130. })
  131. }
  132. }).finally(() => {
  133. loginStatus.value = true;
  134. storage.remove(storageKey.verifier);
  135. clearInterval(timer.value);
  136. })
  137. } else {
  138. loginStatus.value = true;
  139. }
  140. }
  141. </script>
  142. <style lang="less" scoped>
  143. .header {
  144. position: fixed;
  145. z-index: 3;
  146. display: flex;
  147. justify-content: space-between;
  148. align-items: center;
  149. top: 0;
  150. left: 0;
  151. width: 100%;
  152. height: 60px;
  153. background-color: #ffffff;
  154. box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.1);
  155. .logo {
  156. height: 38px;
  157. margin-left: 26px;
  158. }
  159. .operation {
  160. display: flex;
  161. flex-direction: row;
  162. }
  163. .login {
  164. display: flex;
  165. align-items: center;
  166. justify-content: center;
  167. box-sizing: border-box;
  168. height: 38px;
  169. cursor: pointer;
  170. padding: 0 18px;
  171. font-size: 15px;
  172. font-weight: 600;
  173. color: #1D9BF0;
  174. margin-right: 19px;
  175. border-radius: 20px;
  176. border: solid 1px #1D9BF0;
  177. .add {
  178. width: 20px;
  179. margin-right: 4px;
  180. &.loading {
  181. animation: rotate 1s infinite linear;
  182. }
  183. }
  184. &.disabled {
  185. opacity: .5;
  186. border: solid 1px #eee;
  187. }
  188. }
  189. .down {
  190. display: flex;
  191. align-items: center;
  192. justify-content: center;
  193. height: 38px;
  194. cursor: pointer;
  195. padding: 0 24px;
  196. margin-right: 26px;
  197. border-radius: 20px;
  198. background: #1D9BF0;
  199. .text {
  200. color: #fff;
  201. font-size: 15px;
  202. font-weight: 600;
  203. margin-top: -2px;
  204. }
  205. }
  206. &-place {
  207. height: 60px;
  208. }
  209. }
  210. @keyframes rotate {
  211. 0% {
  212. transform: rotate(0deg)
  213. }
  214. 100% {
  215. transform: rotate(360deg)
  216. }
  217. }
  218. </style>