gaoluyang
2026-06-24 c0cb161bb52ce0fbdce5c66ec391d107c75e2452
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<script lang="ts" setup>
import type { ComponentInternalInstance } from 'vue';
 
import type { VerificationProps } from './typing';
 
import {
  getCurrentInstance,
  nextTick,
  onMounted,
  reactive,
  ref,
  toRefs,
} from 'vue';
 
import { IconifyIcon } from '..\..\..\..\..\..\icons\src';
import { $t } from '..\..\..\..\..\..\locales\src';
 
import { AES } from '..\..\..\..\..\..\@core\base\shared\src\utils';
 
import { resetSize } from './utils/util';
 
/**
 * VerifyPoints
 * @description 点选
 */
 
defineOptions({
  name: 'VerifyPoints',
});
 
const props = withDefaults(defineProps<VerificationProps>(), {
  barSize: () => ({
    height: '40px',
    width: '310px',
  }),
  captchaType: 'clickWord',
  imgSize: () => ({
    height: '155px',
    width: '310px',
  }),
  mode: 'fixed',
  space: 5,
});
 
const emit = defineEmits(['onSuccess', 'onError', 'onClose', 'onReady']);
 
const { captchaType, mode, checkCaptchaApi, getCaptchaApi } = toRefs(props);
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const secretKey = ref(); // 后端返回的ase加密秘钥
const checkNum = ref(3); // 默认需要点击的字数
const fontPos = reactive<any[]>([]); // 选中的坐标信息
const checkPosArr = reactive<any[]>([]); // 用户点击的坐标
const num = ref(1); // 点击的记数
const pointBackImgBase = ref(); // 后端获取到的背景图片
const poinTextList = ref<any[]>([]); // 后端返回的点击字体顺序
const backToken = ref(); // 后端返回的token值
const setSize = reactive({
  barHeight: 0,
  barWidth: 0,
  imgHeight: 0,
  imgWidth: 0,
});
const tempPoints = reactive<any[]>([]);
const text = ref();
const barAreaColor = ref();
const barAreaBorderColor = ref();
const showRefresh = ref(true);
const bindingClick = ref(true);
 
function init() {
  // 加载页面
  fontPos.splice(0);
  checkPosArr.splice(0);
  num.value = 1;
  getPictrue();
  nextTick(() => {
    const { barHeight, barWidth, imgHeight, imgWidth } = resetSize(proxy);
    setSize.imgHeight = imgHeight;
    setSize.imgWidth = imgWidth;
    setSize.barHeight = barHeight;
    setSize.barWidth = barWidth;
    emit('onReady', proxy);
  });
}
 
onMounted(() => {
  // 禁止拖拽
  init();
  proxy?.$el?.addEventListener('selectstart', () => {
    return false;
  });
});
const canvas = ref(null);
 
// 获取坐标
const getMousePos = function (_obj: any, e: any) {
  const x = e.offsetX;
  const y = e.offsetY;
  return { x, y };
};
// 创建坐标点
const createPoint = function (pos: any) {
  tempPoints.push(Object.assign({}, pos));
  return num.value + 1;
};
 
// 坐标转换函数
const pointTransfrom = function (pointArr: any, imgSize: any) {
  const newPointArr = pointArr.map((p: any) => {
    const x = Math.round((310 * p.x) / Number.parseInt(imgSize.imgWidth));
    const y = Math.round((155 * p.y) / Number.parseInt(imgSize.imgHeight));
    return { x, y };
  });
  return newPointArr;
};
 
const refresh = async function () {
  tempPoints.splice(0);
  barAreaColor.value = '#000';
  barAreaBorderColor.value = '#ddd';
  bindingClick.value = true;
  fontPos.splice(0);
  checkPosArr.splice(0);
  num.value = 1;
  await getPictrue();
  showRefresh.value = true;
};
 
function canvasClick(e: any) {
  checkPosArr.push(getMousePos(canvas, e));
  if (num.value === checkNum.value) {
    num.value = createPoint(getMousePos(canvas, e));
    // 按比例转换坐标值
    const arr = pointTransfrom(checkPosArr, setSize);
    checkPosArr.length = 0;
    checkPosArr.push(...arr);
    // 等创建坐标执行完
    setTimeout(() => {
      // var flag = this.comparePos(this.fontPos, this.checkPosArr);
      // 发送后端请求
      const captchaVerification = secretKey.value
        ? AES.encrypt(
            `${backToken.value}---${JSON.stringify(checkPosArr)}`,
            secretKey.value,
          )
        : `${backToken.value}---${JSON.stringify(checkPosArr)}`;
      const data = {
        captchaType: captchaType.value,
        pointJson: secretKey.value
          ? AES.encrypt(JSON.stringify(checkPosArr), secretKey.value)
          : JSON.stringify(checkPosArr),
        token: backToken.value,
      };
      checkCaptchaApi?.value?.(data).then((response: any) => {
        const res = response.data;
        if (res.repCode === '0000') {
          barAreaColor.value = '#4cae4c';
          barAreaBorderColor.value = '#5cb85c';
          text.value = $t('ui.captcha.sliderSuccessText');
          bindingClick.value = false;
          if (mode.value === 'pop') {
            setTimeout(() => {
              emit('onClose');
              refresh();
            }, 1500);
          }
          emit('onSuccess', { captchaVerification });
        } else {
          emit('onError', proxy);
          barAreaColor.value = '#d9534f';
          barAreaBorderColor.value = '#d9534f';
          text.value = $t('ui.captcha.sliderRotateFailTip');
          setTimeout(() => {
            refresh();
          }, 700);
        }
      });
    }, 400);
  }
  if (num.value < checkNum.value)
    num.value = createPoint(getMousePos(canvas, e));
}
 
// 请求背景图片和验证图片
async function getPictrue() {
  const data = {
    captchaType: captchaType.value,
  };
  const res = await getCaptchaApi?.value?.(data);
 
  if (res?.data?.repCode === '0000') {
    pointBackImgBase.value = `data:image/png;base64,${res?.data?.repData?.originalImageBase64}`;
    backToken.value = res.data.repData.token;
    secretKey.value = res.data.repData.secretKey;
    poinTextList.value = res.data.repData.wordList;
    text.value = `${$t('ui.captcha.clickInOrder')}【${poinTextList.value.join(',')}】`;
  } else {
    text.value = res?.data?.repMsg;
  }
}
defineExpose({
  init,
  refresh,
});
</script>
 
<template>
  <div style="position: relative">
    <div class="verify-img-out">
      <div
        :style="{
          width: setSize.imgWidth,
          height: setSize.imgHeight,
          'background-size': `${setSize.imgWidth} ${setSize.imgHeight}`,
          'margin-bottom': `${space}px`,
        }"
        class="verify-img-panel"
      >
        <div
          v-show="showRefresh"
          class="verify-refresh"
          style="z-index: 3"
          @click="refresh"
        >
          <IconifyIcon icon="lucide:refresh-ccw" class="mr-2 size-5" />
        </div>
        <img
          ref="canvas"
          :src="pointBackImgBase"
          alt=""
          style="display: block; width: 100%; height: 100%"
          @click="bindingClick ? canvasClick($event) : undefined"
        />
 
        <div
          v-for="(tempPoint, index) in tempPoints"
          :key="index"
          :style="{
            'background-color': '#1abd6c',
            color: '#fff',
            'z-index': 9999,
            width: '20px',
            height: '20px',
            'text-align': 'center',
            'line-height': '20px',
            'border-radius': '50%',
            position: 'absolute',
            top: `${tempPoint.y - 10}px`,
            left: `${tempPoint.x - 10}px`,
          }"
          class="point-area"
        >
          {{ index + 1 }}
        </div>
      </div>
    </div>
    <!-- 'height': this.barSize.height, -->
    <div
      :style="{
        width: setSize.imgWidth,
        color: barAreaColor,
        'border-color': barAreaBorderColor,
        'line-height': barSize.height,
      }"
      class="verify-bar-area"
    >
      <span class="verify-msg">{{ text }}</span>
    </div>
  </div>
</template>