소스 검색

[edit][bugs]

zhangwei 3 년 전
부모
커밋
6bb4323b47
4개의 변경된 파일111개의 추가작업 그리고 30개의 파일을 삭제
  1. 6 0
      src/assets/svg/icon-loading.svg
  2. 14 10
      src/http/request.js
  3. 14 0
      src/uilts/chromeExtension.js
  4. 77 20
      src/view/red-packet.vue

+ 6 - 0
src/assets/svg/icon-loading.svg

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="margin: auto; background: rgb(255, 255, 255); display: block; shape-rendering: auto;" width="200px" height="200px" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid">
+<circle cx="50" cy="50" fill="none" stroke="#389aff" stroke-width="12" r="35" stroke-dasharray="164.93361431346415 56.97787143782138">
+  <animateTransform attributeName="transform" type="rotate" repeatCount="indefinite" dur="1s" values="0 50 50;360 50 50" keyTimes="0;1"></animateTransform>
+</circle>
+<!-- [ldio] generated by https://loading.io/ --></svg>

+ 14 - 10
src/http/request.js

@@ -1,5 +1,5 @@
 import axios from 'axios'
-import {getStorage} from "../uilts/help"
+import { getChromeStorage } from '../uilts/chromeExtension.js'
 
 let baseUrl = 'https://denettestapi.piaoquantv.com/denet';
 let userInfo = '';
@@ -13,16 +13,12 @@ export const service = axios.create({
   },
 })
 
-// request拦截器
-service.interceptors.request.use(config => {
-  if(!userInfo) {
-    userInfo = getStorage('de-userInfo');
-  }
+function checkParams(config) {
   const { accessToken: token = '' } = userInfo || {};
 
   if (config.method === 'get') {
-    let {baseInfo = null} = config.params || {};
-    if(!baseInfo || !baseInfo.token) {
+    let { baseInfo = null } = config.params || {};
+    if (!baseInfo || !baseInfo.token) {
       config['params']['baseInfo'] = {
         token: token,
       }
@@ -34,8 +30,8 @@ service.interceptors.request.use(config => {
   }
 
   if (config.method === 'post') {
-    let {baseInfo = null} = config.data || {};
-    if(!baseInfo || !baseInfo.token) {
+    let { baseInfo = null } = config.data || {};
+    if (!baseInfo || !baseInfo.token) {
       config['data']['baseInfo'] = {
         token: token,
       }
@@ -46,6 +42,14 @@ service.interceptors.request.use(config => {
     config['data'] = data;
   }
   return config
+}
+
+// request拦截器
+service.interceptors.request.use(async (config) => {
+  if (!userInfo) {
+    userInfo = await getChromeStorage('userInfo') || ''
+  }
+  return checkParams(config)
 }, error => {
   // Do something with request error
   console.log(error) // for debug

+ 14 - 0
src/uilts/chromeExtension.js

@@ -10,6 +10,19 @@ export function setChromeStorage(params) {
 export function getChromeStorage(key = '', callback) {
     let params = {}
     params[key] = ''
+
+    if (!callback) {
+        return new Promise((resolve) => {
+            chrome.storage.local.get(params, (item) => {
+                if (item[key]) {
+                    resolve(JSON.parse(item[key]))
+                } else {
+                    resolve(null)
+                }
+            });
+        })
+    }
+
     chrome.storage.local.get(params, (item) => {
         if (item[key]) {
             callback(JSON.parse(item[key]))
@@ -19,6 +32,7 @@ export function getChromeStorage(key = '', callback) {
     });
 }
 
+
 export function getChromeCookie({
     name,
     url

+ 77 - 20
src/view/red-packet.vue

@@ -183,6 +183,10 @@
         {{ data.error_txt }}
       </div>
     </div>
+    <!-- loading -->
+    <div v-show="data.loading_show" class="loading">
+      <img :src="require('../assets/svg/icon-loading.svg')" alt />
+    </div>
   </div>
 </template>
 
@@ -201,6 +205,7 @@ var moment = require('moment');
 
 let data = reactive({
   status: '',
+  loading_show: false,
   detail: {},
   luck_list_end: false,
   page_index: 1,
@@ -217,7 +222,11 @@ let data = reactive({
     retweet: false
   }
 })
-function clickLickBtn() {
+async function clickLickBtn() {
+  let _userInfo = await checkIsLogin()
+  if (!_userInfo) {
+    return
+  }
   data.done.like = true
   // 跳转到推文详情
   oneKeyLike({
@@ -268,7 +277,12 @@ function handleScroll(e) {
 }
 
 
-function clickRetweetBtn() {
+async function clickRetweetBtn() {
+  let _userInfo = await checkIsLogin()
+  if (!_userInfo) {
+    return
+  }
+
   data.done.retweet = true
   oneKeyReTweet({
     params: {
@@ -290,17 +304,25 @@ function clickRetweetBtn() {
 
 
 function getValidity() {
+  let _d1,_d2
   let timer = setInterval(() => {
     let _time = new Date().getTime()
     if (data.detail.endTimestamp - _time > 0) {
-      data.detail.validity = moment(data.detail.endTimestamp - _time).format('HH:mm:ss')
+      _d1= moment(data.detail.endTimestamp)
+      _d2 = moment(_time)
+      data.detail.validity = moment(_d1.diff(_d2)).format('HH:mm:ss')
     } else {
       clearInterval(timer)
     }
   }, 1000)
 }
 
-function clickFollowAll(item) {
+async function clickFollowAll(item) {
+  let _userInfo = await checkIsLogin()
+  if (!_userInfo) {
+    return
+  }
+
   let arr_name = []
   for (let i in item) {
     arr_name.push(item[i].name)
@@ -359,7 +381,7 @@ function init() {
     switch (res.code.toString()) {
       case "0":
         data.srcContentId = res.data.srcContentId
-        if(data.tweetId != data.srcContentId){
+        if (data.tweetId != data.srcContentId) {
           data.error_txt = 'Giveways link Error'
           data.status = 'error'
           return
@@ -414,7 +436,6 @@ function init() {
           data.status = 'close'
         }
         break
-
     }
   })
 
@@ -448,6 +469,7 @@ onMounted(() => {
   data.postId = getQueryString('postId')
   data.tweetId = getQueryString('tweetId')
   init()
+  // data.loading_show = true
 })
 
 // 点击领取
@@ -496,24 +518,41 @@ function handleRedPacket() {
   })
 }
 
+chrome.storage.onChanged.addListener(changes => {
+  if (changes.userInfo) {
+    // let item = JSON.parse(changes.userInfo.newValue)
+    data.loading_show = false
+  }
+})
 
-function clickGetGiveways() {
-  // 
-  getChromeStorage('userInfo', (res) => {
-    // 
-    if (!res) {
-      chrome.runtime.sendMessage(
-        { method: "POPUP_LOGIN", data: "" },
-        (response) => {
-          console.log("res", response);
-        }
-      );
-    } else {
-      handleFinishRedPacket()
-    }
+// 校验是否封路
+function checkIsLogin() {
+  
+  return new Promise((resolve) => {
+    getChromeStorage('userInfo', (_userInfo) => {
+      if (!_userInfo) {
+        data.loading_show = true
+        chrome.runtime.sendMessage(
+          { method: "POPUP_LOGIN", data: "" },
+          (response) => {
+            console.log("res", response);
+          }
+        )
+        resolve(_userInfo)
+      }else{
+        resolve(_userInfo)
+      }
+    })
   })
 }
 
+async function clickGetGiveways() {
+  let _userInfo = await checkIsLogin()
+  if (_userInfo) {
+    handleFinishRedPacket()
+  }
+}
+
 function handleFinishRedPacket() {
   finishRedPacket({
     params: {
@@ -590,6 +629,24 @@ body {
   font-family: "SF Pro Display";
   font-style: normal;
 
+  .loading {
+    background: #FFFFFF;
+    opacity: 0.8;
+    z-index: 222;
+    text-align: center;
+    width: 375px;
+    height: 500px;
+    position: fixed;
+    top: 0;
+    left: 0;
+
+    img {
+      margin-top: 216px;
+      width: 70px;
+      height: 70px;
+    }
+  }
+
   .error {
     text-align: center;