package user import ( "errors" "strings" "zhiyuan/models" "zhiyuan/pkg/app" "zhiyuan/pkg/db" "zhiyuan/pkg/utils" "zhiyuan/services/admin" "zhiyuan/services/form" "zhiyuan/services/log" "zhiyuan/services/sms" ) type EditInfoForm struct { Name string `form:"name" json:"name" label:"名称" binding:"required"` Avatar string `form:"avatar" json:"avatar" label:"头像" binding:"required"` } var User models.User var UserHouse models.UserHouse func Login(form form.UserLogin) (string, error) { userMap := make(map[string]interface{}) if form.LoginType == 1 { userMap = map[string]interface{}{ "openid": form.OpenID, "nickname": form.NickName, "sex": form.Sex, "headimgurl": form.HeadImgUrl, } if user, _ := GetOne(map[string]interface{}{"openid": form.OpenID}, nil, nil); user != nil { db.Update(User.TableName(), map[string]interface{}{"id": user.ID}, userMap) return UpdateToken(user.ID) } } if form.Phone == "" || form.Code == "" { return "", errors.New("请输入正确的手机号和验证码") } if check := sms.CheckVerifyCode(form.Phone, form.Code); !check { return "", errors.New("验证码有误") } // check user user, err := GetOne(map[string]interface{}{"phone": form.Phone}, nil, nil) if err != nil { return "", errors.New("获取用户信息失败") } userID := 0 // add user if user == nil { //return "", errors.New("用户不存在,请联系客服") userMap["phone"] = form.Phone id, err := db.InsertOne(User.TableName(), userMap) userID = int(id) if err != nil { return "", err } } else { if form.LoginType == 1 { db.Update(User.TableName(), map[string]interface{}{"id": user.ID}, userMap) } userID = user.ID } return UpdateToken(userID) } func InsertOne(data map[string]interface{}) (int64, error) { return db.InsertOne(User.TableName(), data) } func Logout(id int) error { user, err := GetInfoByID(id, []string{"id"}, nil) if err != nil || user == nil { return errors.New("invalid access_token") } return ClearToken(user.ID) } func UnBindWeixin(id int) error { userMap := map[string]interface{}{ "openid": "", "nickname": "", "headimgurl": "", "sex": 0, } db.Update(User.TableName(), map[string]interface{}{"id": id}, userMap) return Logout(id) } func GetList(where map[string]interface{}, fields []string, page app.Page, retVal interface{}) ([]*models.User, error) { if page.PageNum > 0 && page.PageSize > 0 { where["_limit"] = db.GetOffset(uint(page.PageNum), uint(page.PageSize)) } return User.GetMulti(where, fields, retVal) } func CountRaw(where string, param map[string]interface{}) (int64, error) { query := "SELECT `zy_user`.* from `zy_user` left join `zy_user_house` on `zy_user_house`.`user_id` = `zy_user`.`id` AND `zy_user_house`.`deleted_at` = 0 left join `zy_mat_pick_order` on `zy_mat_pick_order`.`customer_phone` = `zy_user`.`phone` left join `zy_mat_bid_order` on `zy_mat_bid_order`.`customer_phone` = `zy_user`.`phone` AND `zy_mat_bid_order`.`deleted_at` = 0 left join `zy_as_order` on `zy_as_order`.`user_id` = `zy_user`.`id` AND `zy_as_order`.`deleted_at` = 0 WHERE " + where + " GROUP BY `zy_user`.`id`" return db.GetQueryCount(query, param, nil) } func GetUsersRaw(where map[string]string, param map[string]interface{}, retVal interface{}) error { field := "SELECT `zy_user`.*, count(distinct `zy_user_house`.`id`) as `house_count`, count(distinct if(!ISNULL(`zy_user_house`.`id`) AND `zy_user_house`.`warranty_start` != 0, `zy_user_house`.`id`, NULL)) as `warranty_house_count`, count(distinct if(!ISNULL(`zy_user_house`.`id`) AND `zy_user_house`.`warranty_start` != 0 AND date_add(from_unixtime(`zy_user_house`.`warranty_start`), interval `zy_user_house`.`warranty_period_material` year) >= now(), `zy_user_house`.`id`, NULL)) as `warranty_period_material_house_count`, count(distinct if(!ISNULL(`zy_user_house`.`id`) AND `zy_user_house`.`warranty_start` != 0 AND date_add(from_unixtime(`zy_user_house`.`warranty_start`), interval `zy_user_house`.`warranty_period_base` year) >= now(), `zy_user_house`.`id`, NULL)) as `warranty_period_base_house_count`, count(distinct if(!ISNULL(`zy_user_house`.`id`) AND `zy_user_house`.`warranty_start` != 0 AND date_add(from_unixtime(`zy_user_house`.`warranty_start`), interval `zy_user_house`.`warranty_period_electric` year) >= now(), `zy_user_house`.`id`, NULL)) as `warranty_period_electric_house_count`, count(distinct `zy_mat_pick_order`.`id`) as `pick_order_count`, count(distinct `zy_mat_bid_order`.`id`) as `bid_order_count`, count(distinct `zy_as_order`.`id`) as `as_order_count`, `integral`.`num` as `integral` from `zy_user` left join `zy_user_house` on `zy_user_house`.`user_id` = `zy_user`.`id` AND `zy_user_house`.`deleted_at` = 0 left join `zy_mat_pick_order` on `zy_mat_pick_order`.`customer_phone` = `zy_user`.`phone` left join `zy_mat_bid_order` on `zy_mat_bid_order`.`customer_phone` = `zy_user`.`phone` AND `zy_mat_bid_order`.`deleted_at` = 0 left join `zy_as_order` on `zy_as_order`.`user_id` = `zy_user`.`id` AND `zy_as_order`.`deleted_at` = 0 left join (select `user_id`, ifnull(sum(`num`),0) as `num` from `zy_user_integral` where `deleted_at` = 0 group by `user_id`) as `integral` on `integral`.`user_id` = `zy_user`.`id`" where["_group_by"] = "`zy_user`.`id`" return db.GetMultiRaw(field, where, param, retVal) } func Count(where map[string]interface{}) (int64, error) { return db.Count(User.TableName(), where) } func Add(form form.UserAdd) (int, error) { if CheckUserDuplicate(utils.ToStr(form.Phone)) { userInfo, err := GetOne(map[string]interface{}{"phone": form.Phone}, nil, nil) if err == nil { ids := strings.Split(userInfo.ShowIds, ",") in := false for _, v := range ids { id, _ := db.ToInt64(v) if id == int64(form.AdminID) { in = true break } } if !in { ids = append(ids, db.ToString(form.AdminID)) userMap := map[string]interface{}{ "show_ids": strings.Join(ids, ","), } db.Update(User.TableName(), map[string]interface{}{"id": userInfo.ID}, userMap) } } return 0, errors.New("用户手机号码已存在") } // 添加用户 userMap := map[string]interface{}{ "name": form.Name, "phone": form.Phone, "has_issue": form.HasIssue, "issue": form.Issue, "created_id": form.AdminID, "birthday": utils.DateParseUnix(form.Birthday, "Y-m-d"), } if adminInfo := admin.GetAdminCache(form.AdminID); adminInfo.SiteID == 2 { userMap["site_id"] = adminInfo.SiteID } userID, err := db.InsertOne(User.TableName(), userMap) if err != nil { return 0, nil } return utils.ToInt(userID), nil } func EditByID(userForm form.UserAdd, id int) error { userInfo, err := GetInfoByID(id, nil, nil) if userInfo == nil { return errors.New("invalid auth id") } if userInfo.Phone != userForm.Phone && CheckUserDuplicate(userForm.Phone) { return errors.New("用户手机号码已存在") } // 修改用户 userMap := map[string]interface{}{ "name": userForm.Name, "phone": userForm.Phone, "has_issue": userForm.HasIssue, "issue": userForm.Issue, "birthday": utils.DateParseUnix(userForm.Birthday, "Y-m-d"), } _, err = db.Update(User.TableName(), map[string]interface{}{"id": id}, userMap) log.Add(form.LogAdd{ Type: log.TypeMap.EditUser.ID, Title: "编辑用户信息_" + utils.ToStr(id), AdminID: userForm.AdminID, Content: map[string]interface{}{ "before": userInfo, "after": userMap, }, }) return err } func EditInfoByID(form EditInfoForm, id int) error { userMap := map[string]interface{}{ "name": form.Name, "avatar": form.Avatar, } _, err := db.Update(User.TableName(), map[string]interface{}{"id": id}, userMap) return err } func CheckUserDuplicate(phone string) bool { userInfo, err := GetOne(map[string]interface{}{"phone": phone}, nil, nil) return userInfo != nil && err == nil } func DeleteByID(id int) error { userInfo, _ := GetInfoByID(id, nil, nil) if userInfo == nil { return errors.New("invalid user id") } _, err := db.Delete(User.TableName(), map[string]interface{}{"id": id}) return err } func GetOne(where map[string]interface{}, fields []string, retVal interface{}) (*models.User, error) { return User.GetOne(where, fields, retVal) } func GetInfoByID(id int, fields []string, retVal interface{}) (*models.User, error) { return GetOne(map[string]interface{}{"id": id}, fields, retVal) } func GetInfoByToken(token string, fields []string, retVal interface{}) (*models.User, error) { return GetOne(map[string]interface{}{"access_token": token}, fields, retVal) } func GetIDByToken(token string) (int, error) { ret, err := GetInfoByToken(token, nil, nil) if err != nil { return 0, err } if ret == nil { return 0, errors.New("无法获取用户信息") } return utils.ToInt(ret.ID), nil } func CreateNewUser(name, phone string) int { if userInfo, _ := GetOne(map[string]interface{}{"phone": phone}, nil, nil); userInfo == nil { userID, _ := InsertOne(map[string]interface{}{ "name": name, "phone": phone, }) return utils.ToInt(userID) } else { return userInfo.ID } }