school-photo-upload-box.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <template>
  2. <view class="content">
  3. <!-- 上传到区域 -->
  4. <view class="box" v-if="type == 2">
  5. <view class="box_title">
  6. <text class="text">*</text>
  7. 上传到
  8. </view>
  9. <picker :range="listRange" range-key="name" @change="changeMode">
  10. <view class="box_input">
  11. <view class="picker" :class="{ pick: form.categoryName }">{{ form.categoryName ? form.categoryName : '请选择相册分组' }}</view>
  12. <uni-icons type="down" size="24" color="#A6A6A6"></uni-icons>
  13. </view>
  14. </picker>
  15. </view>
  16. <!-- 上传照片区域 -->
  17. <view class="upload">
  18. <view class="img_box" v-if="form.images.length" v-for="(item, index) in form.images">
  19. <image class="img" :src="item" mode="aspectFill" @click="clickImg(form.images, index)"></image>
  20. <!-- 删除图标区域 -->
  21. <view class="delete" v-if="form.images.length" @click="clickClose(index)">
  22. <uni-icons type="close" size="15" color="#fff"></uni-icons>
  23. </view>
  24. </view>
  25. <view v-if="form.images.length < 3" class="upload_box" @click="handleUpload">
  26. <uni-icons type="plusempty" size="30" color="#A6A6A6"></uni-icons>
  27. 上传照片
  28. </view>
  29. </view>
  30. <view class="btn" @click="handleSubmit">确认发布</view>
  31. </view>
  32. </template>
  33. <script setup>
  34. import { ref, onMounted } from 'vue'
  35. import { getCategoryImages, getInsertImage, getReleaseImage } from '@/api/index.js'
  36. import { uploadImage } from '@/api/uploadImage.js'
  37. const $props = defineProps(['type', 'currentId'])
  38. const $emit = defineEmits(['change'])
  39. // 筛选框绑定数组
  40. const listRange = ref([])
  41. // 提交数据
  42. const form = ref({
  43. // 相册分类名称
  44. categoryName: '',
  45. // 相册分类ID
  46. categoryId: '',
  47. // 照片链接集合
  48. images: [],
  49. // 活动id
  50. id: ''
  51. })
  52. onMounted(() => {
  53. // console.log($props.type)
  54. if ($props.type == 1) {
  55. form.value.id = $props.currentId
  56. }
  57. if ($props.type == 2) {
  58. getTagList()
  59. }
  60. })
  61. // 获取相册分类集合
  62. const getTagList = async () => {
  63. const res = await getCategoryImages()
  64. // console.log(res)
  65. listRange.value = res.data
  66. }
  67. // 筛选框选择时的回调
  68. const changeMode = (e) => {
  69. let index = e.detail.value
  70. form.value.categoryName = listRange.value[index].name
  71. form.value.categoryId = listRange.value[index].id
  72. // console.log(form.value)
  73. }
  74. // 上传照片按钮回调
  75. const handleUpload = () => {
  76. uni.chooseImage({
  77. count: 3 - form.value.images.length,
  78. success: async (res) => {
  79. // console.log(res)
  80. uni.showLoading({
  81. title: '上传中...',
  82. mask: true
  83. })
  84. for (let item of res.tempFilePaths) {
  85. let temp = await uploadImage(item)
  86. let result = JSON.parse(temp.data)
  87. // console.log(result)
  88. if (result.code == 200) {
  89. form.value.images.push(result.data.fileUrl)
  90. }
  91. }
  92. // console.log(form.value)
  93. uni.hideLoading()
  94. }
  95. })
  96. }
  97. // 确认发布按钮回调
  98. const handleSubmit = () => {
  99. uni.showModal({
  100. title: '提示',
  101. content: '确定发布吗?',
  102. success: (res) => {
  103. if (res.confirm) {
  104. if ($props.type == 1) {
  105. // 活动
  106. submit_type1()
  107. } else {
  108. // 校友相册
  109. submit_type2()
  110. }
  111. }
  112. }
  113. })
  114. }
  115. // 上传活动照片
  116. const submit_type1 = async () => {
  117. const res = await getReleaseImage(form.value)
  118. // console.log(res)
  119. if (res.code == 200) {
  120. uni.showToast({
  121. title: '发布成功',
  122. icon: 'success',
  123. mask: true
  124. })
  125. setTimeout(() => {
  126. uni.reLaunch({
  127. url: '/pages/activity/activity'
  128. })
  129. }, 1500)
  130. }
  131. }
  132. // 上传校友相册
  133. const submit_type2 = async () => {
  134. const res = await getInsertImage(form.value)
  135. // console.log(res)
  136. if (res.code == 200) {
  137. uni.showToast({
  138. title: '发布成功',
  139. icon: 'success',
  140. mask: true
  141. })
  142. setTimeout(() => {
  143. $emit('change', 1)
  144. }, 1500)
  145. }
  146. }
  147. // 点击每一张图片的回调
  148. const clickImg = (urls, current) => {
  149. uni.previewImage({
  150. urls,
  151. current
  152. })
  153. }
  154. // 点击删除图标回调
  155. const clickClose = (e) => {
  156. uni.showModal({
  157. title: '提示',
  158. content: '确定删除该照片吗?',
  159. success: (res) => {
  160. if (res.confirm) {
  161. form.value.images.splice(e, 1)
  162. }
  163. }
  164. })
  165. }
  166. </script>
  167. <style lang="scss" scoped>
  168. .content {
  169. position: relative;
  170. margin-top: 20rpx;
  171. min-height: calc(100vh - 130rpx);
  172. .box {
  173. margin-bottom: 15rpx;
  174. font-size: 28rpx;
  175. .box_title {
  176. display: flex;
  177. margin-bottom: 15rpx;
  178. .text {
  179. color: #d43030;
  180. }
  181. }
  182. .box_input {
  183. display: flex;
  184. justify-content: space-between;
  185. align-items: center;
  186. padding: 0 22rpx;
  187. width: 710rpx;
  188. height: 80rpx;
  189. border-radius: 6rpx;
  190. background-color: #f5f5f5;
  191. .input {
  192. height: 100%;
  193. font-size: 28rpx;
  194. }
  195. .picker {
  196. color: #a6a6a6;
  197. }
  198. .pick {
  199. color: #000;
  200. }
  201. }
  202. }
  203. .upload {
  204. margin-top: 56rpx;
  205. .img_box {
  206. position: relative;
  207. display: inline-block;
  208. margin-right: 20rpx;
  209. width: 140rpx;
  210. height: 140rpx;
  211. border-radius: 8rpx;
  212. .img {
  213. width: 100%;
  214. height: 100%;
  215. }
  216. .delete {
  217. z-index: 1;
  218. position: absolute;
  219. top: 5rpx;
  220. left: 102rpx;
  221. display: flex;
  222. align-items: center;
  223. justify-content: center;
  224. width: 30rpx;
  225. height: 30rpx;
  226. }
  227. }
  228. .upload_box {
  229. display: flex;
  230. flex-direction: column;
  231. justify-content: center;
  232. align-items: center;
  233. margin-top: 30rpx;
  234. width: 140rpx;
  235. height: 140rpx;
  236. color: #a6a6a6;
  237. font-size: 24rpx;
  238. border-radius: 8rpx;
  239. background-color: #f0f6fc;
  240. }
  241. }
  242. .btn {
  243. position: absolute;
  244. left: 50%;
  245. bottom: 150rpx;
  246. transform: translateX(-50%);
  247. display: flex;
  248. justify-content: center;
  249. align-items: center;
  250. width: 661rpx;
  251. height: 90rpx;
  252. font-size: 28rpx;
  253. color: #fff;
  254. border-radius: 8rpx;
  255. background-color: #007aff;
  256. }
  257. }
  258. </style>