Selaa lähdekoodia

[add][try catch]

zhangwei 2 vuotta sitten
vanhempi
commit
0c36119deb
1 muutettua tiedostoa jossa 136 lisäystä ja 108 poistoa
  1. 136 108
      src/logic/background/twitter.js

+ 136 - 108
src/logic/background/twitter.js

@@ -22,39 +22,45 @@ const isHasTabByUrl = (url, callback) => {
 }
 
 export function twitterPinLoginToken() {
-    // 1.判断是否登陆了
-    getChromeStorage('userInfo', (res) => {
-        // 没有登陆
-        if (!res) {
-            if (tab_flag == false) {
-                return
-            }
-            tab_flag = false
-            fetchTtwitterRequestToken().then((res) => {
-                tab_flag = true
-                if (res.code == 0) {
-                    authToken = res.data.authToken
-                    consumerKey = res.data.consumerKey
-                    isHasTabByUrl('https://api.twitter.com/oauth/authorize?oauth_token', (tab) => {
-                        if (!tab) {
-                            chrome.tabs.create({
-                                url: `https://api.twitter.com/oauth/authorize?oauth_token=${res.data.authToken}`
-                            })
-                        } else {
-                            chrome.tabs.highlight({ windowId: tab.windowId, tabs: tab.index })
-                        }
-                    })
+    try {
+        // 1.判断是否登陆了
+        getChromeStorage('userInfo', (res) => {
+            // 没有登陆
+            if (!res) {
+                if (tab_flag == false) {
+                    return
                 }
-            }).catch(() => {
-                tab_flag = true
-            })
-        }
-    })
+                tab_flag = false
+                fetchTtwitterRequestToken().then((res) => {
+                    tab_flag = true
+                    if (res.code == 0) {
+                        authToken = res.data.authToken
+                        consumerKey = res.data.consumerKey
+                        isHasTabByUrl('https://api.twitter.com/oauth/authorize?oauth_token', (tab) => {
+                            if (!tab) {
+                                chrome.tabs.create({
+                                    url: `https://api.twitter.com/oauth/authorize?oauth_token=${res.data.authToken}`
+                                })
+                            } else {
+                                chrome.tabs.highlight({ windowId: tab.windowId, tabs: tab.index })
+                            }
+                        })
+                    }
+                }).catch(() => {
+                    tab_flag = true
+                })
+            }
+        })
+    } catch (error) {
+        Report.reportLog({
+            objectType: Report.objectType.background_function_catch,
+            funcName: 'twitterPinLoginToken',
+            errMsg: error.message
+        })
+    }
 }
 
 export function twitterPinLoginCode(sender, code) {
-    // actionType:{}
-
     // port.postMessage({
     //     state: "BACK_TWITTER_LOGIN_SUCCESS",
     // });
@@ -67,39 +73,45 @@ export function twitterPinLoginCode(sender, code) {
     //         }
     //     }
     // })
-    chrome.tabs.sendMessage(sender.tab.id, { actionType: 'BACK_TWITTER_LOGIN_SUCCESS' }, (res) => { console.log(res) });
-    chrome.tabs.remove(sender.tab.id)
-
-
-    chrome.cookies.getAll(LANDING_PAGE, (e = []) => {
-        let _str = '[]'
-        if (e.length > 0) {
-            _str = e[0].value
-        }
-        let _arr = JSON.parse(decodeURIComponent(_str))
-        let receivedIds = []
-        if (_arr.length > 0) {
-            for (let i in _arr) {
-                receivedIds.push(_arr[i].receivedId)
+    try {
+        chrome.tabs.sendMessage(sender.tab.id, { actionType: 'BACK_TWITTER_LOGIN_SUCCESS' }, (res) => { console.log(res) });
+        chrome.tabs.remove(sender.tab.id)
+        chrome.cookies.getAll(LANDING_PAGE, (e = []) => {
+            let _str = '[]'
+            if (e.length > 0) {
+                _str = e[0].value
             }
-        }
-        // 发送请求
-        // token,code
-        fetchTwitterLogin(authToken, consumerKey, code, receivedIds).then(res => {
-            if (res.code == 0) {
-                setChromeStorage({ userInfo: JSON.stringify(res.data) })
-                setChromeCookie(WEBSITE_USER_INFO, res.data)
+            let _arr = JSON.parse(decodeURIComponent(_str))
+            let receivedIds = []
+            if (_arr.length > 0) {
+                for (let i in _arr) {
+                    receivedIds.push(_arr[i].receivedId)
+                }
+            }
+            // 发送请求
+            // token,code
+            fetchTwitterLogin(authToken, consumerKey, code, receivedIds).then(res => {
+                if (res.code == 0) {
+                    setChromeStorage({ userInfo: JSON.stringify(res.data) })
+                    setChromeCookie(WEBSITE_USER_INFO, res.data)
 
-                sendActivetabMessage({
-                    actionType: 'BG_LOGIN_SET_USERINFO_CB'
-                });
+                    sendActivetabMessage({
+                        actionType: 'BG_LOGIN_SET_USERINFO_CB'
+                    });
 
-                // 获取全局消息数据
-                setMessageCount()
-                chrome.cookies.remove(LANDING_PAGE)
-            }
+                    // 获取全局消息数据
+                    setMessageCount()
+                    chrome.cookies.remove(LANDING_PAGE)
+                }
+            })
         })
-    })
+    } catch (error) {
+        Report.reportLog({
+            objectType: Report.objectType.background_function_catch,
+            funcName: 'twitterPinLoginCode',
+            errMsg: error.message
+        })
+    }
 }
 
 let discordAuthWindowId = '';
@@ -109,23 +121,31 @@ export function saveDiscordAuthWindowId(params) {
 }
 
 export function discordLoginCode({ code }, sender) {
-    console.log('sender', sender)
-    let { windowId, id } = sender.tab || {};
-    chrome.tabs.remove(id);
-
-    // 发送请求
-    getDiscordUserInfo({
-        authCode: code,
-        redirectUrl: discordAuthRedirectUri
-    }).then(res => {
-        if (res.code == 0) {
-            setTimeout(() => {
-                sendActivetabMessage({
-                    actionType: 'BACK_DISCORD_LOGIN_SUCCESS'
-                });
-            })
-        }
-    })
+    try {
+        console.log('sender', sender)
+        let { windowId, id } = sender.tab || {};
+        chrome.tabs.remove(id);
+
+        // 发送请求
+        getDiscordUserInfo({
+            authCode: code,
+            redirectUrl: discordAuthRedirectUri
+        }).then(res => {
+            if (res.code == 0) {
+                setTimeout(() => {
+                    sendActivetabMessage({
+                        actionType: 'BACK_DISCORD_LOGIN_SUCCESS'
+                    });
+                })
+            }
+        })
+    } catch (error) {
+        Report.reportLog({
+            objectType: Report.objectType.background_function_catch,
+            funcName: 'discordLoginCode',
+            errMsg: error.message
+        })
+    }
 
     // if(windowId) {
     //     chrome.windows.remove(
@@ -138,44 +158,52 @@ export function discordLoginCode({ code }, sender) {
 }
 
 export function twitterShortUrl(sender, url) {
-    fetchTwitterShortUrl(url).then(res => {
-        let str_arr = res.match(/denetme.net\/([\s\S]*?)"/) || []
-        let post_Id = str_arr[1] || ''
-        if (!post_Id) {
-            return
-        }
-        // 解析
-        let _obj = {
-            short_url: url,
-            post_Id
-        }
-        getChromeStorage('cardData', item => {
-            if (item) {
-                let has = false
-                for (let i in item) {
-                    if (item[i].short_url == _obj.short_url) {
-                        item[i].short_url = _obj.short_url
-                        item[i].post_Id = _obj.post_Id
+    try {
+        fetchTwitterShortUrl(url).then(res => {
+            let str_arr = res.match(/denetme.net\/([\s\S]*?)"/) || []
+            let post_Id = str_arr[1] || ''
+            if (!post_Id) {
+                return
+            }
+            // 解析
+            let _obj = {
+                short_url: url,
+                post_Id
+            }
+            getChromeStorage('cardData', item => {
+                if (item) {
+                    let has = false
+                    for (let i in item) {
+                        if (item[i].short_url == _obj.short_url) {
+                            item[i].short_url = _obj.short_url
+                            item[i].post_Id = _obj.post_Id
+                            setChromeStorage({ cardData: JSON.stringify(item) })
+                            has = true
+                            break
+                        }
+                    }
+                    if (!has) {
+                        item.push(_obj)
                         setChromeStorage({ cardData: JSON.stringify(item) })
-                        has = true
-                        break
                     }
-                }
-                if (!has) {
-                    item.push(_obj)
-                    setChromeStorage({ cardData: JSON.stringify(item) })
-                }
 
-            } else {
-                setChromeStorage({ cardData: JSON.stringify([_obj]) })
-            }
+                } else {
+                    setChromeStorage({ cardData: JSON.stringify([_obj]) })
+                }
 
-            chrome.tabs.sendMessage(sender.tab.id, { actionType: 'BACK_TWITTER_SHORT_URL' }, (response) => { });
-            // port.postMessage({
-            //     state: "BACK_TWITTER_SHORT_URL"
-            // });
+                chrome.tabs.sendMessage(sender.tab.id, { actionType: 'BACK_TWITTER_SHORT_URL' }, (response) => { });
+                // port.postMessage({
+                //     state: "BACK_TWITTER_SHORT_URL"
+                // });
+            })
         })
-    })
+    } catch (error) {
+        Report.reportLog({
+            objectType: Report.objectType.background_function_catch,
+            funcName: 'twitterShortUrl',
+            errMsg: error.message
+        })
+    }
 }