SignaturePad.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <template>
  2. <view v-if="visible" class="signature-landscape">
  3. <!-- 左侧操作栏 -->
  4. <view class="left-toolbar">
  5. <view class="toolbar-item" @click="handleRewrite">重写</view>
  6. <view class="toolbar-item" @click="handleSave">保存</view>
  7. <view class="toolbar-item" @click="handlePreview">预览</view>
  8. <view class="toolbar-item" @click="handleUpload">上传</view>
  9. <view class="back-btn" @click="handleBack">返回</view>
  10. </view>
  11. <!-- 签名画布区域 -->
  12. <view class="canvas-area">
  13. <canvas
  14. class="signature-canvas"
  15. canvas-id="landscapeSignCanvas"
  16. @touchstart="onTouchStart"
  17. @touchmove="onTouchMove"
  18. @touchend="onTouchEnd"
  19. @touchcancel="onTouchEnd"
  20. ></canvas>
  21. <text class="placeholder">请签名</text>
  22. </view>
  23. </view>
  24. </template>
  25. <script setup>
  26. import { ref, watch } from 'vue'
  27. const props = defineProps({
  28. visible: { type: Boolean, default: false }
  29. })
  30. const emit = defineEmits(['back', 'save', 'preview', 'upload'])
  31. // 画布状态
  32. let ctx = null
  33. let canvasWidth = 0
  34. let canvasHeight = 0
  35. let lastX = 0
  36. let lastY = 0
  37. const hasSign = ref(false)
  38. let isDrawing = ref(false)
  39. let retryCount = 0
  40. const MAX_RETRY = 5
  41. // 监听显示,初始化画布
  42. watch(() => props.visible, (val) => {
  43. if (val) {
  44. hasSign.value = false
  45. isDrawing.value = false
  46. retryCount = 0
  47. setTimeout(() => {
  48. initCanvas()
  49. }, 300)
  50. }
  51. })
  52. // 初始化画布
  53. function initCanvas() {
  54. if (retryCount >= MAX_RETRY) {
  55. uni.showToast({ title: '画布加载失败', icon: 'none' })
  56. return
  57. }
  58. const query = uni.createSelectorQuery()
  59. query.select('.signature-canvas').boundingClientRect()
  60. query.exec((res) => {
  61. if (!res || !res[0]) {
  62. retryCount++
  63. setTimeout(initCanvas, 200)
  64. return
  65. }
  66. const rect = res[0]
  67. canvasWidth = rect.width
  68. canvasHeight = rect.height
  69. // 初始化画笔
  70. ctx = uni.createCanvasContext('landscapeSignCanvas')
  71. ctx.setLineWidth(3)
  72. ctx.setStrokeStyle('#000000')
  73. ctx.setLineCap('round')
  74. ctx.setLineJoin('round')
  75. ctx.draw()
  76. })
  77. }
  78. // 触摸开始
  79. function onTouchStart(e) {
  80. if (!ctx) return
  81. isDrawing.value = true
  82. const touch = e.touches[0]
  83. lastX = touch.x
  84. lastY = touch.y
  85. ctx.beginPath()
  86. ctx.moveTo(lastX, lastY)
  87. }
  88. // 触摸移动
  89. function onTouchMove(e) {
  90. if (!isDrawing.value || !ctx) return
  91. const touch = e.touches[0]
  92. const x = touch.x
  93. const y = touch.y
  94. ctx.lineTo(x, y)
  95. ctx.stroke()
  96. ctx.draw(true)
  97. lastX = x
  98. lastY = y
  99. hasSign.value = true
  100. }
  101. // 触摸结束
  102. function onTouchEnd() {
  103. isDrawing.value = false
  104. }
  105. // 重写:清空画布
  106. function handleRewrite() {
  107. if (!ctx) return
  108. ctx.clearRect(0, 0, canvasWidth, canvasHeight)
  109. ctx.draw()
  110. hasSign.value = false
  111. }
  112. // 保存:生成签名图片
  113. function handleSave() {
  114. if (!hasSign.value) {
  115. uni.showToast({ title: '请先签名', icon: 'none' })
  116. return
  117. }
  118. uni.canvasToTempFilePath({
  119. canvasId: 'landscapeSignCanvas',
  120. success: (res) => {
  121. emit('save', res.tempFilePath)
  122. uni.showToast({ title: '保存成功', icon: 'success' })
  123. },
  124. fail: () => {
  125. uni.showToast({ title: '保存失败', icon: 'none' })
  126. }
  127. })
  128. }
  129. // 预览:弹窗预览签名
  130. function handlePreview() {
  131. if (!hasSign.value) {
  132. uni.showToast({ title: '请先签名', icon: 'none' })
  133. return
  134. }
  135. uni.canvasToTempFilePath({
  136. canvasId: 'landscapeSignCanvas',
  137. success: (res) => {
  138. emit('preview', res.tempFilePath)
  139. // 这里可以加预览弹窗,示例直接展示
  140. uni.previewImage({
  141. urls: [res.tempFilePath]
  142. })
  143. }
  144. })
  145. }
  146. // 上传:上传签名图片
  147. function handleUpload() {
  148. if (!hasSign.value) {
  149. uni.showToast({ title: '请先签名', icon: 'none' })
  150. return
  151. }
  152. uni.canvasToTempFilePath({
  153. canvasId: 'landscapeSignCanvas',
  154. success: (res) => {
  155. emit('upload', res.tempFilePath)
  156. uni.showToast({ title: '上传中...', icon: 'loading' })
  157. // 这里写你的上传接口逻辑
  158. }
  159. })
  160. }
  161. // 返回:关闭组件
  162. function handleBack() {
  163. handleRewrite()
  164. emit('back')
  165. }
  166. </script>
  167. <style scoped>
  168. /* 横屏容器:全屏横向布局 */
  169. .signature-landscape {
  170. position: fixed;
  171. top: 0;
  172. left: 0;
  173. right: 0;
  174. bottom: 0;
  175. background: #ffffff;
  176. z-index: 999999;
  177. display: flex;
  178. flex-direction: row;
  179. overflow: hidden;
  180. }
  181. /* 左侧操作栏 */
  182. .left-toolbar {
  183. width: 120rpx;
  184. height: 100vh;
  185. background: #f5f5f5;
  186. display: flex;
  187. flex-direction: column;
  188. align-items: center;
  189. padding: 40rpx 0;
  190. box-sizing: border-box;
  191. gap: 30rpx;
  192. }
  193. .toolbar-item {
  194. width: 100%;
  195. text-align: center;
  196. font-size: 32rpx;
  197. color: #333;
  198. padding: 20rpx 0;
  199. box-sizing: border-box;
  200. }
  201. .back-btn {
  202. margin-top: auto;
  203. width: 100rpx;
  204. height: 100rpx;
  205. background: #007aff;
  206. color: #fff;
  207. border-radius: 10rpx;
  208. display: flex;
  209. align-items: center;
  210. justify-content: center;
  211. font-size: 32rpx;
  212. }
  213. /* 签名画布区域 */
  214. .canvas-area {
  215. flex: 1;
  216. position: relative;
  217. margin: 40rpx;
  218. border: 2rpx dashed #ccc;
  219. box-sizing: border-box;
  220. }
  221. .signature-canvas {
  222. width: 100%;
  223. height: 100%;
  224. display: block;
  225. }
  226. /* 占位文字:右下角 */
  227. .placeholder {
  228. position: absolute;
  229. right: 30rpx;
  230. bottom: 30rpx;
  231. font-size: 32rpx;
  232. color: #999;
  233. transform: rotate(90deg);
  234. transform-origin: right bottom;
  235. }
  236. </style>