123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- package usercenter
- import (
- "encoding/json"
- "io/ioutil"
- "log"
- "app.yhyue.com/moapp/jybase/mongodb"
- "net/http"
- "strings"
- . "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/jwt"
- )
- var ContentType_Json = "application/json"
- var UserCenterAdd = "/userCenter/user/add"
- var UserCenterUpdate = "/userCenter/user/updateById"
- var UserCenterDelete = "/userCenter/user/deleteById"
- var UserCenterGetUserIdentity = "/userCenter/user/identity"
- /*
- 修改header 的post请求
- */
- func PostHeader(url, value string, headers map[string]string, ck *http.Cookie) (string, error) {
- client := &http.Client{}
- req, err := http.NewRequest("POST", url, strings.NewReader(value))
- req.AddCookie(ck)
- if err != nil {
- return "", err
- }
- for key, header := range headers {
- req.Header.Set(key, header)
- }
- resp, err := client.Do(req)
- if err != nil {
- return "", err
- }
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return "", err
- }
- return string(body), nil
- }
- //调用用户中台相关-api接口
- func PostUserCenter(url, contentTpe, userid string, data map[string]interface{}, ck *http.Cookie) (*map[string]interface{}, bool) {
- token, err := jwt.UserJwt.GetToken(map[string]interface{}{
- "userid": userid,
- })
- if err != nil {
- log.Printf("%s获取token失败", userid)
- return nil, false
- }
- log.Println("token jwt:", token)
- log.Println("data:", data)
- data_str, err := json.Marshal(data)
- if err != nil {
- log.Printf("%s序列化失败%+v", userid, data)
- return nil, false
- }
- ret, err := PostHeader(url, string(data_str), map[string]string{
- "Content-Type": contentTpe,
- "Authorization": token,
- }, ck)
- if err != nil {
- log.Printf("%s调用%s接口失败%+v", userid, url, data)
- return nil, false
- }
- log.Println("ret:", ret)
- m := ObjToMap(ret)
- if m == nil || len(*m) == 0 {
- return nil, false
- }
- errmsg := ObjToString((*m)["error_msg"])
- dataM := ObjToMap((*m)["data"])
- status := IntAllDef((*dataM)["status"], 0)
- if errmsg != "" || status != 1 {
- log.Printf("%s调用%s接口返回结果失败:%s", userid, url, errmsg)
- return nil, false
- }
- return dataM, true
- }
- // usercenter
- type UserInfo struct {
- S_openid string //微信openid
- A_openid string //app 微信openid
- Phone string //手机号
- Nickname string //昵称
- Headimg string //头像
- Company string //公司
- Position string //职位
- Password string //密码
- Unionid string //unionid
- Base_user_id int64 //用户中台base_user的主键id,
- }
- //获取base_user需要的数据
- func GetInfoForBaseUser(mg mongodb.MongodbSim, userid string) *UserInfo {
- if userid == "" {
- return nil
- }
- data, ok := mg.FindById("user", userid, `{"base_user_id":1,"s_m_openid":1,"a_m_openid":1,"s_m_phone":1,"s_phone":1,"s_nickname":1,"s_jyname":1,"s_headimageurl":1,"s_headimage":1,"s_company":1,"s_password":1,"s_unionid":1}`)
- if ok && data != nil && len(*data) > 0 {
- userinfo := &UserInfo{
- Base_user_id: Int64All((*data)["base_user_id"]),
- }
- if s_openid := ObjToString((*data)["s_m_openid"]); s_openid != "" {
- userinfo.S_openid = s_openid
- }
- if a_openid := ObjToString((*data)["a_m_openid"]); a_openid != "" {
- userinfo.A_openid = a_openid
- }
- phone := ObjToString((*data)["s_phone"])
- if phone == "" {
- phone = ObjToString((*data)["s_m_phone"])
- }
- if phone != "" {
- userinfo.Phone = phone
- }
- nickname := ObjToString((*data)["s_nickname"])
- if nickname == "" {
- nickname = ObjToString((*data)["s_jyname"])
- }
- if nickname != "" {
- userinfo.Nickname = nickname
- }
- if unionid := ObjToString((*data)["s_unionid"]); unionid != "" {
- userinfo.Unionid = unionid
- }
- if password := ObjToString((*data)["s_password"]); password != "" {
- userinfo.Password = password
- }
- if headimg := ObjToString((*data)["s_headimageurl"]); headimg != "" {
- userinfo.Headimg = headimg
- }
- return userinfo
- }
- return nil
- }
- //获取mongodb中的base_user_id
- func GetBaseUserId(mg mongodb.MongodbSim, userid string) int64 {
- if userid == "" {
- return 0
- }
- data, ok := mg.FindById("user", userid, `{"base_user_id":1}`)
- if ok && data != nil && len(*data) > 0 {
- return Int64All((*data)["base_user_id"])
- }
- return 0
- }
- //更新用户中台相关
- func UpdateBaseUser(mgo mongodb.MongodbSim, href, userId string, ck *http.Cookie) {
- formdata := map[string]interface{}{
- "appid": "10000",
- }
- userinfo := GetInfoForBaseUser(mgo, userId)
- if userinfo == nil {
- return
- }
- formdata["id"] = userinfo.Base_user_id
- if userinfo.A_openid != "" {
- formdata["a_openid"] = userinfo.A_openid
- }
- if userinfo.S_openid != "" {
- formdata["s_openid"] = userinfo.S_openid
- }
- if userinfo.Company != "" {
- formdata["company"] = userinfo.Company
- }
- if userinfo.Headimg != "" {
- formdata["headimg"] = userinfo.Headimg
- }
- if userinfo.Password != "" {
- formdata["password"] = userinfo.Password
- }
- if userinfo.Nickname != "" {
- formdata["nickname"] = userinfo.Nickname
- }
- if userinfo.Phone != "" {
- formdata["phone"] = userinfo.Phone
- }
- if userinfo.Unionid != "" {
- formdata["unionid"] = userinfo.Unionid
- }
- ret, ok := PostUserCenter(href+UserCenterUpdate, ContentType_Json, userId, formdata, ck)
- if ret != nil && ok {
- status := IntAllDef((*ret)["status"], 0)
- if status != 1 {
- log.Printf("mysql同步数据失败,base_user_id:%s ,user_id :%s ,%+v", formdata["id"], userId, formdata)
- }
- }
- }
- //新增用户中台信息。并绑定mongodb表
- func AddBaseUser(mgo mongodb.MongodbSim, href, userId string, formdata map[string]interface{}, ck *http.Cookie) {
- ret, ok := PostUserCenter(href+UserCenterAdd, ContentType_Json, userId, formdata, ck)
- if ret != nil && ok {
- base_id := Int64All((*ret)["id"])
- if base_id == 0 {
- log.Printf("新增用户中台获取base_user_id失败,userid:%s", userId)
- return
- }
- //获取到mysql的用户id存入user表中
- if ok := mgo.UpdateById("user", userId, map[string]interface{}{
- "$set": map[string]interface{}{"base_user_id": base_id},
- }); !ok {
- log.Printf("mysql同步失败,base_user_id:%s ,userid:%s", base_id, userId)
- }
- }
- }
- type UserIdentity struct {
- PersonId int64 //自然人id
- UserAccountId int64 //个人账户id
- EntAccountId int64 //企业账户id
- EntUserAccountId int64 //企业雇员账户id
- UserPositionId int64 // 个人职位id
- EntUserPositionId int64 // 企业雇员职位id
- UserName string //昵称
- }
- //获取用户中台服务 用户相关账户参数
- func GetUserIdentity(href, userId string, baseUserId, entId int64, ck *http.Cookie) *UserIdentity {
- ret, ok := PostUserCenter(href+UserCenterGetUserIdentity, ContentType_Json, userId,
- map[string]interface{}{
- "appId": "10000",
- "newUserId": baseUserId,
- "entId": entId,
- }, ck)
- if ret != nil && ok {
- return &UserIdentity{
- PersonId: Int64All((*ret)["personId"]),
- EntAccountId: Int64All((*ret)["entAccountId"]),
- UserAccountId: Int64All((*ret)["userAccountId"]),
- EntUserAccountId: Int64All((*ret)["entUserAccountId"]),
- UserPositionId: Int64All((*ret)["userPositionId"]),
- EntUserPositionId: Int64All((*ret)["entUserPositionId"]),
- UserName: ObjToString((*ret)["userName"]),
- }
- }
- return nil
- }
|