index.vue 7.7 KB

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