useSmsVerify.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import showAsyncCaptchaDialog from '../components/dialog/async-captcha'
  2. import { ajaxCheckCaptchaCode, ajaxGetSMSCode } from '@/api'
  3. import { useToast } from '@/utils/use'
  4. export async function getSMSVerifyCaptcha(phone, mold) {
  5. const result = await ajaxGetSMSCode({ phone, mold })
  6. // Object.assign(result.data.data, {
  7. // captcha_key: '2dbeb6c6db961938419408483bca30dc',
  8. // image_base64: 'data:image/jpeg;base64,/9j/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEI==',
  9. // tile_base64: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAD8AAAA/CAYAAABXXxDfAAAWSElEQVR4nMx7W49kZ9Xe8x72qar6PNMznhnP2GPjzx4Ptj+fwP4iAsEJWMEBCyJAuUguo/wCbnKfm1zkKj8CRYqEiBAoyICxgwW+IDEk8Ql77PF4jl2HfXqPn9bau5pJggg==',
  10. // tile_width: 63,
  11. // tile_height: 63,
  12. // tile_x: 24,
  13. // tile_y: 24
  14. // })
  15. return result.data
  16. }
  17. function getCaptchaData(x = {}) {
  18. return {
  19. image: x.image_base64 || '',
  20. thumb: x.tile_base64 || '',
  21. captKey: x.captcha_key || '',
  22. thumbX: x.tile_x || 0,
  23. thumbY: x.tile_y || 0,
  24. thumbWidth: x.tile_width || 0,
  25. thumbHeight: x.tile_height || 0
  26. }
  27. }
  28. async function refreshCaptcha(phone, mold) {
  29. const { error_code: code, data } = await getSMSVerifyCaptcha(phone, mold)
  30. if (code === 0 && data) {
  31. if (data.code === 1) {
  32. return getCaptchaData(data)
  33. } else {
  34. return false
  35. }
  36. } else {
  37. return false
  38. }
  39. }
  40. async function checkVerify(params = {}) {
  41. const { data: axiosData } = await ajaxCheckCaptchaCode(params)
  42. const { error_code: code, error_msg: msg, data } = axiosData
  43. if (code === 0 && data && data.code === 0) {
  44. return true
  45. } else {
  46. if (msg) {
  47. useToast(msg)
  48. }
  49. return false
  50. }
  51. }
  52. export async function useSMSVerify(options = {}) {
  53. // /publicapply/captcha/get:mold:1:登录;2:注册;3:找回密码
  54. const { phone, mold } = options
  55. const captchaInfo = {}
  56. const {
  57. error_code: code,
  58. error_msg: msg,
  59. data
  60. } = await getSMSVerifyCaptcha(phone, mold)
  61. if (code === 0 && data) {
  62. if (data.code === 0) {
  63. return {
  64. pass: true
  65. }
  66. } else if (data.code === 1) {
  67. // 初始化值
  68. const captchaData = getCaptchaData(data)
  69. Object.assign(captchaInfo, captchaData)
  70. await showAsyncCaptchaDialog({
  71. captchaData,
  72. async refresh() {
  73. const refresh = await refreshCaptcha(phone, mold)
  74. if (refresh) {
  75. Object.assign(captchaInfo, refresh)
  76. }
  77. return refresh
  78. },
  79. async confirm({ point }) {
  80. const pass = await checkVerify({
  81. phone,
  82. key: captchaInfo.captKey,
  83. point: `${point.x},${point.y}`
  84. })
  85. return pass
  86. }
  87. })
  88. return {
  89. pass: true,
  90. captKey: captchaInfo.captKey,
  91. phone
  92. }
  93. }
  94. } else {
  95. if (msg) {
  96. useToast(msg)
  97. }
  98. }
  99. return {
  100. pass: false
  101. }
  102. }