zhangwei 3 vuotta sitten
vanhempi
commit
f7678c2c13
5 muutettua tiedostoa jossa 120 lisäystä ja 79 poistoa
  1. 17 4
      src/entry/background.js
  2. 8 0
      src/http/request.js
  3. 17 1
      src/uilts/chromeExtension.js
  4. 37 30
      src/uilts/help.js
  5. 41 44
      src/view/red-packet.vue

+ 17 - 4
src/entry/background.js

@@ -5,7 +5,8 @@ import {
     backTwitterPinLoginCode,
 } from "../logic/twitter.js";
 
-import { setChromeStorage, getChromeCookie, LANDING_PAGE } from "../uilts/chromeExtension";
+import { setChromeStorage, getChromeCookie, LANDING_PAGE, setChromeCookie, LANDING_PAGE_MIND } from "../uilts/chromeExtension";
+import { guid } from '../uilts/help.js'
 
 chrome.runtime.onInstalled.addListener(onInstalledMethod);
 
@@ -13,9 +14,9 @@ chrome.runtime.onMessage.addListener(onMessageMethod);
 
 function onInstalledMethod() {
     getChromeCookie(LANDING_PAGE, (res_arr) => {
-        if(res_arr && res_arr.length) {
-            let {postNickName, srcContentId} = res_arr[res_arr.length-1];
-            if(postNickName && srcContentId) {
+        if (res_arr && res_arr.length) {
+            let { postNickName, srcContentId } = res_arr[res_arr.length - 1];
+            if (postNickName && srcContentId) {
                 let url = `https://twitter.com/${postNickName}/status/${srcContentId}`
                 chrome.tabs.create({
                     url
@@ -25,6 +26,18 @@ function onInstalledMethod() {
             backTwitterPinLoginToken();
         }
     })
+    getChromeCookie(LANDING_PAGE_MIND, (res_arr) => {
+        // 没有cookie
+        if (res_arr && res_arr.length) {
+            setChromeStorage({ mid: JSON.stringify(res_arr[0]) })
+        } else {
+            let _params = {
+                mid: guid()
+            }
+            setChromeCookie(LANDING_PAGE, { 'mid': _params.mid })
+            setChromeStorage({ mid: JSON.stringify(_params) })
+        }
+    })
 }
 
 function onMessageMethod(req, sender, sendResponse) {

+ 8 - 0
src/http/request.js

@@ -3,6 +3,8 @@ import { getChromeStorage } from '../uilts/chromeExtension.js'
 
 let baseUrl = 'https://denettestapi.piaoquantv.com/denet';
 let userInfo = '';
+let storage_mid = ''
+
 // 创建axios实例
 export const service = axios.create({
   baseURL: baseUrl, // api的base_url
@@ -15,12 +17,14 @@ export const service = axios.create({
 
 function checkParams(config) {
   const { accessToken: token = '' } = userInfo || {};
+  const { mid } = storage_mid || {}
 
   if (config.method === 'get') {
     let { baseInfo = null } = config.params || {};
     if (!baseInfo || !baseInfo.token) {
       config['params']['baseInfo'] = {
         token: token,
+        mid
       }
     }
     let params = {
@@ -34,6 +38,7 @@ function checkParams(config) {
     if (!baseInfo || !baseInfo.token) {
       config['data']['baseInfo'] = {
         token: token,
+        mid
       }
     }
     let data = {
@@ -49,6 +54,9 @@ service.interceptors.request.use(async (config) => {
   if (!userInfo) {
     userInfo = await getChromeStorage('userInfo') || ''
   }
+  if (!storage_mid) {
+    storage_mid = await getChromeStorage('mid') || ''
+  }
   return checkParams(config)
 }, error => {
   // Do something with request error

+ 17 - 1
src/uilts/chromeExtension.js

@@ -1,6 +1,11 @@
 export const LANDING_PAGE = {
     name: 'received_log',
-    url: 'https://de-net-test.piaoquantv.com'
+    url: 'http://localhost:3000'
+}
+
+export const LANDING_PAGE_MIND = {
+    name: 'mid',
+    url: 'http://localhost:3000'
 }
 
 export function setChromeStorage(params) {
@@ -32,6 +37,17 @@ export function getChromeStorage(key = '', callback) {
     });
 }
 
+export function setChromeCookie({
+    name,
+    url
+}, value_obj) {
+    chrome.cookies.set({
+        expirationDate: new Date().getTime() / 10,
+        name: name,
+        url: url,
+        value: encodeURIComponent(JSON.stringify(value_obj)) || ''
+    })
+}
 
 export function getChromeCookie({
     name,

+ 37 - 30
src/uilts/help.js

@@ -1,24 +1,24 @@
 export function getQueryString(name) {
-    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i")
-    let r = window.location.search.substr(1).match(reg)
-    if (r != null) {
-        return window.decodeURIComponent(r[2])
-    }
-    return null
- }
+  let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i")
+  let r = window.location.search.substr(1).match(reg)
+  if (r != null) {
+    return window.decodeURIComponent(r[2])
+  }
+  return null
+}
 
- export function debounce(fn,delay){
-    let timer; // 定时器
-    return function(...args){ // 形成闭包
-      // args 为函数调用时传的参数。
-      let context = this;
-      timer&&clearTimeout(timer); // 当函数再次执行时,清除定时器,重新开始计时
-      // 利用定时器,让指定函数延迟执行。
-      timer = setTimeout(function(){
-        // 执行传入的指定函数,利用apply更改this绑定和传参
-        fn.apply(context,args);
-      },delay)
-    }
+export function debounce(fn, delay) {
+  let timer; // 定时器
+  return function (...args) { // 形成闭包
+    // args 为函数调用时传的参数。
+    let context = this;
+    timer && clearTimeout(timer); // 当函数再次执行时,清除定时器,重新开始计时
+    // 利用定时器,让指定函数延迟执行。
+    timer = setTimeout(function () {
+      // 执行传入的指定函数,利用apply更改this绑定和传参
+      fn.apply(context, args);
+    }, delay)
+  }
 }
 
 export function throttle(fn, thresh) {
@@ -30,27 +30,34 @@ export function throttle(fn, thresh) {
     var context = this, args = arguments, curr = new Date() - 0
 
     clearTimeout(timeout)//总是干掉事件回调
-    if(curr - start >= threshhold){
+    if (curr - start >= threshhold) {
       fn.apply(context, args) //只执行一部分方法,这些方法是在某个时间段内执行一次
       start = curr
-    }else{
+    } else {
       // 让方法在脱离事件后也能执行一次
-      timeout = setTimeout(function(){
+      timeout = setTimeout(function () {
         fn.apply(context, args)
       }, threshhold);
     }
   }
 }
-  
+
 export function setStorage(key, value) {
-    return localStorage.setItem(key, JSON.stringify(value));
+  return localStorage.setItem(key, JSON.stringify(value));
 }
 
 export function getStorage(key) {
-    const item = localStorage.getItem(key);
-    try {
-        return item ? JSON.parse(item) : '';
-    } catch (e) {
-        return item;
-    }
+  const item = localStorage.getItem(key);
+  try {
+    return item ? JSON.parse(item) : '';
+  } catch (e) {
+    return item;
+  }
+}
+
+export function guid() {
+  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
+    var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
+    return v.toString(16);
+  });
 }

+ 41 - 44
src/view/red-packet.vue

@@ -304,13 +304,19 @@ async function clickRetweetBtn() {
 
 
 function getValidity() {
-  let _d1,_d2
+  let _time = new Date().getTime()
+  let _d1, _d2, _d3, _h, _m, _s
+
   let timer = setInterval(() => {
     let _time = new Date().getTime()
-    if (data.detail.endTimestamp - _time > 0) {
-      _d1= moment(data.detail.endTimestamp)
+    _d3 = data.detail.myReceived.taskEndTimestamp - _time
+    if (_d3 > 0) {
+      _d1 = moment(data.detail.myReceived.taskEndTimestamp)
       _d2 = moment(_time)
-      data.detail.validity = moment(_d1.diff(_d2)).format('HH:mm:ss')
+      _h = moment.duration(_d1.diff(_d2)).hours()
+      _m = moment.duration(_d1.diff(_d2)).minutes()
+      _s = moment.duration(_d1.diff(_d2)).seconds()
+      data.detail.validity = `${_h}:${_m}:${_s}`
     } else {
       clearInterval(timer)
     }
@@ -406,21 +412,11 @@ function init() {
               data.close_title = res.msg
             }
           } else {
-            // 判断本地之前是否领取过这个红包
-            getChromeCookie(LANDING_PAGE, (res_arr) => {
-              if (res_arr.length > 0) {
-                let _res = res_arr.filter((item) => { return item.postId == data.postId })[0] || null
-                // 本地领取过红包
-                if (_res) {
-                  data.status = 'opened'
-                  data.money = _res.receiveAmount
-                } else {
-                  data.status = 'not-open'
-                }
-              } else {
-                data.status = 'not-open'
-              }
-            })
+            if (data.detail.myReceived) {
+              data.status = 'opened'
+            } else {
+              data.status = 'not-open'
+            }
           }
           data.detail.taskCondition = JSON.parse(data.detail.taskCondition)
           data.detail.amountValue = showLastTwoPlace(data.detail.amountValue)
@@ -450,7 +446,7 @@ function init() {
         switch (res.data[i].type) {
           case 1:
             data.done.follow = res.data[i].finished
-            data.detail.taskCondition[0].relatedUsers = res.data[i].detail
+            // data.detail.taskCondition[0].relatedUsers = res.data[i].detail
             break
           case 2:
             data.done.like = res.data[i].finished
@@ -474,26 +470,29 @@ onMounted(() => {
 
 // 点击领取
 function clickOpenRedPacket() {
-  getChromeStorage("userInfo", (_res) => {
-    // 1.没有登陆
-    if (!_res) {
-      // 2.查看cookie里是否有  
-      getChromeCookie(LANDING_PAGE, (res_arr) => {
-        // 
-        if (res_arr.length > 0) {
-          let _res = res_arr.filter((item) => { return item.postId == data.postId })[0] || null
-          if (_res) {
-            data.status = 'opened'
-            data.money = _res.receiveAmount
-          }
-        } else {
-          handleRedPacket()
-        }
-      })
-    } else {
-      handleRedPacket()
-    }
-  })
+  // getChromeStorage("userInfo", (_res) => {
+  //   // 1.没有登陆
+  //   if (!_res) {
+  //     // 2.查看cookie里是否有  
+  //     getChromeCookie(LANDING_PAGE, (res_arr) => {
+  //       // 
+  //       // if (res_arr.length > 0) {
+  //       //   let _res = res_arr.filter((item) => { return item.postId == data.postId })[0] || null
+  //       //   if (_res) {
+  //       //     data.status = 'opened'
+  //       //     data.money = _res.receiveAmount
+  //       //   }
+  //       // } else {
+
+  //       // }
+  //       // handleRedPacket()
+  //     })
+  //   } else {
+  //     handleRedPacket()
+  //   }
+  // })
+  handleRedPacket()
+
 }
 
 function handleRedPacket() {
@@ -502,12 +501,10 @@ function handleRedPacket() {
       postId: data.postId
     }
   }).then((res) => {
-
     switch (res.code.toString()) {
       case "0":
         data.status = 'opened'
         data.money = res.data.receiveAmount
-        concatChromeCookie(LANDING_PAGE, { receivedId: res.data.receivedId, postId: data.postId, receiveAmount: res.data.receiveAmount })
         init()
         break
       case "2008":
@@ -527,7 +524,7 @@ chrome.storage.onChanged.addListener(changes => {
 
 // 校验是否封路
 function checkIsLogin() {
-  
+
   return new Promise((resolve) => {
     getChromeStorage('userInfo', (_userInfo) => {
       if (!_userInfo) {
@@ -539,7 +536,7 @@ function checkIsLogin() {
           }
         )
         resolve(_userInfo)
-      }else{
+      } else {
         resolve(_userInfo)
       }
     })