defp gen_code(conn) do
code = "#{:rand.uniform(9)}#{:rand.uniform(9)}#{:rand.uniform(9)}#{:rand.uniform(9)}"
Logger.info("sms code: #{code}")
# Change conn in here
conn |> put_session(:sms_code, code)
conn |> put_session(:sms_code_created_at, Core.Ecto.Type.CSTDateTime.now())
code
end
I want to get the new conn value below, but i can’t use render to change conn.
“gen_code” just handle the sms code and don’t handle any request and render. “gen_code” is put sms code to session, but can’t update global conn value. As a result, I can’t get SMS code in the create below.
How can I do ?
def create(conn, %{"user_profile" => user_profile}) do
user = current_user(conn)
if validate_sms_code(conn, user_profile["code"]) do
case UserProfiles.create_user_profile(Map.merge(user_profile, %{"user_id" => user.id})) do
{:ok, profile} ->
send_format(conn, profile)
{:error, changeset} ->
send_format(conn, changeset)
end
else
conn
|> json(%{ error_str: "验证码错误", request_state: 1 })
end
end
“gen_code” just handle the sms code and don’t handle any request and render.
Then make it do just that and not bother itself with conns:
defp gen_code do
"#{:rand.uniform(9)}#{:rand.uniform(9)}#{:rand.uniform(9)}#{:rand.uniform(9)}"
end
And in some other function where you actually handle the connection:
code = gen_code()
Logger.info("sms code: #{code}")
conn
|> put_session(:sms_code, code) # note that the client can read it unless you encrypt the code
|> put_session(:sms_code_created_at, Core.Ecto.Type.CSTDateTime.now())
As a result, I can’t get SMS code in the create below.