123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- import showAsyncCaptchaDialog from '../components/dialog/async-captcha'
- import { ajaxCheckCaptchaCode, ajaxGetSMSCode } from '@/api'
- import { useToast } from '@/utils/use'
- export async function getSMSVerifyCaptcha(phone, mold) {
- const result = await ajaxGetSMSCode({ phone, mold })
- // Object.assign(result.data.data, {
- // captcha_key: '2dbeb6c6db961938419408483bca30dc',
- // image_base64: 'data:image/jpeg;base64,/9j/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEI==',
- // tile_base64: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAD8AAAA/CAYAAABXXxDfAAAWSElEQVR4nMx7W49kZ9Xe8x72qar6PNMznhnP2GPjzx4Ptj+fwP4iAsEJWMEBCyJAuUguo/wCbnKfm1zkKj8CRYqEiBAoyICxgwW+IDEk8Ql77PF4jl2HfXqPn9bau5pJggg==',
- // tile_width: 63,
- // tile_height: 63,
- // tile_x: 24,
- // tile_y: 24
- // })
- return result.data
- }
- function getCaptchaData(x = {}) {
- return {
- image: x.image_base64 || '',
- thumb: x.tile_base64 || '',
- captKey: x.captcha_key || '',
- thumbX: x.tile_x || 0,
- thumbY: x.tile_y || 0,
- thumbWidth: x.tile_width || 0,
- thumbHeight: x.tile_height || 0
- }
- }
- async function refreshCaptcha(phone, mold) {
- const { error_code: code, data } = await getSMSVerifyCaptcha(phone, mold)
- if (code === 0 && data) {
- if (data.code === 1) {
- return getCaptchaData(data)
- } else {
- return false
- }
- } else {
- return false
- }
- }
- async function checkVerify(params = {}) {
- const { data: axiosData } = await ajaxCheckCaptchaCode(params)
- const { error_code: code, error_msg: msg, data } = axiosData
- if (code === 0 && data && data.code === 0) {
- return true
- } else {
- if (msg) {
- useToast(msg)
- }
- return false
- }
- }
- export async function useSMSVerify(options = {}) {
- // /publicapply/captcha/get:mold:1:登录;2:注册;3:找回密码
- const { phone, mold } = options
- const captchaInfo = {}
- const {
- error_code: code,
- error_msg: msg,
- data
- } = await getSMSVerifyCaptcha(phone, mold)
- if (code === 0 && data) {
- if (data.code === 0) {
- return {
- pass: true
- }
- } else if (data.code === 1) {
- // 初始化值
- const captchaData = getCaptchaData(data)
- Object.assign(captchaInfo, captchaData)
- await showAsyncCaptchaDialog({
- captchaData,
- async refresh() {
- const refresh = await refreshCaptcha(phone, mold)
- if (refresh) {
- Object.assign(captchaInfo, refresh)
- }
- return refresh
- },
- async confirm({ point }) {
- const pass = await checkVerify({
- phone,
- key: captchaInfo.captKey,
- point: `${point.x},${point.y}`
- })
- return pass
- }
- })
- return {
- pass: true,
- captKey: captchaInfo.captKey,
- phone
- }
- }
- } else {
- if (msg) {
- useToast(msg)
- }
- }
- return {
- pass: false
- }
- }
|