|
@@ -5,11 +5,15 @@ import (
|
|
|
"gas-cylinder-api/app/admin/service"
|
|
|
"gas-cylinder-api/app/admin/service/dto"
|
|
|
"gas-cylinder-api/common/actions"
|
|
|
+ "github.com/dgrijalva/jwt-go"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"github.com/gin-gonic/gin/binding"
|
|
|
"gogs.baozhida.cn/zoie/OAuth-core/api"
|
|
|
- "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
|
|
|
_ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
|
|
|
+ "golang.org/x/crypto/bcrypt"
|
|
|
+ "net/http"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
type AppletCustomerController struct {
|
|
@@ -19,9 +23,9 @@ type AppletCustomerController struct {
|
|
|
// Login 小程序登录
|
|
|
// @Summary 小程序登录
|
|
|
// @Description 小程序登录
|
|
|
-// @Tags 客户小程序
|
|
|
+// @Tags 用户端
|
|
|
// @Success 200 {object} response.Response{data=response.Page{list=[]model.Customer}} "{"code": 200, "data": [...]}"
|
|
|
-// @Router /api/customer [get]
|
|
|
+// @Router /api/applet/login [get]
|
|
|
// @Security Bearer
|
|
|
func (e AppletCustomerController) Login(c *gin.Context) {
|
|
|
s := service.AppletCustomer{}
|
|
@@ -49,12 +53,100 @@ func (e AppletCustomerController) Login(c *gin.Context) {
|
|
|
e.OK(data, "查询成功")
|
|
|
}
|
|
|
|
|
|
+// Refresh 刷新token
|
|
|
+// @Summary 刷新token
|
|
|
+// @Description 刷新token
|
|
|
+// @Tags 用户端
|
|
|
+// @Success 200 {object} response.Response{data=response.Page{list=[]model.Customer}} "{"code": 200, "data": [...]}"
|
|
|
+// @Router /api/applet/refresh [get]
|
|
|
+// @Security Bearer
|
|
|
+func (e AppletCustomerController) Refresh(c *gin.Context) {
|
|
|
+ s := service.AppletCustomer{}
|
|
|
+ err := e.MakeContext(c).
|
|
|
+ MakeOrm().
|
|
|
+ MakeService(&s.Service).
|
|
|
+ Errors
|
|
|
+ if err != nil {
|
|
|
+ e.Logger.Error(err)
|
|
|
+ e.Error(500, err, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ tokenString := c.GetHeader("Authorization")
|
|
|
+ if tokenString == "" {
|
|
|
+ c.JSON(http.StatusUnauthorized, gin.H{
|
|
|
+ "code": 401,
|
|
|
+ "msg": "Unauthorized",
|
|
|
+ })
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ parts := strings.SplitN(tokenString, " ", 2)
|
|
|
+ if !(len(parts) == 2 && parts[0] == "Bearer") {
|
|
|
+ c.JSON(http.StatusOK, gin.H{
|
|
|
+ "code": 401,
|
|
|
+ "msg": "请求头中auth格式有误",
|
|
|
+ })
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析token
|
|
|
+ token, err := jwt.ParseWithClaims(parts[1], &model.CustomerClaims{}, func(token *jwt.Token) (i interface{}, err error) {
|
|
|
+ return model.AppletCustomerSecret, nil
|
|
|
+ })
|
|
|
+
|
|
|
+ if err != nil || !token.Valid {
|
|
|
+ if err.(*jwt.ValidationError).Errors != jwt.ValidationErrorExpired {
|
|
|
+ c.JSON(http.StatusUnauthorized, gin.H{
|
|
|
+ "code": 401,
|
|
|
+ "msg": "Unauthorized",
|
|
|
+ })
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ claims, ok := token.Claims.(*model.CustomerClaims)
|
|
|
+ if !ok {
|
|
|
+ c.JSON(http.StatusUnauthorized, gin.H{
|
|
|
+ "code": 401,
|
|
|
+ "msg": "Unauthorized",
|
|
|
+ })
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if claims.ExpiresAt < time.Now().Unix() {
|
|
|
+ if claims.ExpiresAt > time.Now().Add(-2*time.Hour).Unix() {
|
|
|
+ newToken, expiresAt, _ := s.GeneratorToken(claims.CustomerId)
|
|
|
+ c.Set("customer_id", claims.CustomerId)
|
|
|
+ data := map[string]string{
|
|
|
+ "token": newToken,
|
|
|
+ "expiresAt": expiresAt,
|
|
|
+ }
|
|
|
+ e.OK(data, "查询成功")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ c.JSON(http.StatusUnauthorized, gin.H{
|
|
|
+ "code": 401,
|
|
|
+ "msg": "Token is expired",
|
|
|
+ })
|
|
|
+ return
|
|
|
+ }
|
|
|
+ newToken, expiresAt, _ := s.GeneratorToken(claims.CustomerId)
|
|
|
+ c.Set("customer_id", claims.CustomerId)
|
|
|
+ data := map[string]string{
|
|
|
+ "token": newToken,
|
|
|
+ "expiresAt": expiresAt,
|
|
|
+ }
|
|
|
+ e.OK(data, "查询成功")
|
|
|
+}
|
|
|
+
|
|
|
// Register 注册
|
|
|
// @Summary 小程序登录
|
|
|
// @Description 小程序登录
|
|
|
-// @Tags 客户小程序
|
|
|
+// @Tags 用户端
|
|
|
// @Success 200 {object} response.Response{data=response.Page{list=[]model.Customer}} "{"code": 200, "data": [...]}"
|
|
|
-// @Router /api/customer [get]
|
|
|
+// @Router /api/applet/register [get]
|
|
|
// @Security Bearer
|
|
|
func (e AppletCustomerController) Register(c *gin.Context) {
|
|
|
s := service.AppletCustomer{}
|
|
@@ -81,9 +173,9 @@ func (e AppletCustomerController) Register(c *gin.Context) {
|
|
|
// GetProfile 获取个人中心用户
|
|
|
// @Summary 获取个人中心用户
|
|
|
// @Description 获取个人中心用户
|
|
|
-// @Tags 个人中心
|
|
|
+// @Tags 用户端
|
|
|
// @Success 200 {object} response.Response{user=model.SysUser,role=model.SysRole} "{"code": 200, "data": {"user":[...],"role":[...]}}"
|
|
|
-// @Router /api/user/profile [get]
|
|
|
+// @Router /api/applet/profile [get]
|
|
|
// @Security Bearer
|
|
|
func (e AppletCustomerController) GetProfile(c *gin.Context) {
|
|
|
s := service.Customer{}
|
|
@@ -112,7 +204,7 @@ func (e AppletCustomerController) GetProfile(c *gin.Context) {
|
|
|
// GetOrderPage 获取订单列表
|
|
|
// @Summary 获取订单列表
|
|
|
// @Description 获取订单列表
|
|
|
-// @Tags 订单
|
|
|
+// @Tags 用户端
|
|
|
// @Param state query int false "订单状态 1-已下单 2-已派送 3-已送达 4-已取消"
|
|
|
// @Param source query int false "订单状态 1-坐席下单 2-小程序"
|
|
|
// @Param orderStartTime query string false "下单开始时间"
|
|
@@ -122,7 +214,7 @@ func (e AppletCustomerController) GetProfile(c *gin.Context) {
|
|
|
// @Param pageSize query int false "页条数"
|
|
|
// @Param page query int false "页码"
|
|
|
// @Success 200 {object} response.Response{data=response.Page{list=[]model.Order}} "{"code": 200, "data": [...]}"
|
|
|
-// @Router /api/Order [get]
|
|
|
+// @Router /api/applet/order [get]
|
|
|
// @Security Bearer
|
|
|
func (e AppletCustomerController) GetOrderPage(c *gin.Context) {
|
|
|
s := service.Order{}
|
|
@@ -156,13 +248,13 @@ func (e AppletCustomerController) GetOrderPage(c *gin.Context) {
|
|
|
// OrderInsert 订气
|
|
|
// @Summary 订气
|
|
|
// @Description 订气
|
|
|
-// @Tags 用户H5订单
|
|
|
+// @Tags 用户端
|
|
|
// @Accept application/json
|
|
|
// @Product application/json
|
|
|
-// @Param data body dto.OrderInsertReq true "data"
|
|
|
+// @Param data body dto.OrderAppletInsertReq true "data"
|
|
|
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
|
|
|
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
|
|
|
-// @Router /api/order [post]
|
|
|
+// @Router /api/applet/order [post]
|
|
|
// @Security Bearer
|
|
|
func (e AppletCustomerController) OrderInsert(c *gin.Context) {
|
|
|
s := service.Order{}
|
|
@@ -189,9 +281,20 @@ func (e AppletCustomerController) OrderInsert(c *gin.Context) {
|
|
|
e.OK(req.GetId(), "创建成功")
|
|
|
}
|
|
|
|
|
|
+// OrderUpdate 修改订单
|
|
|
+// @Summary 修改订单
|
|
|
+// @Description 修改订单
|
|
|
+// @Tags 用户端
|
|
|
+// @Accept application/json
|
|
|
+// @Product application/json
|
|
|
+// @Param data body dto.OrderAppletUpdateReq true "data"
|
|
|
+// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
|
|
|
+// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
|
|
|
+// @Router /api/applet/order [put]
|
|
|
+// @Security Bearer
|
|
|
func (e AppletCustomerController) OrderUpdate(c *gin.Context) {
|
|
|
s := service.Order{}
|
|
|
- req := dto.OrderUpdateReq{}
|
|
|
+ req := dto.OrderAppletUpdateReq{}
|
|
|
err := e.MakeContext(c).
|
|
|
MakeOrm().
|
|
|
Bind(&req, binding.JSON).
|
|
@@ -203,11 +306,7 @@ func (e AppletCustomerController) OrderUpdate(c *gin.Context) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- //数据权限检查
|
|
|
- p := actions.GetPermissionFromContext(c)
|
|
|
- // 设置创建人
|
|
|
- req.SetCreateBy(user.GetUserId(c))
|
|
|
- err = s.Update(&req, p)
|
|
|
+ err = s.AppletUpdate(&req)
|
|
|
if err != nil {
|
|
|
e.Error(500, err, err.Error())
|
|
|
return
|
|
@@ -215,13 +314,48 @@ func (e AppletCustomerController) OrderUpdate(c *gin.Context) {
|
|
|
e.OK(req.GetId(), "修改成功")
|
|
|
}
|
|
|
|
|
|
+// OrderCancel 取消订单
|
|
|
+// @Summary 取消订单
|
|
|
+// @Description 取消订单
|
|
|
+// @Tags 用户端
|
|
|
+// @Accept application/json
|
|
|
+// @Product application/json
|
|
|
+// @Param data body dto.OrderCancelReq true "data"
|
|
|
+// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
|
|
|
+// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
|
|
|
+// @Router /api/applet/order [put]
|
|
|
+// @Security Bearer
|
|
|
+func (e AppletCustomerController) OrderCancel(c *gin.Context) {
|
|
|
+ s := service.Order{}
|
|
|
+ req := dto.OrderCancelReq{}
|
|
|
+ err := e.MakeContext(c).
|
|
|
+ MakeOrm().
|
|
|
+ Bind(&req, binding.JSON).
|
|
|
+ MakeService(&s.Service).
|
|
|
+ Errors
|
|
|
+ if err != nil {
|
|
|
+ e.Logger.Error(err)
|
|
|
+ e.Error(500, err, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ err = s.Cancel(&req, nil)
|
|
|
+ if err != nil {
|
|
|
+ e.Error(500, err, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ e.OK(req.GetId(), "取消成功")
|
|
|
+}
|
|
|
+
|
|
|
// StoreList 获取销售门店列表
|
|
|
// @Summary 获取销售门店列表
|
|
|
-// @Description 获取销售门店列表(用户公司及子公司)
|
|
|
-// @Tags 销售门店
|
|
|
+// @Description 获取销售门店列表
|
|
|
+// @Tags 用户端
|
|
|
// @Param name query string false "销售门店名称"
|
|
|
+// @Param district query string false "所在地市"
|
|
|
+// @Param city query string false "所在区县"
|
|
|
// @Success 200 {object} response.Response{data=response.Page{list=[]model.SysDept}} "{"code": 200, "data": [...]}"
|
|
|
-// @Router /api/store [get]
|
|
|
+// @Router /api/applet/store [get]
|
|
|
// @Security Bearer
|
|
|
func (e AppletCustomerController) StoreList(c *gin.Context) {
|
|
|
s := service.Store{}
|
|
@@ -246,3 +380,150 @@ func (e AppletCustomerController) StoreList(c *gin.Context) {
|
|
|
}
|
|
|
e.OK(list, "查询成功")
|
|
|
}
|
|
|
+
|
|
|
+// UpdatePwd 修改密码
|
|
|
+// @Summary 修改密码
|
|
|
+// @Description 修改密码
|
|
|
+// @Tags 用户端
|
|
|
+// @Accept application/json
|
|
|
+// @Product application/json
|
|
|
+// @Param data body dto.PassWord true "body"
|
|
|
+// @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
|
|
|
+// @Router /api/user/pwd/set [put]
|
|
|
+// @Security Bearer
|
|
|
+func (e AppletCustomerController) UpdatePwd(c *gin.Context) {
|
|
|
+ s := service.AppletCustomer{}
|
|
|
+ req := dto.PassWord{}
|
|
|
+ err := e.MakeContext(c).
|
|
|
+ MakeOrm().
|
|
|
+ Bind(&req).
|
|
|
+ MakeService(&s.Service).
|
|
|
+ Errors
|
|
|
+ if err != nil {
|
|
|
+ e.Logger.Error(err)
|
|
|
+ e.Error(500, err, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ var hash []byte
|
|
|
+ if hash, err = bcrypt.GenerateFromPassword([]byte(req.NewPassword), bcrypt.DefaultCost); err != nil {
|
|
|
+ req.NewPassword = string(hash)
|
|
|
+ }
|
|
|
+
|
|
|
+ err = s.UpdatePwd(service.GetAppletCustomerId(c), req.OldPassword, req.NewPassword)
|
|
|
+ if err != nil {
|
|
|
+ e.Error(http.StatusForbidden, err, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ e.OK(nil, "密码修改成功")
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+// GetGoodsPage 获取商品列表
|
|
|
+// @Summary 获取商品列表
|
|
|
+// @Description 获取商品列表
|
|
|
+// @Tags 用户端
|
|
|
+// @Param storeId query int true "销售门店id"
|
|
|
+// @Param name query string false "商品名称"
|
|
|
+// @Param mediaType query string false "介质类型"
|
|
|
+// @Param isShow query int false "是否展示 1-展示 2-不展示"
|
|
|
+// @Success 200 {object} response.Response{data=response.Page{list=[]model.Goods}} "{"code": 200, "data": [...]}"
|
|
|
+// @Router /api/goods [get]
|
|
|
+// @Security Bearer
|
|
|
+func (e AppletCustomerController) GetGoodsPage(c *gin.Context) {
|
|
|
+ s := service.Goods{}
|
|
|
+ req := dto.AppletGoodsGetPageReq{}
|
|
|
+ err := e.MakeContext(c).
|
|
|
+ MakeOrm().
|
|
|
+ Bind(&req, binding.Query).
|
|
|
+ MakeService(&s.Service).
|
|
|
+ Errors
|
|
|
+ if err != nil {
|
|
|
+ e.Logger.Error(err)
|
|
|
+ e.Error(500, err, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ //数据权限检查
|
|
|
+ p := actions.GetPermissionFromContext(c)
|
|
|
+ p.DeptId = req.StoreId
|
|
|
+
|
|
|
+ list := make([]model.Goods, 0)
|
|
|
+ var count int64
|
|
|
+
|
|
|
+ err = s.GetAppletPage(&req, &list, &count)
|
|
|
+ if err != nil {
|
|
|
+ e.Error(500, err, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
|
|
|
+}
|
|
|
+
|
|
|
+// GetGasCylinderSpecPage 获取钢瓶规格列表
|
|
|
+// @Summary 获取钢瓶规格列表
|
|
|
+// @Description 获取钢瓶规格列表
|
|
|
+// @Tags 钢瓶规格
|
|
|
+// @Param name query string false "钢瓶规格名称"
|
|
|
+// @Success 200 {object} response.Response{data=response.Page{list=[]model.GasCylinderSpec}} "{"code": 200, "data": [...]}"
|
|
|
+// @Router /api/dispatch-cost [get]
|
|
|
+// @Security Bearer
|
|
|
+func (e AppletCustomerController) GetGasCylinderSpecPage(c *gin.Context) {
|
|
|
+ s := service.GasCylinderSpec{}
|
|
|
+ req := dto.AppletGasCylinderSpecGetPageReq{}
|
|
|
+ err := e.MakeContext(c).
|
|
|
+ MakeOrm().
|
|
|
+ Bind(&req, binding.Query).
|
|
|
+ MakeService(&s.Service).
|
|
|
+ Errors
|
|
|
+ if err != nil {
|
|
|
+ e.Logger.Error(err)
|
|
|
+ e.Error(500, err, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ //数据权限检查
|
|
|
+ list := make([]model.GasCylinderSpec, 0)
|
|
|
+ var count int64
|
|
|
+
|
|
|
+ err = s.GetAppletPage(&req, &list, &count)
|
|
|
+ if err != nil {
|
|
|
+ e.Error(500, err, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
|
|
|
+}
|
|
|
+
|
|
|
+// UpdateCustomerInfo 修改个人信息
|
|
|
+// @Summary 修改客户信息
|
|
|
+// @Description 修改客户信息
|
|
|
+// @Tags 客户信息
|
|
|
+// @Accept application/json
|
|
|
+// @Product application/json
|
|
|
+// @Param id path string true "客户信息id"
|
|
|
+// @Param data body dto.CustomerUpdateReq true "body"
|
|
|
+// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
|
|
|
+// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
|
|
|
+// @Router /api/customer [put]
|
|
|
+// @Security Bearer
|
|
|
+func (e AppletCustomerController) UpdateCustomerInfo(c *gin.Context) {
|
|
|
+ s := service.Customer{}
|
|
|
+ req := dto.AppletCustomerUpdateReq{}
|
|
|
+ err := e.MakeContext(c).
|
|
|
+ MakeOrm().
|
|
|
+ Bind(&req).
|
|
|
+ MakeService(&s.Service).
|
|
|
+ Errors
|
|
|
+ if err != nil {
|
|
|
+ e.Logger.Error(err)
|
|
|
+ e.Error(500, err, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ req.Id = service.GetAppletCustomerId(c)
|
|
|
+ err = s.AppletUpdate(&req)
|
|
|
+ if err != nil {
|
|
|
+ e.Error(500, err, err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ e.OK(req.GetId(), "更新成功")
|
|
|
+}
|