CreateStore.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <script setup lang="ts">
  2. import {computed, ref} from 'vue'
  3. import {createStore} from "../../api/store.ts"
  4. import {uploadImage} from "../../api/image.ts"
  5. // 输入框值
  6. const name = ref('')
  7. const location = ref('')
  8. let logoUrl = ref('')
  9. // 用于前端阻拦不合法输入
  10. const hasNameInput = computed(() => name.value != '')
  11. const hasLocationInput = computed(() => location.value != '')
  12. const nameLegal = hasNameInput
  13. const locationLegal = hasLocationInput
  14. const isDragging = ref(false)
  15. const files = ref([])
  16. const fileInputRef = ref(null)
  17. function onChange() {
  18. files.value = [...fileInputRef.value.files]
  19. }
  20. function dragover(e) {
  21. e.preventDefault()
  22. isDragging.value = true
  23. }
  24. function dragleave() {
  25. isDragging.value = false
  26. }
  27. function drop(e) {
  28. e.preventDefault()
  29. fileInputRef.value.files = e.dataTransfer.files
  30. onChange()
  31. isDragging.value = false
  32. }
  33. function generateURL(file) {
  34. let formData = new FormData();
  35. formData.append('file', file);
  36. uploadImage(formData).then(res => {
  37. logoUrl = res.result;
  38. });
  39. let fileSrc = URL.createObjectURL(file);
  40. setTimeout(() => {
  41. URL.revokeObjectURL(fileSrc);
  42. }, 1000);
  43. return fileSrc;
  44. }
  45. function remove(i) {
  46. files.value.splice(i, 1);
  47. console.log(files.value)
  48. }
  49. const createDisabled = computed(() => {
  50. return !(nameLegal.value && locationLegal.value && files.value);
  51. })
  52. function handleCreateStore() {
  53. const payload = {
  54. name: name.value,
  55. location: location.value,
  56. logoUrl: logoUrl
  57. };
  58. createStore(payload).then(res => {
  59. console.log(res);
  60. if (res.code === '000') {
  61. ElMessage({
  62. message: '添加商户成功!',
  63. type: 'success',
  64. center: true,
  65. })
  66. } else if (res.code === '400') {
  67. ElMessage({
  68. message: res.msg,
  69. type: 'error',
  70. center: true,
  71. })
  72. }
  73. });
  74. }
  75. </script>
  76. <template>
  77. <div class="flex flex-col items-center justify-center px-6 py-6 space-y-10">
  78. <span class="flex items-center text-2xl font-semibold text-gray-900">
  79. Blue Whale Shopping Online
  80. </span>
  81. <el-card class="w-full bg-white rounded-lg shadow-md md:mt-0 sm:max-w-md xl:p-0">
  82. <div class="p-6 space-y-4 md:space-y-6 sm:p-8">
  83. <h1 class="text-2xl font-bold leading-tight tracking-tight text-gray-900">
  84. 添加商店
  85. </h1>
  86. <el-form class="space-y-4 md:space-y-6">
  87. <el-form-item>
  88. <label for="name" class="inline-block mb-2 text-sm font-medium text-gray-900">商店名</label>
  89. <input id="name" v-model="name"
  90. required
  91. class="bg-gray-50 border border-gray-300 text-gray-900 focus:outline-primary-600 sm:text-sm rounded-lg block w-full p-2.5"
  92. placeholder="请输入商店名">
  93. </el-form-item>
  94. <el-form-item>
  95. <label for="location" class="inline-block mb-2 text-sm font-medium text-gray-900">商店地址</label>
  96. <input id="location" v-model="location"
  97. required
  98. class="bg-gray-50 border border-gray-300 text-gray-900 focus:outline-primary-600 sm:text-sm rounded-lg block w-full p-2.5"
  99. placeholder="楼层-门牌号 如:3楼-305">
  100. </el-form-item>
  101. <el-form-item>
  102. <label for="logoUrl" class="inline-block mb-2 text-sm font-medium text-gray-900">商店Logo</label>
  103. <div class="main">
  104. <div
  105. class="dropzone-container"
  106. @dragover="dragover"
  107. @dragleave="dragleave"
  108. @drop="drop"
  109. >
  110. <input
  111. type="file"
  112. multiple
  113. name="file"
  114. id="fileInput"
  115. class="hidden-input"
  116. @change="onChange"
  117. ref="fileInputRef"
  118. accept=".pdf,.jpg,.jpeg,.png"
  119. />
  120. <label for="fileInput" class="file-label">
  121. <div v-if="isDragging">释放以将文件放到此处。</div>
  122. <div v-else>将文件拖到此处或单击此处上传。</div>
  123. </label>
  124. <div class="preview-container mt-4" v-if="files.length">
  125. <div v-for="file in files" :key="file.name" class="preview-card">
  126. <div>
  127. <img class="preview-img" :src="generateURL(file)"/>
  128. <p>
  129. {{ file.name }}
  130. </p>
  131. </div>
  132. <div>
  133. <button
  134. class="ml-2"
  135. type="button"
  136. @click="remove(files.indexOf(file))"
  137. title="Remove file"
  138. >
  139. <b>×</b>
  140. </button>
  141. </div>
  142. </div>
  143. </div>
  144. </div>
  145. </div>
  146. </el-form-item>
  147. <el-form-item>
  148. <button @click.prevent="handleCreateStore()"
  149. :disabled="createDisabled"
  150. class="block my-3 w-full text-white bg-primary-600 hover:bg-primary-700 focus:ring-4 focus:outline-none focus:ring-primary-300 font-medium rounded-lg text-sm px-5 py-3 text-center disabled:bg-primary-100">
  151. 添加
  152. </button>
  153. </el-form-item>
  154. </el-form>
  155. </div>
  156. </el-card>
  157. </div>
  158. </template>
  159. <style scoped>
  160. </style>