index.vue 9.7 KB

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