CreateStore.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.value = 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.value
  57. };
  58. createStore(payload).then(res => {
  59. if (res.code === '000') {
  60. ElMessage({
  61. message: '添加商户成功!',
  62. type: 'success',
  63. center: true,
  64. })
  65. name.value = '';
  66. location.value = '';
  67. logoUrl.value = '';
  68. files.value.splice(0);
  69. } else if (res.code === '400') {
  70. ElMessage({
  71. message: res.msg,
  72. type: 'error',
  73. center: true,
  74. })
  75. }
  76. });
  77. }
  78. </script>
  79. <template>
  80. <el-main class="main-frame">
  81. <el-card class="current-card">
  82. <div>
  83. <h1 >
  84. 添加商店
  85. </h1>
  86. <el-form>
  87. <el-form-item>
  88. <label for="name">商店名</label>
  89. <el-input id="name" v-model="name"
  90. required
  91. placeholder="请输入商店名"/>
  92. </el-form-item>
  93. <el-form-item>
  94. <label for="location">商店地址</label>
  95. <el-input id="location" v-model="location"
  96. required
  97. placeholder="楼层-门牌号 如:3楼-305" />
  98. </el-form-item>
  99. <el-form-item>
  100. <label for="logoUrl">商店Logo</label>
  101. <div class="main">
  102. <div
  103. class="dropzone-container"
  104. @dragover="dragover"
  105. @dragleave="dragleave"
  106. @drop="drop"
  107. >
  108. <input
  109. type="file"
  110. multiple
  111. name="file"
  112. id="fileInput"
  113. class="hidden-input"
  114. @change="onChange"
  115. ref="fileInputRef"
  116. accept=".pdf,.jpg,.jpeg,.png"
  117. />
  118. <label for="fileInput" class="file-label">
  119. <div v-if="isDragging">释放以将文件放到此处。</div>
  120. <div v-else>将文件拖到此处或单击此处上传。</div>
  121. </label>
  122. <div class="preview-container mt-4" v-if="files.length">
  123. <div v-for="file in files" :key="file.name" class="preview-card">
  124. <div>
  125. <img class="preview-img" :src="generateURL(file)"/>
  126. <p>
  127. {{ file.name }}
  128. </p>
  129. </div>
  130. <div>
  131. <button
  132. class="ml-2"
  133. type="button"
  134. @click="remove(files.indexOf(file))"
  135. title="Remove file"
  136. >
  137. <b>×</b>
  138. </button>
  139. </div>
  140. </div>
  141. </div>
  142. </div>
  143. </div>
  144. </el-form-item>
  145. <el-form-item>
  146. <button @click.prevent="handleCreateStore()"
  147. :disabled="createDisabled"
  148. 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">
  149. 添加
  150. </button>
  151. </el-form-item>
  152. </el-form>
  153. </div>
  154. </el-card>
  155. </el-main>
  156. </template>
  157. <style scoped>
  158. .main-frame {
  159. width: 100%;
  160. height: 100%;
  161. display: flex;
  162. align-items: center;
  163. justify-content: center;
  164. }
  165. .current-card {
  166. width: 60%;
  167. padding: 10px;
  168. }
  169. </style>