search.uvue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <template>
  2. <view class="container">
  3. <view class="search">
  4. <uni-cms-article-search-bar
  5. ref="searchBar"
  6. v-model="searchVal"
  7. :show-placeholder="false"
  8. :focus="true"
  9. @clear="showSearchResultPanel = false"
  10. @confirm="search"
  11. ></uni-cms-article-search-bar>
  12. </view>
  13. <view class="search-result" v-if="showSearchResultPanel">
  14. <uni-cms-article-list
  15. ref="articleList"
  16. :collectionList="colList"
  17. :refresherEnabled="false"
  18. loadTime="manual"
  19. style="flex: 1;"
  20. ></uni-cms-article-list>
  21. </view>
  22. <template v-else>
  23. <view class="panel history-panel" v-if="searchHistory.length > 0">
  24. <view class="panel__title">
  25. <view class="panel__title-text">
  26. <text class="text">搜索历史</text>
  27. </view>
  28. <view class="delete-history-btns" v-if="deleteHistoryLoading">
  29. <text class="text" @click="deleteAllSearchHistory">全部删除</text>
  30. <text class="text danger" @click="deleteHistoryLoading = false">完成</text>
  31. </view>
  32. <uni-cms-article-icons
  33. class="panel__after-icon"
  34. type="trash"
  35. :size="18"
  36. color="#999"
  37. @click="deleteHistoryLoading = true"
  38. v-else
  39. ></uni-cms-article-icons>
  40. </view>
  41. <view class="panel__list">
  42. <view class="panel__list-item" v-for="text in searchHistory">
  43. <text class="text" @click="search(text)">{{text}}</text>
  44. <uni-cms-article-icons
  45. class="icon"
  46. type="closeempty"
  47. :size="12"
  48. color="#999"
  49. v-if="deleteHistoryLoading"
  50. @click="deleteSearchHistory(text)"
  51. ></uni-cms-article-icons>
  52. </view>
  53. </view>
  54. </view>
  55. <unicloud-db ref="udb" #default="{ data, loading, error }" field="content"
  56. collection="opendb-search-hot" orderby="create_date desc,count desc" page-data="replace"
  57. :page-size="10">
  58. <view class="panel recommend-panel">
  59. <view class="panel__title">
  60. <view class="panel__title-text">
  61. <text class="text">搜索发现</text>
  62. <uni-cms-article-icons
  63. class="icon"
  64. type="reload"
  65. :size="14"
  66. color="#999"
  67. v-if="!hideSearchRecommend"
  68. @click="reLoadSearchRecommend"
  69. ></uni-cms-article-icons>
  70. </view>
  71. <uni-cms-article-icons
  72. class="panel__after-icon"
  73. :type="hideSearchRecommend ? 'eye-slash': 'eye'"
  74. :size="18"
  75. color="#999"
  76. @click="hideSearchRecommend = !hideSearchRecommend"
  77. ></uni-cms-article-icons>
  78. </view>
  79. <view class="panel__list">
  80. <view class="panel__list-tip" v-if="loading">
  81. <text class="text">正在加载...</text>
  82. </view>
  83. <view class="panel__list-tip" v-else-if="error != null">
  84. <text class="text">{{error.message}}</text>
  85. </view>
  86. <view class="panel__list-tip" v-else-if="hideSearchRecommend">
  87. <text class="text">当前搜索发现已隐藏</text>
  88. </view>
  89. <template v-else>
  90. <view
  91. class="panel__list-item"
  92. v-for="(word, index) in data"
  93. :key="index"
  94. @click="search(word.getString('content')!)"
  95. >
  96. <text class="text">{{ word.getString('content') }}</text>
  97. </view>
  98. </template>
  99. </view>
  100. </view>
  101. </unicloud-db>
  102. </template>
  103. </view>
  104. </template>
  105. <script lang="uts">
  106. type ArticleAuthor = {
  107. _id: string
  108. nickname: string
  109. }
  110. type ArticleItem = {
  111. _id: string
  112. title: string
  113. publish_date: number
  114. thumbnail: string[]
  115. user_id: ArticleAuthor[]
  116. }
  117. const db = uniCloud.databaseForJQL()
  118. const searchLogDB = db.collection('opendb-search-log')
  119. const cmsArticleDB = db.collection('uni-cms-articles')
  120. const uniIdUsersDB = db.collection('uni-id-users')
  121. const localSearchHistoryKey = '__local_search_history'; // 本地历史存储字段名
  122. const localSearchRecommendHiddenKey = '__local_search_recommend_hidden'; // 本地搜索发现开关字段名
  123. const localSearchHistoryMax = 10; // 本地历史存储最大值
  124. export default {
  125. data() {
  126. const localSearchRecommendHidden = uni.getStorageSync(localSearchRecommendHiddenKey)
  127. return {
  128. searchVal: "",
  129. searchHistory: [] as string[],
  130. searchRecommend: [] as string[],
  131. deleteHistoryLoading: false,
  132. hideSearchRecommend: (localSearchRecommendHidden == "" ? false : localSearchRecommendHidden) as boolean,
  133. showSearchResultPanel: false,
  134. searchResult: [] as ArticleItem[]
  135. }
  136. },
  137. watch: {
  138. hideSearchRecommend(newValue) {
  139. uni.setStorageSync(localSearchRecommendHiddenKey, newValue)
  140. }
  141. },
  142. computed: {
  143. hasSearchValue(): boolean {
  144. return this.searchVal != ""
  145. },
  146. where(): string {
  147. let where = "\"article_status\" == 1"
  148. if (this.searchVal != "") {
  149. where += `&& /${this.searchVal}/.test(title)`
  150. }
  151. return where
  152. },
  153. colList(): any[] {
  154. // 返回文章和用户列表
  155. return [
  156. cmsArticleDB.where(this.where).field('thumbnail,title,publish_date,user_id').getTemp(),
  157. uniIdUsersDB.field('_id,nickname').getTemp()
  158. ]
  159. }
  160. },
  161. mounted() {
  162. // 本地历史存储
  163. const localSearchHistory = uni.getStorageSync(localSearchHistoryKey)
  164. this.searchHistory = (localSearchHistory == "" ? [] as string[] : localSearchHistory) as string[]
  165. },
  166. methods: {
  167. deleteAllSearchHistory() {
  168. uni.showModal({
  169. title: "确定清空搜索历史吗",
  170. confirmText: "删除",
  171. success: (res) => {
  172. if (res.confirm) {
  173. this.deleteSearchHistory(null)
  174. }
  175. }
  176. })
  177. },
  178. deleteSearchHistory(searchText: string | null) {
  179. let history: string[] = []
  180. if (searchText != null) {
  181. history = this.searchHistory.filter((item: string): boolean => item != searchText)
  182. }
  183. this.searchHistory = history
  184. uni.setStorageSync(localSearchHistoryKey, history)
  185. console.log(history.length, 'history.length')
  186. if (history.length <= 0) {
  187. this.deleteHistoryLoading = false
  188. }
  189. },
  190. search(searchText: string) {
  191. searchText = searchText.trim()
  192. if (searchText == "" || this.deleteHistoryLoading) return
  193. // 隐藏键盘
  194. ;(this.$refs['searchBar'] as UniCmsArticleSearchBarComponentPublicInstance)!.hideKeyboard()
  195. // 保存搜索历史
  196. this.setLocalSearchHistory(searchText)
  197. // 显示搜索结果Panel
  198. this.showSearchResultPanel = true
  199. // 搜索
  200. this.loadSearchResult(searchText)
  201. // 添加搜索记录
  202. this.addSearchRecord(searchText)
  203. },
  204. loadSearchResult(searchText: string) {
  205. // 设置查询条件
  206. this.searchVal = searchText
  207. // 延迟0ms后加载数据
  208. setTimeout(() => {
  209. (this.$refs['articleList'] as UniCmsArticleListComponentPublicInstance)!.reLoadList()
  210. }, 0)
  211. },
  212. addSearchRecord(searchText: string) {
  213. const systemInfo = uni.getSystemInfoSync()
  214. /*
  215. 在此处存搜索记录,如果登录则需要存 user_id,若未登录则存device_id
  216. */
  217. searchLogDB.add({
  218. // user_id: device_id,
  219. device_id: systemInfo.deviceId,
  220. // device_uuid: systemInfo.deviceId,
  221. content: searchText,
  222. create_date: Date.now()
  223. })
  224. },
  225. setLocalSearchHistory(searchText: string) {
  226. const history = this.searchHistory.filter((item: string): boolean => item != searchText)
  227. history.unshift(searchText)
  228. if (history.length > localSearchHistoryMax) {
  229. history.pop()
  230. }
  231. this.searchHistory = history
  232. this.deleteHistoryLoading = false
  233. uni.setStorageSync(localSearchHistoryKey, history)
  234. },
  235. reLoadSearchRecommend() {
  236. (this.$refs['udb'] as UniCloudDBElement)!.loadData({
  237. clear: true
  238. })
  239. }
  240. }
  241. }
  242. </script>
  243. <style lang="scss">
  244. .container {
  245. display: flex;
  246. flex-direction: column;
  247. height: 100%;
  248. .search-result {
  249. flex: 1;
  250. display: flex;
  251. flex-direction: column;
  252. }
  253. }
  254. .panel {
  255. margin-top: 40rpx;
  256. padding: 10rpx 20rpx;
  257. &__title {
  258. display: flex;
  259. flex-direction: row;
  260. align-items: center;
  261. justify-content: space-between;
  262. &-text {
  263. flex: 1;
  264. display: flex;
  265. flex-direction: row;
  266. align-items: center;
  267. .text {
  268. color: #3e3e3e;
  269. font-size: 30rpx;
  270. line-height: 36rpx;
  271. font-weight: bold;
  272. }
  273. .icon {
  274. margin-left: 10rpx;
  275. }
  276. }
  277. }
  278. &__after-icon {
  279. margin-left: 10rpx;
  280. }
  281. &__list {
  282. margin-top: 30rpx;
  283. display: flex;
  284. flex-direction: row;
  285. flex-wrap: wrap;
  286. &-item {
  287. background-color: #f6f6f6;
  288. border-radius: 30rpx;
  289. padding: 10rpx 20rpx;
  290. display: flex;
  291. flex-direction: row;
  292. align-items: center;
  293. justify-content: center;
  294. margin-right: 10rpx;
  295. margin-bottom: 10rpx;
  296. .text {
  297. font-size: 26rpx;
  298. color: #666;
  299. }
  300. .icon {
  301. margin-left: 10rpx;
  302. }
  303. }
  304. &-tip {
  305. display: flex;
  306. flex-direction: row;
  307. justify-content: center;
  308. flex: 1;
  309. margin-top: 20rpx;
  310. .text {
  311. font-size: 26rpx;
  312. color: #808080;
  313. }
  314. }
  315. }
  316. }
  317. .delete-history-btns {
  318. display: flex;
  319. flex-direction: row;
  320. align-items: center;
  321. justify-content: center;
  322. .text {
  323. color: #666;
  324. font-size: 22rpx;
  325. margin-left: 20rpx;
  326. &.danger {
  327. color: #c0402b;
  328. }
  329. }
  330. }
  331. </style>