takePhoto.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <template>
  2. <gui-page :custom-header="true" :header-class="['gui-theme-background-color']">
  3. <template #gHeader>
  4. <view style="height:44px;" class="gui-flex gui-nowrap gui-rows gui-align-items-center">
  5. <!-- 使用组件实现返回按钮及返回首页按钮 -->
  6. <text
  7. style="font-size:44rpx;"
  8. class="gui-header-leader-btns gui-color-white font-icons"
  9. @tap="goBack"
  10. >&#xe6c5;</text>
  11. <!-- 导航文本此处也可以是其他自定义内容 -->
  12. <text
  13. class="gui-h4 gui-blod gui-flex1 gui-text-center gui-ellipsis gui-color-white gui-primary-text"
  14. >生产结束</text>
  15. <!-- 此处加一个右侧展位元素与左侧同宽,实现标题居中 -->
  16. <!-- 实际宽度请根据自己情况设置 -->
  17. <view style="width:40px;" />
  18. <!-- 如果右侧有其他内容可以利用条件编译和定位来实现-->
  19. </view>
  20. </template>
  21. <template #gBody>
  22. <view class="list-content">
  23. <view class="table-title" style="margin-top: 16px;">
  24. <span>图片列表</span>
  25. </view>
  26. <view class="photo-list">
  27. <view v-for="(item, key) in photoList" :key="key" class="photo-card">
  28. <image
  29. :src="item"
  30. class="photo"
  31. mode="scaleToFill"
  32. @click="handleRemoveImage(item, key)"
  33. />
  34. </view>
  35. </view>
  36. </view>
  37. <gui-right-menus>
  38. <!-- 扩展按钮 -->
  39. <template #menus-more>
  40. <view
  41. hover-class="gui-tap"
  42. class="menu-items gui-bg-blue gui-flex gui-columns gui-justify-content-center"
  43. @click="takePhoto"
  44. >
  45. <text class="menu-text gui-block gui-text-center gui-color-white">拍照</text>
  46. </view>
  47. <view
  48. hover-class="gui-tap"
  49. class="menu-items gui-bg-green gui-flex gui-columns gui-justify-content-center"
  50. @click="handleComplete"
  51. >
  52. <text class="menu-text gui-block gui-text-center gui-color-white">结束</text>
  53. </view>
  54. </template>
  55. <!-- 核心按钮 -->
  56. <template #menus-primary>
  57. <view class="menu-items gui-bg-primary gui-flex gui-columns gui-justify-content-center">
  58. <text class="menu-icon gui-color-white gui-block gui-text-center gui-icons">&#xe614;</text>
  59. <text class="menu-text gui-color-white gui-block gui-text-center">操作</text>
  60. </view>
  61. </template>
  62. </gui-right-menus>
  63. </template>
  64. </gui-page>
  65. </template>
  66. <script>
  67. import axios from 'axios'
  68. import {
  69. ref,
  70. defineComponent,
  71. onBeforeMount
  72. } from 'vue'
  73. export default defineComponent({
  74. setup() {
  75. const parentRow = uni.getStorageSync('HotPressInfo') ?? {}
  76. const hotPressId = ref('')
  77. const photoList = ref([])
  78. const fileIds = ref([])
  79. onBeforeMount(() => {
  80. hotPressId.value = JSON.parse(parentRow)?.id
  81. })
  82. const goBack = function() {
  83. uni.$goBack('/pages/workbranch/production/processExecution/processExecution')
  84. }
  85. const takePhoto = function() {
  86. uni.chooseImage({
  87. count: 6, // count: 6, //默认9
  88. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  89. sourceType: ['album', 'camera'], // 从相册选择
  90. success: function(res) {
  91. res.tempFilePaths.forEach(async item => {
  92. // #ifdef APP-PLUS
  93. // 移动端无法使用FormData类型
  94. // 使用uniapp官方提供的uploadFile函数替代实现文件上传
  95. await uni.uploadFile({
  96. url: uni.$baseUrl +
  97. '/admin-api/minio/files/uploadFiles',
  98. name: 'file',
  99. filePath: item,
  100. header: {
  101. 'Authorization': 'Bearer ' + uni
  102. .getStorageSync('token')
  103. },
  104. success: function(uploadFileRes) {
  105. const resultJSON = JSON.parse(
  106. uploadFileRes.data)
  107. if (fileIds.value == null) {
  108. fileIds.value = []
  109. }
  110. fileIds.value.push(resultJSON.data
  111. .fileId)
  112. }
  113. })
  114. // #endif
  115. photoList.value.push(item)
  116. })
  117. // #ifdef H5
  118. res.tempFiles.forEach(async item => {
  119. const formData = new FormData()
  120. formData.append('file', item)
  121. // 上传文件
  122. const {
  123. data
  124. } = await axios({
  125. url: uni.$baseUrl +
  126. '/admin-api/minio/files/uploadFiles',
  127. name: '上传文件',
  128. method: 'post',
  129. headers: {
  130. 'Authorization': 'Bearer ' + uni.getStorageSync('token'),
  131. 'Content-Type': 'multipart/form-data;'
  132. },
  133. data: formData
  134. })
  135. if (fileIds.value == null) {
  136. fileIds.value = []
  137. }
  138. fileIds.value.push(data.data.fileId)
  139. })
  140. // #endif
  141. }
  142. })
  143. }
  144. const handleRemoveImage = function(opt, index) {
  145. fileIds.value.splice(index, 1)
  146. photoList.value.splice(index, 1)
  147. }
  148. const handleComplete = function() {
  149. // 生产执行
  150. uni.$reqPost('completeProduction', {
  151. hotPressId: hotPressId.value,
  152. fileIdList: fileIds.value
  153. })
  154. .then(({
  155. code,
  156. data,
  157. msg
  158. }) => {
  159. if (code === 0) {
  160. uni.showToast({
  161. title: '执行结束,标签打印中',
  162. icon: 'none',
  163. duration: 2000
  164. })
  165. } else {
  166. uni.showToast({
  167. title: msg,
  168. icon: 'none',
  169. duration: 2000
  170. })
  171. }
  172. })
  173. }
  174. return {
  175. goBack,
  176. photoList,
  177. takePhoto,
  178. handleComplete,
  179. handleRemoveImage
  180. }
  181. }
  182. })
  183. </script>
  184. <style lang="scss" scoped>
  185. .gui-header-leader-btns {
  186. color: black;
  187. font-size: 24px !important;
  188. margin-left: 24rpx;
  189. }
  190. .list-content {
  191. margin-top: 80px;
  192. background-color: #edeeee;
  193. }
  194. .table-title {
  195. height: 40px;
  196. line-height: 40px;
  197. margin: 0 0 -3px 0;
  198. padding: 0 12px;
  199. font-size: 16px;
  200. font-weight: bold;
  201. background-color: white;
  202. }
  203. .photo-list {
  204. display: grid;
  205. grid-template-rows: max-content;
  206. grid-template-columns: 1fr 1fr;
  207. .photo-card {
  208. display: flex;
  209. align-items: center;
  210. justify-content: center;
  211. margin: 8px 0;
  212. .photo {
  213. width: calc(100vw / 2 - 12px);
  214. height: calc(100vw / 3.2 - 6px);
  215. border-radius: 8px;
  216. pointer-events: none;
  217. }
  218. .photo::after {
  219. content: "×";
  220. width: 22px;
  221. height: 22px;
  222. position: absolute;
  223. top: 5px;
  224. right: 5px;
  225. display: flex;
  226. justify-content: center;
  227. align-items: center;
  228. color: rgba(255, 255, 255, 0.9);
  229. background-color: rgba(0, 0, 0, 0.5);
  230. border-radius: 50%;
  231. font-size: 24px;
  232. /*给伪元素开启鼠标事件,将事件冒泡到父元素的点击事件中*/
  233. pointer-events: auto;
  234. }
  235. }
  236. }
  237. </style>