index.vue 9.8 KB

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