WatermarkCamera.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <template>
  2. <view class="watermark-camera">
  3. <view class="camera-container" v-if="!previewImage">
  4. <view class="camera-placeholder" @click="handleTakePhoto">
  5. <view class="camera-icon">
  6. <view class="camera-body"></view>
  7. <view class="camera-lens"></view>
  8. </view>
  9. <text class="camera-text">点击拍照</text>
  10. </view>
  11. <view class="watermark-preview">
  12. <view class="watermark-item">
  13. <text class="watermark-label">时间:</text>
  14. <text class="watermark-value">{{ watermarkConfig.time }}</text>
  15. </view>
  16. <view class="watermark-item">
  17. <text class="watermark-label">位置:</text>
  18. <text class="watermark-value">{{ watermarkConfig.location }}</text>
  19. </view>
  20. <view class="watermark-item">
  21. <text class="watermark-label">人员:</text>
  22. <text class="watermark-value">{{ watermarkConfig.userName }} ({{ watermarkConfig.userNo }})</text>
  23. </view>
  24. <view class="watermark-item">
  25. <text class="watermark-label">类型:</text>
  26. <text class="watermark-value">{{ watermarkConfig.checkinType }}</text>
  27. </view>
  28. </view>
  29. </view>
  30. <view class="preview-container" v-else>
  31. <image
  32. class="preview-image"
  33. :src="previewImage"
  34. mode="aspectFit"
  35. @click="handlePreview"
  36. />
  37. <view class="watermark-overlay">
  38. <view class="watermark-info">
  39. <view class="watermark-row">
  40. <text class="wm-label">时间:</text>
  41. <text class="wm-value">{{ watermarkConfig.time }}</text>
  42. </view>
  43. <view class="watermark-row">
  44. <text class="wm-label">位置:</text>
  45. <text class="wm-value">{{ watermarkConfig.location.split('\n')[0] }}</text>
  46. </view>
  47. <view class="watermark-row">
  48. <text class="wm-label">地址:</text>
  49. <text class="wm-value">{{ watermarkConfig.location.split('\n')[1] || watermarkConfig.location }}</text>
  50. </view>
  51. <view class="watermark-row">
  52. <text class="wm-label">人员:</text>
  53. <text class="wm-value">{{ watermarkConfig.userName }} ({{ watermarkConfig.userNo }})</text>
  54. </view>
  55. <view class="watermark-row">
  56. <text class="wm-label">类型:</text>
  57. <text class="wm-value">{{ watermarkConfig.checkinType }}</text>
  58. </view>
  59. </view>
  60. </view>
  61. <view class="preview-actions">
  62. <view class="action-btn retake" @click="handleRetake">
  63. <view class="btn-icon retake-icon"></view>
  64. <text class="btn-text">重拍</text>
  65. </view>
  66. <view class="action-btn confirm" @click="handleConfirm">
  67. <view class="btn-icon confirm-icon"></view>
  68. <text class="btn-text">确认</text>
  69. </view>
  70. </view>
  71. </view>
  72. <canvas
  73. canvas-id="watermarkCanvas"
  74. class="watermark-canvas"
  75. :style="{ width: canvasWidth + 'px', height: canvasHeight + 'px' }"
  76. />
  77. </view>
  78. </template>
  79. <script setup lang="ts">
  80. import { ref, computed, onMounted, watch } from 'vue';
  81. import { CheckinType } from '@/api/checkin';
  82. import type { LocationInfo } from '@/api/checkin';
  83. import {
  84. createWatermarkConfig,
  85. formatWatermarkTime,
  86. formatWatermarkLocation,
  87. getCheckinTypeName
  88. } from '@/utils/checkin';
  89. import { getUserInfo } from '@/utils/auth';
  90. interface Props {
  91. checkinType: CheckinType;
  92. location: LocationInfo;
  93. }
  94. const props = defineProps<Props>();
  95. const emit = defineEmits<{
  96. (e: 'confirm', photo: { path: string; watermarkPath: string }): void;
  97. (e: 'cancel'): void;
  98. }>();
  99. const previewImage = ref('');
  100. const canvasWidth = ref(750);
  101. const canvasHeight = ref(1000);
  102. const watermarkConfig = computed(() => {
  103. const userInfo = getUserInfo() || { nickname: '未知用户', userNo: 'N/A' };
  104. return {
  105. time: formatWatermarkTime(),
  106. location: formatWatermarkLocation(props.location),
  107. userName: userInfo.nickname || userInfo.username || '未知用户',
  108. userNo: userInfo.userNo || 'N/A',
  109. checkinType: getCheckinTypeName(props.checkinType)
  110. };
  111. });
  112. onMounted(() => {
  113. const systemInfo = uni.getSystemInfoSync();
  114. canvasWidth.value = systemInfo.windowWidth * 2;
  115. canvasHeight.value = systemInfo.windowWidth * 2 * 1.33;
  116. });
  117. watch(() => props.location, () => {
  118. }, { deep: true });
  119. const handleTakePhoto = () => {
  120. uni.chooseImage({
  121. count: 1,
  122. sizeType: ['compressed'],
  123. sourceType: ['camera'],
  124. success: (res) => {
  125. previewImage.value = res.tempFilePaths[0];
  126. },
  127. fail: (err) => {
  128. console.error('拍照失败', err);
  129. uni.showToast({
  130. title: '拍照失败',
  131. icon: 'none'
  132. });
  133. }
  134. });
  135. };
  136. const handlePreview = () => {
  137. if (previewImage.value) {
  138. uni.previewImage({
  139. urls: [previewImage.value],
  140. current: previewImage.value
  141. });
  142. }
  143. };
  144. const handleRetake = () => {
  145. previewImage.value = '';
  146. };
  147. const handleConfirm = () => {
  148. if (!previewImage.value) {
  149. uni.showToast({
  150. title: '请先拍照',
  151. icon: 'none'
  152. });
  153. return;
  154. }
  155. emit('confirm', {
  156. path: previewImage.value,
  157. watermarkPath: previewImage.value
  158. });
  159. };
  160. </script>
  161. <style lang="scss" scoped>
  162. .watermark-camera {
  163. width: 100%;
  164. }
  165. .camera-container {
  166. width: 100%;
  167. }
  168. .camera-placeholder {
  169. width: 100%;
  170. height: 400rpx;
  171. background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
  172. border: 2rpx dashed #0ea5e9;
  173. border-radius: 20rpx;
  174. display: flex;
  175. flex-direction: column;
  176. align-items: center;
  177. justify-content: center;
  178. transition: all 0.2s;
  179. &:active {
  180. transform: scale(0.98);
  181. background: linear-gradient(135deg, #e0f2fe 0%, #bae6fd 100%);
  182. }
  183. }
  184. .camera-icon {
  185. position: relative;
  186. width: 80rpx;
  187. height: 60rpx;
  188. margin-bottom: 16rpx;
  189. .camera-body {
  190. width: 80rpx;
  191. height: 50rpx;
  192. background: #0ea5e9;
  193. border-radius: 10rpx;
  194. position: absolute;
  195. bottom: 0;
  196. }
  197. .camera-lens {
  198. width: 30rpx;
  199. height: 30rpx;
  200. background: $bg-color-card;
  201. border: 4rpx solid #0ea5e9;
  202. border-radius: 50%;
  203. position: absolute;
  204. bottom: 10rpx;
  205. left: 50%;
  206. transform: translateX(-50%);
  207. }
  208. }
  209. .camera-text {
  210. font-size: 28rpx;
  211. color: #0ea5e9;
  212. font-weight: 500;
  213. }
  214. .watermark-preview {
  215. margin-top: 24rpx;
  216. padding: 24rpx;
  217. background: #f8fafc;
  218. border-radius: 16rpx;
  219. }
  220. .watermark-item {
  221. display: flex;
  222. align-items: flex-start;
  223. margin-bottom: 12rpx;
  224. &:last-child {
  225. margin-bottom: 0;
  226. }
  227. }
  228. .watermark-label {
  229. font-size: 24rpx;
  230. color: #64748b;
  231. width: 80rpx;
  232. flex-shrink: 0;
  233. }
  234. .watermark-value {
  235. font-size: 24rpx;
  236. color: #1e293b;
  237. flex: 1;
  238. word-break: break-all;
  239. }
  240. .preview-container {
  241. width: 100%;
  242. position: relative;
  243. }
  244. .preview-image {
  245. width: 100%;
  246. height: 500rpx;
  247. border-radius: 16rpx;
  248. background: #f1f5f9;
  249. }
  250. .watermark-overlay {
  251. position: absolute;
  252. bottom: 120rpx;
  253. left: 0;
  254. right: 0;
  255. padding: 0 24rpx;
  256. pointer-events: none;
  257. }
  258. .watermark-info {
  259. background: rgba(0, 0, 0, 0.7);
  260. border-radius: 12rpx;
  261. padding: 20rpx;
  262. }
  263. .watermark-row {
  264. display: flex;
  265. align-items: flex-start;
  266. margin-bottom: 8rpx;
  267. &:last-child {
  268. margin-bottom: 0;
  269. }
  270. }
  271. .wm-label {
  272. font-size: 22rpx;
  273. color: #94a3b8;
  274. width: 70rpx;
  275. flex-shrink: 0;
  276. }
  277. .wm-value {
  278. font-size: 22rpx;
  279. color: $bg-color-card;
  280. flex: 1;
  281. }
  282. .preview-actions {
  283. display: flex;
  284. justify-content: center;
  285. gap: 40rpx;
  286. margin-top: 32rpx;
  287. }
  288. .action-btn {
  289. display: flex;
  290. flex-direction: column;
  291. align-items: center;
  292. &:active {
  293. opacity: 0.7;
  294. }
  295. }
  296. .btn-icon {
  297. width: 80rpx;
  298. height: 80rpx;
  299. border-radius: 50%;
  300. display: flex;
  301. align-items: center;
  302. justify-content: center;
  303. margin-bottom: 12rpx;
  304. &.retake-icon {
  305. background: #f1f5f9;
  306. position: relative;
  307. &::before {
  308. content: '';
  309. width: 24rpx;
  310. height: 24rpx;
  311. border-left: 4rpx solid #64748b;
  312. border-bottom: 4rpx solid #64748b;
  313. transform: rotate(45deg);
  314. margin-left: 8rpx;
  315. }
  316. }
  317. &.confirm-icon {
  318. background: $primary-color;
  319. position: relative;
  320. &::before {
  321. content: '';
  322. width: 20rpx;
  323. height: 12rpx;
  324. border-left: 4rpx solid $bg-color-card;
  325. border-bottom: 4rpx solid $bg-color-card;
  326. transform: rotate(-45deg);
  327. margin-top: -8rpx;
  328. }
  329. }
  330. }
  331. .btn-text {
  332. font-size: 24rpx;
  333. color: #475569;
  334. .confirm & {
  335. color: $primary-color;
  336. font-weight: 500;
  337. }
  338. }
  339. .watermark-canvas {
  340. position: fixed;
  341. left: -9999rpx;
  342. top: -9999rpx;
  343. }
  344. </style>