index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <script>
  2. import {handle} from "../../../until/handle";
  3. import axios from "axios";
  4. import {apiMap, baseUrl} from "../../../map/apiMap";
  5. import {rCode} from "../../../map/rcodeMap_esm";
  6. import RoundedTitle from "../../../components/public/roundedTitle.vue";
  7. import copy from "../../../until/copy";
  8. import {isEmpty} from "../../../until/typeTool";
  9. import TableSelect from "../../../components/public/tableSelect.vue";
  10. import dbField_esm from "../../../map/dbField_esm";
  11. import ImageViewer from "../../../components/public/imageViewer.vue";
  12. import {timestampToTime} from "../../../until/time";
  13. const newsColumns = [
  14. {
  15. title: "ID",
  16. dataIndex: 'id',
  17. width: '3%',
  18. },
  19. {
  20. title: "文章标题",
  21. dataIndex: 'title',
  22. width: '15%',
  23. },
  24. {
  25. title: "文章类型",
  26. dataIndex: 'type_key',
  27. width: '5%',
  28. scopedSlots: {customRender: 'newsType'},
  29. },
  30. {
  31. title: "文章封面",
  32. dataIndex: 'image',
  33. width: '20%',
  34. scopedSlots: {customRender: 'image'},
  35. },
  36. {
  37. title: "文章作者",
  38. dataIndex: 'source',
  39. width: '10%',
  40. },
  41. {
  42. title: "阅读量",
  43. dataIndex: 'hits',
  44. width: '5%',
  45. },
  46. {
  47. title: "创建时间",
  48. dataIndex: 'date_time',
  49. scopedSlots: {customRender: 'time'},
  50. width: '10%',
  51. },
  52. {
  53. title: "操作",
  54. scopedSlots: {customRender: 'operation'},
  55. width: '25%',
  56. },
  57. ]
  58. export default {
  59. name: 'news',
  60. components: {ImageViewer, TableSelect, RoundedTitle},
  61. computed: {
  62. // 产品类型数据
  63. newsTypes(){
  64. let arr = [];
  65. if (this.form.pType.val === dbField_esm.db_base.newsType.all){
  66. arr = this.$store.getters.allNewsTypes;
  67. }else if (this.form.pType.val === dbField_esm.db_base.newsType.news){
  68. arr = this.$store.getters.newsTypes;
  69. }else if (this.form.pType.val === dbField_esm.db_base.newsType.solution){
  70. arr = this.$store.getters.solutionTypes;
  71. }
  72. if (arr[0].key !== 'all'){
  73. // 添加 all
  74. arr.unshift({text: '全部', key: 'all' });
  75. }
  76. return arr;
  77. },
  78. },
  79. data(){
  80. return {
  81. loading: false,
  82. newsColumns: newsColumns,
  83. dataList: [],
  84. page: 1,
  85. pageSize: 10,
  86. total: 0,
  87. form: {
  88. pType: {
  89. val: dbField_esm.db_base.newsType.all,
  90. oldVal: dbField_esm.db_base.newsType.all,
  91. init: dbField_esm.db_base.newsType.all,
  92. msg: '',
  93. state: 0,
  94. options: [
  95. {text: '全部', key: dbField_esm.db_base.newsType.all},
  96. {text: '新闻', key: dbField_esm.db_base.newsType.news},
  97. {text: '解决方案', key: dbField_esm.db_base.newsType.solution},
  98. ]
  99. },
  100. key: {
  101. val: '',
  102. oldVal: '',
  103. init: '',
  104. msg: '',
  105. state: 0
  106. },
  107. type: {
  108. val: '',
  109. init: '',
  110. msg: '',
  111. state: 0,
  112. options: []
  113. },
  114. },
  115. loadErr: false,
  116. }
  117. },
  118. async asyncData(){
  119. let searchParam = {
  120. page: 1,
  121. pageSize: 10,
  122. key: '',
  123. type: 'all'
  124. };
  125. console.log('搜索文章数据');
  126. let [err,res] = await handle(axios.get(baseUrl + apiMap.searchAllNews.path,{params: searchParam}));
  127. if(err){
  128. console.error(err);
  129. return {};
  130. }
  131. let result = res.data;
  132. if(result.code === rCode.OK){
  133. return {
  134. dataList: result.data,
  135. page: 1,
  136. pageSize: 10,
  137. total: result.total,
  138. }
  139. }else{
  140. console.log(`搜索数据失败,${result.msg}`);
  141. return {}
  142. }
  143. return {}
  144. },
  145. beforeMount() {
  146. this.form.type.options = this.newsTypes;
  147. this.form.type.val = this.form.type.options[0].key;
  148. this.form.type.oldVal = this.form.type.val;
  149. this.form.type.init = this.form.type.val;
  150. },
  151. methods: {
  152. timestampToTime,
  153. copy(str){
  154. copy(str).then(()=>{
  155. this.$message.success('已经将内容复制到剪切板')
  156. }).catch(err=>{
  157. console.error(err.message);
  158. this.$message.error('内容复制失败,请手动复制内容')
  159. })
  160. },
  161. keyLight(str){
  162. return str.replace(this.form.key.oldVal,`<span class="text-red-500 text-xl">${this.form.key.oldVal}</span>`)
  163. },
  164. // 搜索
  165. async searchExecute(params = {}){
  166. let err,res;
  167. let url = apiMap.searchAllNews.path;
  168. console.log(params);
  169. // 如果没有传入参数,则使用表单数据
  170. if(isEmpty(params.key)){
  171. params.key = this.form.key.oldVal;
  172. }
  173. if(isEmpty(params.type)){
  174. params.type = this.form.type.oldVal;
  175. }
  176. if(isEmpty(params.pType)){
  177. params.pType = this.form.pType.oldVal;
  178. }
  179. params.p = this.page;
  180. params.l = this.pageSize;
  181. console.log(`搜索产品数据:${JSON.stringify(params)}`);
  182. this.loading = true;
  183. [err,res] = await handle(axios.get(url,{params}));
  184. this.loading = false;
  185. if(err){
  186. return this.$message.error(err.message);
  187. }
  188. let result = res.data;
  189. if(result.code === rCode.OK){
  190. console.log(`搜索成功`)
  191. this.form.key.oldVal = params.key;
  192. this.form.type.oldVal = params.type;
  193. this.form.pType.oldVal = params.pType;
  194. if (params.p === 1){
  195. this.$message.success('搜索成功');
  196. this.total = result.total;
  197. }
  198. // 触发更新,设置数据
  199. this.$nextTick(()=>{
  200. this.dataList = result.data;
  201. })
  202. }else{
  203. this.$message.error(result.msg);
  204. }
  205. },
  206. // 页码改变
  207. pageChange(page){
  208. console.log(`页码改变:${page}`);
  209. this.page = page;
  210. console.log(this.page);
  211. this.searchExecute();
  212. },
  213. // 页数改变
  214. pageSizeChange(pageSize){
  215. console.log('页数改变');
  216. this.pageSize = pageSize;
  217. this.searchExecute();
  218. },
  219. onSearchHandle(){
  220. this.page = 1;
  221. console.log(this.form.type.val);
  222. this.searchExecute({
  223. key: this.form.key.val,
  224. type: this.form.type.val,
  225. pType: this.form.pType.val
  226. });
  227. },
  228. onPTypeChange(){
  229. console.log('文章主类型改变');
  230. console.log(this.newsTypes);
  231. this.form.type.val = this.newsTypes[0].key;
  232. this.form.type.oldVal = this.newsTypes[0].key;
  233. this.form.type.init = this.newsTypes[0].key;
  234. this.form.type.options = this.newsTypes;
  235. this.$nextTick(()=>{
  236. // this.form.type.val = this.newsTypes[0].key;
  237. // this.form.type.oldVal = this.newsTypes[0].key;
  238. // this.form.type.init = this.newsTypes[0].key;
  239. // this.form.type.options = this.newsTypes;
  240. this.searchExecute({
  241. pType: this.form.pType.val
  242. });
  243. })
  244. },
  245. onTypeChange(){
  246. this.$nextTick(()=>{
  247. this.searchExecute({
  248. type: this.form.type.val
  249. });
  250. })
  251. },
  252. refreshHandel(){
  253. this.page = 1;
  254. this.searchExecute();
  255. },
  256. getProTypeText(key){
  257. let type = this.newsTypes.find(item=>item.key === key);
  258. return type ? type.text : '';
  259. },
  260. getTypeText(key){
  261. let type = this.form.type.options.find(item=>item.key === key);
  262. return type ? type.text : key;
  263. }
  264. }
  265. }
  266. </script>
  267. <template>
  268. <div class="w-full p-2">
  269. <rounded-title class="text-xl">
  270. <span>文章管理</span>
  271. <a href="/manger/news/type"
  272. class="px-10 h-full ml-5 rounded bg-blue-400 text-white
  273. cursor-pointer hover:text-orange-500 ">
  274. 文章分类管理</a>
  275. <a href="/manger/product/add" class="px-10 h-full ml-5 rounded bg-blue-400 text-white cursor-pointer hover:text-orange-500 ">新增文章</a>
  276. </rounded-title>
  277. <div class="w-full rounded border mt-2 bg-white p-2">
  278. <div class="w-full flex h-14 items-center justify-between border-b">
  279. <div class="flex">
  280. <!-- 文章主分类-->
  281. <a-radio-group class="flex flex-shrink-0 items-center" v-model="form.pType.val" @change="onPTypeChange">
  282. <a-radio-button class="" :value="item.key" v-for="item in form.pType.options" :key="item.key">{{item.text}}</a-radio-button>
  283. </a-radio-group>
  284. <table-select
  285. class="w-48 !mx-3 flex-shrink-0"
  286. :options="form.type.options"
  287. v-model="form.type.val"
  288. @change="onTypeChange"
  289. />
  290. <a-input-search class="!ml-2 w-64" type="text" allow-clear placeholder="产品关键字"
  291. @search="onSearchHandle"
  292. :loading="loading" v-model="form.key.val" />
  293. </div>
  294. <div class="flex">
  295. <a-button class="ml-2" type="primary" @click="refreshHandel" :loading="loading">刷新</a-button>
  296. </div>
  297. </div>
  298. <div class="w-full">
  299. <a-table
  300. :columns="newsColumns"
  301. :loading="loading"
  302. :row-key="record => record.id"
  303. :pagination="false"
  304. :data-source="dataList"
  305. >
  306. <template slot="newsType" slot-scope="text">
  307. <span>{{getTypeText(text)}}</span>
  308. </template>
  309. <template slot="image" slot-scope="text,record">
  310. <image-viewer :src="text?text.charAt(0) == '/'? text : '/public/'+text : '' " :alt="record.name" />
  311. </template>
  312. <template slot="keyLight" class="flex" slot-scope="text">
  313. <div class="w-full flex items-center">
  314. <div v-html="keyLight(text)"></div>
  315. <svg-icon :name="'copy'" :icon-class="'copy'" class="text-2xl cursor-pointer mx-2" title="点击复制" @click.native="copy(text)" />
  316. </div>
  317. </template>
  318. <template slot="time" slot-scope="text">
  319. <span>{{timestampToTime(text)}}</span>
  320. </template>
  321. <template class="flex justify-center" slot="operation" slot-scope="text,record">
  322. <a-button type="primary">
  323. <a :href="`/manger/news/info?id=${record.id}`">文章详情</a>
  324. </a-button>
  325. <a-button class="mx-3" type="primary">
  326. <a :href="`/manger/news/edit?id=${record.id}`">编辑文章</a>
  327. </a-button>
  328. </template>
  329. </a-table>
  330. <a-pagination
  331. class="mt-2"
  332. :total="total"
  333. :page-size="pageSize"
  334. :current="page"
  335. @change="pageChange"
  336. @show-size-change="pageSizeChange"
  337. />
  338. </div>
  339. </div>
  340. </div>
  341. </template>
  342. <style scoped>
  343. </style>