feiyu02
2022-09-23 497475defd5d0ebf90ae6a8e2b080a16d78043ab
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
package cn.flightfeather.supervision.lightshare.service.Impl
 
import cn.flightfeather.supervision.common.net.WXHttpService
import cn.flightfeather.supervision.domain.entity.UserInfoWx
import cn.flightfeather.supervision.domain.entity.Userinfo
import cn.flightfeather.supervision.domain.entity.Version
import cn.flightfeather.supervision.domain.enumeration.UserType
import cn.flightfeather.supervision.domain.mapper.UserInfoWxMapper
import cn.flightfeather.supervision.domain.mapper.UserinfoMapper
import cn.flightfeather.supervision.domain.mapper.VersionMapper
import cn.flightfeather.supervision.infrastructure.utils.FileUtil
import cn.flightfeather.supervision.infrastructure.utils.UUIDGenerator
import cn.flightfeather.supervision.lightshare.service.VersionService
import cn.flightfeather.supervision.lightshare.service.WxUserService
import cn.flightfeather.supervision.lightshare.vo.AccessTokenPW
import cn.flightfeather.supervision.lightshare.vo.AccessTokenWX
import cn.flightfeather.supervision.lightshare.vo.BaseResponse
import cn.flightfeather.supervision.lightshare.vo.VersionVo
import com.alibaba.fastjson.JSONObject
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile
import tk.mybatis.mapper.entity.Example
 
@Service
class WxUserServiceImpl(val userinfoMapper: UserinfoMapper, val userInfoWxMapper: UserInfoWxMapper): WxUserService {
 
    override fun loginWx(accessTokenWX: AccessTokenWX): BaseResponse<Userinfo> {
        accessTokenWX.code ?: return BaseResponse(false, "登录凭证不能为空")
 
        val res = WXHttpService.code2Session(accessTokenWX.code!!)
 
//        return BaseResponse(false)
        if (res.success) {
            val json = JSONObject.parseObject(res.m.responseBodyAsString)
            if (json["errcode"] == 0 || json["errcode"] == null) {
                val openid = json["openid"] as String
                val unionid = json["unionid"] as String
                val user = userInfoWxMapper.selectByPrimaryKey(openid)
                return if (user.uiOpenId == null) {
                    val newUserWx = UserInfoWx().apply {
//                        uiGuid = UUIDGenerator.generate16ShortUUID()
                        uiOpenId = openid
                        uiNickName = accessTokenWX.nickName
                        uiGender
                        uiCountry
                        uiProvince
                        uiCity
                        uiAvatarUrl = accessTokenWX.avatarUrl
                        uiUnionid = unionid
                    }
//                    val newUser = Userinfo().apply {
//                        guid = newUserWx.uiGuid
//                        headIconUrl = newUserWx.uiAvatarUrl
//                        acountname
//                        realname = newUserWx.uiNickName
//                        password
//                        usertypeid = UserType.Enterprise.value.toByte()
//                        usertype = UserType.Enterprise.des
//                        isenable = true
//                        wechatid = newUserWx.uiOpenId
//                    }
                    var r = userInfoWxMapper.insert(newUserWx)
//                    r += userinfoMapper.insert(newUser)
                    return if (r == 1) {
                        BaseResponse(true, "微信用户注册成功")
                    } else {
                        BaseResponse(false, "微信用户注册失败")
                    }
                } else {
                    if (user.uiGuid != null) {
                        val userinfo = userinfoMapper.selectByPrimaryKey(user.uiGuid)
                        BaseResponse(true, "微信用户登录成功", data = userinfo)
                    } else {
                        BaseResponse(true, "微信用户未绑定企业")
                    }
                }
            } else {
                return BaseResponse(false, "请求失败:errcode=${json["errcode"]}")
            }
        } else {
            return BaseResponse(false, "请求失败, 无法访问微信接口")
        }
    }
 
    override fun loginPw(accessTokenPW: AccessTokenPW): BaseResponse<Userinfo> {
        TODO("Not yet implemented")
    }
}