// Package service /** * @ File: verifyCode * @ Date: 2021/5/12 10:15 * @ Author: JYQ * @ Description: 图片验证码相关 */ package service import ( "encoding/json" pb "sportfitness/base/api/grpc/base" "sportfitness/base/assembly/base/repository/postgre" "sportfitness/base/assembly/base/repository/redis" "sportfitness/base/assembly/base/repository/sms" "sportfitness/base/errors" "strconv" "time" "github.com/mojocn/base64Captcha" ) type VerifyCode struct { base } func (VerifyCode) GenImage(height, width int) (id, pic string) { if height == 0 { height = 80 } if width == 0 { width = 240 } if height < 80 || width < 80 { panic(errors.ParamError("height or width", "小于80")) } captcha := base64Captcha.NewCaptcha( base64Captcha.NewDriverDigit( height, width, 4, 0.7, 20, ), redis.CaptchaStore{}) id, pic, err := captcha.Generate() if err != nil { panic(err) } return } func (v VerifyCode) GetPhoneVFCode(sId int64, account, ip, templateCode, codeId, imgCode string, verifyType pb.VerifyType, expireDuration time.Duration) string { // 校验图形验证码是否正确 if !(redis.CaptchaStore{}.Verify(codeId, imgCode, true)) { panic(errors.ErrVerifyCode) } // 校验手机号是否存在 todo 调用手机号校验接口 // 根据sId查询短信签名 todo 以后会存在数据库中,目前只是写在代码层 signName, _ := postgre.PGVerifyCode{}.GetShopSmsConf(sId) code := strconv.Itoa(RandomInt(6)) now := time.Now() vf := &postgre.VCode{ SId: sId, Account: account, SendType: postgre.VFCodeTypePhone, CodeType: verifyType, Code: code, Ip: ip, CreateAt: now.Unix(), ExpireAt: now.Add(expireDuration).Unix(), } str, err := json.Marshal(vf) if err != nil { panic(errors.M2JError) } rs, err := postgre.PGVerifyCode{}.CreateVerifyCode(string(str), sId) if err != nil { panic(errors.PGError) } sms.PhoneCode{}.SendCode(vf, templateCode, signName) return rs } func (v VerifyCode) CheckPhoneVFCode(sId int64, account, code string, verifyType pb.VerifyType) bool { vf := &postgre.VCode{ SId: sId, Account: account, SendType: postgre.VFCodeTypePhone, CodeType: verifyType, Code: code, } str, err := json.Marshal(vf) if err != nil { panic(errors.M2JError) } b, err := postgre.PGVerifyCode{}.CheckPhoneVerifyCode(string(str), sId) if err != nil { panic(errors.PGError) } return b }