talent_account.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. package youngee_talent_service
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/gogf/gf/os/gtime"
  7. "io/ioutil"
  8. "net/http"
  9. "net/url"
  10. "regexp"
  11. "strconv"
  12. "youngmini_server/app/dao"
  13. "youngmini_server/app/model/youngee_talent_model"
  14. "youngmini_server/app/utils"
  15. "github.com/gogf/gf/frame/g"
  16. "github.com/gogf/gf/net/ghttp"
  17. "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
  18. ocr "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ocr/v1"
  19. "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ocr/v1/model"
  20. region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/ocr/v1/region"
  21. "strings"
  22. )
  23. func GetTalentAccount(r *ghttp.Request) *TalentHttpResult {
  24. //达人id获取
  25. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  26. //定义除了youngee_platform_account_info表结构外额外还有info_third_platform的结构体(用表连接)
  27. if err != nil {
  28. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  29. }
  30. //查找给定达人id之后,该达人绑定了几个平台的账号
  31. res, err := g.DB().Model("youngee_platform_account_info").All("talent_id", tid)
  32. fmt.Println("该账号社媒平台绑定结果为__________:", res)
  33. if err != nil {
  34. return &TalentHttpResult{Code: -2, Msg: "query database error"}
  35. }
  36. if res == nil {
  37. return &TalentHttpResult{Code: -3, Msg: "未绑定任何平台"}
  38. }
  39. return &TalentHttpResult{Code: 0, Msg: "success", Data: res}
  40. }
  41. func GetTalentAccountKuaishou(r *ghttp.Request) *TalentHttpResult {
  42. //达人id获取
  43. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  44. if err != nil {
  45. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  46. }
  47. //查找给定达人id之后,该达人绑定了几个平台的账号
  48. res, err := g.DB().Model("platform_kuaishou_user_info").All("talent_id", tid)
  49. if err != nil {
  50. return &TalentHttpResult{Code: -2, Msg: "query database error"}
  51. }
  52. if res == nil {
  53. return &TalentHttpResult{Code: -3, Msg: "未绑定任何平台"}
  54. }
  55. return &TalentHttpResult{Code: 0, Msg: "success", Data: res}
  56. }
  57. func DelTalentAccount(r *ghttp.Request) *TalentHttpResult {
  58. tid, _ := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  59. var DelAccountReq *youngee_talent_model.DelAccountReq
  60. err := r.ParseForm(&DelAccountReq)
  61. if err != nil {
  62. return &TalentHttpResult{Code: -1, Msg: "Parse form failed"}
  63. }
  64. if DelAccountReq.PlatformId == 4 || DelAccountReq.PlatformId == 8 { //删除快手
  65. _, err := g.Model("platform_kuaishou_user_info").Where("talent_id=? AND platform_id IN (4,8) And open_id=?", tid, DelAccountReq.OpenId).Delete()
  66. if err != nil {
  67. return &TalentHttpResult{Code: -1, Msg: "delete account failed"}
  68. }
  69. } else {
  70. _, err := g.Model("platform_kuaishou_user_info").Where("talent_id=? AND platform_id =? And open_id=?", tid, DelAccountReq.PlatformId, DelAccountReq.OpenId).Delete()
  71. if err != nil {
  72. return &TalentHttpResult{Code: -1, Msg: "delete account failed"}
  73. }
  74. }
  75. return &TalentHttpResult{Code: 0, Msg: "success"}
  76. }
  77. func OnAddTalentAccount(r *ghttp.Request) *TalentHttpResult {
  78. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  79. if err != nil {
  80. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  81. }
  82. account := youngee_talent_model.AddAccountReq{}
  83. //account := youngee_talent_model.KuaishouUserInfo{}
  84. err = r.ParseForm(&account)
  85. if err != nil {
  86. return &TalentHttpResult{Code: -2, Msg: err.Error()}
  87. }
  88. //获取 基本信息,头像昵称、粉丝数、作品数目、点赞数
  89. //小红书
  90. if account.PlatformId == 1 {
  91. //获取用户唯一标识
  92. shareText := account.HomePageUrl
  93. //调用MoreAPI
  94. userDetail := getXHSUserDetail(shareText)
  95. if userDetail == nil {
  96. return &TalentHttpResult{Code: -1, Msg: "MoreApi调用出错"}
  97. }
  98. //是否被其他人绑定
  99. BindCode := CheckAccountBind(account.PlatformId, userDetail.RedID)
  100. if BindCode == -1 {
  101. return &TalentHttpResult{Code: -1, Msg: "查询账号表出错"}
  102. } else if BindCode == 1 {
  103. return &TalentHttpResult{Code: -5, Msg: "此小红书Id已经被绑定"}
  104. } else { //没有被其他人绑定
  105. //调用OCR
  106. var orcInfo = getXHSOCRresult(account.HomePageCaptureUrl, account.PlatformId)
  107. if orcInfo == nil {
  108. return &TalentHttpResult{Code: -1, Msg: "ORC接口出错"}
  109. }
  110. //小红书id一致且是自己的主页
  111. if orcInfo.RedId == userDetail.RedID && orcInfo.SelfHome == 1 {
  112. //获取用户基本信息
  113. XHSdata := g.Map{
  114. "talent_id": tid,
  115. "open_id": userDetail.RedID,
  116. "platform_id": account.PlatformId,
  117. "fan": userDetail.Fans,
  118. "nick_name": userDetail.Nickname,
  119. "head_uri": userDetail.Images,
  120. "city": userDetail.IPLocation,
  121. "gender": userDetail.Gender,
  122. "like_num": userDetail.LikeNum,
  123. "create_time": gtime.Now(),
  124. "update_time": gtime.Now(),
  125. "home_page_url": account.HomePageUrl,
  126. "home_page_capture_url": account.HomePageCaptureUrl,
  127. }
  128. //入库
  129. _, err := g.Model("platform_kuaishou_user_info").Insert(XHSdata)
  130. if err != nil {
  131. return &TalentHttpResult{Code: -1, Msg: "数据入库失败"}
  132. }
  133. // 修改talent表字段is_bind_account值为1
  134. _, err = g.Model(dao.YoungeeTalentInfo.Table).Data("is_bind_account=1").Where("id", tid).Update()
  135. if err != nil {
  136. return &TalentHttpResult{Code: -2, Msg: err.Error()}
  137. }
  138. return &TalentHttpResult{Code: 0, Msg: "小红书账号绑定成功"}
  139. } else {
  140. return &TalentHttpResult{Code: -6, Msg: "图片中小红书ID和主页链接不一致"}
  141. }
  142. }
  143. } else if account.PlatformId == 3 { //微博
  144. //获取用户唯一标识
  145. shareText := account.HomePageUrl
  146. //调用MoreAPI
  147. userDetail := getWBUserDetail(shareText)
  148. if userDetail == nil {
  149. return &TalentHttpResult{Code: -1, Msg: "MoreApi调用出错"}
  150. }
  151. //是否被其他人绑定
  152. BindCode := CheckAccountBind(account.PlatformId, userDetail.WBID)
  153. if BindCode == -1 {
  154. return &TalentHttpResult{Code: -1, Msg: "查询账号表出错"}
  155. } else if BindCode == 1 {
  156. return &TalentHttpResult{Code: -5, Msg: "此微博Id已经被绑定"}
  157. } else {
  158. //调用OCR
  159. var orcInfo = getWBOCRresult(account.HomePageCaptureUrl, userDetail.Nickname)
  160. if orcInfo == nil {
  161. return &TalentHttpResult{Code: -1, Msg: "ORC接口出错"}
  162. }
  163. //orc的昵称和moreapi返回的昵称一致,且主页截图是自己的截图
  164. if orcInfo.SelfHome == 1 {
  165. //获取用户基本信息
  166. WBdata := g.Map{
  167. "talent_id": tid,
  168. "open_id": userDetail.WBID,
  169. "platform_id": account.PlatformId,
  170. "fan": userDetail.Fans,
  171. "nick_name": userDetail.Nickname,
  172. "head_uri": userDetail.HeadImage,
  173. "gender": userDetail.Gender,
  174. "create_time": gtime.Now(),
  175. "update_time": gtime.Now(),
  176. "home_page_url": account.HomePageUrl,
  177. "home_page_capture_url": account.HomePageCaptureUrl,
  178. }
  179. //入库
  180. _, err := g.Model("platform_kuaishou_user_info").Insert(WBdata)
  181. if err != nil {
  182. return &TalentHttpResult{Code: -1, Msg: "数据入库失败"}
  183. }
  184. // 修改talent表字段is_bind_account值为1
  185. _, err = g.Model(dao.YoungeeTalentInfo.Table).Data("is_bind_account=1").Where("id", tid).Update()
  186. if err != nil {
  187. return &TalentHttpResult{Code: -2, Msg: err.Error()}
  188. }
  189. return &TalentHttpResult{Code: 0, Msg: "微博账号绑定成功"}
  190. } else {
  191. return &TalentHttpResult{Code: -6, Msg: "主页截图非本人主页,获取着与主页链接不匹配"}
  192. }
  193. }
  194. } else if account.PlatformId == 5 { //B站
  195. //获取用户主页URL以及uid
  196. shareText := account.HomePageUrl
  197. //调用MoreAPI
  198. userDetail := getBiLiUserDetail(shareText)
  199. if userDetail == nil {
  200. return &TalentHttpResult{Code: -1, Msg: "MoreApi调用出错"}
  201. }
  202. //是否被其他人绑定
  203. BindCode := CheckAccountBind(account.PlatformId, userDetail.UID)
  204. if BindCode == -1 {
  205. return &TalentHttpResult{Code: -1, Msg: "查询账号表出错"}
  206. } else if BindCode == 1 {
  207. return &TalentHttpResult{Code: -5, Msg: "此bilibili已经被绑定"}
  208. } else {
  209. //调用OCR
  210. var orcInfo = getBliOCRresult(account.HomePageCaptureUrl, userDetail.Nickname)
  211. if orcInfo == nil {
  212. return &TalentHttpResult{Code: -1, Msg: "ORC接口出错"}
  213. }
  214. //orc的昵称和moreapi返回的昵称一致,且主页截图是自己的截图
  215. if orcInfo.SelfHome == 1 {
  216. //获取用户基本信息
  217. Blidata := g.Map{
  218. "talent_id": tid,
  219. "open_id": userDetail.UID,
  220. "platform_id": account.PlatformId,
  221. "fan": userDetail.Fans,
  222. "nick_name": userDetail.Nickname,
  223. "head_uri": userDetail.HeadImage,
  224. "gender": userDetail.Gender,
  225. "create_time": gtime.Now(),
  226. "update_time": gtime.Now(),
  227. "home_page_url": account.HomePageUrl,
  228. "home_page_capture_url": account.HomePageCaptureUrl,
  229. }
  230. //入库
  231. _, err := g.Model("platform_kuaishou_user_info").Insert(Blidata)
  232. if err != nil {
  233. return &TalentHttpResult{Code: -1, Msg: "数据入库失败"}
  234. }
  235. // 修改talent表字段is_bind_account值为1
  236. _, err = g.Model(dao.YoungeeTalentInfo.Table).Data("is_bind_account=1").Where("id", tid).Update()
  237. if err != nil {
  238. return &TalentHttpResult{Code: -2, Msg: err.Error()}
  239. }
  240. return &TalentHttpResult{Code: 0, Msg: "bli账号绑定成功"}
  241. } else {
  242. return &TalentHttpResult{Code: -6, Msg: "主页截图非本人主页,或者与主页链接不匹配"}
  243. }
  244. }
  245. } else {
  246. return &TalentHttpResult{Code: -1, Msg: "平台ID有误"}
  247. }
  248. return &TalentHttpResult{Code: 0, Msg: "success"}
  249. }
  250. //func OnUpdateTalentAccount(r *ghttp.Request) *TalentHttpResult {
  251. // tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  252. // if err != nil {
  253. // return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  254. // }
  255. // account := youngee_talent_model.PlatformAccountInfo{}
  256. // err = r.ParseForm(&account)
  257. // if err != nil {
  258. // return &TalentHttpResult{Code: -2, Msg: err.Error()}
  259. // }
  260. // account.TalentId = tid
  261. // _, err = g.DB().Model(dao.YoungeePlatformAccountInfo.Table).Update(account, "account_id = ? and talent_id = ?", account.PlatformId, tid)
  262. // if err != nil {
  263. // return &TalentHttpResult{Code: -3, Msg: "update failed"}
  264. // }
  265. // return &TalentHttpResult{Code: 0, Msg: "success"}
  266. //}
  267. // 检查该账号是否被其他人绑定了
  268. func CheckAccountBind(pId int, openId string) int {
  269. count, err := g.Model("platform_kuaishou_user_info").Where("platform_id = ? and open_id = ?", pId, openId).Count()
  270. if err != nil {
  271. return -1
  272. }
  273. if count > 0 { //已经被绑定
  274. return 1
  275. } else {
  276. return 0
  277. }
  278. }
  279. // 用于获取 XHS 用户详细信息的函数
  280. func getXHSUserDetail(homeURL string) *youngee_talent_model.XHSBasicInfo {
  281. cookie := "acw_tc=0a00d7db17326882742197649ed1bf15848a5ff06232f5c37b7eaa2663f722; abRequestId=028b7a52-6c20-5315-aaf5-3bf365d26f20; webBuild=4.35.0; xsecappid=xhs-pc-web; a1=1936c44e2c4IjfanuIengZ3lEpOKmXeKdOan0rcbl50000544495; webId=021b26332dd3b74978a347eb885a1761; gid=yjJJWyiYfKMyyjJJWyiYYVIvqK2EfyMuuyMdE016fE2vSU286k88SE888yKJJq4800jJd8qq; websectiga=16f444b9ff5e3d7e258b5f7674489196303a0b160e16647c6c2b4dcb609f4134; sec_poison_id=58886b56-6f01-4f86-9ea1-0a1febfde070; unread={\\\"ub\\\":\\\"66e2f3690000000012011cb0\\\",\\\"ue\\\":\\\"66e823b9000000001e018443\\\",\\\"uc\\\":14}; web_session=040069b41c29aed16ac492f173354bfc129950"
  282. GetDetailUrl := "http://120.46.92.62:6888/api/xhs/user_detail"
  283. // 构建请求体
  284. requestBody := map[string]string{"share_text": homeURL}
  285. jsonBody, err := json.Marshal(requestBody)
  286. if err != nil {
  287. fmt.Println("Error marshalling request body:", err)
  288. return nil
  289. }
  290. // 创建 POST 请求
  291. req, err := http.NewRequest("POST", GetDetailUrl, bytes.NewBuffer(jsonBody))
  292. if err != nil {
  293. fmt.Println("Error creating POST request:", err)
  294. return nil
  295. }
  296. // 设置请求头,包括 cookie
  297. req.Header.Set("Content-Type", "application/json")
  298. req.Header.Set("cookie", cookie)
  299. // 发送请求
  300. client := &http.Client{}
  301. resp, err := client.Do(req)
  302. if err != nil {
  303. fmt.Println("Error sending POST request:", err)
  304. return nil
  305. }
  306. defer resp.Body.Close()
  307. // 读取响应体
  308. body, err := ioutil.ReadAll(resp.Body)
  309. if err != nil {
  310. fmt.Println("Error reading response body:", err)
  311. return nil
  312. }
  313. // 创建一个空的 map 用于存放解析后的 JSON 数据
  314. var responseMap map[string]interface{}
  315. err = json.Unmarshal(body, &responseMap)
  316. if err != nil {
  317. fmt.Println("Error parsing JSON:", err)
  318. return nil
  319. }
  320. // 提取 data.response_body.data.basic_info
  321. if responseBody, ok := responseMap["data"].(map[string]interface{}); ok {
  322. if responseBodyData, ok := responseBody["response_body"].(map[string]interface{}); ok {
  323. if data, ok := responseBodyData["data"].(map[string]interface{}); ok {
  324. if basicInfo, ok := data["basic_info"].(map[string]interface{}); ok {
  325. // 使用 json.Unmarshal 将 basic_info 直接解析到结构体中
  326. var userInfo youngee_talent_model.XHSBasicInfo
  327. userInfo.Imageb = fmt.Sprintf("%v", basicInfo["imageb"])
  328. userInfo.Nickname = fmt.Sprintf("%v", basicInfo["nickname"])
  329. userInfo.Images = fmt.Sprintf("%v", basicInfo["images"])
  330. userInfo.RedID = fmt.Sprintf("%v", basicInfo["red_id"])
  331. userInfo.Gender = fmt.Sprintf("%v", basicInfo["gender"])
  332. userInfo.IPLocation = fmt.Sprintf("%v", basicInfo["ip_location"])
  333. userInfo.Desc = fmt.Sprintf("%v", basicInfo["desc"])
  334. // 提取 interactions
  335. if interactions, ok := data["interactions"].([]interface{}); ok {
  336. for _, interaction := range interactions {
  337. interactionData := interaction.(map[string]interface{})
  338. // 提取粉丝数(name 为 "粉丝")
  339. if interactionData["name"] == "粉丝" {
  340. countStr := fmt.Sprintf("%v", interactionData["count"])
  341. countInt, _ := strconv.Atoi(countStr)
  342. userInfo.Fans = countInt
  343. }
  344. // 提取获赞与收藏数(name 为 "获赞与收藏")
  345. if interactionData["name"] == "获赞与收藏" {
  346. LikeNum := fmt.Sprintf("%v", interactionData["count"])
  347. LikeNumInt, _ := strconv.Atoi(LikeNum)
  348. userInfo.LikeNum = LikeNumInt
  349. }
  350. }
  351. }
  352. // 返回 BasicInfo 结构体
  353. return &userInfo
  354. }
  355. }
  356. }
  357. }
  358. // 如果没有找到 basic_info,返回错误
  359. return nil
  360. }
  361. // 获取WB用户详细信息
  362. func getWBUserDetail(homeURL string) *youngee_talent_model.WBBasicInfo {
  363. GetDetailUrl := "http://120.46.92.62:6888/api/weibo/user_detail"
  364. // 构建请求体
  365. requestBody := map[string]string{"share_text": homeURL}
  366. jsonBody, err := json.Marshal(requestBody)
  367. if err != nil {
  368. fmt.Println("Error marshalling request body:", err)
  369. return nil
  370. }
  371. // 创建 POST 请求
  372. req, err := http.NewRequest("POST", GetDetailUrl, bytes.NewBuffer(jsonBody))
  373. if err != nil {
  374. fmt.Println("Error creating POST request:", err)
  375. return nil
  376. }
  377. // 设置请求头,包括 cookie
  378. req.Header.Set("Content-Type", "application/json")
  379. // 发送请求
  380. client := &http.Client{}
  381. resp, err := client.Do(req)
  382. if err != nil {
  383. fmt.Println("Error sending POST request:", err)
  384. return nil
  385. }
  386. defer resp.Body.Close()
  387. // 读取响应体
  388. body, err := ioutil.ReadAll(resp.Body)
  389. if err != nil {
  390. fmt.Println("Error reading response body:", err)
  391. return nil
  392. }
  393. // 创建一个空的 map 用于存放解析后的 JSON 数据
  394. var responseMap map[string]interface{}
  395. err = json.Unmarshal(body, &responseMap)
  396. if err != nil {
  397. fmt.Println("Error parsing JSON:", err)
  398. return nil
  399. }
  400. //提取数据
  401. // 提取数据
  402. if responseBody, ok := responseMap["data"].(map[string]interface{}); ok {
  403. // 提取所需字段 responseBody["id"]
  404. openId := fmt.Sprintf("%.0f", responseBody["id"])
  405. fmt.Println("openId----->:", openId)
  406. followers, _ := utils.TypeTran.AnyToInt(responseBody["followers_count"]) // 粉丝
  407. avatarHd, _ := utils.TypeTran.AnyToString(responseBody["avatar_hd"]) //头像
  408. nickName, _ := utils.TypeTran.AnyToString(responseBody["screen_name"]) //昵称
  409. fmt.Println("nickName----->:", nickName)
  410. statusesCount, _ := utils.TypeTran.AnyToInt(responseBody["statuses_count"]) // 作品数目
  411. gender, _ := utils.TypeTran.AnyToString(responseBody["gender"])
  412. // 创建并返回 BasicInfo 结构体
  413. userInfo := youngee_talent_model.WBBasicInfo{
  414. WBID: openId,
  415. HeadImage: avatarHd,
  416. Fans: followers,
  417. PostNum: statusesCount,
  418. Gender: gender,
  419. Nickname: nickName,
  420. }
  421. return &userInfo
  422. }
  423. // 如果没有找到 basic_info,返回错误
  424. return nil
  425. }
  426. func getBiLiUserDetail(homeURL string) *youngee_talent_model.BliBasicInfo {
  427. redirectedURL, err := getRedirectedURL(homeURL)
  428. if err != nil {
  429. fmt.Println("Error:", err)
  430. return nil
  431. }
  432. userID, err := extractUserID(redirectedURL)
  433. if err != nil {
  434. fmt.Println("Error extracting user ID:", err)
  435. return nil
  436. }
  437. fmt.Println("Extracted user ID:", userID)
  438. GetDetailUrl := "http://120.46.92.62:6888/api/bilibili/user_info"
  439. // 构建请求体
  440. requestBody := map[string]string{"uid": userID}
  441. jsonBody, err := json.Marshal(requestBody)
  442. if err != nil {
  443. fmt.Println("Error marshalling request body:", err)
  444. return nil
  445. }
  446. // 创建 POST 请求
  447. req, err := http.NewRequest("POST", GetDetailUrl, bytes.NewBuffer(jsonBody))
  448. if err != nil {
  449. fmt.Println("Error creating POST request:", err)
  450. return nil
  451. }
  452. // 设置请求头,包括 cookie
  453. req.Header.Set("Content-Type", "application/json")
  454. // 发送请求
  455. client := &http.Client{}
  456. resp, err := client.Do(req)
  457. if err != nil {
  458. fmt.Println("Error sending POST request:", err)
  459. return nil
  460. }
  461. defer resp.Body.Close()
  462. // 读取响应体
  463. body, err := ioutil.ReadAll(resp.Body)
  464. if err != nil {
  465. fmt.Println("Error reading response body:", err)
  466. return nil
  467. }
  468. // 创建一个空的 map 用于存放解析后的 JSON 数据
  469. var responseMap map[string]interface{}
  470. err = json.Unmarshal(body, &responseMap)
  471. if err != nil {
  472. fmt.Println("Error parsing JSON:", err)
  473. return nil
  474. }
  475. // 提取数据
  476. if responseBody, ok := responseMap["data"].(map[string]interface{}); ok {
  477. // 提取 space 信息
  478. spaceInfo := responseBody["space"].(map[string]interface{})
  479. // 提取 space.info 中的字段
  480. mid := fmt.Sprintf("%.0f", spaceInfo["info"].(map[string]interface{})["mid"])
  481. name, _ := utils.TypeTran.AnyToString(spaceInfo["info"].(map[string]interface{})["name"]) // name
  482. sex, _ := utils.TypeTran.AnyToString(spaceInfo["info"].(map[string]interface{})["sex"]) // sex
  483. face, _ := utils.TypeTran.AnyToString(spaceInfo["info"].(map[string]interface{})["face"]) // face
  484. // 提取 stat 信息
  485. stat := responseBody["stat"].(map[string]interface{})
  486. follower, _ := utils.TypeTran.AnyToInt(stat["follower"]) // follower
  487. // 创建并返回 BasicInfo 结构体
  488. userInfo := youngee_talent_model.BliBasicInfo{
  489. UID: mid,
  490. HeadImage: face,
  491. Fans: follower, // 转换为整数
  492. Gender: sex,
  493. Nickname: name,
  494. }
  495. return &userInfo
  496. }
  497. // 如果没有找到 basic_info,返回错误
  498. return nil
  499. }
  500. // *youngee_talent_model.OCRResponse
  501. func getXHSOCRresult(homePicURL string, platform_id int) *youngee_talent_model.OCRResponse {
  502. // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
  503. auth := basic.NewCredentialsBuilder().
  504. WithAk("YJRGTOWEITSYOXN3YPJY").
  505. WithSk("Kd7WDeS2CxlLXjMhGpLW0AmdOZHafitQpBy13a0s").
  506. Build()
  507. client := ocr.NewOcrClient(
  508. ocr.OcrClientBuilder().
  509. WithRegion(region.ValueOf("cn-north-4")).
  510. WithCredential(auth).
  511. Build())
  512. request := &model.RecognizeWebImageRequest{}
  513. request.Body = &model.WebImageRequestBody{
  514. Url: &homePicURL,
  515. }
  516. // 发送请求
  517. response, err := client.RecognizeWebImage(request)
  518. if err != nil {
  519. return nil
  520. }
  521. // 输出返回的完整响应
  522. fmt.Printf("%+v\n", response)
  523. resultText := response.String()
  524. fmt.Println("resultText--->:", resultText)
  525. var orcInfo youngee_talent_model.OCRResponse
  526. if strings.Contains(resultText, "编辑资料") &&
  527. strings.Contains(resultText, "购物车") &&
  528. strings.Contains(resultText, "查看我的订单") {
  529. orcInfo.SelfHome = 1
  530. } else {
  531. orcInfo.SelfHome = 0
  532. }
  533. //获取小红书ID
  534. re := regexp.MustCompile(`小红书号:([\w-]+)`) // 匹配字母、数字或破折号的组合
  535. match := re.FindStringSubmatch(resultText)
  536. if len(match) > 1 {
  537. xhsID := match[1] // match[1] 是第一个括号中的内容,即小红书号
  538. fmt.Println("xhsID-->:", xhsID)
  539. orcInfo.RedId = xhsID
  540. }
  541. return &orcInfo
  542. }
  543. func getWBOCRresult(homePicURL string, nickName string) *youngee_talent_model.OCRResponse {
  544. // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
  545. auth := basic.NewCredentialsBuilder().
  546. WithAk("YJRGTOWEITSYOXN3YPJY").
  547. WithSk("Kd7WDeS2CxlLXjMhGpLW0AmdOZHafitQpBy13a0s").
  548. Build()
  549. client := ocr.NewOcrClient(
  550. ocr.OcrClientBuilder().
  551. WithRegion(region.ValueOf("cn-north-4")).
  552. WithCredential(auth).
  553. Build())
  554. request := &model.RecognizeWebImageRequest{}
  555. request.Body = &model.WebImageRequestBody{
  556. Url: &homePicURL,
  557. }
  558. // 发送请求
  559. response, err := client.RecognizeWebImage(request)
  560. if err != nil {
  561. return nil
  562. }
  563. // 输出返回的完整响应
  564. fmt.Printf("%+v\n", response)
  565. resultText := response.String()
  566. fmt.Println("resultText--->:", resultText)
  567. var orcInfo youngee_talent_model.OCRResponse
  568. //检测是否是自己的主页
  569. //检查是否是自己的首页
  570. if strings.Contains(resultText, "编辑个人资料") &&
  571. strings.Contains(resultText, "全部微博") &&
  572. strings.Contains(resultText, nickName) {
  573. orcInfo.SelfHome = 1
  574. } else {
  575. orcInfo.SelfHome = 0
  576. }
  577. return &orcInfo
  578. }
  579. func getBliOCRresult(homePicURL string, nickName string) *youngee_talent_model.OCRResponse {
  580. // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
  581. auth := basic.NewCredentialsBuilder().
  582. WithAk("YJRGTOWEITSYOXN3YPJY").
  583. WithSk("Kd7WDeS2CxlLXjMhGpLW0AmdOZHafitQpBy13a0s").
  584. Build()
  585. client := ocr.NewOcrClient(
  586. ocr.OcrClientBuilder().
  587. WithRegion(region.ValueOf("cn-north-4")).
  588. WithCredential(auth).
  589. Build())
  590. request := &model.RecognizeWebImageRequest{}
  591. request.Body = &model.WebImageRequestBody{
  592. Url: &homePicURL,
  593. }
  594. // 发送请求
  595. response, err := client.RecognizeWebImage(request)
  596. if err != nil {
  597. return nil
  598. }
  599. // 输出返回的完整响应
  600. fmt.Printf("%+v\n", response)
  601. resultText := response.String()
  602. fmt.Println("resultText--->:", resultText)
  603. var orcInfo youngee_talent_model.OCRResponse
  604. //检测是否是自己的主页
  605. if strings.Contains(resultText, "编辑资料") &&
  606. strings.Contains(resultText, nickName) {
  607. orcInfo.SelfHome = 1
  608. } else {
  609. orcInfo.SelfHome = 0
  610. }
  611. return &orcInfo
  612. }
  613. // 获取WB用户详细信息
  614. func TestImage(r *ghttp.Request) *TalentHttpResult {
  615. one, _ := g.Model("platform_kuaishou_user_info").Where("id", 188).One()
  616. return &TalentHttpResult{Code: 0, Msg: "success", Data: one}
  617. }
  618. func getRedirectedURL(urlStr string) (string, error) {
  619. client := &http.Client{
  620. CheckRedirect: func(req *http.Request, via []*http.Request) error {
  621. if len(via) > 0 { // 如果发生重定向,返回最终的重定向 URL
  622. return nil
  623. }
  624. return fmt.Errorf("final URL: %s", req.URL) // 获取最终的重定向 URL
  625. },
  626. }
  627. resp, err := client.Get(urlStr)
  628. if err != nil {
  629. return "", err
  630. }
  631. defer resp.Body.Close()
  632. return resp.Request.URL.String(), nil // 返回最终目标 URL
  633. }
  634. func extractUserID(urlStr string) (string, error) {
  635. // 解析 URL
  636. parsedURL, err := url.Parse(urlStr)
  637. if err != nil {
  638. return "", err
  639. }
  640. // 提取路径部分中的用户 ID
  641. // 这里我们假设用户 ID 是 URL 的路径中的一部分(如 space.bilibili.com/29408510)
  642. segments := parsedURL.Path
  643. var userID string
  644. // 假设 ID 是路径中的第二部分,如 /29408510
  645. fmt.Sscanf(segments, "/%s", &userID)
  646. return userID, nil
  647. }