Przeglądaj źródła

完善健康档案

Rick.Lan 2 miesięcy temu
rodzic
commit
245cce40ec

+ 4 - 4
conf/app.conf Wyświetl plik

@@ -15,9 +15,9 @@ qiniu_secretkey = DmZSp_Bmnp-9aUB7xUvoyViZpzmx1Rs2RL69GvlW
15 15
 qiniu_domain = https://images.shengws.com/
16 16
 qiniu_bucket = syhclub-storage
17 17
 
18
-appid = "wxcdf53b48b7df107e"
18
+#appid = "wxcdf53b48b7df107e"
19 19
 key="Yz1HgsFX3yJvWPJSEdwJDA=="
20
-appsecret="94e944a69ad1d43ac447f5a8769ab801"
20
+#appsecret="94e944a69ad1d43ac447f5a8769ab801"
21 21
 
22 22
 [test]
23 23
 redishost = kuyi6666.redis.rds.aliyuncs.com
@@ -38,5 +38,5 @@ writemysqlpass = 1Q2W3e4r!@#$
38 38
 writemysqlname = sws_xcx
39 39
 
40 40
 
41
-#appid = "wx25576346fbca6905"
42
-#appsecret="f6d53ccb4a529dc4d3bd543a7634b6bd"
41
+appid = "wx25576346fbca6905"
42
+appsecret="f6d53ccb4a529dc4d3bd543a7634b6bd"

+ 7 - 7
controllers/api_base_controller.go Wyświetl plik

@@ -66,7 +66,7 @@ func (c *BaseApiAuthController) Prepare() {
66 66
 		tokenString = authHeader[len("Bearer "):]
67 67
 	}
68 68
 
69
-	token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
69
+	token, err := jwt.ParseWithClaims(tokenString, &CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
70 70
 		if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
71 71
 			return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
72 72
 		}
@@ -82,7 +82,7 @@ func (c *BaseApiAuthController) Prepare() {
82 82
 	}
83 83
 
84 84
 	// 假设JWT中包含了用户ID
85
-	claims, ok := token.Claims.(CustomClaims)
85
+	claims, ok := token.Claims.(*CustomClaims)
86 86
 	if !ok {
87 87
 		c.ServeFailJsonSendAndStop(http.StatusUnauthorized, enums.ErrorCodeNotLogin, "无法解析Token中的信息")
88 88
 		return
@@ -92,7 +92,7 @@ func (c *BaseApiAuthController) Prepare() {
92 92
 	c.sessionKey = key
93 93
 	redisCli := service.RedisClient()
94 94
 	defer redisCli.Close()
95
-	uJson, err := redisCli.Get(key).Bytes()
95
+	uJson, err := redisCli.Get(fmt.Sprintf("session:%v", key)).Bytes()
96 96
 
97 97
 	if err != nil {
98 98
 		c.ServeFailJsonSendAndStop(http.StatusUnauthorized, enums.ErrorCodeNotLogin, fmt.Sprintf("redis err:%v", err))
@@ -172,13 +172,13 @@ func (c *BaseApiController) login(u models.XcxUser) (string, error) {
172 172
 	uJson, _ := json.Marshal(u)
173 173
 	redisCli := service.RedisClient()
174 174
 	defer redisCli.Close()
175
-	service.RedisClient().Set(key, uJson, time.Second*time.Duration(expire))
175
+	redisCli.Set(fmt.Sprintf("session:%v", key), uJson, time.Hour*time.Duration(expire))
176 176
 	// 创建一个我们自己的声明
177 177
 	claims := CustomClaims{
178 178
 		UserID: key,
179 179
 		StandardClaims: jwt.StandardClaims{
180
-			ExpiresAt: time.Now().Add(time.Second * time.Duration(expire)).Unix(), // Token有效期(默认为7200秒)
181
-			Issuer:    "sws_xcx",                                                  // 签发者
180
+			ExpiresAt: time.Now().Add(time.Hour * time.Duration(expire)).Unix(), // Token有效期(默认为7200秒)
181
+			Issuer:    "sws_xcx",                                                // 签发者
182 182
 		},
183 183
 	}
184 184
 
@@ -205,7 +205,7 @@ func (c *BaseApiAuthController) updateCurrentUser(updateAction func(*models.XcxU
205 205
 	if expire == 0 {
206 206
 		expire = 7200
207 207
 	}
208
-	err = redisCli.Set(c.sessionKey, uJson, time.Second*time.Duration(expire)).Err()
208
+	err = redisCli.Set(fmt.Sprintf("session:%v", c.sessionKey), uJson, time.Second*time.Duration(expire)).Err()
209 209
 	if err != nil {
210 210
 		return err
211 211
 	}

+ 5 - 0
controllers/user_api_controller.go Wyświetl plik

@@ -17,6 +17,7 @@ type UserApiController struct {
17 17
 // @Description 获取个人中心信息
18 18
 // @Success 200 {object} models.UserInfoResp success
19 19
 // @Failure 500 error
20
+// @Security token
20 21
 // @router /getuserinfo [get]
21 22
 func (c *UserApiController) GetUserInfo() {
22 23
 
@@ -43,6 +44,7 @@ func (c *UserApiController) GetUserInfo() {
43 44
 // @Param	body	body 	models.WxXcxLoginReq	true  "小程序登录请求参数"
44 45
 // @Success 200 {object} models.XcxUser success
45 46
 // @Failure 500 error
47
+// @Security token
46 48
 // @router /updatephonebycode [post]
47 49
 func (c *UserApiController) UpdatePhoneByCode() {
48 50
 	dataBody := models.WxXcxLoginReq{}
@@ -82,6 +84,7 @@ func (c *UserApiController) UpdatePhoneByCode() {
82 84
 // @Param	body	body 	models.SaveUserInfoReq	true  "小程序登录请求参数"
83 85
 // @Success 200  success
84 86
 // @Failure 500 error
87
+// @Security token
85 88
 // @router /saveuserinfo [post]
86 89
 func (c *UserApiController) SaveUserInfo() {
87 90
 	req := &models.SaveUserInfoReq{}
@@ -123,6 +126,7 @@ func (c *UserApiController) SaveUserInfo() {
123 126
 // @Param	body	body 	models.SaveHealthProfileReq	true  "小程序登录请求参数"
124 127
 // @Success 200  success
125 128
 // @Failure 500 error
129
+// @Security token
126 130
 // @router /savehealthprofile [post]
127 131
 func (c *UserApiController) SaveHealthProfile() {
128 132
 
@@ -144,6 +148,7 @@ func (c *UserApiController) SaveHealthProfile() {
144 148
 // @Description 获取健康档案
145 149
 // @Success 200 {object} models.HealthProfileResp success
146 150
 // @Failure 500 error
151
+// @Security token
147 152
 // @router /gethealthprofile [get]
148 153
 func (c *UserApiController) GetHealthProfile() {
149 154
 	p, err := service.NewUserHealthProfileService().GetUserHealthProfileByUserId(c.CurrentUser.Id)

+ 1 - 1
enums/error_code.go Wyświetl plik

@@ -496,7 +496,7 @@ func MakeFailResponseJSON(errMsg string, errCode int) CommonResp {
496 496
 }
497 497
 
498 498
 func MakeDynamicFailResponseJSON(errMsg string) CommonResp {
499
-	resp := CommonResp{Msg: errMsg, State: 0}
499
+	resp := CommonResp{Code: ErrorCommitFail, Msg: errMsg, State: 0}
500 500
 	return resp
501 501
 }
502 502
 

+ 8 - 0
models/dbmodels.go Wyświetl plik

@@ -2,6 +2,7 @@ package models
2 2
 
3 3
 import "time"
4 4
 
5
+//检测项目
5 6
 type CheckItem struct {
6 7
 	Id              int       `json:"id" gorm:"type:int(11) auto_increment; NOT NULL; primary_key; COMMENT:'检测项目ID'"`
7 8
 	CheckItemNumber int       `json:"check_item_number" gorm:"type:int(11); COMMENT:'排序'"`
@@ -25,6 +26,7 @@ func (CheckItem) TableName() string {
25 26
 	return "check_item"
26 27
 }
27 28
 
29
+//检测记录
28 30
 type CheckRecord struct {
29 31
 	Id                  int64     `json:"id" gorm:"type:bigint(20); NOT NULL; primary_key; COMMENT:'检测记录ID'"`
30 32
 	CheckType           string    `json:"check_type" gorm:"type:varchar(255); COMMENT:'检测类型(试纸类型)'"`
@@ -46,6 +48,7 @@ func (CheckRecord) TableName() string {
46 48
 	return "check_record"
47 49
 }
48 50
 
51
+//检测记录详情
49 52
 type CheckRecordItem struct {
50 53
 	Id              int64     `json:"id" gorm:"type:bigint(20) auto_increment; NOT NULL; primary_key"`
51 54
 	CheckId         int64     `json:"check_id" gorm:"type:bigint(20); NOT NULL; DEFAULT:'0'"`
@@ -61,6 +64,7 @@ func (CheckRecordItem) TableName() string {
61 64
 	return "check_record_item"
62 65
 }
63 66
 
67
+//设备表
64 68
 type Device struct {
65 69
 	Id                   uint64    `json:"id" gorm:"type:bigint(20) unsigned auto_increment; NOT NULL; primary_key; COMMENT:'设备ID'"`
66 70
 	Name                 string    `json:"name" gorm:"type:varchar(255); COMMENT:'设备名称'"`
@@ -92,6 +96,7 @@ func (Device) TableName() string {
92 96
 	return "device"
93 97
 }
94 98
 
99
+//设备消息日志
95 100
 type DeviceMessageLog struct {
96 101
 	Id         uint64    `json:"id" gorm:"type:bigint(20) unsigned auto_increment; NOT NULL; primary_key"`
97 102
 	MessageId  string    `json:"message_id" gorm:"type:varchar(255)"`
@@ -106,6 +111,7 @@ func (DeviceMessageLog) TableName() string {
106 111
 	return "device_message_log"
107 112
 }
108 113
 
114
+//设备绑定表
109 115
 type DeviceRelate struct {
110 116
 	Id         int64     `json:"id" gorm:"type:bigint(20) auto_increment; NOT NULL; primary_key; COMMENT:'id'"`
111 117
 	Name       string    `json:"name" gorm:"type:varchar(255); COMMENT:'名称'"`
@@ -134,6 +140,7 @@ func (SysDictionary) TableName() string {
134 140
 	return "sys_dictionary"
135 141
 }
136 142
 
143
+//用户健康档案
137 144
 type UserHealthProfile struct {
138 145
 	Id                  uint64    `json:"id" gorm:"type:bigint(20) unsigned auto_increment; NOT NULL; primary_key; COMMENT:'Primary Key ID'"`
139 146
 	UserId              uint64    `json:"user_id" gorm:"type:bigint(20) unsigned; NOT NULL; COMMENT:'用户ID'"`
@@ -165,6 +172,7 @@ func (UserHealthProfile) TableName() string {
165 172
 	return "user_health_profile"
166 173
 }
167 174
 
175
+//小程序用户表(个人中心)
168 176
 type XcxUser struct {
169 177
 	Id                      uint64    `json:"id" gorm:"type:bigint(20) unsigned auto_increment; NOT NULL; primary_key; COMMENT:'Primary Key ID'"`
170 178
 	Phone                   string    `json:"phone" gorm:"type:varchar(32); COMMENT:'手机号码'"`

+ 1 - 0
routers/router.go Wyświetl plik

@@ -2,6 +2,7 @@
2 2
 // @Title sws xcx api doc
3 3
 // @Description 圣卫士小程序API接口文档
4 4
 // @License 领透科技
5
+// @SecurityDefinition token apiKey Authorization header
5 6
 package routers
6 7
 
7 8
 import (

+ 2 - 2
service/redis.go Wyświetl plik

@@ -12,8 +12,8 @@ func RedisClient() *redis.Client {
12 12
 	address := fmt.Sprintf("%s:%s", beego.AppConfig.String("redishost"), beego.AppConfig.String("redisport"))
13 13
 	client := redis.NewClient(&redis.Options{
14 14
 		Addr:     address,
15
-		Password: beego.AppConfig.String("redispasswrod"), // no password set
16
-		DB:       0,                                       // use default DB
15
+		Password: beego.AppConfig.String("redispasswrod"),  // no password set
16
+		DB:       beego.AppConfig.DefaultInt("redisdb", 4), // use default DB
17 17
 	})
18 18
 	pong, err := client.Ping().Result()
19 19
 	if err != nil {

+ 5 - 1
service/userservice.go Wyświetl plik

@@ -28,7 +28,11 @@ func (s *XcxUserService) GetUser(id uint64) (*models.XcxUser, error) {
28 28
 func (s *XcxUserService) GetOrCreate(openId string, unionId string) (*models.XcxUser, error) {
29 29
 
30 30
 	user := &models.XcxUser{OpenId: openId, UnionId: unionId}
31
-	db := writeDb.Where("open_id = ?", openId).Or("union_id = ?", unionId).FirstOrCreate(user)
31
+	db := writeDb.Where("open_id = ?", openId)
32
+	if unionId != "" {
33
+		db = db.Or("union_id = ?", unionId)
34
+	}
35
+	db.FirstOrCreate(user)
32 36
 
33 37
 	return user, db.Error
34 38
 

+ 37 - 5
swagger/swagger.json Wyświetl plik

@@ -68,7 +68,12 @@
68 68
                     "500": {
69 69
                         "description": "error"
70 70
                     }
71
-                }
71
+                },
72
+                "security": [
73
+                    {
74
+                        "token": []
75
+                    }
76
+                ]
72 77
             }
73 78
         },
74 79
         "/api/user/getuserinfo": {
@@ -88,7 +93,12 @@
88 93
                     "500": {
89 94
                         "description": "error"
90 95
                     }
91
-                }
96
+                },
97
+                "security": [
98
+                    {
99
+                        "token": []
100
+                    }
101
+                ]
92 102
             }
93 103
         },
94 104
         "/api/user/login": {
@@ -147,7 +157,12 @@
147 157
                     "500": {
148 158
                         "description": "error"
149 159
                     }
150
-                }
160
+                },
161
+                "security": [
162
+                    {
163
+                        "token": []
164
+                    }
165
+                ]
151 166
             }
152 167
         },
153 168
         "/api/user/saveuserinfo": {
@@ -175,7 +190,12 @@
175 190
                     "500": {
176 191
                         "description": "error"
177 192
                     }
178
-                }
193
+                },
194
+                "security": [
195
+                    {
196
+                        "token": []
197
+                    }
198
+                ]
179 199
             }
180 200
         },
181 201
         "/api/user/updatephonebycode": {
@@ -206,7 +226,12 @@
206 226
                     "500": {
207 227
                         "description": "error"
208 228
                     }
209
-                }
229
+                },
230
+                "security": [
231
+                    {
232
+                        "token": []
233
+                    }
234
+                ]
210 235
             }
211 236
         }
212 237
     },
@@ -519,5 +544,12 @@
519 544
             "title": "type",
520 545
             "type": "object"
521 546
         }
547
+    },
548
+    "securityDefinitions": {
549
+        "token": {
550
+            "type": "apiKey",
551
+            "name": "Authorization",
552
+            "in": "header"
553
+        }
522 554
     }
523 555
 }

+ 15 - 0
swagger/swagger.yml Wyświetl plik

@@ -53,6 +53,8 @@ paths:
53 53
             $ref: '#/definitions/models.HealthProfileResp'
54 54
         "500":
55 55
           description: error
56
+      security:
57
+      - token: []
56 58
   /api/user/getuserinfo:
57 59
     get:
58 60
       tags:
@@ -68,6 +70,8 @@ paths:
68 70
             $ref: '#/definitions/models.UserInfoResp'
69 71
         "500":
70 72
           description: error
73
+      security:
74
+      - token: []
71 75
   /api/user/login:
72 76
     post:
73 77
       tags:
@@ -110,6 +114,8 @@ paths:
110 114
           description: success
111 115
         "500":
112 116
           description: error
117
+      security:
118
+      - token: []
113 119
   /api/user/saveuserinfo:
114 120
     post:
115 121
       tags:
@@ -130,6 +136,8 @@ paths:
130 136
           description: success
131 137
         "500":
132 138
           description: error
139
+      security:
140
+      - token: []
133 141
   /api/user/updatephonebycode:
134 142
     post:
135 143
       tags:
@@ -152,6 +160,8 @@ paths:
152 160
             $ref: '#/definitions/models.XcxUser'
153 161
         "500":
154 162
           description: error
163
+      security:
164
+      - token: []
155 165
 definitions:
156 166
   models.HealthProfileResp:
157 167
     title: HealthProfileResp
@@ -371,3 +381,8 @@ definitions:
371 381
   models.type:
372 382
     title: type
373 383
     type: object
384
+securityDefinitions:
385
+  token:
386
+    type: apiKey
387
+    name: Authorization
388
+    in: header