index.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <template>
  2. <div class="go-canvas-setting">
  3. <n-form inline :label-width="45" size="small" label-placement="left">
  4. <n-form-item label="宽度">
  5. <!-- 尺寸选择 -->
  6. <n-input-number
  7. size="small"
  8. v-model:value="canvasConfig.width"
  9. :disabled="editCanvas.lockScale"
  10. :validator="validator"
  11. @update:value="changeSizeHandle"
  12. ></n-input-number>
  13. </n-form-item>
  14. <n-form-item label="高度">
  15. <n-input-number
  16. size="small"
  17. v-model:value="canvasConfig.height"
  18. :disabled="editCanvas.lockScale"
  19. :validator="validator"
  20. @update:value="changeSizeHandle"
  21. ></n-input-number>
  22. </n-form-item>
  23. </n-form>
  24. <n-card class="upload-box">
  25. <n-upload
  26. v-model:file-list="uploadFileListRef"
  27. :show-file-list="false"
  28. :customRequest="customRequest"
  29. :onBeforeUpload="beforeUploadHandle"
  30. >
  31. <n-upload-dragger>
  32. <img
  33. v-if="canvasConfig.backgroundImage"
  34. class="upload-show"
  35. :src="canvasConfig.backgroundImage"
  36. alt="背景"
  37. />
  38. <div class="upload-img" v-show="!canvasConfig.backgroundImage">
  39. <img src="@/assets/images/canvas/noImage.png" />
  40. <n-text class="upload-desc" depth="3">
  41. 背景图需小于 {{ backgroundImageSize }}M ,格式为 png/jpg/gif
  42. 的文件
  43. </n-text>
  44. </div>
  45. </n-upload-dragger>
  46. </n-upload>
  47. </n-card>
  48. <n-space vertical :size="12">
  49. <n-space>
  50. <n-text>背景色</n-text>
  51. <n-color-picker
  52. style="width: 326px;"
  53. :showPreview="true"
  54. :swatches="swatchesColors"
  55. v-model:value="canvasConfig.background"
  56. ></n-color-picker>
  57. </n-space>
  58. <n-space>
  59. <n-text>颜色应用</n-text>
  60. <n-switch
  61. size="small"
  62. v-model:value="canvasConfig.selectColor"
  63. :loading="switchSelectColorLoading"
  64. :round="false"
  65. :disabled="!canvasConfig.backgroundImage"
  66. :onUpdate="switchSelectColorHandle"
  67. ></n-switch>
  68. </n-space>
  69. <n-space>
  70. <n-text>背景控制</n-text>
  71. <n-button size="small" :disabled="!canvasConfig.backgroundImage" @click="clearImage">清除背景图</n-button>
  72. <n-button size="small" :disabled="!canvasConfig.background" @click="clearColor">清除颜色</n-button>
  73. </n-space>
  74. <n-space>
  75. <n-text>预览方式</n-text>
  76. <n-button-group>
  77. <n-button
  78. ghost
  79. v-for="item in previewTypeList"
  80. :key="item.key"
  81. :type="canvasConfig.previewScaleType === item.key ? 'primary' : 'tertiary'"
  82. size="small"
  83. @click="selectPreviewType(item.key)">
  84. <n-tooltip :show-arrow="false" trigger="hover">
  85. <template #trigger>
  86. <n-icon size="18">
  87. <component :is="item.icon"></component>
  88. </n-icon>
  89. </template>
  90. {{ item.desc }}
  91. </n-tooltip>
  92. </n-button>
  93. </n-button-group>
  94. </n-space>
  95. </n-space>
  96. <!-- 滤镜 -->
  97. <styles-setting :is-canvas="true" :chartStyles="canvasConfig"></styles-setting>
  98. <n-divider style="margin: 10px 0;"></n-divider>
  99. <!-- 主题选择和全局配置 -->
  100. <n-tabs class="tabs-box" size="small" type="segment">
  101. <n-tab-pane
  102. v-for="item in globalTabList"
  103. :key="item.key"
  104. :name="item.key"
  105. size="small"
  106. display-directive="show:lazy"
  107. >
  108. <template #tab>
  109. <n-space>
  110. <span>{{ item.title }}</span>
  111. <n-icon size="16" class="icon-position">
  112. <component :is="item.icon"></component>
  113. </n-icon>
  114. </n-space>
  115. </template>
  116. <component :is="item.render"></component>
  117. </n-tab-pane>
  118. </n-tabs>
  119. </div>
  120. </template>
  121. <script setup lang="ts">
  122. import { ref, nextTick } from 'vue'
  123. import { backgroundImageSize } from '@/settings/designSetting'
  124. import { FileTypeEnum } from '@/enums/fileTypeEnum'
  125. import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
  126. import { EditCanvasConfigEnum } from '@/store/modules/chartEditStore/chartEditStore.d'
  127. import { StylesSetting } from '@/components/Pages/ChartItemSetting'
  128. import { UploadCustomRequestOptions } from 'naive-ui'
  129. import { fileToUrl, loadAsyncComponent, fetchRouteParamsLocation } from '@/utils'
  130. import { PreviewScaleEnum } from '@/enums/styleEnum'
  131. import { ResultEnum } from '@/enums/httpEnum'
  132. import { icon } from '@/plugins'
  133. import { uploadFile} from '@/api/path/project'
  134. const { ColorPaletteIcon } = icon.ionicons5
  135. const { ZAxisIcon, ScaleIcon, FitToScreenIcon, FitToHeightIcon, FitToWidthIcon } = icon.carbon
  136. const chartEditStore = useChartEditStore()
  137. const canvasConfig = chartEditStore.getEditCanvasConfig
  138. const editCanvas = chartEditStore.getEditCanvas
  139. const uploadFileListRef = ref()
  140. const switchSelectColorLoading = ref(false)
  141. const ChartThemeColor = loadAsyncComponent(() =>
  142. import('./components/ChartThemeColor/index.vue')
  143. )
  144. const ChartDataSetting = loadAsyncComponent(() =>
  145. import('./components/ChartDataSetting/index.vue')
  146. )
  147. // 北京默认展示颜色列表
  148. const swatchesColors = [
  149. '#232324',
  150. '#2a2a2b',
  151. '#313132',
  152. '#373739',
  153. '#757575',
  154. '#e0e0e0',
  155. '#eeeeee',
  156. '#fafafa'
  157. ]
  158. const globalTabList = [
  159. {
  160. key: 'ChartTheme',
  161. title: '主题颜色',
  162. icon: ColorPaletteIcon,
  163. render: ChartThemeColor
  164. },
  165. {
  166. key: 'ChartSysSetting',
  167. title: '数据配置',
  168. icon: ZAxisIcon,
  169. render: ChartDataSetting
  170. }
  171. ]
  172. const previewTypeList = [
  173. {
  174. key: PreviewScaleEnum.FIT,
  175. title: '自适应',
  176. icon: ScaleIcon,
  177. desc: '自适应比例展示,页面会有留白'
  178. },
  179. {
  180. key: PreviewScaleEnum.SCROLL_Y,
  181. title: 'Y轴滚动',
  182. icon: FitToWidthIcon,
  183. desc: 'X轴铺满,Y轴自适应滚动'
  184. },
  185. {
  186. key: PreviewScaleEnum.SCROLL_X,
  187. title: 'X轴滚动',
  188. icon: FitToHeightIcon,
  189. desc: 'Y轴铺满,X轴自适应滚动'
  190. },
  191. {
  192. key: PreviewScaleEnum.FULL,
  193. title: '铺满',
  194. icon: FitToScreenIcon,
  195. desc: '强行拉伸画面,填充所有视图'
  196. },
  197. ]
  198. // 画布尺寸规则
  199. const validator = (x: number) => x > 50
  200. // 修改尺寸
  201. const changeSizeHandle = () => {
  202. chartEditStore.computedScale()
  203. }
  204. // 上传图片前置处理
  205. //@ts-ignore
  206. const beforeUploadHandle = async ({ file }) => {
  207. uploadFileListRef.value = []
  208. const type = file.file.type
  209. const size = file.file.size
  210. if (size > 1024 * 1024 * backgroundImageSize) {
  211. window['$message'].warning(
  212. `图片超出 ${backgroundImageSize}M 限制,请重新上传!`
  213. )
  214. return false
  215. }
  216. if (type !== FileTypeEnum.PNG && type !== FileTypeEnum.JPEG && type !== FileTypeEnum.GIF) {
  217. window['$message'].warning('文件格式不符合,请重新上传!')
  218. return false
  219. }
  220. return true
  221. }
  222. // 清除背景
  223. const clearImage = () => {
  224. chartEditStore.setEditCanvasConfig(
  225. EditCanvasConfigEnum.BACKGROUND_IAMGE,
  226. undefined
  227. )
  228. chartEditStore.setEditCanvasConfig(
  229. EditCanvasConfigEnum.SELECT_COLOR,
  230. true
  231. )
  232. }
  233. // 清除颜色
  234. const clearColor = () => {
  235. chartEditStore.setEditCanvasConfig(
  236. EditCanvasConfigEnum.BACKGROUND,
  237. undefined
  238. )
  239. if (canvasConfig.backgroundImage) {
  240. chartEditStore.setEditCanvasConfig(
  241. EditCanvasConfigEnum.SELECT_COLOR,
  242. false
  243. )
  244. }
  245. }
  246. // 启用/关闭 颜色
  247. const switchSelectColorHandle = () => {
  248. switchSelectColorLoading.value = true
  249. setTimeout(() => {
  250. switchSelectColorLoading.value = false
  251. }, 1000)
  252. }
  253. // 自定义上传操作
  254. const customRequest = (options: UploadCustomRequestOptions) => {
  255. const { file } = options
  256. nextTick(async () => {
  257. if (file.file) {
  258. // 修改名称
  259. const newNameFile = new File(
  260. [file.file],
  261. `${fetchRouteParamsLocation()}_index_background.png`,
  262. { type: file.file.type }
  263. )
  264. let uploadParams = new FormData()
  265. uploadParams.append('object', newNameFile)
  266. const uploadRes:any = await uploadFile(uploadParams)
  267. if(uploadRes.code === ResultEnum.SUCCESS) {
  268. chartEditStore.setEditCanvasConfig(
  269. EditCanvasConfigEnum.BACKGROUND_IAMGE,
  270. uploadRes.data.objectContent.httpRequest.uri
  271. )
  272. chartEditStore.setEditCanvasConfig(
  273. EditCanvasConfigEnum.SELECT_COLOR,
  274. false
  275. )
  276. return
  277. }
  278. window['$message'].error('添加图片失败,请稍后重试!')
  279. } else {
  280. window['$message'].error('添加图片失败,请稍后重试!')
  281. }
  282. })
  283. }
  284. // 选择预览方式
  285. const selectPreviewType = (key: PreviewScaleEnum) => {
  286. chartEditStore.setEditCanvasConfig(EditCanvasConfigEnum.PREVIEW_SCALE_TYPE, key)
  287. }
  288. </script>
  289. <style lang="scss" scoped>
  290. $updloadWidth: 326px;
  291. $updloadHeight: 193px;
  292. @include go(canvas-setting) {
  293. padding-top: 20px;
  294. .upload-box {
  295. cursor: pointer;
  296. margin-bottom: 20px;
  297. @include deep() {
  298. .n-card__content {
  299. padding: 0;
  300. overflow: hidden;
  301. }
  302. .n-upload-dragger {
  303. padding: 5px;
  304. width: $updloadWidth;
  305. }
  306. }
  307. .upload-show {
  308. width: -webkit-fill-available;
  309. height: $updloadHeight;
  310. border-radius: 5px;
  311. }
  312. .upload-img {
  313. display: flex;
  314. flex-direction: column;
  315. align-items: center;
  316. img {
  317. height: 150px;
  318. }
  319. .upload-desc {
  320. padding: 10px 0;
  321. }
  322. }
  323. }
  324. .icon-position {
  325. padding-top: 2px;
  326. }
  327. .tabs-box {
  328. margin-top: 20px;
  329. }
  330. }
  331. </style>