async startAuth() {
const httpRequest = http.createHttp()
const tokenUrl =
`https://api.weixin.qq.com/cgi-bin/token?appid=${APP_ID}&secret=${APP_SECRET}&grant_type=client_credential`
try {
const data = await httpRequest.request(tokenUrl)
const tokenJson: Record<string, Object> = JSON.parse(data.result as string)
const token = tokenJson['access_token'] as string
console.info('onAuthError token: ' + token)
const ticketUrl = `https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=${token}&type=2`
const ticketData = await httpRequest.request(ticketUrl)
const ticketJson: Record<string, Object> = JSON.parse(ticketData.result as string)
const ticket = ticketJson['ticket'] as string
console.info('onAuthError ticket: ' + ticket)
const signature = await this.getSignature(APP_ID, ticket, 'noncestr', 'timestamp')
console.info('onAuthError signature: ' + signature)
const callback: wxopensdk.OAuthCallback = {
onGotQRCode: (base64JpegImageBuffer: string) => {
this.base64ImageBuffer = `data:image/jpeg;base64,${base64JpegImageBuffer}`
this.loadStatus = LoadingStatus.SCANNING
},
onQRCodeScanned: () => {
this.loadStatus = LoadingStatus.SCANNED
},
onAuthFinish: (authCode: string) => {
this.loadStatus = LoadingStatus.SUCCESS
this.wxCode = authCode
},
onAuthError: (errCode: wxopensdk.OAuthErrCode, errMsg: string) => {
console.info('onAuthError errCode:', errCode, 'errMsg:', errMsg)
this.loadStatus = LoadingStatus.FAILED
this.errStr = `errCode: ${errCode}, errMsg: ${errMsg}`
}
}
if (this.lastOAuth) {
this.lastOAuth.stopOAuth()
}
let oauth = wxopensdk.DiffDevOAuthFactory.getDiffDevOAuth()!
this.lastOAuth = oauth
let scope = "snsapi_login,snsapi_friend,snsapi_message,snsapi_contact"
oauth.startOAuth(APP_ID, scope, 'noncestr', 'timestamp', signature, callback)
} catch (err) {
console.error('request error:', err)
this.loadStatus = LoadingStatus.FAILED
this.errStr = ' 网络请求失败'
}
}