MCPcopy Create free account
hub / github.com/aiprodcoder/MIXAPI / GenerateAccessToken

Function GenerateAccessToken

controller/user.go:304–343  ·  view source on GitHub ↗
(c *gin.Context)

Source from the content-addressed store, hash-verified

302}
303
304func GenerateAccessToken(c *gin.Context) {
305 id := c.GetInt("id")
306 user, err := model.GetUserById(id, true)
307 if err != nil {
308 common.ApiError(c, err)
309 return
310 }
311 // get rand int 28-32
312 randI := common.GetRandomInt(4)
313 key, err := common.GenerateRandomKey(29 + randI)
314 if err != nil {
315 c.JSON(http.StatusOK, gin.H{
316 "success": false,
317 "message": "生成失败",
318 })
319 common.SysError("failed to generate key: " + err.Error())
320 return
321 }
322 user.SetAccessToken(key)
323
324 if model.DB.Where("access_token = ?", user.AccessToken).First(user).RowsAffected != 0 {
325 c.JSON(http.StatusOK, gin.H{
326 "success": false,
327 "message": "请重试,系统生成的 UUID 竟然重复了!",
328 })
329 return
330 }
331
332 if err := user.Update(false); err != nil {
333 common.ApiError(c, err)
334 return
335 }
336
337 c.JSON(http.StatusOK, gin.H{
338 "success": true,
339 "message": "",
340 "data": user.AccessToken,
341 })
342 return
343}
344
345type TransferAffQuotaRequest struct {
346 Quota int `json:"quota" binding:"required"`

Callers

nothing calls this directly

Calls 8

GetUserByIdFunction · 0.92
ApiErrorFunction · 0.92
GetRandomIntFunction · 0.92
GenerateRandomKeyFunction · 0.92
SysErrorFunction · 0.92
ErrorMethod · 0.80
SetAccessTokenMethod · 0.80
UpdateMethod · 0.45

Tested by

no test coverage detected