Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复login接口以及优化测试用例 #196

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'
import Taro from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
import { View } from '@tarojs/components'
import { TestConsole } from '@/util/util'
import ButtonList from '@/components/buttonList'
import './index.scss'

Expand All @@ -13,57 +14,28 @@ export default class Index extends React.Component {
state = {
list: [
{
id: 'login-code',
func: (apiIndex) => {
Taro.login({
// @ts-ignore
appid: '1208731172335528704',
success: function (res) {
Taro.showModal({ content: 'Taro.login code success ' + JSON.stringify(res) })
},
fail: (res) => {
console.log('login fail ', res)
},
complete: (res) => {
console.log('login complete ', res)
},
})
id: 'login',
inputData: {
timeout: 2000,
},
},
{
id: 'login-token',
func: (apiIndex) => {
func: (apiIndex, data) => {
Taro.login({
// @ts-ignore
appid: '1208731172335528704',
type: 'token',
success: function (res) {
Taro.showModal({ content: 'Taro.login token success ' + JSON.stringify(res) })
...data,
success: (res) => {
TestConsole.consoleSuccess.call(this, res, apiIndex)
},
fail: (res) => {
console.log('login fail ', res)
TestConsole.consoleFail.call(this, res, apiIndex)
},
complete: (res) => {
console.log('login complete ', res)
TestConsole.consoleComplete.call(this, res, apiIndex)
},
})
},
},
{
id: 'checkSession',
func: (apiIndex) => {
Taro.checkSession({
success: function (res) {
console.log('checkSession success ', res)
},
fail: function (res) {
console.log('checkSession fail ', res)
},
complete: (res) => {
console.log('checkSession complete ', res)
},
})
},
func: null,
},
],
}
Expand Down
42 changes: 26 additions & 16 deletions packages/taro-mpharmony/src/api/open-api/login/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,34 @@ export const login: typeof Taro.login = (options) => {
}

const { timeout, success, fail, complete } = options as Exclude<typeof options, undefined>

const handle = new MethodHandler({ name, success, fail, complete })

// @ts-ignore
const ret = native.authorize({
// @ts-ignore
appid: options.appid,
let loginPromise = new Promise((resolve, reject) => {
// @ts-ignore
type: options.type || 'code',
// @ts-ignore
scope: options.scope || 'scope.baseProfile',
timeout: timeout,
success: (res: any) => {
return handle.success(res)
},
fail: (err: any) => {
return handle.fail(err)
},
native.login({
success: (res: any) => {
resolve(res)
},
fail: (err: any) => {
reject(err)
},
})
})
return ret
if (typeof timeout === 'number' && timeout >= 0) {
const timeoutPromise = new Promise((_resolve, reject) => {
setTimeout(() => reject(new Error('timeout')), timeout)
})
loginPromise = Promise.race([loginPromise, timeoutPromise])
}
return loginPromise.then(
(res: any) => handle.success(res),
(err) => {
if (err instanceof Error) {
return handle.fail({ errMsg: err.message })
} else {
const { errMsg, errCode: code } = err
return handle.fail({ errMsg, code })
}
}
)
}