customer.go 11 KB

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