user.go 13 KB
Newer Older
haoyanbin's avatar
1  
haoyanbin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
package mobile

import (
	"fmt"
	"gin-vue-admin/global"
	"gin-vue-admin/middleware"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"gin-vue-admin/model/response"
	"gin-vue-admin/service"
	"gin-vue-admin/utils"
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strings"
	"time"
)

// @Tags mobile
// @Summary 用户登录
// @Produce  application/json
// @Param data body request.MobileLogin true "用户名, 密码, 验证码"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"登陆成功"}"
// @Router /mobile/login [post]
func Login(c *gin.Context) {
	var r request.MobileLogin
	_ = c.ShouldBindJSON(&r)
	var err error

	fmt.Println(r, "userType:", r.UserType)
	if r.UserType == 0 {
		r.UserType = 1
	}

	hospitalCode := ""
	doctorPhone := ""
	otherNo := ""
	qrType := ""

	qrData := response.QrData{}

	if r.Scene != "" {
		awardData := strings.Split(r.Scene, ";")
		if len(awardData) > 3 {
			hospitalCode = awardData[0]
			doctorPhone = awardData[1]
			otherNo = awardData[2]
			qrType = awardData[3]
		}
	}

	err, wxReply := utils.WxUserLogin(r)
	if err != nil {
		response.FailWithDetailedCode(8, gin.H{"reload": true}, "code已过期", c)
		return
	}
	user := model.SysUser{Mobile: wxReply.Mobile, Openid: wxReply.OpenID, HospitalCode: hospitalCode, UserType: r.UserType}
	//user := model.SysUser{Mobile: "15330073239", Openid: "", HospitalCode: hospitalCode}

	if user.Mobile == "" {
		global.GVA_LOG.Error("获取数据失败!", zap.Any("err", err))
		response.FailWithMessage("获取数据失败", c)
		return
	}

	if qrType != "" {
		user.QrType = qrType
	}

	isReg := service.IsRegister(user)
	userData := model.SysUser{}
	if isReg == 1 {
		if hospitalCode != "" {
			isBind := service.IsBindCode(user)
			if isBind != 1 {
				service.UpdateUserHospitalCode(user.Mobile, hospitalCode)
			}
		}

		userData = service.FindUserByMobile(user.Mobile)

		if userData.Openid == "" {
			service.UpdateUserOpenid(user, int(userData.ID))
		}
	} else {
		_, userData = service.UserRegister(user)
	}

	//医生手机号与登录手机号相同 且 唯一值字段不为空
	if qrType == "1" && user.Mobile == doctorPhone && otherNo != "" {
		fmt.Println(otherNo)
		if service.IsPointsLog(otherNo) == 0 {

			cTime := time.Now()
			//积分流水
			reqPointsLog := model.PointsLog{}
			reqPointsLog.HospitalCode = hospitalCode
			reqPointsLog.UserId = int(userData.ID)
			reqPointsLog.ChangeType = 2
			reqPointsLog.ChangeTime = cTime
			reqPointsLog.ChangeNum = 100
			reqPointsLog.NowPointsNum = userData.PointsNum + reqPointsLog.ChangeNum
			reqPointsLog.Remark = "绑定领取金像奖积分"
			reqPointsLog.OtherNo = otherNo
			reqPointsLog.Source = 1
			reqPointsLog.CreateTime = cTime

			err = service.CreatePointsLog(reqPointsLog)
			if err != nil {
				fmt.Println("CreatePointsLog ERR:", err)
			}

			//修改积分
			err = service.UpdateUserPoints(int(userData.ID), 1, reqPointsLog.ChangeNum)
			if err != nil {
				fmt.Println("UpdateUserPoints ERR:", err)
			}
		}
	}


	j := &middleware.JWT{SigningKey: []byte(global.GVA_CONFIG.JWT.SigningKey)} // 唯一签名
	claims := request.CustomClaims{
		UUID:        userData.UUID,
		ID:          userData.ID,
		AuthorityId: userData.AuthorityId,
		Mobile:      userData.Mobile,
		Openid:      userData.Openid,
		BufferTime:  global.GVA_CONFIG.JWT.BufferTime, // 缓冲时间1天 缓冲时间内会获得新的token刷新令牌 此时一个用户会存在两个有效令牌 但是前端只留一个 另一个会丢失
		StandardClaims: jwt.StandardClaims{
			NotBefore: time.Now().Unix() - 1000,                              // 签名生效时间
			ExpiresAt: time.Now().Unix() + global.GVA_CONFIG.JWT.ExpiresTime, // 过期时间 7天  配置文件
			Issuer:    "qmPlus",                                              // 签名的发行者
		},
	}
	token, err := j.CreateToken(claims)
	if err != nil {
		global.GVA_LOG.Error("获取token失败!", zap.Any("err", err))
		response.FailWithMessage("获取token失败", c)
		return
	}

	response.OkWithDetailed(response.SysUserResponse{User: userData, Token: token, QrData: qrData}, "成功", c)
}

func Register(c *gin.Context) {
	var r request.MobileRegisterReq
	_ = c.ShouldBindJSON(&r)
	var err error

	hospitalCode := r.HospitalCode

	user := model.SysUser{Mobile: r.Mobile, Openid: "", HospitalCode: hospitalCode, RegType: r.RegType}

	isReg := service.IsRegister(user)
	userData := model.SysUser{}
	if isReg == 1 {
		if hospitalCode != "" {
			isBind := service.IsBindCode(user)
			if isBind != 1 {
				service.UpdateUserHospitalCode(user.Mobile, hospitalCode)
			}
		}

		userData = service.FindUserByMobile(user.Mobile)
	} else {
		_, userData = service.UserRegister(user)
	}

	j := &middleware.JWT{SigningKey: []byte(global.GVA_CONFIG.JWT.SigningKey)} // 唯一签名
	claims := request.CustomClaims{
		UUID:        userData.UUID,
		ID:          userData.ID,
		AuthorityId: userData.AuthorityId,
		Mobile:      userData.Mobile,
		Openid:      userData.Openid,
		BufferTime:  global.GVA_CONFIG.JWT.BufferTime, // 缓冲时间1天 缓冲时间内会获得新的token刷新令牌 此时一个用户会存在两个有效令牌 但是前端只留一个 另一个会丢失
		StandardClaims: jwt.StandardClaims{
			NotBefore: time.Now().Unix() - 1000,                              // 签名生效时间
			ExpiresAt: time.Now().Unix() + global.GVA_CONFIG.JWT.ExpiresTime, // 过期时间 7天  配置文件
			Issuer:    "qmPlus",                                              // 签名的发行者
		},
	}
	token, err := j.CreateToken(claims)
	if err != nil {
		global.GVA_LOG.Error("获取token失败!", zap.Any("err", err))
		response.FailWithMessage("获取token失败", c)
		return
	}

	response.OkWithDetailed(response.SysUserResponse{User: userData, Token: token, QrData: response.QrData{}}, "成功", c)
}

func SetUserBind(c *gin.Context) {
	var r request.MobileLogin
	_ = c.ShouldBindJSON(&r)
	var err error

	userId := int(getUserID(c))

	err, userInfo := service.FindUserById(userId)

	hospitalCode := ""
	doctorPhone := ""
	otherNo := ""

	if r.Scene != "" {
		awardData := strings.Split(r.Scene, ";")
		hospitalCode = awardData[0]
		doctorPhone = awardData[1]
		otherNo = awardData[2]
	} else {
		global.GVA_LOG.Error("获取数据失败!", zap.Any("err", err))
		response.FailWithMessage("获取数据失败", c)
		return
	}

	user := model.SysUser{Mobile: userInfo.Mobile, HospitalCode: hospitalCode}

	if user.Mobile == "" {
		global.GVA_LOG.Error("获取数据失败!", zap.Any("err", err))
		response.FailWithMessage("获取数据失败", c)
		return
	}
	isReg := service.IsRegister(user)
	userData := model.SysUser{}
	if isReg == 1 {
		isBind := service.IsBindCode(user)
		if isBind != 1 {
			service.UpdateUserHospitalCode(user.Mobile, user.HospitalCode)
		}
		userData = service.FindUserByMobile(user.Mobile)
	} else {
		_, userData = service.UserRegister(user)
	}

	//医生手机号与登录手机号相同 且 唯一值字段不为空
	if user.Mobile == doctorPhone && otherNo != "" {
		fmt.Println(otherNo)
		if service.IsPointsLog(otherNo) == 0 {

			cTime := time.Now()
			//积分流水
			reqPointsLog := model.PointsLog{}
			reqPointsLog.HospitalCode = user.HospitalCode
			reqPointsLog.UserId = int(userData.ID)
			reqPointsLog.ChangeType = 2
			reqPointsLog.ChangeTime = cTime
			reqPointsLog.ChangeNum = 100
			reqPointsLog.NowPointsNum = userData.PointsNum + reqPointsLog.ChangeNum
			reqPointsLog.Remark = "绑定领取金像奖积分"
			reqPointsLog.OtherNo = otherNo
			reqPointsLog.Source = 1
			reqPointsLog.CreateTime = cTime

			err = service.CreatePointsLog(reqPointsLog)
			if err != nil {
				fmt.Println("CreatePointsLog", err)
			}

			//修改积分
			err = service.UpdateUserPoints(int(userData.ID), 1, reqPointsLog.ChangeNum)
			if err != nil {
				fmt.Println("UpdateUserPoints", err)
			}
		}
	}

	response.OkWithMessage("修改成功", c)
	return
}

func GetOpenid(c *gin.Context) {
	var r request.MobileLogin
	_ = c.ShouldBindJSON(&r)

	if r.UserType == 0 {
		r.UserType = 1
	}

	err, wxReply := utils.GetOpenid(r)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
		return
	}

	response.OkWithData(gin.H{"openid": wxReply}, c)
}

// 从Gin的Context中获取从jwt解析出来的用户ID
func getUserID(c *gin.Context) uint {
	if claims, exists := c.Get("claims"); !exists {
		//global.GVA_LOG.Error("从Gin的Context中获取从jwt解析出来的用户ID失败, 请检查路由是否使用jwt中间件!")
		return 1
	} else {
		waitUse := claims.(*request.CustomClaims)
		return waitUse.ID
	}
}

// @Tags mobile
// @Summary 用户信息修改
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysUserAuthe true "用户信息修改"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /mobile/updateUser [post]
func UpdateUser(c *gin.Context) {
	var req request.UpdateUserReq
	_ = c.ShouldBindJSON(&req)

	userId := int(getUserID(c))

	user := model.SysUser{HeaderImg: req.HeaderImg, NickName: req.NickName}
	err := service.UpdateUser(user, userId)

	if err != nil {
		fmt.Println(err)
		response.FailWithMessage("修改失败!", c)
		return
	}

	response.OkWithMessage("修改成功", c)
	return
}

// @Tags mobile
// @Summary 用户认证详情
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /mobile/userAutheInfo [post]
func UserAutheInfo(c *gin.Context) {
	userId := getUserID(c)
	var reply request.GetUserInfoReply
	var err error

	if service.IsAuthe(int(userId)) == 1 {
		err, reply = service.UserAutheInfo(userId)
		if err != nil {
			global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
			response.FailWithMessage("获取失败", c)
			return
		}
	}

	//积分信息
	err, userData := service.GetUserInfo(userId)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
		return
	}

	reply.UserId = int(userId)
	reply.PointsNum = userData.PointsNum

	//谛宝多多认证信息
	//dbddAuthUrl := ""
	//dbddAuthReq := ""
	//dbddAuthData := utils.Post(dbddAuthUrl, dbddAuthReq, "")
	//
	//dbddAuthReply := new(interface{})
	//utils.UnserislizeJson(dbddAuthData, dbddAuthReply)
	//
	//reply.DbddAuth = dbddAuthReply

	//im专家信息
	imUrl := global.GVA_CONFIG.Im.Host + "/expert/getUserInfoByMobile"
	imExpertReq := request.ImExpertReq{
		Phone: userData.Mobile,
	}
	imExpertData := utils.Post(imUrl, imExpertReq, "", 1)

	if imExpertData != "" {
		imExpertReply := new(request.ImExpertReply)
		utils.UnserislizeJson(imExpertData, imExpertReply)

		reply.ImExpert = imExpertReply.Data
	}


	response.OkWithData(gin.H{"reply": reply}, c)
	return
}

// @Tags mobile
// @Summary 身份认证
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysUserAuthe true "身份认证"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /mobile/createUserAuthe [post]
func CreateUserAuthe(c *gin.Context) {
	var req model.SysUserAuthe
	_ = c.ShouldBindJSON(&req)

	req.UserId = int(getUserID(c))

	user := model.SysUserAuthe{UserId: req.UserId}
	isYes := service.IsUserAuthe(user)

	if isYes == 1 {
		service.DeleteUserAuthe(req.UserId)
	}

	err := service.CreateUserAuthe(req)
	if err != nil {
		response.FailWithMessage("创建失败!", c)
		return
	}


	response.OkWithMessage("创建成功", c)
	return
}

// @Tags mobile
// @Summary 省市区获取
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"成功"}"
// @Router /mobile/getRegoinList [post]
func GetRegoinList(c *gin.Context) {
	if err, regionList := service.GetRegion(); err != nil {
		global.GVA_LOG.Error("设置失败!", zap.Any("err", err))
		response.FailWithMessage("设置失败", c)
	} else {
		reply := utils.GetMenu(regionList, 0)
		response.OkWithDetailed(gin.H{"region": reply}, "设置成功", c)
	}
}

func UserVipInfo(c *gin.Context) {
	userId := getUserID(c)
	var reply model.VipUser
	var err error

	if service.IsVipUser(int(userId)) == 1 {
		err, reply = service.GetVipUser(int(userId))
		if err != nil {
			fmt.Println(err)
		}
	}

	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
		return
	}

	//用户操作日志
	//userReportLog := model.UserReportLog{
	//	UserId:            getUserID(c),
	//	ReportMainOrginId: "",
	//	Api:               "/mobile/userInfo",
	//	Req:               "",
	//	Reply:             "",
	//}
	//service.CreateUserReportLog(userReportLog)

	response.OkWithData(gin.H{"reply": reply}, c)
	return
}

func GetUserName(c *gin.Context) {
	var req request.GetUserNameReq
	_ = c.ShouldBindJSON(&req)
	var err error

	//积分信息
	err, userData := service.GetUserName(req.Mobile)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
		return
	}

	response.OkWithData(userData, c)
	return
}