| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 | package userMgrimport (	"encoding/json"	"errors"	"log"	"os"	"path/filepath"	"strconv"	"strings"	"time")const (	data = "data/user")func New(name, email, password, phone, company string) (*Body, error) {	if old, ok := is(phone); ok {		return old, errors.New("account_exists")	}	b := getDefault(name, email, password, phone, company)	body, err := json.Marshal(b)	if err != nil {		return nil, err	}	return &b, os.WriteFile(filepath.Join(data, phone+".json"), body, os.ModePerm)}func Get(phone, password string) (*Body, bool) {	if v, ok := is(phone); ok {		return v, v.Password == password	}	return nil, false}func ResetPwd(phone, password string) error {	b, ok := is(phone)	if !ok {		return errors.New("no_user")	}	b.Password = password	body, err := json.Marshal(b)	if err != nil {		return err	}	return os.WriteFile(filepath.Join(data, phone+".json"), body, os.ModePerm)}func Is(phone string) (*Body, bool) {	return is(phone)}func is(phone string) (*Body, bool) {	if _, err := os.Stat(data); err != nil {		panic(err)	}	user := getUsers()	for i := 0; i < len(user); i++ {		if trimName(user[i].Name()) == phone {			return readFile(user[i])		}	}	return nil, false}func getUsers() []os.DirEntry {	user, err := os.ReadDir(data)	if err != nil {		panic(err)	}	return user}func trimName(name string) string {	return strings.TrimSuffix(name, ".json")}func readFile(info os.DirEntry) (*Body, bool) {	body, err := os.ReadFile(filepath.Join(data, info.Name()))	if err != nil {		log.Printf("userMgr.readFile: ReadFile: %s\n", err)		return nil, false	}	var b Body	if err = json.Unmarshal(body, &b); err != nil {		log.Printf("userMgr.readFile: Unmarshal: %s\n", err)		return nil, false	}	return &b, true}func getDefault(name, email, password, phone, company string) Body {	t := time.Now().Unix()	return Body{		Id:                    strconv.Itoa(int(t)),		IPAddress:             "",		Username:              phone,		Password:              password,		Salt:                  "",		Email:                 email,		ActivationCode:        "",		ForgottenPasswordCode: "",		ForgottenPasswordTime: "",		RememberCode:          "",		CreatedOn:             strconv.Itoa(int(t)),		LastLogin:             strconv.Itoa(int(t)),		LoginCount:            "",		Active:                "1",		Name:                  name,		Company:               company,		Phone:                 phone,		Discount:              "0",		Newsletter:            "0",		Role:                  "0",		Master:                "0",		Projects:              "0",		Saves:                 "0",		TutorialPassed:        "0",		TutorialSkiped:        "0",		Downloads:             "0",		Contact:               "0",		Feedback:              "0",		Price:                 "0",		DownloadCAD:           "0",		Simulations:           "0",		SimulationsCompleted:  "0",	}}func init() {	if err := os.MkdirAll(data, os.ModePerm); err != nil {		panic(err)	}}
 |