package auth

import (
	"net/http"

	"git.i2edu.net/i2/go-zero/rest/httpx"
	"git.i2edu.net/i2/i2-bill-api/internal/logic/auth"
	"git.i2edu.net/i2/i2-bill-api/internal/svc"
	"git.i2edu.net/i2/i2-bill-api/internal/types"
	"git.i2edu.net/i2/i2-bill-api/internal/utils"
)

func LoginByWeixinHandler(ctx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.LoginByWeixinRequest
		if err := httpx.Parse(r, &req); err != nil {
			httpx.OkJson(w, utils.ReturnHTTPFail(err))
			return
		}
		l := auth.NewLoginByWeixinLogic(r.Context(), ctx)
		resp, err := l.LoginByWeixin(req)
		if err != nil {
			httpx.OkJson(w, utils.ReturnHTTPFail(err))
		} else {
			httpx.OkJson(w, utils.ReturnHTTPSuccess(resp))
		}
	}
}
