Browse Source

學生簡易Login功能
CLOSE #430

jeff 4 years ago
parent
commit
7b995b649f
1 changed files with 79 additions and 0 deletions
  1. 79 0
      TEAMModelOS/Controllers/School/StudentController.cs

+ 79 - 0
TEAMModelOS/Controllers/School/StudentController.cs

@@ -1924,5 +1924,84 @@ namespace TEAMModelOS.Controllers
             }
             }
 
 
         }
         }
+
+        /// <summary>
+        /// 學生簡易登入
+        /// </summary>
+        /// <param name = "request" ></ param >
+        [AllowAnonymous]
+        [HttpPost("login-simple")]
+        public async Task<IActionResult> LoginSimple(JsonElement request)
+        {
+            try
+            {
+                var client = _azureCosmos.GetCosmosClient();
+                var schoolClient = client.GetContainer("TEAMModelOS", "School");
+                var studentClient = client.GetContainer("TEAMModelOS", "Student");
+                //參數取得
+                if (!request.TryGetProperty("school_code", out JsonElement school_code)) return BadRequest();
+                if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
+                if (!request.TryGetProperty("pw", out JsonElement pw)) return BadRequest();
+
+                var response = await studentClient.ReadItemStreamAsync(id.GetString(), new PartitionKey($"Base-{school_code.GetString().ToLower()}"));
+                if (response.Status == 200)
+                {
+                    var rjson = await JsonDocument.ParseAsync(response.ContentStream);
+                    rjson.RootElement.TryGetProperty("salt", out JsonElement salt);
+                    rjson.RootElement.TryGetProperty("pw", out JsonElement dbpw);
+                    rjson.RootElement.TryGetProperty("name", out JsonElement name);
+                    rjson.RootElement.TryGetProperty("picture", out JsonElement picture);
+                    rjson.RootElement.TryGetProperty("classId", out JsonElement classId);
+                    rjson.RootElement.TryGetProperty("no", out JsonElement no);
+                    rjson.RootElement.TryGetProperty("groupId", out JsonElement groupId);
+                    rjson.RootElement.TryGetProperty("groupName", out JsonElement groupName);
+                    dynamic user = new ExpandoObject();
+                    user.no = no;
+                    user.groupId = groupId;
+                    user.groupName = groupName;
+
+                    var HashedPW = Utils.HashedPassword(pw.ToString(), salt.ToString());
+                    if (HashedPW.Equals(dbpw.GetString()))
+                    {
+                        //取得所屬預設班級信息
+                        object classinfo = null;
+                        if (!classId.ValueKind.Equals(JsonValueKind.Null) && classId.ValueKind.Equals(JsonValueKind.String))
+                        {
+                            var query = $"SELECT c.id, c.no, c.name FROM c WHERE c.id = '{classId.GetString()}'";
+                            await foreach (var item in schoolClient.GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{school_code}") }))
+                            {
+                                using var json = await JsonDocument.ParseAsync(item.ContentStream);
+                                if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
+                                {
+                                    foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
+                                    {
+                                        classinfo = obj.ToObject<object>();
+                                    }
+                                }
+                            }
+                        }
+                        //換取AuthToken,提供給前端
+                        var auth_token = JwtAuthExtension.CreateAuthToken(_option.HostName, id.GetString(), name.GetString(), picture.GetString(), _option.JwtSecretKey, schoolID: school_code.GetString(), roles: new[] { "student" });
+                        //其他訊息
+                        dynamic school = new ExpandoObject();
+                        //回傳
+                        return Ok(new { error = 0, auth_token, classinfo, user });
+                    }
+                    else
+                    {
+                        return Ok(new { error = 1, message = "Invalid account or password" });
+                    }
+                }
+                else
+                {
+                    return Ok(new { error = 2, message = "Invalid account" });
+                }
+            }
+            catch (Exception ex)
+            {
+                await _dingDing.SendBotMsg($"IES5,{_option.Location},StudentController/login-simple()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
+                return BadRequest();
+            }
+        }
     }
     }
 }
 }