You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.4 KiB
67 lines
1.4 KiB
4 years ago
|
package cmd
|
||
|
|
||
|
import (
|
||
|
"github.com/spf13/cobra"
|
||
|
"github.com/ztino/jd_seckill/common"
|
||
|
"github.com/ztino/jd_seckill/jd_seckill"
|
||
|
"log"
|
||
|
"os"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func init() {
|
||
|
rootCmd.AddCommand(loginCmd)
|
||
|
}
|
||
|
|
||
|
var loginCmd = &cobra.Command{
|
||
|
Use: "login",
|
||
|
Short: "Open JD’s simulated login",
|
||
|
Run: startLogin,
|
||
|
}
|
||
|
|
||
|
func startLogin(cmd *cobra.Command, args []string) {
|
||
|
session:=jd_seckill.NewSession(common.CookieJar)
|
||
|
//检测是否登录过
|
||
|
if common.Exists("./cookie.txt") {
|
||
|
//已登录,检测登录状态
|
||
|
err:=session.CheckLoginStatus()
|
||
|
if err!=nil {
|
||
|
log.Println("登录失效,请重新登录")
|
||
|
return
|
||
|
}
|
||
|
user:=jd_seckill.NewUser(common.Client,common.Config)
|
||
|
log.Println("登录成功")
|
||
|
userInfo,_:=user.GetUserInfo()
|
||
|
log.Println("用户:"+userInfo)
|
||
|
}else{
|
||
|
//未登录
|
||
|
user:=jd_seckill.NewUser(common.Client,common.Config)
|
||
|
wlfstkSmdl,err:=user.QrLogin()
|
||
|
if err!=nil{
|
||
|
os.Exit(0)
|
||
|
}
|
||
|
ticket:=""
|
||
|
for {
|
||
|
ticket,err=user.QrcodeTicket(wlfstkSmdl)
|
||
|
if err==nil && ticket!=""{
|
||
|
break
|
||
|
}
|
||
|
time.Sleep(2*time.Second)
|
||
|
}
|
||
|
_,err=user.TicketInfo(ticket)
|
||
|
if err==nil {
|
||
|
if status:=user.RefreshStatus();status==nil {
|
||
|
//保存cookie
|
||
|
_=session.SaveCookieToFile("./cookie.txt")
|
||
|
log.Println("登录成功")
|
||
|
userInfo,_:=user.GetUserInfo()
|
||
|
log.Println("用户:"+userInfo)
|
||
|
}else{
|
||
|
log.Println("登录失效")
|
||
|
}
|
||
|
}else{
|
||
|
log.Println("登录失败")
|
||
|
}
|
||
|
}
|
||
|
}
|