ckLogin.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. package login
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "encoding/json"
  6. "fmt"
  7. "log"
  8. "strings"
  9. "time"
  10. "git.listensoft.net/tool/jspkit/common"
  11. "git.listensoft.net/tool/jspkit/common/models"
  12. "git.listensoft.net/tool/jspkit/logger"
  13. "git.listensoft.net/tool/jspkit/taxerr"
  14. "github.com/forgoer/openssl"
  15. "github.com/go-rod/rod"
  16. "github.com/go-rod/rod/lib/proto"
  17. "github.com/go-rod/rod/lib/utils"
  18. "github.com/tidwall/gjson"
  19. )
  20. func CheckSessionAndLogin(ctx context.Context, p *rod.Page, BaseUri string, info models.CompanyInfo) (err error) {
  21. //return taxerr.NewUser("税局代理机构登录存在风险,预计6号恢复,请等待税局修复后使用")//12.6 09:20左右 确认完了
  22. if info.Dlfs == "代理登录" && info.IdNo == "" {
  23. return taxerr.DlTaxNoError
  24. }
  25. return AutoCheckSessionAndLogin(ctx, p, BaseUri, info)
  26. }
  27. func LoadCookie(sessionInfos models.SsoSession, p *rod.Page, BaseUri string, info models.CompanyInfo) (name string, err error) {
  28. err = rod.Try(func() {
  29. defer AddErr()
  30. p.Navigate("")
  31. _ = p.Timeout(common.ClickTimeOut).MustNavigate(areaURLmap[info.Area+".pro"]).StopLoading() // 此时必须得去tpass域名 要不然写入cookie后进不去发票
  32. p.MustSetCookies(sessionInfos.Cookies...)
  33. {
  34. p.MustEval(`() => localStorage.clear()`)
  35. utils.Sleep(1)
  36. // 设置 localStorage 在第二个页面中,逐条设置
  37. logger.Info("Setting localStorage in second browser...")
  38. for key, value := range sessionInfos.LocalStorage {
  39. if key == "logConfig" {
  40. continue
  41. }
  42. script := fmt.Sprintf(`() => localStorage.setItem('%s', '%s')`, key, value)
  43. _, err := p.Eval(script)
  44. if err != nil {
  45. log.Printf("Error setting localStorage %s=%s: %v", key, value, err)
  46. }
  47. }
  48. }
  49. utils.Sleep(1)
  50. if info.Area == "shaanxi" {
  51. //陕西地区必须带后面/才能登录上去
  52. p.Timeout(common.ClickTimeOut).MustNavigate(BaseUri + "/loginb/")
  53. } else {
  54. p.Timeout(common.ClickTimeOut).MustNavigate(BaseUri + "/loginb")
  55. }
  56. utils.Sleep(4)
  57. _ = rod.Try(func() {
  58. p.Timeout(common.LoadTimeout * 2).MustWaitStable()
  59. p.Timeout(common.LoadTimeout * 2).MustWaitDOMStable()
  60. })
  61. //p.MustStopLoading()
  62. logger.Info("加载完成")
  63. if info.Area == "hebei" && !common.MustHasXV(p, `//div[@class='leftMain']//div[contains(@class,'title')]`) {
  64. logger.Info("河北重新访问loginb")
  65. p.Timeout(common.ClickTimeOut).MustNavigate(`https://tpass.hebei.chinatax.gov.cn:8443/#/login?redirect_uri=https%3A%2F%2Fetax.hebei.chinatax.gov.cn%3A8443%2Fmhzx%2Fapi%2Fmh%2Ftpass%2Fcode&client_id=ap5bfdavp66a49979apa5848fpaa5353&response_type=code&state=8f8f3db7ecb948f08b1da177cda40854&client_pid=ap5bfdavp66a49979apa5848fpaa5353`)
  66. utils.Sleep(4)
  67. p.MustWaitStable()
  68. p.MustWaitDOMStable()
  69. }
  70. if common.MustHasXV(p, "//div[text()=' 关于电子税务局“新办智能开业”的通知 ']") {
  71. p.Timeout(common.ClickTimeOut).MustSearch("//span[text()='我知道了']").MustClick()
  72. utils.Sleep(1)
  73. common.MustElementXV(p, "//span[text()='确认']").MustClick()
  74. utils.Sleep(40) //要转很久
  75. p.Timeout(common.ClickTimeOut).MustNavigate(BaseUri + "/loginb")
  76. }
  77. if p.MustHasX(`//div[@class='leftMain']//div[contains(@class,'title')]`) {
  78. _ = p.Timeout(common.ClickTimeOut).MustElementX(`//div[@class='leftMain']//div[contains(@class,'title')]`).WaitStable(300 * time.Millisecond)
  79. if *p.Timeout(common.ClickTimeOut).MustElementX(`//div[@class='leftMain']//div[contains(@class,'title')]`).MustAttribute(`title`) == "" {
  80. logger.Info("公司名空刷新")
  81. p.Reload()
  82. utils.Sleep(4)
  83. p.MustWaitDOMStable()
  84. }
  85. }
  86. if common.MustHasXV(p, `//div[@class='leftMain']//div[contains(@class,'title')]`) {
  87. name = *common.WaitElementX(p, `//div[@class='leftMain']//div[contains(@class,'title')]`, 0.5).MustAttribute(`title`)
  88. if name == "" {
  89. err = taxerr.New("代理登录切换公司后异常公司名")
  90. return
  91. }
  92. return
  93. }
  94. DlSy := false //登录完是代理登录的首页
  95. if common.MustHasXV(p, `//a[@title="单户办理"]`) {
  96. common.MustElementX(p, `//a[@title="单户办理"]`).MustClick()
  97. DlSy = true
  98. utils.Sleep(3)
  99. }
  100. if !strings.Contains(p.MustInfo().URL, "tpass") {
  101. panic(taxerr.New("cookie失效"))
  102. }
  103. if !p.MustHasR(`[class="s_Breadcrumb"]`, "企业办税") {
  104. panic(taxerr.New("cookie失效"))
  105. }
  106. if !DlSy {
  107. r := p.MustWaitRequestIdle()
  108. p.Timeout(common.ClickTimeOut).MustSearch("返回").MustClick()
  109. r()
  110. } else {
  111. //这里需要手动切换到首页
  112. common.MustInputX(p, `//input[@placeholder="请输入统一社会信用代码"]`, info.TaxNo)
  113. common.MustElementX(p, `//span[text()="查询"]`).MustClick()
  114. utils.Sleep(2)
  115. tbody := common.MustElementsX(p, `//div[@class="el-table__body-wrapper is-scrolling-none"]//table//tr`)
  116. if len(tbody) == 0 {
  117. common.MustInputX(p, `//input[@placeholder="请输入统一社会信用代码"]`, "")
  118. p.Timeout(common.ClickTimeOut).MustElementX(`//input[@placeholder="请输入纳税人名称"]`).MustInput(info.ComName)
  119. common.MustElementX(p, `//span[text()="查询"]`).MustClick()
  120. utils.Sleep(2)
  121. tbody = common.MustElementsX(p, `//div[@class="el-table__body-wrapper is-scrolling-none"]//table//tr`)
  122. if len(tbody) == 0 {
  123. panic(taxerr.NewUserV3("未在当前账号下["+info.Zzridno+"]找到该企业", "请检查手机号与企业的关联关系"))
  124. }
  125. panic(taxerr.NewUserV3("该公司账号跟公司名称不匹配", "请检查后重新发起"))
  126. } else if len(tbody) == 1 {
  127. common.MustElementX(p, `//span[text()='进入']`).MustClick()
  128. if common.MustHasXV(p, `//span[text()="身份切换"]/../../..//span[contains(text(),"确定")]`) {
  129. common.MustElementX(p, `//span[text()="身份切换"]/../../..//span[contains(text(),"确定")]`).MustClick()
  130. time.Sleep(5 * time.Second)
  131. }
  132. } else {
  133. panic(taxerr.NewSystemV3("未在当前税号下["+info.TaxNo+"]找到多家企业", "请联系运维处理"))
  134. }
  135. }
  136. utils.Sleep(2)
  137. p.MustWaitDOMStable()
  138. name = *common.WaitElementX(p, `//div[@class='leftMain']//div[contains(@class,'title')]`, 0.5).MustAttribute(`title`)
  139. })
  140. // 这是panic的失效
  141. if err != nil {
  142. rod.Try(func() {
  143. patjh := common.GeneratePath("png")
  144. p.MustScreenshot(patjh)
  145. logger.Info("代理登录切换公司后异常公司名或判定为失效截图:" + patjh)
  146. })
  147. logger.Info(err.Error())
  148. return "", err
  149. }
  150. if (err != nil) && name == "" {
  151. logger.Info("cookie失效,当前url:" + p.MustInfo().URL)
  152. return "", taxerr.NewSystemV3("税局加载异常", "系统将于30分钟后重试")
  153. }
  154. return
  155. }
  156. func LoadCookieFast(sessionInfos models.SsoSession, p *rod.Page, BaseUri string, info models.CompanyInfo) (name string, err error) {
  157. err = rod.Try(func() {
  158. defer AddErr()
  159. p.Navigate("")
  160. p.Timeout(common.ClickTimeOut).MustNavigate(areaURLmap[info.Area+".pro"]).MustStopLoading() // 此时必须得去tpass域名 要不然写入cookie后进不去发票
  161. p.MustSetCookies(sessionInfos.Cookies...)
  162. {
  163. p.MustEval(`() => localStorage.clear()`)
  164. //utils.Sleep(1)
  165. // 设置 localStorage 在第二个页面中,逐条设置
  166. logger.Info("Setting localStorage in second browser...")
  167. for key, value := range sessionInfos.LocalStorage {
  168. if key == "logConfig" {
  169. continue
  170. }
  171. script := fmt.Sprintf(`() => localStorage.setItem('%s', '%s')`, key, value)
  172. _, err := p.Eval(script)
  173. if err != nil {
  174. log.Printf("Error setting localStorage %s=%s: %v", key, value, err)
  175. }
  176. }
  177. }
  178. utils.Sleep(0.5)
  179. if info.Area == "shaanxi" {
  180. //陕西地区必须带后面/才能登录上去
  181. p.Timeout(common.ClickTimeOut).MustNavigate(BaseUri + "/loginb/")
  182. } else {
  183. p.Timeout(common.ClickTimeOut).MustNavigate(BaseUri + "/loginb")
  184. }
  185. utils.Sleep(1)
  186. _ = rod.Try(func() {
  187. p.Timeout(2 * time.Second).MustWaitStable()
  188. p.Timeout(2 * time.Second).MustWaitDOMStable()
  189. })
  190. //p.MustStopLoading()
  191. logger.Info("加载完成")
  192. if info.Area == "hebei" && !common.MustHasXV(p, `//div[@class='leftMain']//div[contains(@class,'title')]`) {
  193. logger.Info("河北重新访问loginb")
  194. p.Timeout(common.ClickTimeOut).MustNavigate(`https://tpass.hebei.chinatax.gov.cn:8443/#/login?redirect_uri=https%3A%2F%2Fetax.hebei.chinatax.gov.cn%3A8443%2Fmhzx%2Fapi%2Fmh%2Ftpass%2Fcode&client_id=ap5bfdavp66a49979apa5848fpaa5353&response_type=code&state=8f8f3db7ecb948f08b1da177cda40854&client_pid=ap5bfdavp66a49979apa5848fpaa5353`)
  195. utils.Sleep(4)
  196. p.MustWaitStable()
  197. p.MustWaitDOMStable()
  198. }
  199. if common.MustHasXV(p, "//div[text()=' 关于电子税务局“新办智能开业”的通知 ']") {
  200. p.Timeout(common.ClickTimeOut).MustSearch("//span[text()='我知道了']").MustClick()
  201. utils.Sleep(1)
  202. common.MustElementXV(p, "//span[text()='确认']").MustClick()
  203. utils.Sleep(40) //要转很久
  204. p.Timeout(common.ClickTimeOut).MustNavigate(BaseUri + "/loginb")
  205. }
  206. if p.MustHasX(`//div[@class='leftMain']//div[contains(@class,'title')]`) {
  207. _ = p.Timeout(common.ClickTimeOut).MustElementX(`//div[@class='leftMain']//div[contains(@class,'title')]`).WaitStable(300 * time.Millisecond)
  208. if *p.Timeout(common.ClickTimeOut).MustElementX(`//div[@class='leftMain']//div[contains(@class,'title')]`).MustAttribute(`title`) == "" {
  209. logger.Info("公司名空刷新")
  210. p.Reload()
  211. utils.Sleep(4)
  212. p.MustWaitDOMStable()
  213. }
  214. }
  215. if common.MustHasXV(p, `//div[@class='leftMain']//div[contains(@class,'title')]`) {
  216. name = *common.WaitElementX(p, `//div[@class='leftMain']//div[contains(@class,'title')]`, 0.5).MustAttribute(`title`)
  217. if name == "" {
  218. err = taxerr.New("代理登录切换公司后异常公司名")
  219. return
  220. }
  221. return
  222. }
  223. DlSy := false //登录完是代理登录的首页
  224. if common.MustHasXV(p, `//a[@title="单户办理"]`) {
  225. common.MustElementX(p, `//a[@title="单户办理"]`).MustClick()
  226. DlSy = true
  227. utils.Sleep(3)
  228. }
  229. if !strings.Contains(p.MustInfo().URL, "tpass") {
  230. panic(taxerr.New("cookie失效"))
  231. }
  232. if !p.MustHasR(`[class="s_Breadcrumb"]`, "企业办税") {
  233. panic(taxerr.New("cookie失效"))
  234. }
  235. if !DlSy {
  236. r := p.MustWaitRequestIdle()
  237. p.Timeout(common.ClickTimeOut).MustSearch("返回").MustClick()
  238. r()
  239. } else {
  240. //这里需要手动切换到首页
  241. common.MustInputX(p, `//input[@placeholder="请输入统一社会信用代码"]`, info.TaxNo)
  242. common.MustElementX(p, `//span[text()="查询"]`).MustClick()
  243. utils.Sleep(2)
  244. tbody := common.MustElementsX(p, `//div[@class="el-table__body-wrapper is-scrolling-none"]//table//tr`)
  245. if len(tbody) == 0 {
  246. common.MustInputX(p, `//input[@placeholder="请输入统一社会信用代码"]`, "")
  247. p.Timeout(common.ClickTimeOut).MustElementX(`//input[@placeholder="请输入纳税人名称"]`).MustInput(info.ComName)
  248. common.MustElementX(p, `//span[text()="查询"]`).MustClick()
  249. utils.Sleep(2)
  250. tbody = common.MustElementsX(p, `//div[@class="el-table__body-wrapper is-scrolling-none"]//table//tr`)
  251. if len(tbody) == 0 {
  252. panic(taxerr.NewUserV3("未在当前账号下["+info.Zzridno+"]找到该企业", "请检查手机号与企业的关联关系"))
  253. }
  254. panic(taxerr.NewUserV3("该公司账号跟公司名称不匹配", "请检查后重新发起"))
  255. } else if len(tbody) == 1 {
  256. common.MustElementX(p, `//span[text()='进入']`).MustClick()
  257. if common.MustHasXV(p, `//span[text()="身份切换"]/../../..//span[contains(text(),"确定")]`) {
  258. common.MustElementX(p, `//span[text()="身份切换"]/../../..//span[contains(text(),"确定")]`).MustClick()
  259. time.Sleep(5 * time.Second)
  260. }
  261. } else {
  262. panic(taxerr.NewSystemV3("未在当前税号下["+info.TaxNo+"]找到多家企业", "请联系运维处理"))
  263. }
  264. }
  265. utils.Sleep(0.5)
  266. p.MustWaitDOMStable()
  267. name = *common.WaitElementX(p, `//div[@class='leftMain']//div[contains(@class,'title')]`, 0.5).MustAttribute(`title`)
  268. logger.Info(name)
  269. })
  270. // 这是panic的失效
  271. if err != nil {
  272. rod.Try(func() {
  273. patjh := common.GeneratePath("png")
  274. p.MustScreenshot(patjh)
  275. logger.Info("代理登录切换公司后异常公司名或判定为失效截图:" + patjh)
  276. })
  277. logger.Info(err.Error())
  278. return "", err
  279. }
  280. if (err != nil) && name == "" {
  281. logger.Info("cookie失效,当前url:" + p.MustInfo().URL)
  282. return "", taxerr.NewSystemV3("税局加载异常", "系统将于30分钟后重试")
  283. }
  284. if name == "" {
  285. logger.Info("公司名加载异常,当前url:" + p.MustInfo().URL)
  286. path := common.GeneratePath("png")
  287. p.MustScreenshot(path)
  288. logger.Info(path)
  289. }
  290. return
  291. }
  292. func AddErr() {
  293. if err := recover(); err != nil {
  294. e := err.(error)
  295. if _, ok := err.(*taxerr.UserErr); ok {
  296. err = taxerr.NewUser(e.Error() + "(代理登录失败)")
  297. }
  298. if _, ok := err.(*taxerr.SystemErr); ok {
  299. err = taxerr.New(e.Error() + "(代理登录失败)")
  300. }
  301. panic(err)
  302. }
  303. }
  304. func AutoCheckSessionAndLogin(ctx context.Context, p *rod.Page, BaseUri string, info models.CompanyInfo) (err error) {
  305. if info.Area == "hubei" && info.Dlfs == "代理登录" && len(info.Tel) != 11 {
  306. return taxerr.NewUserV3("湖北代理登录需要接受验证码", "请在登录信息中修改为手机号")
  307. }
  308. if info.Zzrmm == "" {
  309. return taxerr.PasswdEmpty
  310. }
  311. var LoginResponse []byte
  312. var Session models.SsoSession
  313. // 直接登录获取cookie
  314. {
  315. logger.Info("开始代理登陆获取cookie")
  316. LoginData := MakeLoginData(ctx, info)
  317. a, _ := json.Marshal(LoginData)
  318. logger.Info("代理登录加密前参数" + string(a))
  319. R := AesECBEncrypt(LoginData)
  320. headder := map[string]string{
  321. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36",
  322. "content-type": "application/json",
  323. "x-token": XToken,
  324. }
  325. marshal, _ := json.Marshal(LoginData)
  326. logger.Info(fmt.Sprintf("PostLoin参数%s" + string(marshal)))
  327. LoginResponse, err = PostLoin(R, headder, info)
  328. logger.Info("代理登陆获取cookie成功")
  329. if err != nil {
  330. if info.Dlfs == "代理登录" {
  331. e := err.(error)
  332. if _, ok := err.(*taxerr.UserErr); ok {
  333. err = taxerr.NewUser(e.Error() + "(代理登录失败)")
  334. }
  335. if _, ok := err.(*taxerr.SystemErr); ok {
  336. err = taxerr.New(e.Error() + "(代理登录失败)")
  337. }
  338. }
  339. return err
  340. }
  341. }
  342. // 整理cookie
  343. {
  344. gdata := gjson.GetBytes(LoginResponse, "data")
  345. cookies := []*proto.NetworkCookieParam{}
  346. domain := ""
  347. gdata.Get("cookies").ForEach(func(key, value gjson.Result) bool {
  348. if key.String() == "lzkqow38189" {
  349. domain = "dppt." + info.Area + ".chinatax.gov.cn"
  350. } else if key.String() == "token" {
  351. domain = "tpass." + info.Area + ".chinatax.gov.cn"
  352. } else {
  353. domain = ".chinatax.gov.cn"
  354. }
  355. cookies = append(cookies, &proto.NetworkCookieParam{
  356. Name: key.String(),
  357. Value: value.String(),
  358. Domain: domain,
  359. Path: "/",
  360. })
  361. return true
  362. })
  363. localStorage := map[string]string{}
  364. gdata.Get("localStorage").ForEach(func(key, value gjson.Result) bool {
  365. localStorage[key.String()] = value.String()
  366. return true
  367. })
  368. Session.Cookies = cookies
  369. Session.LocalStorage = localStorage
  370. //dpptURL = gdata.Get("redirectUri").String()
  371. }
  372. //if ctxValue := ctx.Value("website"); ctxValue != nil { //只有发票任务会传website
  373. // // 写入浏览器
  374. // name, err := LoadCookieFast(Session, p, BaseUri, info)
  375. // if err != nil {
  376. // fmt.Println(err.Error())
  377. // return err
  378. // }
  379. // if name != info.ComName {
  380. // return taxerr.CompanyNameError(name)
  381. // }
  382. //} else {
  383. // 写入浏览器
  384. name, err := LoadCookie(Session, p, BaseUri, info)
  385. if err != nil {
  386. return err
  387. }
  388. if name != info.ComName {
  389. return taxerr.CompanyNameError(name)
  390. }
  391. //}
  392. return
  393. }
  394. func AesECBEncrypt(data map[string]any) []byte {
  395. jsonData, _ := json.Marshal(data)
  396. fmt.Println(string(jsonData))
  397. padded := make([]byte, 16)
  398. copy(padded, key)
  399. encryptData, err := openssl.AesECBEncrypt(jsonData, []byte(padded), openssl.PKCS5_PADDING)
  400. if err != nil {
  401. return []byte("")
  402. }
  403. jsonData, _ = json.Marshal(map[string]string{"data": base64.StdEncoding.EncodeToString(encryptData)})
  404. return jsonData
  405. }