index.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. :validator="validator"
  10. @update:value="chartEditStoreStore.computedScale"
  11. />
  12. </n-form-item>
  13. <n-form-item label="高度">
  14. <n-input-number
  15. size="small"
  16. v-model:value="canvasConfig.height"
  17. :validator="validator"
  18. @update:value="chartEditStoreStore.computedScale"
  19. />
  20. </n-form-item>
  21. </n-form>
  22. <n-card class="upload-box">
  23. <n-upload
  24. v-model:file-list="uploadFileListRef"
  25. :show-file-list="false"
  26. :customRequest="customRequest"
  27. :onBeforeUpload="beforeUploadHandle"
  28. >
  29. <n-upload-dragger>
  30. <img
  31. v-if="canvasConfig.backgroundImage"
  32. class="upload-show"
  33. :src="canvasConfig.backgroundImage"
  34. alt="背景"
  35. />
  36. <div class="upload-img" v-show="!canvasConfig.backgroundImage">
  37. <img src="@/assets/images/canvas/noImage.png" />
  38. <n-text class="upload-desc" depth="3">
  39. 背景图需小于 {{ backgroundImageSize }}M ,格式为 png/jpg/gif
  40. 的文件
  41. </n-text>
  42. </div>
  43. </n-upload-dragger>
  44. </n-upload>
  45. </n-card>
  46. <n-space vertical :size="12">
  47. <n-space>
  48. <n-text>背景色</n-text>
  49. <n-color-picker
  50. style="width: 326px;"
  51. :showPreview="true"
  52. :swatches="[
  53. '#232324',
  54. '#2a2a2b',
  55. '#313132',
  56. '#373739',
  57. '#757575',
  58. '#e0e0e0',
  59. '#eeeeee',
  60. '#fafafa'
  61. ]"
  62. v-model:value="canvasConfig.background"
  63. />
  64. </n-space>
  65. <n-space>
  66. <n-text>使用颜色</n-text>
  67. <n-switch
  68. size="small"
  69. v-model:value="canvasConfig.selectColor"
  70. :loading="switchSelectColorLoading"
  71. :round="false"
  72. :disabled="!canvasConfig.backgroundImage"
  73. :onUpdate="switchSelectColorHandle"
  74. />
  75. </n-space>
  76. <n-space>
  77. <n-text>背景</n-text>
  78. <n-button
  79. size="small"
  80. :disabled="!canvasConfig.backgroundImage"
  81. @click="clearImage"
  82. >
  83. 清除背景图
  84. </n-button>
  85. <n-button
  86. size="small"
  87. :disabled="!canvasConfig.background"
  88. @click="clearColor"
  89. >
  90. 清除颜色
  91. </n-button>
  92. </n-space>
  93. </n-space>
  94. <n-divider />
  95. <!-- 主题选择和全局配置 -->
  96. <n-tabs class="tabs-box" size="small" type="segment">
  97. <n-tab-pane
  98. v-for="item in globalTabList"
  99. :key="item.key"
  100. :name="item.key"
  101. size="small"
  102. display-directive="show:lazy"
  103. >
  104. <template #tab>
  105. <n-space>
  106. <span>{{ item.title }}</span>
  107. <n-icon size="16" class="icon-position">
  108. <component :is="item.icon"></component>
  109. </n-icon>
  110. </n-space>
  111. </template>
  112. <component :is="item.render"></component>
  113. </n-tab-pane>
  114. </n-tabs>
  115. </div>
  116. </template>
  117. <script setup lang="ts">
  118. import { ref, nextTick } from 'vue'
  119. import { backgroundImageSize } from '@/settings/designSetting'
  120. import { useChartEditStoreStore } from '@/store/modules/chartEditStore/chartEditStore'
  121. import { EditCanvasConfigEnum } from '@/store/modules/chartEditStore/chartEditStore.d'
  122. import { UploadCustomRequestOptions } from 'naive-ui'
  123. import { ChartTheme } from './components/ChartTheme/index'
  124. import { ChartSysSetting } from './components/ChartSysSetting/index'
  125. import { fileToUrl } from '@/utils'
  126. import { icon } from '@/plugins'
  127. const { ColorPaletteIcon } = icon.ionicons5
  128. const { ZAxisIcon } = icon.carbon
  129. const chartEditStoreStore = useChartEditStoreStore()
  130. const canvasConfig = chartEditStoreStore.getEditCanvasConfig
  131. const uploadFileListRef = ref()
  132. const switchSelectColorLoading = ref(false)
  133. // 页面设置
  134. const globalTabList = [
  135. {
  136. key: 'ChartTheme',
  137. title: '主题',
  138. icon: ColorPaletteIcon,
  139. render: ChartTheme
  140. },
  141. {
  142. key: 'ChartSysSetting',
  143. title: '全局图表',
  144. icon: ZAxisIcon,
  145. render: ChartSysSetting
  146. }
  147. ]
  148. // 规则
  149. const validator = (x: number) => x > 50
  150. // 前置处理
  151. //@ts-ignore
  152. const beforeUploadHandle = async ({ file }) => {
  153. uploadFileListRef.value = []
  154. const type = file.file.type
  155. const size = file.file.size
  156. if (size > 1024 * 1024 * backgroundImageSize) {
  157. window['$message'].warning(
  158. `图片超出 ${backgroundImageSize}M 限制,请重新上传!`
  159. )
  160. return false
  161. }
  162. if (type !== 'image/png' && type !== 'image/jpeg' && type !== 'image/gif') {
  163. window['$message'].warning('文件格式不符合,请重新上传!')
  164. return false
  165. }
  166. return true
  167. }
  168. // 清除背景
  169. const clearImage = () => {
  170. chartEditStoreStore.setCanvasConfig(
  171. EditCanvasConfigEnum.BACKGROUND_IAMGE,
  172. undefined
  173. )
  174. chartEditStoreStore.setCanvasConfig(EditCanvasConfigEnum.SELECT_COLOR, true)
  175. }
  176. // 清除颜色
  177. const clearColor = () => {
  178. chartEditStoreStore.setCanvasConfig(
  179. EditCanvasConfigEnum.BACKGROUND,
  180. undefined
  181. )
  182. if (canvasConfig.backgroundImage) {
  183. chartEditStoreStore.setCanvasConfig(
  184. EditCanvasConfigEnum.SELECT_COLOR,
  185. false
  186. )
  187. }
  188. }
  189. // 启用/关闭 颜色
  190. const switchSelectColorHandle = () => {
  191. switchSelectColorLoading.value = true
  192. setTimeout(() => {
  193. switchSelectColorLoading.value = false
  194. }, 1000)
  195. }
  196. // 自定义上传操作
  197. const customRequest = (options: UploadCustomRequestOptions) => {
  198. const {
  199. file,
  200. data,
  201. headers,
  202. withCredentials,
  203. action,
  204. onFinish,
  205. onError,
  206. onProgress
  207. } = options
  208. nextTick(() => {
  209. if (file.file) {
  210. const ImageUrl = fileToUrl(file.file)
  211. chartEditStoreStore.setCanvasConfig(
  212. EditCanvasConfigEnum.BACKGROUND_IAMGE,
  213. ImageUrl
  214. )
  215. chartEditStoreStore.setCanvasConfig(
  216. EditCanvasConfigEnum.SELECT_COLOR,
  217. false
  218. )
  219. } else {
  220. window['$message'].error('添加图片失败,请稍后重试!')
  221. }
  222. })
  223. }
  224. </script>
  225. <style lang="scss" scoped>
  226. $updloadWidth: 326px;
  227. $updloadHeight: 193px;
  228. @include go(canvas-setting) {
  229. padding-top: 20px;
  230. .upload-box {
  231. cursor: pointer;
  232. margin-bottom: 20px;
  233. @include deep() {
  234. .n-card__content {
  235. padding: 0;
  236. overflow: hidden;
  237. }
  238. .n-upload-dragger {
  239. padding: 5px;
  240. width: $updloadWidth;
  241. }
  242. }
  243. .upload-show {
  244. width: -webkit-fill-available;
  245. height: $updloadHeight;
  246. border-radius: 5px;
  247. }
  248. .upload-img {
  249. display: flex;
  250. flex-direction: column;
  251. align-items: center;
  252. img {
  253. height: 150px;
  254. }
  255. .upload-desc {
  256. padding: 10px 0;
  257. }
  258. }
  259. }
  260. .icon-position {
  261. padding-top: 2px;
  262. }
  263. }
  264. </style>