Skip to content

Commit

Permalink
fix: 实现跳转小程序、获取启动参数、菜单位置API
Browse files Browse the repository at this point in the history
  • Loading branch information
guoenxuan committed Sep 8, 2023
1 parent fe1005e commit d9bc3d0
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 20 deletions.
4 changes: 2 additions & 2 deletions examples/mini-program-example/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { TestConsole } from '@/util/util'
import './app.scss'

class App extends React.Component {
onLaunch() {
TestConsole.consoleNormal('onLaunch')
onLaunch(res) {
TestConsole.consoleNormal('onLaunch', res)
}

onError(error) {
Expand Down
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'

/**
Expand All @@ -13,11 +14,19 @@ export default class Index extends React.Component {
list: [
{
id: 'getLaunchOptionsSync',
func: null,
func: () => {
TestConsole.consoleTest('Taro.getLaunchOptionsSync')
const options = Taro.getLaunchOptionsSync()
TestConsole.consoleNormal('getLaunchOptionsSync', options)
},
},
{
id: 'getEnterOptionsSync',
func: null,
func: () => {
TestConsole.consoleTest('Taro.getEnterOptionsSync')
const options = Taro.getEnterOptionsSync()
TestConsole.consoleNormal('getEnterOptionsSync', options)
},
},
],
}
Expand Down
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 @@ -14,7 +15,11 @@ export default class Index extends React.Component {
list: [
{
id: 'getMenuButtonBoundingClientRect',
func: null,
func: () => {
TestConsole.consoleTest('Taro.getMenuButtonBoundingClientRect')
const rect = Taro.getMenuButtonBoundingClientRect()
TestConsole.consoleNormal('getMenuButtonBoundingClientRect', rect)
},
},
],
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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 Down
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 { TestConsole } from '@/util/util'
import ButtonList from '@/components/buttonList'
import './index.scss'

Expand All @@ -22,7 +23,26 @@ export default class Index extends React.Component {
},
{
id: 'navigateToMiniProgram',
func: null,
inputData: {
appId: 'com.example.myapplication',
path: 'EntryAbility',
extraData: {},
},
func: (apiIndex, data) => {
TestConsole.consoleTest('Taro.navigateToMiniProgram')
Taro.navigateToMiniProgram({
...data,
success: (res) => {
TestConsole.consoleSuccess.call(this, res, apiIndex)
},
fail: (res) => {
TestConsole.consoleFail.call(this, res, apiIndex)
},
complete: (res) => {
TestConsole.consoleComplete.call(this, res, apiIndex)
},
})
},
},
{
id: 'navigateBackMiniProgram',
Expand Down
33 changes: 26 additions & 7 deletions packages/taro-mpharmony/src/api/base/weapp/life-cycle.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
import Taro from '@tarojs/api'

const launchOptions: Taro.getLaunchOptionsSync.LaunchOptions = {
path: '',
query: {},
scene: 0,
shareTicket: '',
referrerInfo: {},
function getReferrerInfo () {
let referrerInfo
try {
// @ts-ignore
const callerBundle = bundleMap.get('callerBundle')
// @ts-ignore
const callerParams = bundleMap.get('callerParams')

referrerInfo = {
referrerInfo: {
appId: callerBundle || '',
extraData: JSON.parse(callerParams),
},
}
} catch (err) {
referrerInfo = {
referrerInfo: {
appId: '',
extraData: {},
},
}
}
return referrerInfo
}

let launchOptions
function initLaunchOptions (options = {}) {
Object.assign(launchOptions, options)
Object.assign(options, getReferrerInfo())
launchOptions = options
}

Taro.eventCenter.once('__taroRouterLaunch', initLaunchOptions)
Expand Down
28 changes: 26 additions & 2 deletions packages/taro-mpharmony/src/api/navigate/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
import { temporarilyNotSupport } from '../../utils'
import Taro from '@tarojs/api'
import { MethodHandler } from 'src/utils/handler'

import { shouldBeObject, temporarilyNotSupport } from '../../utils'

// 跳转
export const openEmbeddedMiniProgram = /* @__PURE__ */ temporarilyNotSupport('openEmbeddedMiniProgram')
export const navigateToMiniProgram = /* @__PURE__ */ temporarilyNotSupport('navigateToMiniProgram')
export const navigateBackMiniProgram = /* @__PURE__ */ temporarilyNotSupport('navigateBackMiniProgram')
export const exitMiniProgram = /* @__PURE__ */ temporarilyNotSupport('exitMiniProgram')
export const openBusinessView = /* @__PURE__ */ temporarilyNotSupport('openBusinessView')

export const navigateToMiniProgram: typeof Taro.navigateToMiniProgram = (options) => {
const apiName = 'navigateToMiniProgram'
// options must be an Object
const isObject = shouldBeObject(options)
if (!isObject.flag) {
const res = { errMsg: `${apiName}:fail ${isObject.msg}` }
return Promise.reject(res)
}
const { success, fail, complete, ...otherOptions } = options as Exclude<typeof options, undefined>
const handle = new MethodHandler({ name: apiName, success, fail, complete })

// @ts-ignore
return native.navigateToMiniProgram(otherOptions).then(
(res: any) => {
handle.success(res)
},
(res: any) => {
handle.fail(res)
}
)
}
16 changes: 14 additions & 2 deletions packages/taro-mpharmony/src/api/ui/menu.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
import { temporarilyNotSupport } from '../../utils'
import Taro from '@tarojs/api'

// 菜单
export const getMenuButtonBoundingClientRect = /* @__PURE__ */ temporarilyNotSupport('getMenuButtonBoundingClientRect')
export const getMenuButtonBoundingClientRect: typeof Taro.getMenuButtonBoundingClientRect = () => {
// @ts-ignore
const data = native.getMenuButtonBoundingClientRect()
const rect = JSON.parse(JSON.stringify(data))
return {
left: rect.left,
top: rect.top,
right: rect.right,
bottom: rect.bottom,
height: rect.height,
width: rect.width,
}
}

0 comments on commit d9bc3d0

Please sign in to comment.