Skip to content

Commit 2762cad

Browse files
committed
实现联通cookie获取
1 parent d27790c commit 2762cad

File tree

2 files changed

+114
-72
lines changed

2 files changed

+114
-72
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
import './scripts/helloWorld.tsx'
1+
import './scripts/china10010.tsx'

src/scripts/china10010.tsx

Lines changed: 113 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
showPreviewOptions,
1212
useStorage,
1313
request,
14+
sleep,
1415
} from '@app/lib/help'
1516
import {FC} from 'react'
1617

@@ -65,7 +66,7 @@ interface PageInfo {
6566
cookie: string | null
6667

6768
/**js执行的错误信息*/
68-
err: Error
69+
err: string
6970
}
7071

7172
/**有用的手机卡数据*/
@@ -121,6 +122,19 @@ class China10010 {
121122
// 渲染尺寸
122123
const size = config.widgetFamily
123124

125+
// 获取数据
126+
const usefulPhoneDatas = await this.getUserfulPhoneData(getStorage<string>('phoneNumber') || '')
127+
128+
if (typeof usefulPhoneDatas === 'string') {
129+
return (
130+
<wbox>
131+
<wtext textAlign="center" font={20}>
132+
{usefulPhoneDatas}
133+
</wtext>
134+
</wbox>
135+
)
136+
}
137+
console.log(usefulPhoneDatas)
124138
return (
125139
<wbox
126140
padding={[0, 0, 0, 0]}
@@ -146,62 +160,37 @@ class China10010 {
146160
<wspacer></wspacer>
147161
<wspacer></wspacer>
148162
{/* 内容 */}
149-
{size === 'small' && this.renderSmall(articleList)}
150-
{size === 'medium' && this.renderMedium(articleList)}
151-
{size === 'large' && this.renderLarge(articleList)}
163+
{size === 'small' && this.renderSmall(usefulPhoneDatas)}
164+
{size === 'medium' && this.renderMedium(usefulPhoneDatas)}
165+
{size === 'large' && this.renderLarge(usefulPhoneDatas)}
152166
</wstack>
153167
</wbox>
154168
)
155169
}
156170

157171
// 渲染小尺寸
158-
renderSmall(articleList: ArticleInfo[]) {
159-
const article = articleList[0]
172+
renderSmall(usefulPhoneDatas: UsefulPhoneData[]) {
160173
return (
161-
<wstack flexDirection="column">
162-
<wtext font={Font.lightSystemFont(14)} textColor={textColor}>
163-
{article.title}
164-
</wtext>
165-
<wspacer></wspacer>
166-
<wstack>
167-
<wspacer></wspacer>
168-
<wtext maxLine={1} font={Font.lightSystemFont(12)} opacity={0.6} textColor={textColor}>
169-
{article.hot}
170-
</wtext>
171-
</wstack>
172-
<wspacer></wspacer>
173-
</wstack>
174+
<>
175+
<wtext>hello</wtext>
176+
</>
174177
)
175178
}
176179

177180
// 渲染中尺寸
178-
renderMedium(articleList: ArticleInfo[]) {
179-
const _articleList = articleList.slice(0, 4)
181+
renderMedium(usefulPhoneDatas: UsefulPhoneData[]) {
180182
return (
181183
<>
182-
<wstack flexDirection="column">
183-
{_articleList.map((article, index) => (
184-
<Article article={article} sort={index + 1}></Article>
185-
))}
186-
</wstack>
187-
<wspacer></wspacer>
188-
<wspacer></wspacer>
184+
<wtext>hello</wtext>
189185
</>
190186
)
191187
}
192188

193189
// 渲染大尺寸
194-
renderLarge(articleList: ArticleInfo[]) {
195-
const _articleList = articleList.slice(0, 10)
190+
renderLarge(usefulPhoneDatas: UsefulPhoneData[]) {
196191
return (
197192
<>
198-
<wstack flexDirection="column">
199-
{_articleList.map((article, index) => (
200-
<Article article={article} sort={index + 1}></Article>
201-
))}
202-
</wstack>
203-
<wspacer></wspacer>
204-
<wspacer></wspacer>
193+
<wtext>hello</wtext>
205194
</>
206195
)
207196
}
@@ -210,21 +199,85 @@ class China10010 {
210199
async showMenu() {
211200
const selectIndex = await showActionSheet({
212201
title: '菜单',
213-
itemList: ['登录', '设置手机号', '设置颜色', '设置透明背景', '预览组件'],
202+
itemList: ['登录', '设置手机号和cookie', '设置颜色', '设置透明背景', '预览组件'],
214203
})
215204
switch (selectIndex) {
216205
case 0:
217206
const {cancel: cancelLogin} = await showModal({
218207
title: '为什么要登录',
219208
content:
220-
'获取手机号码信息需要 cookie,而 cookie 不登录获取不到\n\n若 cookie 失效,再次登录即可\n\n登录完成后,自行关闭网页',
209+
'获取手机号码信息需要 cookie,而 cookie 不登录获取不到\n\n若 cookie 失效,再次登录即可\n\n登录完成后,关闭网页,网页会再自动打开\n\n此时点击底部按钮复制 cookie ,然后关网页去设置cookie',
221210
confirmText: '去登录',
222211
})
223212
if (cancelLogin) return
224-
const loginUrl = 'https://uac.10010.com/oauth2/new_auth?display=wap&page_type=05'
225-
await WebView.loadURL(loginUrl, undefined, true)
213+
const loginUrl = 'http://wap.10010.com/mobileService/myunicom.htm'
214+
const webview = new WebView()
215+
await webview.loadURL(loginUrl)
216+
await webview.present()
217+
218+
/**循环插入脚本等待时间,单位:毫秒*/
219+
const sleepTime = 1000
220+
221+
/**循环时间统计,单位:毫秒*/
222+
let waitTimeCount = 0
223+
224+
/**最大循环时间,单位:毫秒*/
225+
const maxWaitTime = 10 * 60 * sleepTime
226+
227+
while (true) {
228+
if (waitTimeCount >= maxWaitTime) break
229+
const {isAddCookieBtn} = (await webview.evaluateJavaScript(`
230+
window.isAddCookieBtn = false
231+
if (document.cookie.match('jsessionid')) {
232+
const copyWrap = document.createElement('div')
233+
copyWrap.innerHTML = \`
234+
<div style="position: fixed; bottom: 0; left: 0; z-index: 999999; width: 100vw; height: 10vh; text-align: center; line-height: 10vh; background: #000000; color: #ffffff; font-size: 16px;" id="copy-btn">复制cookie</div>
235+
\`
236+
function copy(text) {
237+
var input = document.createElement('input');
238+
input.setAttribute('value', text);
239+
document.body.appendChild(input);
240+
input.select();
241+
var result = document.execCommand('copy');
242+
document.body.removeChild(input);
243+
return result;
244+
}
245+
document.body.appendChild(copyWrap)
246+
const copyBtn = document.querySelector('#copy-btn')
247+
copyBtn.onclick = () => {
248+
copy(document.cookie)
249+
copyBtn.innerText = '复制成功'
250+
copyBtn.style.background = 'green'
251+
}
252+
window.isAddCookieBtn = true
253+
}
254+
Object.assign({}, {isAddCookieBtn: window.isAddCookieBtn})
255+
`)) as {isAddCookieBtn: boolean}
256+
if (isAddCookieBtn) break
257+
await sleep(sleepTime)
258+
waitTimeCount += sleepTime
259+
}
260+
await webview.present()
226261
break
227262
case 1:
263+
const {texts: phoneInfo, cancel: phoneInfoCancel} = await showModal({
264+
title: '设置手机号和cookie',
265+
content: '请务必先登录,在登录处复制好 cookie 再来,不懂就仔细看登录说明',
266+
inputItems: [
267+
{
268+
text: getStorage<string>('phoneNumber') || '',
269+
placeholder: '这里填你的手机号',
270+
},
271+
{
272+
text: getStorage<string>('cookie') || '',
273+
placeholder: '这里填cookie',
274+
},
275+
],
276+
})
277+
if (phoneInfoCancel) return
278+
setStorage('phoneNumber', phoneInfo[0])
279+
setStorage('cookie', phoneInfo[1])
280+
await showNotification({title: '设置完成', sound: 'default'})
228281
break
229282
case 2:
230283
const {texts, cancel} = await showModal({
@@ -261,38 +314,26 @@ class China10010 {
261314
}
262315

263316
// 获取手机卡数据
264-
async getPhoneData(phoneNumber: number): Promise<UsefulPhoneData[] | string> {
265-
if (!isLaunchInsideApp() && !getStorage('cookie')) {
266-
await showNotification({title: 'cookie 不存在,请先登录', sound: 'failure'})
267-
return 'cookie 不存在,请先登录'
268-
}
317+
async getUserfulPhoneData(phoneNumber: string): Promise<UsefulPhoneData[] | string> {
318+
if (!phoneNumber) return '请设置手机号'
319+
if (!isLaunchInsideApp() && !getStorage('cookie')) return 'cookie 不存在,请先登录'
269320
const api = `https://wap.10010.com/mobileService/home/queryUserInfoSeven.htm?version=iphone_c@7.0403&desmobiel=${phoneNumber}&showType=3`
270-
const getPageInfo = async (): Promise<PageInfo> => {
271-
const webview = new WebView()
272-
await webview.loadURL(api)
273-
await webview.waitForLoad()
274-
const setCookie = `document.cookie = "${getStorage('cookie') || ''}"`
275-
const {phoneDatas, cookie, err} = (await webview.evaluateJavaScript(`
276-
let cookie = document.cookie
277-
let phoneDatas = {}
278-
let err = ''
279-
${isLaunchInsideApp() ? '' : setCookie}
280-
try {
281-
phoneDatas = JSON.parse(document.body.innerText)
282-
} catch (error) {
283-
err = error
284-
}
285-
Object.assign({}, {phoneDatas, cookie, err})
286-
`)) as PageInfo
287-
err && console.warn('获取数据出错', err)
288-
return {phoneDatas, cookie, err}
289-
}
290-
const {phoneDatas, cookie} = await getPageInfo()
291-
isLaunchInsideApp() && cookie && setStorage('cookie', cookie)
292-
let usefulPhoneData: UsefulPhoneData[] = []
321+
// 获取手机卡信息列表
322+
const res = await request<PhoneDatas>({
323+
url: api,
324+
dataType: 'json',
325+
header: {
326+
'user-agent':
327+
'Mozilla/5.0 (iPhone; CPU iPhone OS 14_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1',
328+
cookie: getStorage<string>('cookie') || '',
329+
},
330+
})
331+
// isLaunchInsideApp() && cookie && setStorage('cookie', cookie)
332+
let usefulPhoneDatas: UsefulPhoneData[] = []
293333
try {
334+
const phoneDatas: PhoneData[] = res.data?.data.dataList || []
294335
// 提取有用的信息
295-
usefulPhoneData = phoneDatas.data.dataList.map(info => {
336+
usefulPhoneDatas = phoneDatas.map(info => {
296337
const present = info.usedTitle.replace(/(|)([\d\.]+)?\%/, (...args) => {
297338
return args[1] === '剩余' ? args[2] : 100 - args[2]
298339
})
@@ -305,10 +346,11 @@ class China10010 {
305346
}
306347
})
307348
} catch (err) {
349+
console.warn(`获取联通卡信息失败: ${err}`)
308350
await showNotification({title: '获取联通卡信息失败', body: '检查一下网络,或重新登录', sound: 'failure'})
309351
return '获取联通卡信息失败\n检查一下网络,或重新登录'
310352
}
311-
return usefulPhoneData
353+
return usefulPhoneDatas
312354
}
313355
// // 获取手机卡数据
314356
// async getPhoneData(phoneNumber: number): Promise<UsefulPhoneData[] | string> {

0 commit comments

Comments
 (0)