123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- 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
- }
|