cocos2dx:生成短網址

你是否遇到過url過長,既不利於ui展示也不利於生成二維碼的情況?

額,既然長了,那就搞短一點!

短網址有以下優勢:

  • 短,美觀;
  • 生成的二維碼比較簡單,便於識別;
  • 便於數據統計;

這裏記錄兩個生成短網址的第三方API(免費)!

SOHU短網址

API:接口文檔

準備:註冊賬號獲取API key

TestDemo:

local TestDemo = class("TestDemo")

function TestDemo:ctor()
    self:testSOHUShortLink()
end

-- SOHO短網址
function TestDemo:testSOHUShortLink()
    local key = "YourKey"
    local longURL = string.urlencode("https://www.baidu.com")  -- 必須要對原始url進行UrlEncode
    self:getSOHUShortLink(key, longURL, handler(self, self.shortLinkCallFunc))
end

function TestDemo:shortLinkCallFunc(shortURL)
    print("shortURL->", shortURL)
end

function TestDemo:getSOHUShortLink(key, longURL, callFunc)
    local xhr = cc.XMLHttpRequest:new()
    xhr.timeout = 5
    xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_STRING
    local reqUrl = string.format("https://sohu.gg/api/?key=%s&url=%s", key, longURL)
    xhr:open("GET", reqUrl)
    local function httpCallback()
        if xhr.readyState == 4 and xhr.status == 200 then
            local response = xhr.response
            if callFunc then
                if response == "" then
                    callFunc(reqUrl)
                else
                    callFunc(response)
                end
            end
        end
        xhr:unregisterScriptHandler()
    end
    xhr:registerScriptHandler(httpCallback)
    xhr:send()
end

return TestDemo

ps:

  1. 必須要對原始url進行UrlEncode!
  2. 同一個url每次生成出來的短網址都是一樣的!如果需要同一個url每次生成出來的短網址都不同,可以在原始url後面拼接一個時間戳!
  3. 生成的短網址永久有效,不會過期!

SuoLink

API:接口文檔

準備:註冊賬號獲取key

TestDemo:

local TestDemo = class("TestDemo")

function TestDemo:ctor()
    self:testSLShortLink()
end

-- SuoLink短網址
function TestDemo:testSLShortLink()
    local longURL = string.urlencode("https://www.baidu.com")  -- 必須要對原始url進行UrlEncode
    local key = "YourKey"
    self:getSLShortLink(longURL, key, nil, handler(self, self.shortLinkCallFunc))
    -- self:getSLShortLink(longURL, key, "2040-06-06", handler(self, self.shortLinkCallFunc))
end

function TestDemo:shortLinkCallFunc(shortURL)
    print("shortURL->", shortURL)
end

function TestDemo:getSLShortLink(longURL, key, expireDate, callFunc)
    local xhr = cc.XMLHttpRequest:new()
    xhr.timeout = 5
    xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_STRING
    local reqUrl = ""
    if expireDate and expireDate ~= "" then
        reqUrl = string.format("http://api.suolink.cn/api.htm?url=%s&key=%s&expireDate=%s", longURL, key, expireDate)
    else
        reqUrl = string.format("http://api.suolink.cn/api.htm?url=%s&key=%s", longURL, key)
    end
    xhr:open("GET", reqUrl)
    local function httpCallback()
        if xhr.readyState == 4 and xhr.status == 200 then
            local response = xhr.response
            if callFunc then
                if response == "" then
                    callFunc(reqUrl)
                else
                    callFunc(response)
                end
            end
        end
        xhr:unregisterScriptHandler()
    end
    xhr:registerScriptHandler(httpCallback)
    xhr:send()
end

return TestDemo

ps:

  1. 必須要對原始url進行UrlEncode!
  2. 同一個url每次生成出來的短網址都是不同的!
  3. 可以設置短網址的有效期,如果不設置就默認三個月的有效期,設置到2040年之後就默認爲永久有效!短網址的有效期在後臺可以修改!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章