需求說明
1断医、首頁進入顯示搜索歷史敷扫,網(wǎng)頁進入對關(guān)鍵詞進行聯(lián)想搜索。
2、搜索需要用web打開都統(tǒng)一新建標簽頁打開菱涤,加在最后面。
3慕购、按照標簽震桶、工具、應用毫蚓、經(jīng)驗占键、說明書、百度聯(lián)想排序元潘。
4畔乙、顯示1條工具,點擊更多顯示工具列表翩概。
5牲距、顯示3條相關(guān)的經(jīng)驗,點擊更多進入更多經(jīng)驗列表钥庇。
6牍鞠、顯示3條相關(guān)的說明書,點擊更多進入更多相關(guān)說明書的列表评姨。
7难述、關(guān)鍵詞聯(lián)想,由百度提供該關(guān)鍵詞的聯(lián)想龄广,并顯示在底部硫眯。
需求地址
接口說明
URL:http://114larc.com/q/api/1.0/android/1.0/search
請求方式:GET
- 請求參數(shù)說明:
參數(shù)名稱 | 必選 | 類型 | 描述 |
---|---|---|---|
q | true | string | 關(guān)鍵字 |
返回結(jié)果說明:
列表中的type值:1-經(jīng)驗 2-說明書 3-工具 4-標簽 5-應用,
不同type對應不同的結(jié)構(gòu)模型择同,經(jīng)驗和說明書結(jié)構(gòu)一樣两入,請參考下面的json數(shù)據(jù)結(jié)構(gòu):
share:分享地址返回結(jié)果:
{
"state": 1,
"code": 0,
"message": "",
"data": {
"share": "114larc.com/p/search?q=1",
"list": [
{
"name": "標簽",
"type": 4,
"list": [
"比較"
]
},
{
"name": "工具",
"type": 3,
"list": [
{
"name": "比較6",
"url": "http://6.com",
"logo": "http://img.114larc.com/thumb/P/RC/3D/PRC3D74AC06B2B5772DF7ED285C6C31F5B0_100_100?s=6dt6DCcT4y78R5jmWyrsjQ"
}
]
},
{
"name": "應用",
"type": 5,
"list": [
{
"pinyin": "bilin-402anzhuoyingyongapk",
"size": "31.1M",
"name": "比鄰",
"description": "<p style=\"text-indent: 2em;\">軟件簡介:</p><p style=\"text-indent: 2em;\">比鄰是一款<strong>電話社交</strong>平臺APP,是一個刺激年輕人心跳的社交工具敲才。</p><p style=\"text-indent: 2em;\">主要通過<strong>即時通話</strong>裹纳,隨時暢聊你<strong>感興趣</strong>的話題,并且可以通過精彩的圖片和文字<strong>分享</strong>生活中的美好點滴紧武,</p><p style=\"text-indent: 2em;\">也可以<strong>欣賞</strong>其它用戶個性十足的個人資料和生活相冊剃氧。</p><hr k_oof_k=\"line\" style=\"border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: dashed; border-top-color: rgb(204, 204, 204); height: 0px; overflow: hidden;\"><p style=\"text-indent: 2em;\">功能簡介:</p><p style=\"text-indent: 2em;\">√<strong>隨機電話</strong> - 無距離、無時差隨時開聊</p><p style=\"text-indent: 2em;\">√<strong>超能力標簽</strong> - 找到自己的獨特話題</p><p style=\"text-indent: 2em;\">√<strong>最近在線</strong> - 溫暖的陌生人都在這里</p><p style=\"text-indent: 2em;\">√<strong>連線好友</strong> - 免費無限時阻星、無限次通話</p><hr k_oof_k=\"line\" style=\"border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: dashed; border-top-color: rgb(204, 204, 204); height: 0px; overflow: hidden;\"><p style=\"text-indent: 2em;\">使用場景:</p><p style=\"text-indent: 2em;\">√當你<strong>寂寞</strong>的時候朋鞍,打打比鄰,這里有人為你唱歌</p><p style=\"text-indent: 2em;\">√當你<strong>失眠</strong>的時候妥箕,打打比鄰滥酥,這里有人哄你入睡</p><p style=\"text-indent: 2em;\">√當你<strong>難過</strong>的時候,打打比鄰畦幢,這里有人逗你開心</p><p style=\"text-indent: 2em;\">√當你覺得<strong>冷</strong>的時候坎吻,打打比鄰,這里有暖男熱線</p><hr k_oof_k=\"line\" style=\"border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: dashed; border-top-color: rgb(204, 204, 204); height: 0px; overflow: hidden;\"><p style=\"text-indent: 2em;\">更新內(nèi)容:</p><div style=\"text-indent: 2em;\">1宇葱、解決部分崩潰問題</div><div style=\"text-indent: 2em;\">2瘦真、優(yōu)化隨機群聊匹配</div>",
"logo": "http://img.anxia.com/thumb/P/DL/BB/PDLBB6E146E341F7900B35E690F7813E1B3_0?s=Sv8uwHljz5_qEeEM9hq7nA",
"sid": "18081",
"release_time": "1496073600",
"has_history": 0,
"url": "/a/bilin-402anzhuoyingyongapk"
}
]
},
{
"name": "說明書",
"type": 2,
"list": [
{
"t_id": 90,
"c_id": 4362,
"type": 2,
"is_hot": 0,
"is_top": 0,
"subject": "苯巴比妥片",
"summary": "",
"state": 1,
"post_time": 1500968344,
"update_time": 1500982779,
"url": "http://114larc.com/s/detail/23D60020000004Y5BJDPASGK.html",
"cat_id": 0,
"useful_num": 0,
"is_useful": 0,
"location": "",
"icon_url": "http://img.114larc.com/thumb/P/RC/2D/PRC2D0CF8CCC0AAC491D879969C5B4BF3AD_800_800?s=TRfGkLQUmrIzy_5wETifbA",
"pics": [],
"tags": [
"失眠",
"心腦血管"
]
}
]
}
],
"count": 4
}
}
核心代碼
進入搜索界面都先調(diào)用該方法,進行初始化操作
func urlBarViewDidClick(_ urlBarView: URLBarView) {
if let keyworkSearch = MainViewFacade.sharedInstance.cache?.object(forKey: "keywork_search") as? [NSCoding] {
let temp = NSMutableArray(array: keyworkSearch)
//覆蓋安裝時黍瞧,緩存的內(nèi)容為【string】诸尽,需轉(zhuǎn)化為模型
if temp as AnyObject is [String] {
for i in 0..<temp.count {
temp.replaceObject(at: i, with: SearchCellModel(keyword: temp[i] as? String ?? ""))
}
}
if !isEqualContains(self.keyworkSearch, other: temp as AnyObject as? [SearchCellModel] ?? []) {
self.keyworkSearch = temp as AnyObject as? [SearchCellModel] ?? []
}
}
//文字修改
changeUrlBarViewText()
if self.contenView.superview == nil {
self.addContenView()
if urlBarView.addressTextField.text?.isEmpty == false {
self.datas = []
}
shouldUpdateDatas = true
} else {
////已經(jīng)在搜索界面點擊。成為焦點
shouldUpdateDatas = false
}
textFieldText = urlBarView.addressTextField.text
self.tableView.reloadData()
}
修改該屬性則會延遲0.6s執(zhí)行搜索操作印颤,還帶有cancel上一次未完成的請求您机。
var textFieldText: String? {
didSet {
if textFieldText?.characters.count == 0 && shouldUpdateDatas {
NSObject.cancelPreviousPerformRequests(withTarget: self)
updateDatas("")
return
}
guard textFieldText != oldValue else {
if shouldUpdateDatas {
NSObject.cancelPreviousPerformRequests(withTarget: self)
self.perform(#selector(NewSearchViewManager.updateDatas(_:)), with:textFieldText, afterDelay: 0.1)
shouldUpdateDatas = false
}
return
}
NSObject.cancelPreviousPerformRequests(withTarget: self)
self.perform(#selector(NewSearchViewManager.updateDatas(_:)), with:textFieldText, afterDelay: 0.6)
}
}
該方法則是真正發(fā)出請求。
該處分為兩個接口膀哲,同時發(fā)出請求往产,先回來數(shù)據(jù)則先顯示被碗,后回來的則進行處理拼接起來后再刷新數(shù)據(jù)某宪。
返回的結(jié)果是有固定的排序方式的。
func updateDatas(_ text: String?) {
if text?.trimmingCharacters(in: CharacterSet.whitespaces).isEmpty == false {
let enginModel = SearchGroupModel(SearchGroupType.engine)
enginModel.searchDatas = [SearchCellModel(keyword: text!)]
self.datas = [enginModel]
self.tableView.reloadData()
//114啦 熱詞聯(lián)想搜索接口
self.api.associateSearch(withKeywork: text!) { [weak self](keyworks, searchEngineURL) -> Void in
guard let strongSelf = self else {
return
}
guard let keyworks = keyworks as? [String], strongSelf.textFieldText! == text else {
return
}
var levdatas = [SearchCellModel]()
for kword in keyworks {
let model = SearchCellModel(keyword: kword)
levdatas.append(model)
}
if levdatas.count > 0 || text == "114la.com" || text == "115.com" {
if text == "w" || text == "ww" || text == "www" || text == "www."
|| text == "114la.com" || text == "115.com" {
levdatas.insert(SearchCellModel(keyword: "www.115.com"), at: 0)
levdatas.insert(SearchCellModel(keyword: "www.114la.com"), at: 0)
}
let lenovoModel = SearchGroupModel(SearchGroupType.lenovo)
lenovoModel.searchDatas = levdatas
if let datas = strongSelf.datas, datas.last?.type == .share {
strongSelf.datas?.insert(lenovoModel, at: datas.count - 1)
} else {
strongSelf.datas?.append(lenovoModel)
}
}
strongSelf.tableView.isHidden = false
strongSelf.tableView.dk_backgroundColorPicker = DKColor.picker(withNormalColor: UIConstants.NormalBackgroundColor, nightColor: UIConstants.NightDarkBackgroundColor)
strongSelf.tableView.reloadData()
}
// 綜合搜索
YYW114LaService.comprehensiveSearch(keyword: text!, { (json, error) -> AnyObject? in
if error != nil {
self.urlBarView?.addressTextField.resignFirstResponder()
return nil
}
if let json = json {
guard self.textFieldText! == text, json["state"].intValue == 1 else {
return nil
}
let listDatas = json["data"]["list"].arrayValue
var tempDatas = [SearchGroupModel]()
for type in showOrder {
if let dict = listDatas.filter({ (dict) -> Bool in
return dict["type"].intValue == type
}).first {
switch type {
case 1:
if dict["list"].arrayValue.count > 0 {
tempDatas.append(SearchGroupModel(SearchGroupType.experience,dict["list"].arrayValue))
}
case 2:
if dict["list"].arrayValue.count > 0 {
tempDatas.append(SearchGroupModel(SearchGroupType.instructions,dict["list"].arrayValue))
}
case 3:
if dict["list"].arrayValue.count > 0 {
tempDatas.append(SearchGroupModel(SearchGroupType.tool,dict["list"].arrayValue))
}
case 4:
if dict["list"].arrayValue.count > 0 {
let model = SearchGroupModel(.label)
model.searchDatas = SearchGroupModel.getLabelModels(dict["list"].arrayValue)
tempDatas.append(model)
}
case 5:
if dict["list"].arrayValue.count > 0 {
tempDatas.append(SearchGroupModel(SearchGroupType.application,dict["list"].arrayValue))
}
case 999:
tempDatas.append(SearchGroupModel(SearchGroupType.ads,[dict["list"]]))
default:
break
}
}
}
self.datas?.insert(contentsOf: tempDatas, at: 1)
if !json["data"]["share"].stringValue.isEmpty && tempDatas.count > 0
&& self.datas?.last?.type != .share {
let shareModel = SearchGroupModel(SearchGroupType.share)
shareModel.searchDatas = [SearchCellModel(keyword: json["data"]["share"].stringValue)]
self.datas?.append(shareModel)
}
self.tableView.dk_backgroundColorPicker = DKColor.picker(withNormalColor: UIConstants.NormalBackgroundColor, nightColor: UIConstants.NightDarkBackgroundColor)
self.tableView.isHidden = false
self.tableView.reloadData()
}
return nil
})
}
else
{
shouldShowKeyWordSearch()
}
}
當點擊搜索記錄發(fā)生頁面跳轉(zhuǎn)锐朴,并且會記錄搜索歷史兴喂。
//處理url后進行加載
fileprivate func loadAndAddToHistoryWithURLString(_ URLString: String) {
guard !isSearchUrlToChangeSearchKey(URLString) else { return }
var text = self.searchEngineURL + URLString
if let _ = MainViewFacade.sharedInstance.mainViewModel.verificationURL(URLString)
{
text = URLString
}
if UserDefaults.standard.bool(forKey: "isOpenSearch")
{
self.addKeyworkToSearchHistory(URLString)
}
if let request = MainViewFacade.sharedInstance.mainViewModel.verificationURL(text) {
urlBarView?.clearText(UITapGestureRecognizer())
urlBarView?.cancel()
// MainViewFacade.sharedInstance.currentPageView().loadRequest(request)
mainViewController()?.pageViewManager.newTabLoadRequest(request)
if let absoluteString = request.url?.absoluteString {
urlBarView?.addressTextString = absoluteString
}
cancelFocus()
}
}
//添加搜索歷史記錄 隱身模式不添加
func addKeyworkToSearchHistory(_ keywork: String) {
guard !self.isPrivate else {
return
}
let index = isContainsKeyWord(keywork)
if index >= 0 {
self.keyworkSearch.remove(at: index)
}
let searchModel:SearchCellModel
if let _ = MainViewFacade.sharedInstance.mainViewModel.verificationURL(keywork) {
searchModel = SearchCellModel(dict: ["keyWord": keywork as AnyObject], type: .url)
} else {
searchModel = SearchCellModel(keyword: keywork)
}
self.keyworkSearch.insert(searchModel, at: 0)
if self.keyworkSearch.count > 10 {
self.keyworkSearch.removeSubrange(Range(10 ..< self.keyworkSearch.count))
}
MainViewFacade.sharedInstance.cache?.setObject(self.keyworkSearch as NSCoding, forKey: "keywork_search")
}
添加分享搜索結(jié)果以后需要判斷是不是打開分享出去的搜索結(jié)果url。
是的話則用原生UI打開搜索頁面,不進行打開網(wǎng)頁衣迷。
fileprivate func isSearchUrlToChangeSearchKey(_ text: String) -> Bool {//http://114larc.com/p/search?q=keyword
if text.contains("114larc.com/p/search?q=") || text.contains("114la.com/p/search?q=") {
let range = text.range(of: ".com/p/search?q=")
var changeText = text.substring(from: range!.upperBound)
changeText = changeText.removingPercentEncoding ?? changeText
urlBarView?.addressTextField.text = changeText
textFieldText = changeText
return true
}
return false
}
該搜索界面是將view添加在首頁的view中的畏鼓,顯示時添加,取消搜索時將view移除壶谒。
使用的是同一個tableview云矫。不會重新創(chuàng)建,因此每次需要初始化汗菜。