admin_wx.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "hongze/hongze_mobile_admin/models/tables/admin"
  5. "hongze/hongze_mobile_admin/models/tables/company_remind_record"
  6. "hongze/hongze_mobile_admin/models/tables/eta_business_remind_record"
  7. "hongze/hongze_mobile_admin/services"
  8. "hongze/hongze_mobile_admin/utils"
  9. "time"
  10. )
  11. type AdminWxController struct {
  12. BaseCommon
  13. }
  14. // SendSmsCode
  15. // @Title 发送短信验证码
  16. // @Description 发送短信验证码
  17. // @Param Mobile query string true "手机号"
  18. // @Param Token query string true "授权token"
  19. // @Success 200 {object} wechat.WxLoginResp
  20. // @router /send_sms_code [post]
  21. func (c *AdminWxController) SendSmsCode() {
  22. var req admin.SmsCodeReq
  23. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  24. if err != nil {
  25. c.FailWithMessage("参数解析异常!", "参数解析失败,Err:"+err.Error())
  26. return
  27. }
  28. if req.Mobile == "" {
  29. c.FailWithMessage("请输入手机号", "请输入手机号")
  30. return
  31. }
  32. if req.Token == "" {
  33. c.FailWithMessage("请输入授权标识", "请输入授权标识")
  34. return
  35. }
  36. areaNum := req.AreaNum
  37. //获取token,如果没有token不允许发送验证码
  38. infoJson, err := utils.Rc.RedisString(utils.HZ_MOBILE_ADMIN_WXLOGIN_TOKEN + req.Token)
  39. if err != nil {
  40. c.FailWithMessage("授权信息不存在", "授权信息不存在"+err.Error())
  41. return
  42. }
  43. info := map[string]string{"open_id": "", "union_id": ""}
  44. err = json.Unmarshal([]byte(infoJson), &info)
  45. if err != nil {
  46. c.FailWithMessage("授权信息解析失败", "授权信息解析失败"+err.Error())
  47. return
  48. }
  49. openId := info["open_id"]
  50. if openId == "" {
  51. c.FailWithMessage("授权信息不存在", "授权信息不存在")
  52. return
  53. }
  54. //如果该token中的openID已经被其他手机号绑定,则不允许发送验证码
  55. adminInfo, err := admin.GetAdminWxByAdminOpenId(openId)
  56. if err != nil {
  57. if err.Error() == utils.ErrNoRow() {
  58. err = nil
  59. } else {
  60. c.FailWithMessage("查询管理员信息失败", "查询管理员信息失败"+err.Error())
  61. return
  62. }
  63. } else {
  64. if adminInfo.Mobile != req.Mobile {
  65. c.FailWithMessage("该微信已被其他账号绑定", "该微信已被其他账号绑定")
  66. return
  67. } else {
  68. resp := &admin.WxSmsResp{
  69. BindFlag: true,
  70. }
  71. c.OkDetailed(resp, "已绑定")
  72. return
  73. }
  74. }
  75. //判断手机号是否已绑定,如果已绑定openId,并且和token相等,则不允许发送验证码
  76. adminMobileInfo, err := admin.GetAdminByMobile(req.Mobile)
  77. if err != nil {
  78. if err.Error() == utils.ErrNoRow() {
  79. c.FailWithMessage("账号不存在,联系管理员", "账号不存在,联系管理员")
  80. return
  81. }
  82. c.FailWithMessage("管理员账号查询失败", "管理员账号查询失败"+err.Error())
  83. return
  84. }
  85. if adminMobileInfo.OpenId == openId {
  86. resp := &admin.WxSmsResp{
  87. BindFlag: true,
  88. }
  89. c.OkDetailed(resp, "已绑定")
  90. return
  91. }
  92. //发送短信
  93. var sendRet bool
  94. code := utils.GetRandDigit(4)
  95. if areaNum == "86" || areaNum == "" || areaNum == "0" {
  96. //国内的短信
  97. sendRet = services.SendSmsCode(req.Mobile, code)
  98. } else {
  99. //国际短信
  100. sendRet = services.SendSmsCodeGj(req.Mobile, code, areaNum)
  101. }
  102. if !sendRet {
  103. c.FailWithMessage("发送验证码失败", "发送验证码失败")
  104. return
  105. }
  106. // 存储验证码,15分钟过期
  107. err = utils.Rc.Put(utils.HZ_MOBILE_ADMIN_WXLOGIN_MOBILE_CODE+req.Mobile, code, 15*time.Minute)
  108. if err != nil {
  109. c.FailWithMessage("发送验证码失败", "保存验证码到redis中失败"+err.Error())
  110. return
  111. }
  112. resp := &admin.WxSmsResp{
  113. SmsFlag: true,
  114. }
  115. c.OkDetailed(resp, "发送成功")
  116. return
  117. }
  118. // BindMobile
  119. // @Title 绑定手机号
  120. // @Description 绑定手机号
  121. // @Param Mobile query string true "手机号"
  122. // @Param Token query string true "授权token"
  123. // @Param Code query string true "短信验证码"
  124. // @Success 200 {object} wechat.WxLoginResp
  125. // @router /bind_mobile [post]
  126. func (c *AdminWxController) BindMobile() {
  127. var req admin.BindMobileReq
  128. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  129. if err != nil {
  130. c.FailWithMessage("参数解析异常!", "参数解析失败,Err:"+err.Error())
  131. return
  132. }
  133. if req.Mobile == "" {
  134. c.FailWithMessage("请输入手机号", "请输入手机号")
  135. return
  136. }
  137. if req.Token == "" {
  138. c.FailWithMessage("请输入授权标识", "请输入授权标识")
  139. return
  140. }
  141. if req.Code == "" {
  142. c.FailWithMessage("请输入验证码", "请输入验证码")
  143. return
  144. }
  145. //校验验证码
  146. code, err := utils.Rc.RedisString(utils.HZ_MOBILE_ADMIN_WXLOGIN_MOBILE_CODE + req.Mobile)
  147. if err != nil {
  148. c.FailWithMessage("验证码错误或者已过期", "验证码错误或者已过期"+err.Error())
  149. return
  150. }
  151. if req.Code != code {
  152. c.FailWithMessage("验证码错误", "验证码错误")
  153. return
  154. }
  155. //获取token,如果没有token不允许发送验证码
  156. infoJson, err := utils.Rc.RedisString(utils.HZ_MOBILE_ADMIN_WXLOGIN_TOKEN + req.Token)
  157. if err != nil {
  158. c.FailWithMessage("授权信息不存在", "授权信息不存在"+err.Error())
  159. return
  160. }
  161. info := map[string]string{"open_id": "", "union_id": ""}
  162. err = json.Unmarshal([]byte(infoJson), &info)
  163. if err != nil {
  164. c.FailWithMessage("授权信息解析失败", "授权信息解析失败"+err.Error())
  165. return
  166. }
  167. openId := info["open_id"]
  168. unionId := info["union_id"]
  169. if openId == "" {
  170. c.FailWithMessage("授权信息不存在", "授权信息不存在")
  171. return
  172. }
  173. //如果该token中的openID已经被其他手机号绑定,则绑定openId
  174. adminInfo, err := admin.GetAdminWxByAdminOpenId(openId)
  175. if err != nil {
  176. if err.Error() == utils.ErrNoRow() {
  177. err = nil
  178. } else {
  179. c.FailWithMessage("查询管理员信息失败", "查询管理员信息失败"+err.Error())
  180. return
  181. }
  182. } else {
  183. if adminInfo.Mobile != req.Mobile {
  184. c.FailWithMessage("该微信已被其他账号绑定", "该微信已被其他账号绑定")
  185. return
  186. } else {
  187. c.Ok()
  188. return
  189. }
  190. }
  191. //判断手机号是否已绑定,如果已绑定openId,并且和token不相等,则更新
  192. adminMobileInfo, err := admin.GetAdminByMobile(req.Mobile)
  193. if err != nil {
  194. if err.Error() == utils.ErrNoRow() {
  195. c.FailWithMessage("账号不存在,联系管理员", "账号不存在,联系管理员")
  196. return
  197. }
  198. c.FailWithMessage("管理员账号查询失败", "管理员账号查询失败"+err.Error())
  199. return
  200. }
  201. if adminMobileInfo.OpenId == openId {
  202. c.Ok()
  203. return
  204. }
  205. //更新openid
  206. err = admin.UpdateAdminOpenIdUnionId(adminMobileInfo.AdminId, openId, unionId)
  207. if err != nil {
  208. c.FailWithMessage("绑定手机号失败", "绑定手机号失败"+err.Error())
  209. return
  210. }
  211. c.Ok()
  212. return
  213. }
  214. // CompanyRemindList
  215. // @Title 客户过期提醒记录
  216. // @Description 客户过期提醒记录
  217. // @Param UniqueCode query string true "唯一编码"
  218. // @Success 200 {object} company_remind_record.CompanyRemindRecordResp
  219. // @router /company_remind/list [get]
  220. func (c *AdminWxController) CompanyRemindList() {
  221. uniqueCode := c.GetString("UniqueCode")
  222. if uniqueCode == "" {
  223. c.FailWithMessage("来源异常", "UniqueCode异常")
  224. return
  225. }
  226. list, err := company_remind_record.GetAllCompanyRemindRecordList(uniqueCode)
  227. if err != nil {
  228. c.FailWithMessage("获取失败", "获取客户即将过期提醒失败:"+err.Error())
  229. return
  230. }
  231. lenList := len(list)
  232. response := company_remind_record.CompanyRemindRecordResp{
  233. Total: lenList,
  234. List: list,
  235. }
  236. if len(list) > 0 {
  237. response.RemindType = list[0].Type
  238. response.EndDate = list[0].EndDate
  239. }
  240. c.OkDetailed(response, "获取成功")
  241. return
  242. }
  243. // EtaBusinessRemindList
  244. // @Title ETA商家过期提醒记录
  245. // @Description ETA商家过期提醒记录
  246. // @Param UniqueCode query string true "唯一编码"
  247. // @Success 200 {object} eta_business_remind_record.EtaBusinessRemindRecordResp
  248. // @router /eta_business_remind/list [get]
  249. func (c *AdminWxController) EtaBusinessRemindList() {
  250. uniqueCode := c.GetString("UniqueCode")
  251. if uniqueCode == "" {
  252. c.FailWithMessage("来源异常", "UniqueCode异常")
  253. return
  254. }
  255. remindOb := new(eta_business_remind_record.EtaBusinessRemindRecord)
  256. cond := ` AND unique_code = ?`
  257. pars := make([]interface{}, 0)
  258. pars = append(pars, uniqueCode)
  259. list, err := remindOb.GetItemsByCondition(cond, pars, []string{}, "")
  260. if err != nil {
  261. c.FailWithMessage("获取失败", "获取客户即将过期提醒失败:"+err.Error())
  262. return
  263. }
  264. lenList := len(list)
  265. response := eta_business_remind_record.EtaBusinessRemindRecordResp{
  266. Total: lenList,
  267. List: list,
  268. }
  269. if lenList > 0 {
  270. response.RemindType = list[0].Type
  271. response.EndDate = list[0].EndDate
  272. }
  273. c.OkDetailed(response, "获取成功")
  274. return
  275. }