userCenter.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package usercenter
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "log"
  6. "app.yhyue.com/moapp/jybase/mongodb"
  7. "net/http"
  8. "strings"
  9. . "app.yhyue.com/moapp/jybase/common"
  10. "app.yhyue.com/moapp/jybase/jwt"
  11. )
  12. var ContentType_Json = "application/json"
  13. var UserCenterAdd = "/userCenter/user/add"
  14. var UserCenterUpdate = "/userCenter/user/updateById"
  15. var UserCenterDelete = "/userCenter/user/deleteById"
  16. var UserCenterGetUserIdentity = "/userCenter/user/identity"
  17. /*
  18. 修改header 的post请求
  19. */
  20. func PostHeader(url, value string, headers map[string]string, ck *http.Cookie) (string, error) {
  21. client := &http.Client{}
  22. req, err := http.NewRequest("POST", url, strings.NewReader(value))
  23. req.AddCookie(ck)
  24. if err != nil {
  25. return "", err
  26. }
  27. for key, header := range headers {
  28. req.Header.Set(key, header)
  29. }
  30. resp, err := client.Do(req)
  31. if err != nil {
  32. return "", err
  33. }
  34. defer resp.Body.Close()
  35. body, err := ioutil.ReadAll(resp.Body)
  36. if err != nil {
  37. return "", err
  38. }
  39. return string(body), nil
  40. }
  41. //调用用户中台相关-api接口
  42. func PostUserCenter(url, contentTpe, userid string, data map[string]interface{}, ck *http.Cookie) (*map[string]interface{}, bool) {
  43. token, err := jwt.UserJwt.GetToken(map[string]interface{}{
  44. "userid": userid,
  45. })
  46. if err != nil {
  47. log.Printf("%s获取token失败", userid)
  48. return nil, false
  49. }
  50. log.Println("token jwt:", token)
  51. log.Println("data:", data)
  52. data_str, err := json.Marshal(data)
  53. if err != nil {
  54. log.Printf("%s序列化失败%+v", userid, data)
  55. return nil, false
  56. }
  57. ret, err := PostHeader(url, string(data_str), map[string]string{
  58. "Content-Type": contentTpe,
  59. "Authorization": token,
  60. }, ck)
  61. if err != nil {
  62. log.Printf("%s调用%s接口失败%+v", userid, url, data)
  63. return nil, false
  64. }
  65. log.Println("ret:", ret)
  66. m := ObjToMap(ret)
  67. if m == nil || len(*m) == 0 {
  68. return nil, false
  69. }
  70. errmsg := ObjToString((*m)["error_msg"])
  71. dataM := ObjToMap((*m)["data"])
  72. status := IntAllDef((*dataM)["status"], 0)
  73. if errmsg != "" || status != 1 {
  74. log.Printf("%s调用%s接口返回结果失败:%s", userid, url, errmsg)
  75. return nil, false
  76. }
  77. return dataM, true
  78. }
  79. // usercenter
  80. type UserInfo struct {
  81. S_openid string //微信openid
  82. A_openid string //app 微信openid
  83. Phone string //手机号
  84. Nickname string //昵称
  85. Headimg string //头像
  86. Company string //公司
  87. Position string //职位
  88. Password string //密码
  89. Unionid string //unionid
  90. Base_user_id int64 //用户中台base_user的主键id,
  91. }
  92. //获取base_user需要的数据
  93. func GetInfoForBaseUser(mg mongodb.MongodbSim, userid string) *UserInfo {
  94. if userid == "" {
  95. return nil
  96. }
  97. 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}`)
  98. if ok && data != nil && len(*data) > 0 {
  99. userinfo := &UserInfo{
  100. Base_user_id: Int64All((*data)["base_user_id"]),
  101. }
  102. if s_openid := ObjToString((*data)["s_m_openid"]); s_openid != "" {
  103. userinfo.S_openid = s_openid
  104. }
  105. if a_openid := ObjToString((*data)["a_m_openid"]); a_openid != "" {
  106. userinfo.A_openid = a_openid
  107. }
  108. phone := ObjToString((*data)["s_phone"])
  109. if phone == "" {
  110. phone = ObjToString((*data)["s_m_phone"])
  111. }
  112. if phone != "" {
  113. userinfo.Phone = phone
  114. }
  115. nickname := ObjToString((*data)["s_nickname"])
  116. if nickname == "" {
  117. nickname = ObjToString((*data)["s_jyname"])
  118. }
  119. if nickname != "" {
  120. userinfo.Nickname = nickname
  121. }
  122. if unionid := ObjToString((*data)["s_unionid"]); unionid != "" {
  123. userinfo.Unionid = unionid
  124. }
  125. if password := ObjToString((*data)["s_password"]); password != "" {
  126. userinfo.Password = password
  127. }
  128. if headimg := ObjToString((*data)["s_headimageurl"]); headimg != "" {
  129. userinfo.Headimg = headimg
  130. }
  131. return userinfo
  132. }
  133. return nil
  134. }
  135. //获取mongodb中的base_user_id
  136. func GetBaseUserId(mg mongodb.MongodbSim, userid string) int64 {
  137. if userid == "" {
  138. return 0
  139. }
  140. data, ok := mg.FindById("user", userid, `{"base_user_id":1}`)
  141. if ok && data != nil && len(*data) > 0 {
  142. return Int64All((*data)["base_user_id"])
  143. }
  144. return 0
  145. }
  146. //更新用户中台相关
  147. func UpdateBaseUser(mgo mongodb.MongodbSim, href, userId string, ck *http.Cookie) {
  148. formdata := map[string]interface{}{
  149. "appid": "10000",
  150. }
  151. userinfo := GetInfoForBaseUser(mgo, userId)
  152. if userinfo == nil {
  153. return
  154. }
  155. formdata["id"] = userinfo.Base_user_id
  156. if userinfo.A_openid != "" {
  157. formdata["a_openid"] = userinfo.A_openid
  158. }
  159. if userinfo.S_openid != "" {
  160. formdata["s_openid"] = userinfo.S_openid
  161. }
  162. if userinfo.Company != "" {
  163. formdata["company"] = userinfo.Company
  164. }
  165. if userinfo.Headimg != "" {
  166. formdata["headimg"] = userinfo.Headimg
  167. }
  168. if userinfo.Password != "" {
  169. formdata["password"] = userinfo.Password
  170. }
  171. if userinfo.Nickname != "" {
  172. formdata["nickname"] = userinfo.Nickname
  173. }
  174. if userinfo.Phone != "" {
  175. formdata["phone"] = userinfo.Phone
  176. }
  177. if userinfo.Unionid != "" {
  178. formdata["unionid"] = userinfo.Unionid
  179. }
  180. ret, ok := PostUserCenter(href+UserCenterUpdate, ContentType_Json, userId, formdata, ck)
  181. if ret != nil && ok {
  182. status := IntAllDef((*ret)["status"], 0)
  183. if status != 1 {
  184. log.Printf("mysql同步数据失败,base_user_id:%s ,user_id :%s ,%+v", formdata["id"], userId, formdata)
  185. }
  186. }
  187. }
  188. //新增用户中台信息。并绑定mongodb表
  189. func AddBaseUser(mgo mongodb.MongodbSim, href, userId string, formdata map[string]interface{}, ck *http.Cookie) {
  190. ret, ok := PostUserCenter(href+UserCenterAdd, ContentType_Json, userId, formdata, ck)
  191. if ret != nil && ok {
  192. base_id := Int64All((*ret)["id"])
  193. if base_id == 0 {
  194. log.Printf("新增用户中台获取base_user_id失败,userid:%s", userId)
  195. return
  196. }
  197. //获取到mysql的用户id存入user表中
  198. if ok := mgo.UpdateById("user", userId, map[string]interface{}{
  199. "$set": map[string]interface{}{"base_user_id": base_id},
  200. }); !ok {
  201. log.Printf("mysql同步失败,base_user_id:%s ,userid:%s", base_id, userId)
  202. }
  203. }
  204. }
  205. type UserIdentity struct {
  206. PersonId int64 //自然人id
  207. UserAccountId int64 //个人账户id
  208. EntAccountId int64 //企业账户id
  209. EntUserAccountId int64 //企业雇员账户id
  210. UserPositionId int64 // 个人职位id
  211. EntUserPositionId int64 // 企业雇员职位id
  212. UserName string //昵称
  213. }
  214. //获取用户中台服务 用户相关账户参数
  215. func GetUserIdentity(href, userId string, baseUserId, entId int64, ck *http.Cookie) *UserIdentity {
  216. ret, ok := PostUserCenter(href+UserCenterGetUserIdentity, ContentType_Json, userId,
  217. map[string]interface{}{
  218. "appId": "10000",
  219. "newUserId": baseUserId,
  220. "entId": entId,
  221. }, ck)
  222. if ret != nil && ok {
  223. return &UserIdentity{
  224. PersonId: Int64All((*ret)["personId"]),
  225. EntAccountId: Int64All((*ret)["entAccountId"]),
  226. UserAccountId: Int64All((*ret)["userAccountId"]),
  227. EntUserAccountId: Int64All((*ret)["entUserAccountId"]),
  228. UserPositionId: Int64All((*ret)["userPositionId"]),
  229. EntUserPositionId: Int64All((*ret)["entUserPositionId"]),
  230. UserName: ObjToString((*ret)["userName"]),
  231. }
  232. }
  233. return nil
  234. }