package controllers

import (
	"errors"
	"spaceofcheng/services/utils"
)

// SendCaptcha 发送验证码
func (c *BaseController) SendCaptcha() {
	phone := c.GetString("phone")

	if phone == "" {
		c.ResponseError(errors.New("请确定手机号码"))
	}

	if c.RunMode == "dev" {
		c.ResponseJSON("ok")
	}

	if err := utils.SendCaptcha(phone); err != nil {
		utils.LogError(err)
		c.ResponseError(errors.New("验证码发送失败, 请重试"))
	}

	c.ResponseJSON("ok")
}

// ValidCaptcha 发送验证码
func (c *BaseController) ValidCaptcha() {
	phone := c.GetString("phone")

	if phone == "" {
		c.ResponseError(errors.New("请确定手机号码"))
	}

	captcha := c.GetString("captcha")
	if captcha == "" {
		c.ResponseError(errors.New("验证码不能为空"))
	}

	if c.RunMode == "dev" {
		c.ResponseJSON("ok")
	}

	if err := utils.ValidCaptcha(phone, captcha); err != nil {
		c.ResponseError(err)
	}

	c.ResponseJSON("ok")
}
