Browse Source

完善健康档案

Rick.Lan 2 months ago
parent
commit
245cce40ec

+ 4 - 4
conf/app.conf View File

15
 qiniu_domain = https://images.shengws.com/
15
 qiniu_domain = https://images.shengws.com/
16
 qiniu_bucket = syhclub-storage
16
 qiniu_bucket = syhclub-storage
17
 
17
 
18
-appid = "wxcdf53b48b7df107e"
18
+#appid = "wxcdf53b48b7df107e"
19
 key="Yz1HgsFX3yJvWPJSEdwJDA=="
19
 key="Yz1HgsFX3yJvWPJSEdwJDA=="
20
-appsecret="94e944a69ad1d43ac447f5a8769ab801"
20
+#appsecret="94e944a69ad1d43ac447f5a8769ab801"
21
 
21
 
22
 [test]
22
 [test]
23
 redishost = kuyi6666.redis.rds.aliyuncs.com
23
 redishost = kuyi6666.redis.rds.aliyuncs.com
38
 writemysqlname = sws_xcx
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 View File

66
 		tokenString = authHeader[len("Bearer "):]
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
 		if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
70
 		if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
71
 			return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
71
 			return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
72
 		}
72
 		}
82
 	}
82
 	}
83
 
83
 
84
 	// 假设JWT中包含了用户ID
84
 	// 假设JWT中包含了用户ID
85
-	claims, ok := token.Claims.(CustomClaims)
85
+	claims, ok := token.Claims.(*CustomClaims)
86
 	if !ok {
86
 	if !ok {
87
 		c.ServeFailJsonSendAndStop(http.StatusUnauthorized, enums.ErrorCodeNotLogin, "无法解析Token中的信息")
87
 		c.ServeFailJsonSendAndStop(http.StatusUnauthorized, enums.ErrorCodeNotLogin, "无法解析Token中的信息")
88
 		return
88
 		return
92
 	c.sessionKey = key
92
 	c.sessionKey = key
93
 	redisCli := service.RedisClient()
93
 	redisCli := service.RedisClient()
94
 	defer redisCli.Close()
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
 	if err != nil {
97
 	if err != nil {
98
 		c.ServeFailJsonSendAndStop(http.StatusUnauthorized, enums.ErrorCodeNotLogin, fmt.Sprintf("redis err:%v", err))
98
 		c.ServeFailJsonSendAndStop(http.StatusUnauthorized, enums.ErrorCodeNotLogin, fmt.Sprintf("redis err:%v", err))
172
 	uJson, _ := json.Marshal(u)
172
 	uJson, _ := json.Marshal(u)
173
 	redisCli := service.RedisClient()
173
 	redisCli := service.RedisClient()
174
 	defer redisCli.Close()
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
 	claims := CustomClaims{
177
 	claims := CustomClaims{
178
 		UserID: key,
178
 		UserID: key,
179
 		StandardClaims: jwt.StandardClaims{
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
 	if expire == 0 {
205
 	if expire == 0 {
206
 		expire = 7200
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
 	if err != nil {
209
 	if err != nil {
210
 		return err
210
 		return err
211
 	}
211
 	}

+ 5 - 0
controllers/user_api_controller.go View File

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

+ 1 - 1
enums/error_code.go View File

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

+ 8 - 0
models/dbmodels.go View File

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

+ 1 - 0
routers/router.go View File

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

+ 2 - 2
service/redis.go View File

12
 	address := fmt.Sprintf("%s:%s", beego.AppConfig.String("redishost"), beego.AppConfig.String("redisport"))
12
 	address := fmt.Sprintf("%s:%s", beego.AppConfig.String("redishost"), beego.AppConfig.String("redisport"))
13
 	client := redis.NewClient(&redis.Options{
13
 	client := redis.NewClient(&redis.Options{
14
 		Addr:     address,
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
 	pong, err := client.Ping().Result()
18
 	pong, err := client.Ping().Result()
19
 	if err != nil {
19
 	if err != nil {

+ 5 - 1
service/userservice.go View File

28
 func (s *XcxUserService) GetOrCreate(openId string, unionId string) (*models.XcxUser, error) {
28
 func (s *XcxUserService) GetOrCreate(openId string, unionId string) (*models.XcxUser, error) {
29
 
29
 
30
 	user := &models.XcxUser{OpenId: openId, UnionId: unionId}
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
 	return user, db.Error
37
 	return user, db.Error
34
 
38
 

+ 37 - 5
swagger/swagger.json View File

68
                     "500": {
68
                     "500": {
69
                         "description": "error"
69
                         "description": "error"
70
                     }
70
                     }
71
-                }
71
+                },
72
+                "security": [
73
+                    {
74
+                        "token": []
75
+                    }
76
+                ]
72
             }
77
             }
73
         },
78
         },
74
         "/api/user/getuserinfo": {
79
         "/api/user/getuserinfo": {
88
                     "500": {
93
                     "500": {
89
                         "description": "error"
94
                         "description": "error"
90
                     }
95
                     }
91
-                }
96
+                },
97
+                "security": [
98
+                    {
99
+                        "token": []
100
+                    }
101
+                ]
92
             }
102
             }
93
         },
103
         },
94
         "/api/user/login": {
104
         "/api/user/login": {
147
                     "500": {
157
                     "500": {
148
                         "description": "error"
158
                         "description": "error"
149
                     }
159
                     }
150
-                }
160
+                },
161
+                "security": [
162
+                    {
163
+                        "token": []
164
+                    }
165
+                ]
151
             }
166
             }
152
         },
167
         },
153
         "/api/user/saveuserinfo": {
168
         "/api/user/saveuserinfo": {
175
                     "500": {
190
                     "500": {
176
                         "description": "error"
191
                         "description": "error"
177
                     }
192
                     }
178
-                }
193
+                },
194
+                "security": [
195
+                    {
196
+                        "token": []
197
+                    }
198
+                ]
179
             }
199
             }
180
         },
200
         },
181
         "/api/user/updatephonebycode": {
201
         "/api/user/updatephonebycode": {
206
                     "500": {
226
                     "500": {
207
                         "description": "error"
227
                         "description": "error"
208
                     }
228
                     }
209
-                }
229
+                },
230
+                "security": [
231
+                    {
232
+                        "token": []
233
+                    }
234
+                ]
210
             }
235
             }
211
         }
236
         }
212
     },
237
     },
519
             "title": "type",
544
             "title": "type",
520
             "type": "object"
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 View File

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