index.vue 7.5 KB

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