123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587 |
- package models
- import (
- "bytes"
- "fmt"
- "github.com/jinzhu/gorm"
- "gopkg"
- "lzyd-user-api/common"
- "math/rand"
- "strconv"
- "time"
- )
- /**
- 修改用户信息请求参数
- */
- type UpdateProfileReq struct {
- UserId string `json:"user_id"` // 用户代码
- UserName string `json:"username"` // 会员昵称
- Birthday string `json:"birthday"` // 生日
- Height int `json:"height,omitempty"` // 用户身高
- Weight int `json:"weight,omitempty"` // 用户体重
- Gender int `json:"gender,omitempty"` // 性别
- Area string `json:"area"` // 地区
- UserAvatar string `json:"user_avatar"` // 地区
- }
- type UserRelationResp struct {
- AttentionUserId string `json:"attention_user_id" gorm:"column:attention_user_id"`
- }
- type TaskList struct {
- TaskId string `json:"task_id" gorm:"column:task_id"`
- }
- /**
- 修改用户头像请求参数
- */
- type UpdateAvatarReq struct {
- UserId string `json:"user_id"` // 用户代码
- UserAvatar string `json:"user_avatar"` // 头像
- }
- /**
- 上传身份证图片请求参数
- */
- type EditCardReq struct {
- UserCode string `json:"user_code"` // 用户代码
- CardId string `json:"card_id"` // 身份证号码
- CardFront string `json:"card_front"` // 身份证正面照
- CardBack string `json:"card_back"` // 身份证反面照
- }
- /**
- 修改用户密码请求参数
- */
- type UpdatePwdReq struct {
- UserCode string `json:"user_code"` // 用户代码
- OldPwd string `json:"old_pwd"` // 旧密码
- NewPwd string `json:"new_pwd"` // 新密码
- }
- /**
- 修改绑定手机号请求参数
- */
- type UpdateMobileReq struct {
- UserCode string `json:"user_code"` // 用户代码
- OldMobile string `json:"old_mobile"` // 旧的手机号码
- NewMobile string `json:"new_mobile"` // 需要绑定的手机号
- ValidCode string `json:"valid_code"` // 手机验证码
- }
- /**
- 验证手机号请求参数
- */
- type ValidMobileReq struct {
- Mobile string `json:"mobile"` // 手机号码
- ValidCode string `json:"valid_code"` // 手机验证码
- }
- type YuedongCode struct {
- YuedongId string `gorm:"yuedong_id:id;" json:"yuedong_id"` // 悦动id
- }
- /**
- 获取黑白名单请求参数
- */
- type GetBlackListReq struct {
- UserId string `json:"user_id"` // 用户ID
- Type int `json:"type"` // 类型
- }
- /**
- 绑定微信请求参数
- */
- type BindOpenIdReq struct {
- UserId string `json:"user_id"` // 用户代码
- OpenID string `json:"open_id"` // 微信号码
- BindType int `json:"bind_type"` // 绑定类型(10=微信,11=QQ,12=微博)
- }
- /**
- 获取黑名单列表
- */
- type BlackListReq struct {
- UserId string `json:"user_id"` // 用户代码
- UserAvatar string `json:"user_avatar"` // 用户头像
- Username string `json:"username"` // 用户昵称
- FansNum int `json:"fans_num"` // 用户粉丝数
- VideosNum int `json:"videos_num"` // 用户粉丝数
- }
- /**
- 获取黑名单列表
- */
- type VideoListReq struct {
- UserId string `json:"user_id"` // 用户代码
- }
- /**
- 获取黑名单信息
- */
- type BlackListResp struct {
- UserId string `json:"user_code"` // 用户代码
- UserAvatar string `json:"user_avatar"` // 用户头像
- UserName string `json:"user_name"` // 用户昵称
- }
- /**
- 客户信息
- */
- type UserResp struct {
- Id int64 `gorm:"column:id;primary_key;auto_increment" json:"id"` //自增ID
- UserId string `gorm:"column:user_id" json:"user_id"` //用户ID
- UserName string `gorm:"column:username" json:"username"` //昵称
- Password string `gorm:"column:password" json:"password"`//密码
- RealName string `gorm:"column:real_name" json:"real_name"`//真实姓名
- UserAvatar string `gorm:"column:user_avatar" json:"user_avatar"`//头像
- MobilePhone string `gorm:"column:mobile_phone" json:"mobile_phone"`//手机号
- Gender int `gorm:"column:gender" json:"gender"`//性别
- Height int `gorm:"column:hieght" json:"height"`//身高
- Weight int `gorm:"column:weight" json:"weight"`//体重
- Birthday string `gorm:"column:birthday" json:"birthday"`//生日
- Address string `gorm:"column:address" json:"address"`//详细地址
- Area string `gorm:"column:area" json:"area"`//地区
- QrCode string `gorm:"column:qr_code" json:"qr_code"` //二维码
- UserGrade string `gorm:"column:user_grade" json:"user_grade"`//排位赛等级
- LoginTime string `gorm:"column:login_time" json:"login_time"`//登录时间
- RegisterTime string `gorm:"column:register_time" json:"register_time"`//注册时间
- LikeNum int64 `gorm:"column:like_num" json:"like_num"`//点赞数
- FansNum int64 `gorm:"column:fans_num" json:"fans_num"`//粉丝数
- AttenNum int64 `gorm:"column:atten_num" json:"atten_num"`//关注数
- TotalDistance int `gorm:"column:total_distance" json:"total_distance"`//总公里数
- TimesMarathon int `gorm:"column:times_marathon" json:"times_marathon"`//马拉松场次
- TimesQualifier int `gorm:"column:times_qualifier" json:"times_qualifier"`//排位赛场次
- CreateTime string `gorm:"column:create_time" json:"create_time"`//创建时间
- UpdateTime string `gorm:"column:update_time" json:"update_time"`//更新时间
- IsDelete int64 `gorm:"column:is_delete" json:"is_delete"`//是否删除
- Status int64 `gorm:"column:status" json:"status"`//账号状态
- UserPoint int64 `gorm:"column:user_point" json:"user_point"`//积分
- Role int64 `gorm:"column:role" json:"role"`//账号类型
- UserMoney int64 `gorm:"column:user_money" json:"user_money"`//账号余额
- LoginType int64 `gorm:"column:login_type" json:"login_type"`//登录方式
- AppId string `gorm:"column:app_id" json:"app_id"`//appid
- }
- type UserUpload struct {
- UserName string `gorm:"column:username" json:"username"` //昵称
- UserAvatar string `gorm:"column:user_avatar" json:"user_avatar"`//头像
- Gender int `gorm:"column:gender" json:"gender"`//性别
- Height int `gorm:"column:hieght" json:"height"`//身高
- Weight int `gorm:"column:weight" json:"weight"`//体重
- Birthday string `gorm:"column:birthday" json:"birthday"`//生日
- Area string `gorm:"column:area" json:"area"`//地区
- }
- /// 用于插入user_info表中的数据
- type UserInfo struct {
- Id int64 `gorm:"column:id;primary_key;auto_increment" json:"id"` //自增ID
- UserId string `gorm:"column:user_id" json:"user_id"` //用户ID
- YuedongId string `gorm:"column:yuedong_id" json:"yuedong_id"` //用户ID
- UserName string `gorm:"column:username" json:"username"` //昵称
- Password string `gorm:"column:password" json:"password"`//密码
- RealName string `gorm:"column:real_name" json:"real_name"`//真实姓名
- UserAvatar string `gorm:"column:user_avatar" json:"user_avatar"`//头像
- MobilePhone string `gorm:"column:mobile_phone" json:"mobile_phone"`//手机号
- Gender int `gorm:"column:gender" json:"gender"`//性别
- Height int `gorm:"column:height" json:"height"`//身高
- Weight int `gorm:"column:weight" json:"weight"`//体重
- Birthday string `gorm:"column:birthday" json:"birthday"`//生日
- Address string `gorm:"column:address" json:"address"`//详细地址
- Area string `gorm:"column:area" json:"area"`//地区
- QrCode string `gorm:"column:qr_code" json:"qr_code"` //二维码
- UserGrade string `gorm:"column:user_grade" json:"user_grade"`//排位赛等级
- LoginTime string `gorm:"column:login_time" json:"login_time"`//登录时间
- RegisterTime string `gorm:"column:register_time" json:"register_time"`//注册时间
- LikeNum int64 `gorm:"column:like_num" json:"like_num"`//点赞数
- FansNum int64 `gorm:"column:fans_num" json:"fans_num"`//粉丝数
- AttenNum int64 `gorm:"column:atten_num" json:"atten_num"`//关注数
- TotalDistance int `gorm:"column:total_distance" json:"total_distance"`//总公里数
- TimesMarathon int `gorm:"column:times_marathon" json:"times_marathon"`//马拉松场次
- TimesQualifier int `gorm:"column:times_qualifier" json:"times_qualifier"`//排位赛场次
- CreateTime string `gorm:"column:create_time" json:"create_time"`//创建时间
- UpdateTime string `gorm:"column:update_time" json:"update_time"`//更新时间
- IsDelete int64 `gorm:"column:is_delete" json:"is_delete"`//是否删除
- Status int64 `gorm:"column:status" json:"status"`//账号状态
- UserPoint int64 `gorm:"column:user_point" json:"user_point"`//积分
- Role int64 `gorm:"column:role" json:"role"`//账号类型
- UserMoney int64 `gorm:"column:user_money" json:"user_money"`//账号余额
- LoginType int64 `gorm:"column:login_type" json:"login_type"`//登录方式
- AppId string `gorm:"column:app_id" json:"app_id"`//appid
- AttentionUserId string `gorm:"column:attention_user_id" json:"attention_user_id"`//attention_user_id
- }
- type TotalDistances struct {
- TotalDistance int `gorm:"column:course" json:"course"`//总公里数
- MarathonId string `gorm:"column:marathon_id" json:"marathon_id"`//总马拉松数
- QualifyingId string `gorm:"column:qualifying_id" json:"qualifying_id"`//总排位赛数
- }
- func (u UserInfo) ToResp() *UserResp {
- return &UserResp{
- Id: u.Id,
- UserId: u.UserId,
- UserName: u.UserName,
- RealName: u.RealName,
- UserAvatar: u.UserAvatar,
- MobilePhone: u.MobilePhone,
- Gender: u.Gender,
- Height: u.Height,
- Weight: u.Weight,
- Birthday: u.Birthday,
- Address: u.Address,
- Area: u.Area,
- QrCode: u.QrCode,
- UserGrade: u.UserGrade,
- LoginTime: u.LoginTime,
- RegisterTime: u.RegisterTime,
- LikeNum: u.LikeNum,
- FansNum: u.FansNum,
- AttenNum: u.AttenNum,
- TotalDistance: u.TotalDistance,
- TimesMarathon: u.TimesMarathon,
- TimesQualifier: u.TimesQualifier,
- CreateTime: u.CreateTime,
- UpdateTime: u.UpdateTime,
- IsDelete: u.IsDelete,
- Status: u.Status,
- UserPoint: u.UserPoint,
- Role: u.Role,
- UserMoney: u.UserMoney,
- LoginType: u.LoginType,
- AppId: u.AppId,
- }
- }
- /**
- 赛事订单列表
- */
- type Competition struct {
- MarathonId string `json:"marathon_id"` // 马拉松id
- //UserId string `json:"user_id"` // 用户id
- Name string `json:"name"` // 用户id
- RecvMan string `json:"recv_man"` // 收件人名称
- DealStatus int `json:"deal_status"` // 订单状态
- PostNumber string `json:"post_number"` // 快递单号
- PostCompany string `json:"post_company"` // 快递公司名称
- RecvPhone string `json:"recv_phone"` // 收件人手机号
- PayMoney float64 `json:"pay_money"` // 支付金额
- DealSn string `json:"deal_sn"` // 订单id
- List []CompetitionGoodsList `json:"list"`
- }
- /**
- 赛事订单商品
- */
- type CompetitionGoodsList struct {
- PkgName string `json:"pkg_name"` // 商品名称
- SpecId string `json:"spec_id"` // 商品名称
- Amout int `json:"amout"` // 数量
- Spec string `json:"spec"` // 规格
- Price float64 `json:"price"` // 商品价格
- Cover string `json:"cover"` // 商品图片
- }
- /**
- 排位赛列表
- */
- type QualifyingList struct {
- First int `json:"first"`
- TopThree int `json:"top_three"`
- Average int `json:"average"`
- TenAverage int `json:"ten_average"`
- List []QualifyingResp `json:"list"`
- }
- type QualifyingResp struct {
- QualifyingId string `json:"qualifying_id"` // 排位赛id
- UserId string `json:"user_id"` // 用户id
- Rank string `json:"rank"` // 赛事排名
- Name string `json:"name"` // 赛事名称
- UpdateAt string `json:"update_at"` // 赛事更新时间
- }
- type UploadBaseReq struct {
- Base64 string `json:"base64"` // base64字符串
- MapBase string `json:"map_base"` // mapBase64
- UserId string `json:"user_id"` // 用户id
- TaskId string `json:"task_id"` // 任务id
- }
- type UploadBaseErr struct {
- MapUrl string `json:"map_url"` // base64字符串
- MapBase string `json:"map_base"` // mapBase64
- UserId string `json:"user_id"` // 用户id
- TaskId string `json:"task_id"` // 任务id
- }
- type UploadBaseResp struct {
- Url string `json:"url"` // 返回的图片url
- }
- /**
- [Mysql]获取黑名单列表
- */
- func (u BlackListReq) BlackToResp() interface{} {
- return &BlackListResp{
- UserId: u.UserId,
- UserName: u.Username,
- UserAvatar: u.UserAvatar,
- }
- }
- /**
- [Mysql]修改用户密码
- */
- func (u *UserInfo) UpdatePwd(db *gorm.DB) error {
- sql := "UPDATE lzyd_user SET password = ? WHERE user_code = ? AND user_status IN (1, 4)"
- err := db.Exec(sql, u.Password, u.UserId).Error
- return err
- }
- /**
- [Mysql]修改用户头像
- */
- func (u *UserInfo) UpdateAvatar(db *gorm.DB) error {
- // 修改用户头像
- sql := "UPDATE lzyd_user SET avatar = ? WHERE user_code = ? AND user_status IN (1, 4)"
- err := db.Exec(sql, u.UserAvatar, u.UserId).Error
- if err != nil {
- common.LogInfo(fmt.Sprintf("Update lzyd_user error : %s", err.Error()))
- return err
- }
- return nil
- }
- /**
- 修改用户身份证正反面照片
- */
- //func (u *UserInfo) UpdateCard(db *gorm.DB) error {
- // sql := "UPDATE lzyd_user SET card_id = ?, card_front = ?, card_backend = ? WHERE user_code = ? AND user_status IN (1, 4)"
- // return db.Exec(sql, u.CardId, u.CardFront, u.CardBack, u.UserCode).Error
- //}
- /**
- [Mysql]修改用户资料
- */
- func (u *UserInfo) UpdateProfile(db *gorm.DB) error {
- // 修改用户信息
- var model UserUpload
- sqlUser := "select username,gender,birthday,user_avatar,area,height,weight from lzyd_user where user_id = ?"
- common.DB.Raw(sqlUser, u.UserId).First(&model)
- //fmt.Println("你输入的是:", u)
- //fmt.Println("你输入的是:", model)
- var sqlBuf bytes.Buffer
- sqlBuf.WriteString("UPDATE lzyd_user SET ")
- if len(u.UserName) > 0 {
- sqlBuf.WriteString("username='" + u.UserName + "',")
- }else {
- sqlBuf.WriteString("username='" + model.UserName + "',")
- }
- if u.Gender > 0 {
- sqlBuf.WriteString("gender=" + strconv.Itoa(u.Gender) + ",")
- }else {
- sqlBuf.WriteString("gender=" + strconv.Itoa(model.Gender) + ",")
- }
- if len(u.Birthday) > 0 {
- sqlBuf.WriteString("birthday='" + u.Birthday + "',")
- }else {
- sqlBuf.WriteString("birthday='" + model.Birthday + "',")
- }
- if len(u.UserAvatar) > 0 {
- sqlBuf.WriteString("user_avatar='" + u.UserAvatar + "',")
- }else {
- sqlBuf.WriteString("user_avatar='" + model.UserAvatar + "',")
- }
- if len(u.Area) > 0 {
- sqlBuf.WriteString("area='" + u.Area + "',")
- }else {
- sqlBuf.WriteString("area='" + model.Area + "',")
- }
- if u.Height > 0 {
- sqlBuf.WriteString("height=" + strconv.Itoa(u.Height) + ",")
- }else {
- sqlBuf.WriteString("height=" + strconv.Itoa(model.Height) + ",")
- }
- if u.Weight > 0 {
- sqlBuf.WriteString("weight=" + strconv.Itoa(u.Weight))
- }else {
- sqlBuf.WriteString("weight=" + strconv.Itoa(model.Weight))
- }
- sqlBuf.WriteString(" WHERE user_id = ?")
- err := db.Exec(sqlBuf.String(), u.UserId).Error
- //fmt.Println("你输入的是:", err,u.UserId,sqlBuf.String())
- if err != nil {
- common.LogInfo(fmt.Sprintf("Update lzyd_user error : %s", err.Error()))
- return err
- }
- return nil
- }
- func CreateCaptcha() string {
- return "1" + fmt.Sprintf("%08v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(100000000))
- }
- func YueDongId(randomNum string) bool {
- var yuedongCode YuedongCode
- yuedongId:= randomNum
- yuedongIdSql := "SELECT yuedong_id from lzyd_user WHERE yuedong_id = ?"
- common.DB.Raw(yuedongIdSql,yuedongId).Find(&yuedongCode)
- //common.db.Raw(yuedongIdSql,yuedongId).Find(&yuedongCode)
- //fmt.Println(111,randomNum,len(yuedongCode.YuedongId))
- if len(yuedongCode.YuedongId) == 0 {
- return true
- }else {
- return false
- }
- }
- /**
- [Mysql]修改已实名认证
- */
- func (u *UserInfo) UpdateIsValid(db *gorm.DB) error {
- sql := "UPDATE lzyd_user SET realname_valid = 1 WHERE user_code = ? AND user_status IN (1, 4)"
- err := db.Exec(sql, u.UserId).Error
- if err != nil {
- common.LogInfo(fmt.Sprintf("Update user_status error : %s", err.Error()))
- return err
- }
- return nil
- }
- /**
- [Mysql]绑定手机号
- */
- func (u *UserInfo) BindMobile(db *gorm.DB) error {
- sql := "UPDATE lzyd_user SET mobile_phone = ? WHERE user_id = ? AND status IN (1, 4)"
- return db.Exec(sql, u.MobilePhone, u.UserId).Error
- }
- /**
- [Mysql]删除指定账号
- */
- func (u *UserInfo) Rem(db *gorm.DB) error {
- sql := "UPDATE lzyd_user SET status = 3 WHERE user_id = ?"
- return db.Exec(sql, u.UserId).Error
- }
- /**
- [Mysql]注册用户信息
- */
- func (cus *UserInfo) Register(db *gorm.DB) error {
- fmt.Println(cus)
- now := time.Now()
- var (
- insertLoginSql string
- insertInfoSql string
- insertLoginErr error
- insertInfoErr error
- )
- yuedongId := CreateCaptcha()
- yueDongerr := YueDongId(yuedongId)
- //fmt.Println(111,yueDongerr)
- if yueDongerr == false {
- yuedongId = CreateCaptcha()
- yueDongerr = YueDongId(CreateCaptcha())
- }
- // 使用事务提交数据, 方便数据回滚
- tx := db.Begin()
- // 插入数据信息
- insertInfoSql = "INSERT INTO lzyd_user(user_id, username, user_avatar, mobile_phone, gender, birthday, register_time, login_time, app_id,yuedong_id) VALUES (?,?,?,?,?,?,?,?,?,?)"
- insertInfoErr = db.Exec(insertInfoSql, cus.UserId, cus.UserName, cus.UserAvatar, cus.MobilePhone, cus.Gender, cus.Birthday, now, now, cus.AppId,yuedongId).Error
- // 插入登录信息
- insertLoginSql = "INSERT INTO lzyd_user_login(user_id, mobile_phone, login_type, app_id, status) VALUES(?,?,?,?,?)"
- insertLoginErr = db.Exec(insertLoginSql, cus.UserId, cus.MobilePhone, cus.LoginType, cus.AppId, 1).Error
- //fmt.Println()
- if insertLoginErr != nil || insertInfoErr != nil {
- tx.Rollback()
- //return errors.New(common.ErrorInfo[common.DbInsertErr])
- }
- tx.Commit()
- return nil
- }
- /**
- [Mysql]登录用户信息
- */
- func (cus *UserInfo) Login(db *gorm.DB) error {
- //now := time.Now()
- var (
- insertLoginSql string
- insertLoginErr error
- insertInfoErr error
- )
- // 使用事务提交数据, 方便数据回滚
- tx := db.Begin()
- // 插入登录信息
- insertLoginSql = "INSERT INTO lzyd_user_login(user_id, mobile_phone, login_type, app_id, status) VALUES(?,?,?,?,?)"
- insertLoginErr = db.Exec(insertLoginSql, cus.UserId, cus.MobilePhone, cus.LoginType, cus.AppId, 1).Error
- if insertLoginErr != nil || insertInfoErr != nil {
- tx.Rollback()
- //return errors.New(common.ErrorInfo[common.DbInsertErr])
- }
- tx.Commit()
- return nil
- }
- /**
- [Mysql]运动图片存入数据库
- */
- func (cus *UploadBaseErr) InsetMaps(db *gorm.DB,taskId string) error {
- fmt.Println(cus)
- now := time.Now()
- var (
- insertInfoSql string
- insertInfoErr error
- task_id TaskList
- )
- yuedongIdSql := "SELECT task_id from lzyd_user_maps WHERE task_id = ?"
- common.DB.Raw(yuedongIdSql,taskId).First(&task_id)
- fmt.Println(1111,task_id)
- tx := db.Begin()
- if len(task_id.TaskId) > 0 {
- insertInfoSql = "UPDATE lzyd_user_maps SET map_url = ?,map_line_url =? WHERE task_id = ?"
- insertInfoErr = db.Exec(insertInfoSql,cus.MapUrl,cus.MapBase,taskId ).Error
- }else {
- mapId := gopkg.GenUUID()
- // 使用事务提交数据, 方便数据回滚
- insertInfoSql = "INSERT INTO lzyd_user_maps(user_id, task_id, map_id,map_url,map_line_url, create_at, app_id) VALUES (?,?,?,?,?,?,?)"
- insertInfoErr = db.Exec(insertInfoSql, cus.UserId, cus.TaskId, mapId, cus.MapUrl,cus.MapBase,now, "lzyd").Error
- }
- if insertInfoErr != nil {
- tx.Rollback()
- //return errors.New(common.ErrorInfo[common.DbInsertErr])
- }
- return nil
- }
|