user.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package user
  2. import (
  3. "errors"
  4. "strings"
  5. "zhiyuan/models"
  6. "zhiyuan/pkg/app"
  7. "zhiyuan/pkg/db"
  8. "zhiyuan/pkg/utils"
  9. "zhiyuan/services/admin"
  10. "zhiyuan/services/form"
  11. "zhiyuan/services/log"
  12. "zhiyuan/services/sms"
  13. )
  14. type EditInfoForm struct {
  15. Name string `form:"name" json:"name" label:"名称" binding:"required"`
  16. Avatar string `form:"avatar" json:"avatar" label:"头像" binding:"required"`
  17. }
  18. var User models.User
  19. var UserHouse models.UserHouse
  20. func Login(form form.UserLogin) (string, error) {
  21. userMap := make(map[string]interface{})
  22. if form.LoginType == 1 {
  23. userMap = map[string]interface{}{
  24. "openid": form.OpenID,
  25. "nickname": form.NickName,
  26. "sex": form.Sex,
  27. "headimgurl": form.HeadImgUrl,
  28. }
  29. if user, _ := GetOne(map[string]interface{}{"openid": form.OpenID}, nil, nil); user != nil {
  30. db.Update(User.TableName(), map[string]interface{}{"id": user.ID}, userMap)
  31. return UpdateToken(user.ID)
  32. }
  33. }
  34. if form.Phone == "" || form.Code == "" {
  35. return "", errors.New("请输入正确的手机号和验证码")
  36. }
  37. if check := sms.CheckVerifyCode(form.Phone, form.Code); !check {
  38. return "", errors.New("验证码有误")
  39. }
  40. // check user
  41. user, err := GetOne(map[string]interface{}{"phone": form.Phone}, nil, nil)
  42. if err != nil {
  43. return "", errors.New("获取用户信息失败")
  44. }
  45. userID := 0
  46. // add user
  47. if user == nil {
  48. //return "", errors.New("用户不存在,请联系客服")
  49. userMap["phone"] = form.Phone
  50. id, err := db.InsertOne(User.TableName(), userMap)
  51. userID = int(id)
  52. if err != nil {
  53. return "", err
  54. }
  55. } else {
  56. if form.LoginType == 1 {
  57. db.Update(User.TableName(), map[string]interface{}{"id": user.ID}, userMap)
  58. }
  59. userID = user.ID
  60. }
  61. return UpdateToken(userID)
  62. }
  63. func InsertOne(data map[string]interface{}) (int64, error) {
  64. return db.InsertOne(User.TableName(), data)
  65. }
  66. func Logout(id int) error {
  67. user, err := GetInfoByID(id, []string{"id"}, nil)
  68. if err != nil || user == nil {
  69. return errors.New("invalid access_token")
  70. }
  71. return ClearToken(user.ID)
  72. }
  73. func UnBindWeixin(id int) error {
  74. userMap := map[string]interface{}{
  75. "openid": "",
  76. "nickname": "",
  77. "headimgurl": "",
  78. "sex": 0,
  79. }
  80. db.Update(User.TableName(), map[string]interface{}{"id": id}, userMap)
  81. return Logout(id)
  82. }
  83. func GetList(where map[string]interface{}, fields []string, page app.Page, retVal interface{}) ([]*models.User, error) {
  84. if page.PageNum > 0 && page.PageSize > 0 {
  85. where["_limit"] = db.GetOffset(uint(page.PageNum), uint(page.PageSize))
  86. }
  87. return User.GetMulti(where, fields, retVal)
  88. }
  89. func CountRaw(where string, param map[string]interface{}) (int64, error) {
  90. 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`"
  91. return db.GetQueryCount(query, param, nil)
  92. }
  93. func GetUsersRaw(where map[string]string, param map[string]interface{}, retVal interface{}) error {
  94. 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`"
  95. where["_group_by"] = "`zy_user`.`id`"
  96. return db.GetMultiRaw(field, where, param, retVal)
  97. }
  98. func Count(where map[string]interface{}) (int64, error) {
  99. return db.Count(User.TableName(), where)
  100. }
  101. func Add(form form.UserAdd) (int, error) {
  102. if CheckUserDuplicate(utils.ToStr(form.Phone)) {
  103. userInfo, err := GetOne(map[string]interface{}{"phone": form.Phone}, nil, nil)
  104. if err == nil {
  105. ids := strings.Split(userInfo.ShowIds, ",")
  106. in := false
  107. for _, v := range ids {
  108. id, _ := db.ToInt64(v)
  109. if id == int64(form.AdminID) {
  110. in = true
  111. break
  112. }
  113. }
  114. if !in {
  115. ids = append(ids, db.ToString(form.AdminID))
  116. userMap := map[string]interface{}{
  117. "show_ids": strings.Join(ids, ","),
  118. }
  119. db.Update(User.TableName(), map[string]interface{}{"id": userInfo.ID}, userMap)
  120. }
  121. }
  122. return 0, errors.New("用户手机号码已存在")
  123. }
  124. // 添加用户
  125. userMap := map[string]interface{}{
  126. "name": form.Name,
  127. "phone": form.Phone,
  128. "has_issue": form.HasIssue,
  129. "issue": form.Issue,
  130. "created_id": form.AdminID,
  131. "birthday": utils.DateParseUnix(form.Birthday, "Y-m-d"),
  132. }
  133. if adminInfo := admin.GetAdminCache(form.AdminID); adminInfo.SiteID == 2 {
  134. userMap["site_id"] = adminInfo.SiteID
  135. }
  136. userID, err := db.InsertOne(User.TableName(), userMap)
  137. if err != nil {
  138. return 0, nil
  139. }
  140. return utils.ToInt(userID), nil
  141. }
  142. func EditByID(userForm form.UserAdd, id int) error {
  143. userInfo, err := GetInfoByID(id, nil, nil)
  144. if userInfo == nil {
  145. return errors.New("invalid auth id")
  146. }
  147. if userInfo.Phone != userForm.Phone && CheckUserDuplicate(userForm.Phone) {
  148. return errors.New("用户手机号码已存在")
  149. }
  150. // 修改用户
  151. userMap := map[string]interface{}{
  152. "name": userForm.Name,
  153. "phone": userForm.Phone,
  154. "has_issue": userForm.HasIssue,
  155. "issue": userForm.Issue,
  156. "birthday": utils.DateParseUnix(userForm.Birthday, "Y-m-d"),
  157. }
  158. _, err = db.Update(User.TableName(), map[string]interface{}{"id": id}, userMap)
  159. log.Add(form.LogAdd{
  160. Type: log.TypeMap.EditUser.ID,
  161. Title: "编辑用户信息_" + utils.ToStr(id),
  162. AdminID: userForm.AdminID,
  163. Content: map[string]interface{}{
  164. "before": userInfo,
  165. "after": userMap,
  166. },
  167. })
  168. return err
  169. }
  170. func EditInfoByID(form EditInfoForm, id int) error {
  171. userMap := map[string]interface{}{
  172. "name": form.Name,
  173. "avatar": form.Avatar,
  174. }
  175. _, err := db.Update(User.TableName(), map[string]interface{}{"id": id}, userMap)
  176. return err
  177. }
  178. func CheckUserDuplicate(phone string) bool {
  179. userInfo, err := GetOne(map[string]interface{}{"phone": phone}, nil, nil)
  180. return userInfo != nil && err == nil
  181. }
  182. func DeleteByID(id int) error {
  183. userInfo, _ := GetInfoByID(id, nil, nil)
  184. if userInfo == nil {
  185. return errors.New("invalid user id")
  186. }
  187. _, err := db.Delete(User.TableName(), map[string]interface{}{"id": id})
  188. return err
  189. }
  190. func GetOne(where map[string]interface{}, fields []string, retVal interface{}) (*models.User, error) {
  191. return User.GetOne(where, fields, retVal)
  192. }
  193. func GetInfoByID(id int, fields []string, retVal interface{}) (*models.User, error) {
  194. return GetOne(map[string]interface{}{"id": id}, fields, retVal)
  195. }
  196. func GetInfoByToken(token string, fields []string, retVal interface{}) (*models.User, error) {
  197. return GetOne(map[string]interface{}{"access_token": token}, fields, retVal)
  198. }
  199. func GetIDByToken(token string) (int, error) {
  200. ret, err := GetInfoByToken(token, nil, nil)
  201. if err != nil {
  202. return 0, err
  203. }
  204. if ret == nil {
  205. return 0, errors.New("无法获取用户信息")
  206. }
  207. return utils.ToInt(ret.ID), nil
  208. }
  209. func CreateNewUser(name, phone string) int {
  210. if userInfo, _ := GetOne(map[string]interface{}{"phone": phone}, nil, nil); userInfo == nil {
  211. userID, _ := InsertOne(map[string]interface{}{
  212. "name": name,
  213. "phone": phone,
  214. })
  215. return utils.ToInt(userID)
  216. } else {
  217. return userInfo.ID
  218. }
  219. }