package controllers import ( "encoding/json" "hongze/hongze_mobile_admin/models/tables/admin" "hongze/hongze_mobile_admin/models/tables/company_remind_record" "hongze/hongze_mobile_admin/models/tables/eta_business_remind_record" "hongze/hongze_mobile_admin/services" "hongze/hongze_mobile_admin/utils" "time" ) type AdminWxController struct { BaseCommon } // SendSmsCode // @Title 发送短信验证码 // @Description 发送短信验证码 // @Param Mobile query string true "手机号" // @Param Token query string true "授权token" // @Success 200 {object} wechat.WxLoginResp // @router /send_sms_code [post] func (c *AdminWxController) SendSmsCode() { var req admin.SmsCodeReq err := json.Unmarshal(c.Ctx.Input.RequestBody, &req) if err != nil { c.FailWithMessage("参数解析异常!", "参数解析失败,Err:"+err.Error()) return } if req.Mobile == "" { c.FailWithMessage("请输入手机号", "请输入手机号") return } if req.Token == "" { c.FailWithMessage("请输入授权标识", "请输入授权标识") return } areaNum := req.AreaNum //获取token,如果没有token不允许发送验证码 infoJson, err := utils.Rc.RedisString(utils.HZ_MOBILE_ADMIN_WXLOGIN_TOKEN + req.Token) if err != nil { c.FailWithMessage("授权信息不存在", "授权信息不存在"+err.Error()) return } info := map[string]string{"open_id": "", "union_id": ""} err = json.Unmarshal([]byte(infoJson), &info) if err != nil { c.FailWithMessage("授权信息解析失败", "授权信息解析失败"+err.Error()) return } openId := info["open_id"] if openId == "" { c.FailWithMessage("授权信息不存在", "授权信息不存在") return } //如果该token中的openID已经被其他手机号绑定,则不允许发送验证码 adminInfo, err := admin.GetAdminWxByAdminOpenId(openId) if err != nil { if err.Error() == utils.ErrNoRow() { err = nil } else { c.FailWithMessage("查询管理员信息失败", "查询管理员信息失败"+err.Error()) return } } else { if adminInfo.Mobile != req.Mobile { c.FailWithMessage("该微信已被其他账号绑定", "该微信已被其他账号绑定") return } else { resp := &admin.WxSmsResp{ BindFlag: true, } c.OkDetailed(resp, "已绑定") return } } //判断手机号是否已绑定,如果已绑定openId,并且和token相等,则不允许发送验证码 adminMobileInfo, err := admin.GetAdminByMobile(req.Mobile) if err != nil { if err.Error() == utils.ErrNoRow() { c.FailWithMessage("账号不存在,联系管理员", "账号不存在,联系管理员") return } c.FailWithMessage("管理员账号查询失败", "管理员账号查询失败"+err.Error()) return } if adminMobileInfo.OpenId == openId { resp := &admin.WxSmsResp{ BindFlag: true, } c.OkDetailed(resp, "已绑定") return } //发送短信 var sendRet bool code := utils.GetRandDigit(4) if areaNum == "86" || areaNum == "" || areaNum == "0" { //国内的短信 sendRet = services.SendSmsCode(req.Mobile, code) } else { //国际短信 sendRet = services.SendSmsCodeGj(req.Mobile, code, areaNum) } if !sendRet { c.FailWithMessage("发送验证码失败", "发送验证码失败") return } // 存储验证码,15分钟过期 err = utils.Rc.Put(utils.HZ_MOBILE_ADMIN_WXLOGIN_MOBILE_CODE+req.Mobile, code, 15*time.Minute) if err != nil { c.FailWithMessage("发送验证码失败", "保存验证码到redis中失败"+err.Error()) return } resp := &admin.WxSmsResp{ SmsFlag: true, } c.OkDetailed(resp, "发送成功") return } // BindMobile // @Title 绑定手机号 // @Description 绑定手机号 // @Param Mobile query string true "手机号" // @Param Token query string true "授权token" // @Param Code query string true "短信验证码" // @Success 200 {object} wechat.WxLoginResp // @router /bind_mobile [post] func (c *AdminWxController) BindMobile() { var req admin.BindMobileReq err := json.Unmarshal(c.Ctx.Input.RequestBody, &req) if err != nil { c.FailWithMessage("参数解析异常!", "参数解析失败,Err:"+err.Error()) return } if req.Mobile == "" { c.FailWithMessage("请输入手机号", "请输入手机号") return } if req.Token == "" { c.FailWithMessage("请输入授权标识", "请输入授权标识") return } if req.Code == "" { c.FailWithMessage("请输入验证码", "请输入验证码") return } //校验验证码 code, err := utils.Rc.RedisString(utils.HZ_MOBILE_ADMIN_WXLOGIN_MOBILE_CODE + req.Mobile) if err != nil { c.FailWithMessage("验证码错误或者已过期", "验证码错误或者已过期"+err.Error()) return } if req.Code != code { c.FailWithMessage("验证码错误", "验证码错误") return } //获取token,如果没有token不允许发送验证码 infoJson, err := utils.Rc.RedisString(utils.HZ_MOBILE_ADMIN_WXLOGIN_TOKEN + req.Token) if err != nil { c.FailWithMessage("授权信息不存在", "授权信息不存在"+err.Error()) return } info := map[string]string{"open_id": "", "union_id": ""} err = json.Unmarshal([]byte(infoJson), &info) if err != nil { c.FailWithMessage("授权信息解析失败", "授权信息解析失败"+err.Error()) return } openId := info["open_id"] unionId := info["union_id"] if openId == "" { c.FailWithMessage("授权信息不存在", "授权信息不存在") return } //如果该token中的openID已经被其他手机号绑定,则绑定openId adminInfo, err := admin.GetAdminWxByAdminOpenId(openId) if err != nil { if err.Error() == utils.ErrNoRow() { err = nil } else { c.FailWithMessage("查询管理员信息失败", "查询管理员信息失败"+err.Error()) return } } else { if adminInfo.Mobile != req.Mobile { c.FailWithMessage("该微信已被其他账号绑定", "该微信已被其他账号绑定") return } else { c.Ok() return } } //判断手机号是否已绑定,如果已绑定openId,并且和token不相等,则更新 adminMobileInfo, err := admin.GetAdminByMobile(req.Mobile) if err != nil { if err.Error() == utils.ErrNoRow() { c.FailWithMessage("账号不存在,联系管理员", "账号不存在,联系管理员") return } c.FailWithMessage("管理员账号查询失败", "管理员账号查询失败"+err.Error()) return } if adminMobileInfo.OpenId == openId { c.Ok() return } //更新openid err = admin.UpdateAdminOpenIdUnionId(adminMobileInfo.AdminId, openId, unionId) if err != nil { c.FailWithMessage("绑定手机号失败", "绑定手机号失败"+err.Error()) return } c.Ok() return } // CompanyRemindList // @Title 客户过期提醒记录 // @Description 客户过期提醒记录 // @Param UniqueCode query string true "唯一编码" // @Success 200 {object} company_remind_record.CompanyRemindRecordResp // @router /company_remind/list [get] func (c *AdminWxController) CompanyRemindList() { uniqueCode := c.GetString("UniqueCode") if uniqueCode == "" { c.FailWithMessage("来源异常", "UniqueCode异常") return } list, err := company_remind_record.GetAllCompanyRemindRecordList(uniqueCode) if err != nil { c.FailWithMessage("获取失败", "获取客户即将过期提醒失败:"+err.Error()) return } lenList := len(list) response := company_remind_record.CompanyRemindRecordResp{ Total: lenList, List: list, } if len(list) > 0 { response.RemindType = list[0].Type response.EndDate = list[0].EndDate } c.OkDetailed(response, "获取成功") return } // EtaBusinessRemindList // @Title ETA商家过期提醒记录 // @Description ETA商家过期提醒记录 // @Param UniqueCode query string true "唯一编码" // @Success 200 {object} eta_business_remind_record.EtaBusinessRemindRecordResp // @router /eta_business_remind/list [get] func (c *AdminWxController) EtaBusinessRemindList() { uniqueCode := c.GetString("UniqueCode") if uniqueCode == "" { c.FailWithMessage("来源异常", "UniqueCode异常") return } remindOb := new(eta_business_remind_record.EtaBusinessRemindRecord) cond := ` AND unique_code = ?` pars := make([]interface{}, 0) pars = append(pars, uniqueCode) list, err := remindOb.GetItemsByCondition(cond, pars, []string{}, "") if err != nil { c.FailWithMessage("获取失败", "获取客户即将过期提醒失败:"+err.Error()) return } lenList := len(list) response := eta_business_remind_record.EtaBusinessRemindRecordResp{ Total: lenList, List: list, } if lenList > 0 { response.RemindType = list[0].Type response.EndDate = list[0].EndDate } c.OkDetailed(response, "获取成功") return }