index.vue 7.4 KB

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