customer.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. package controller
  2. import (
  3. "cold-delivery/app/admin/model"
  4. "cold-delivery/app/admin/service"
  5. "cold-delivery/app/admin/service/dto"
  6. "cold-delivery/common/actions"
  7. "cold-delivery/common/middleware/handler"
  8. "cold-delivery/conf"
  9. "errors"
  10. "fmt"
  11. "github.com/gin-gonic/gin"
  12. "github.com/gin-gonic/gin/binding"
  13. "go.uber.org/zap"
  14. "gogs.baozhida.cn/zoie/OAuth-core/api"
  15. "gogs.baozhida.cn/zoie/OAuth-core/pkg"
  16. "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
  17. _ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
  18. "gogs.baozhida.cn/zoie/OAuth-core/pkg/sms"
  19. "golang.org/x/crypto/bcrypt"
  20. "net/http"
  21. )
  22. // Customer 定义客户控制器
  23. type Customer struct {
  24. api.Api
  25. }
  26. // GetPage 获取客户列表
  27. // @Summary 获取客户列表
  28. // @Description 获取客户列表
  29. // @Tags 客户
  30. // @Param username query string false "用户名"
  31. // @Param name query string false "姓名"
  32. // @Param type query int false "类型"
  33. // @Param pageSize query int false "页条数"
  34. // @Param page query int false "页码"
  35. // @Success 200 {object} response.Response{data=response.Page{list=[]model.SysUser}} "{"code": 200, "data": [...]}"
  36. // @Router /api/customer [get]
  37. // @Security Bearer
  38. func (e Customer) GetPage(c *gin.Context) {
  39. s := service.Customer{}
  40. req := dto.CustomerGetPageReq{}
  41. err := e.MakeContext(c).
  42. MakeOrm().
  43. Bind(&req, binding.Query).
  44. MakeService(&s.Service).
  45. Errors
  46. if err != nil {
  47. e.Logger.Error(err)
  48. e.Error(500, err, err.Error())
  49. return
  50. }
  51. //数据权限检查
  52. p := actions.GetPermissionFromContext(c)
  53. list := make([]model.SysUser, 0)
  54. var count int64
  55. err = s.GetPage(&req, p, &list, &count)
  56. if err != nil {
  57. e.Error(500, err, err.Error())
  58. return
  59. }
  60. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  61. }
  62. // Get 通过id获取客户
  63. // @Summary 通过id获取客户
  64. // @Description 通过id获取客户
  65. // @Tags 客户
  66. // @Param id path int true "客户id"
  67. // @Success 200 {object} response.Response{data=model.SysUser} "{"code": 200, "data": [...]}"
  68. // @Router /api/customer/{id} [get]
  69. // @Security Bearer
  70. func (e Customer) Get(c *gin.Context) {
  71. s := service.Customer{}
  72. req := dto.CustomerGetReq{}
  73. err := e.MakeContext(c).
  74. MakeOrm().
  75. Bind(&req, nil).
  76. MakeService(&s.Service).
  77. Errors
  78. if err != nil {
  79. e.Logger.Error(err)
  80. e.Error(500, err, err.Error())
  81. return
  82. }
  83. //数据权限检查
  84. //p := actions.GetPermissionFromContext(c)
  85. var object model.SysUser
  86. err = s.Get(&req, nil, &object)
  87. if err != nil {
  88. e.Error(http.StatusUnprocessableEntity, err, err.Error())
  89. return
  90. }
  91. e.OK(object, "查询成功")
  92. }
  93. // Insert 创建客户
  94. // @Summary 创建客户
  95. // @Description 创建客户
  96. // @Tags 客户
  97. // @Accept application/json
  98. // @Product application/json
  99. // @Param data body dto.CustomerInsertReq true "body"
  100. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  101. // @Router /api/customer [post]
  102. // @Security Bearer
  103. func (e Customer) Insert(c *gin.Context) {
  104. s := service.Customer{}
  105. req := dto.CustomerInsertReq{}
  106. err := e.MakeContext(c).
  107. MakeOrm().
  108. Bind(&req, binding.JSON).
  109. MakeService(&s.Service).
  110. Errors
  111. if err != nil {
  112. e.Logger.Error(err)
  113. e.Error(500, err, err.Error())
  114. return
  115. }
  116. //数据权限检查
  117. p := actions.GetPermissionFromContext(c)
  118. if p.DeptId == 0 {
  119. err = errors.New("无权添加,请联系管理员!")
  120. e.Error(500, err, err.Error())
  121. return
  122. }
  123. // 设置创建人
  124. req.SetCreateBy(user.GetUserId(c))
  125. req.SetDeptId(p.DeptId)
  126. err = s.Insert(&req)
  127. if err != nil {
  128. e.Error(500, err, err.Error())
  129. return
  130. }
  131. e.OK(req.GetId(), "添加成功")
  132. }
  133. // Update 修改客户数据
  134. // @Summary 修改客户数据
  135. // @Description 修改客户数据
  136. // @Tags 客户
  137. // @Accept application/json
  138. // @Product application/json
  139. // @Param data body dto.CustomerUpdateReq true "body"
  140. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  141. // @Router /api/customer [put]
  142. // @Security Bearer
  143. func (e Customer) Update(c *gin.Context) {
  144. s := service.Customer{}
  145. req := dto.CustomerUpdateReq{}
  146. err := e.MakeContext(c).
  147. MakeOrm().
  148. Bind(&req).
  149. MakeService(&s.Service).
  150. Errors
  151. if err != nil {
  152. e.Logger.Error(err)
  153. e.Error(500, err, err.Error())
  154. return
  155. }
  156. req.SetUpdateBy(user.GetUserId(c))
  157. //数据权限检查
  158. //p := actions.GetPermissionFromContext(c)
  159. err = s.Update(&req, nil)
  160. if err != nil {
  161. e.Error(500, err, err.Error())
  162. return
  163. }
  164. e.OK(req.GetId(), "修改成功")
  165. }
  166. // Delete 通过id删除客户数据
  167. // @Summary 通过id删除客户数据
  168. // @Description 通过id删除客户数据
  169. // @Tags 客户
  170. // @Param data body dto.CustomerDeleteReq true "body"
  171. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  172. // @Router /api/customer [delete]
  173. // @Security Bearer
  174. func (e Customer) Delete(c *gin.Context) {
  175. s := service.Customer{}
  176. req := dto.CustomerDeleteReq{}
  177. err := e.MakeContext(c).
  178. MakeOrm().
  179. Bind(&req, binding.JSON).
  180. MakeService(&s.Service).
  181. Errors
  182. if err != nil {
  183. e.Logger.Error(err)
  184. e.Error(500, err, err.Error())
  185. return
  186. }
  187. userId := user.GetUserId(c)
  188. if userId == req.Id {
  189. err := errors.New("禁止删除自己")
  190. e.Error(500, err, err.Error())
  191. return
  192. }
  193. // 设置编辑人
  194. req.SetUpdateBy(userId)
  195. //数据权限检查
  196. //p := actions.GetPermissionFromContext(c)
  197. err = s.Remove(&req, nil)
  198. if err != nil {
  199. e.Error(500, err, err.Error())
  200. return
  201. }
  202. e.OK(req.GetId(), "删除成功")
  203. }
  204. // ResetPwd 重置客户密码
  205. // @Summary 重置客户密码
  206. // @Description 重置客户密码
  207. // @Tags 客户
  208. // @Accept application/json
  209. // @Product application/json
  210. // @Param data body dto.ResetCustomerPwdReq true "body"
  211. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  212. // @Router /api/user/pwd/reset [put]
  213. // @Security Bearer
  214. func (e Customer) ResetPwd(c *gin.Context) {
  215. s := service.Customer{}
  216. req := dto.ResetCustomerPwdReq{}
  217. err := e.MakeContext(c).
  218. MakeOrm().
  219. Bind(&req, binding.JSON).
  220. MakeService(&s.Service).
  221. Errors
  222. if err != nil {
  223. e.Logger.Error(err)
  224. e.Error(500, err, err.Error())
  225. return
  226. }
  227. req.SetUpdateBy(user.GetUserId(c))
  228. //数据权限检查
  229. //p := actions.GetPermissionFromContext(c)
  230. err = s.ResetPwd(&req, nil)
  231. if err != nil {
  232. e.Logger.Error(err)
  233. e.Error(500, err, err.Error())
  234. return
  235. }
  236. e.OK(req.GetId(), "修改成功")
  237. }
  238. // UpdatePwd 修改密码
  239. // @Summary 修改密码
  240. // @Description 修改密码
  241. // @Tags 个人中心
  242. // @Accept application/json
  243. // @Product application/json
  244. // @Param data body dto.CustomerPassWord true "body"
  245. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  246. // @Router /api/customer/pwd/set [put]
  247. // @Security Bearer
  248. func (e Customer) UpdatePwd(c *gin.Context) {
  249. s := service.Customer{}
  250. req := dto.CustomerPassWord{}
  251. err := e.MakeContext(c).
  252. MakeOrm().
  253. Bind(&req).
  254. MakeService(&s.Service).
  255. Errors
  256. if err != nil {
  257. e.Logger.Error(err)
  258. e.Error(500, err, err.Error())
  259. return
  260. }
  261. var hash []byte
  262. if hash, err = bcrypt.GenerateFromPassword([]byte(req.NewPassword), bcrypt.DefaultCost); err != nil {
  263. req.NewPassword = string(hash)
  264. }
  265. err = s.UpdatePwd(user.GetUserId(c), req.OldPassword, req.NewPassword)
  266. if err != nil {
  267. e.Error(http.StatusForbidden, err, err.Error())
  268. return
  269. }
  270. e.OK(nil, "密码修改成功")
  271. }
  272. // GetProfile 获取个人中心客户
  273. // @Summary 获取个人中心客户
  274. // @Description 获取个人中心客户
  275. // @Tags 个人中心
  276. // @Success 200 {object} response.Response{user=model.SysUser,role=model.SysRole} "{"code": 200, "data": {"user":[...],"role":[...]}}"
  277. // @Router /api/user/profile [get]
  278. // @Security Bearer
  279. func (e Customer) GetProfile(c *gin.Context) {
  280. s := service.Customer{}
  281. req := dto.CustomerById{}
  282. err := e.MakeContext(c).
  283. MakeOrm().
  284. MakeService(&s.Service).
  285. Errors
  286. if err != nil {
  287. e.Logger.Error(err)
  288. e.Error(500, err, err.Error())
  289. return
  290. }
  291. req.Id = user.GetUserId(c)
  292. sysUser := model.SysUser{}
  293. err = s.GetProfile(&req, &sysUser)
  294. if err != nil {
  295. e.Logger.Errorf("get user profile error, %s", err.Error())
  296. e.Error(500, err, "获取客户信息失败")
  297. return
  298. }
  299. e.OK(gin.H{
  300. "user": sysUser,
  301. }, "查询成功")
  302. }
  303. // GetInfo 获取个人信息
  304. // @Summary 获取个人信息
  305. // @Description 获取个人信息
  306. // @Tags 个人中心
  307. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  308. // @Router /api/user/info [get]
  309. // @Security Bearer
  310. func (e Customer) GetInfo(c *gin.Context) {
  311. req := dto.CustomerGetReq{}
  312. s := service.Customer{}
  313. r := service.SysRole{}
  314. err := e.MakeContext(c).
  315. MakeOrm().
  316. MakeService(&r.Service).
  317. MakeService(&s.Service).
  318. Errors
  319. if err != nil {
  320. e.Logger.Error(err)
  321. e.Error(500, err, err.Error())
  322. return
  323. }
  324. var roles = make([]string, 1)
  325. roles[0] = user.GetRoleName(c)
  326. var permissions = make([]string, 1)
  327. permissions[0] = "*:*:*"
  328. var buttons = make([]string, 1)
  329. buttons[0] = "*:*:*"
  330. var mp = make(map[string]interface{})
  331. mp["roles"] = roles
  332. list, _ := r.GetById(user.GetRoleId(c))
  333. mp["permissions"] = list
  334. mp["buttons"] = list
  335. sysUser := model.SysUser{}
  336. req.Id = user.GetUserId(c)
  337. err = s.Get(&req, nil, &sysUser)
  338. if err != nil {
  339. e.Logger.Errorf("get user info error, %s", err.Error())
  340. e.Error(http.StatusUnauthorized, err, err.Error())
  341. return
  342. }
  343. mp["userName"] = sysUser.Username
  344. mp["userId"] = sysUser.Id
  345. mp["deptId"] = sysUser.DeptId
  346. mp["name"] = sysUser.NickName
  347. mp["code"] = 200
  348. e.OK(mp, "查询成功")
  349. }
  350. // VerifyCode 获取短信验证码
  351. // @Summary 获取短信验证码
  352. // @Description 获取短信验证码
  353. // @Tags 登录
  354. // @Param phone query string false "电话号码"
  355. // @Success 200 {string} string "{"code": 200, "data": "18888888888"}"
  356. // @Router /verify-code [get]
  357. // @Security Bearer
  358. func (e Customer) VerifyCode(c *gin.Context) {
  359. s := service.Customer{}
  360. req := dto.CustomerGetSMSVerifyCodeReq{}
  361. err := e.MakeContext(c).
  362. MakeService(&s.Service).
  363. Bind(&req, binding.Query).
  364. Errors
  365. if err != nil {
  366. e.Logger.Error(err)
  367. e.Error(500, err, err.Error())
  368. return
  369. }
  370. key := handler.GetVerifyCodeCacheKey(req.Phone)
  371. _, err = e.Cache.Get(key)
  372. // 验证吗缓存已存在
  373. if err == nil {
  374. e.Error(500, err, "验证吗已发送,请注意查收")
  375. return
  376. }
  377. code := pkg.GenerateRandomFigureKey6()
  378. ss := sms.NewSMS(conf.ExtConfig.SubMail.Appid, conf.ExtConfig.SubMail.Signature)
  379. content := fmt.Sprintf("【冷链送药平台】您的短信验证码:%s,请在10分钟内输入", code)
  380. res, err := ss.Send(req.Phone, content)
  381. if err != nil || res.Status != sms.SUCCESS {
  382. e.Logger.Error("发送短信验证码出现异常", zap.Any("res", res), zap.Error(err))
  383. e.Error(500, err, "验证码发送失败,请重试")
  384. return
  385. }
  386. _ = e.Cache.Set(key, code, 600)
  387. e.OK(req.Phone, "发送成功")
  388. }