Skip to content

feat(uni-mp-weibo): 新增对微博小程序的支持 #4367

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

Open
wants to merge 4 commits into
base: next
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ module.exports = {
// Packages targeting DOM
{
files: [
'packages/{uni-api,uni-app,uni-components,uni-core,uni-h5,uni-h5-vue,uni-i18n,uni-shared,uni-vue,uni-app-plus}/**',
'packages/{uni-api,uni-app,uni-components,uni-core,uni-h5,uni-h5-vue,uni-i18n,uni-shared,uni-vue,uni-app-plus,uni-mp-weibo}/**',
],
rules: {
'no-restricted-globals': ['error', ...NodeGlobals],
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -20,7 +20,8 @@
"postinstall": "simple-git-hooks",
"e2e:ssr:dev": "cd packages/playground/ssr && npm run dev:ssr",
"e2e:ssr:open": "cd packages/playground/ssr && npx cypress open",
"release": "node scripts/release.js"
"release": "node scripts/release.js",
"build:weibo": "npm run build:h5 && node scripts/build.js uni-mp-weibo uni-mp-weibo-vite"
},
"types": "test-dts/index.d.ts",
"tsd": {
1 change: 1 addition & 0 deletions packages/shims-uni-app.d.ts
Original file line number Diff line number Diff line change
@@ -197,6 +197,7 @@ declare namespace UniNamespace {
'mp-kuaishou'?: PagesJsonPageStyle
'mp-lark'?: PagesJsonPageStyle
'mp-jd'?: PagesJsonPageStyle
'mp-weibo'?: PagesJsonPageStyle
'mp-xhs'?: PagesJsonPageStyle
'quickapp-webview'?: PagesJsonPageStyle
'quickapp-webview-huawei'?: PagesJsonPageStyle
10 changes: 10 additions & 0 deletions packages/uni-api/src/helpers/api/index.ts
Original file line number Diff line number Diff line change
@@ -172,6 +172,16 @@ function wrapperSyncApi<T extends ApiLike>(
options?: ApiOptions<T>
) {
return (...args: any[]) => {
try {
// @ts-ignore
if (window.weibo && window.weibo[name]) {
// @ts-ignore
window.currentWeiboApiName = name
// @ts-ignore
var value = window.weibo[name](args)
return value
}
} catch (e2) {}
const errMsg = beforeInvokeApi(name, args, protocol, options)
if (errMsg) {
throw new Error(errMsg)
12 changes: 12 additions & 0 deletions packages/uni-api/src/helpers/api/promise.ts
Original file line number Diff line number Diff line change
@@ -28,6 +28,18 @@ export function handlePromise(promise: Promise<unknown>) {

export function promisify(name: string, fn: Function) {
return (args = {}, ...rest: unknown[]) => {
try {
// @ts-ignore
if (__PLATFORM__ === 'mp-weibo' && window.weibo && window.weibo[name]) {
// @ts-ignore
window.currentWeiboApiName = name
// @ts-ignore
const value = window.weibo[name](args)
if (name !== 'switchTab') {
return value
}
}
} catch (e) {}
if (hasCallback(args)) {
return wrapperReturnValue(name, invokeApi(name, fn, args, rest))
}
12 changes: 10 additions & 2 deletions packages/uni-api/src/service/base/upx2px.ts
Original file line number Diff line number Diff line change
@@ -44,7 +44,11 @@ export const upx2px = defineSyncApi<API_TYPE_UPX2PX>(
}
if (deviceWidth === 0) {
checkDeviceWidth()
if (__PLATFORM__ === 'app' || __PLATFORM__ === 'h5') {
if (
__PLATFORM__ === 'app' ||
__PLATFORM__ === 'h5' ||
__PLATFORM__ === 'mp-weibo'
) {
checkMaxWidth()
}
}
@@ -54,7 +58,11 @@ export const upx2px = defineSyncApi<API_TYPE_UPX2PX>(
return 0
}
let width = newDeviceWidth || deviceWidth
if (__PLATFORM__ === 'app' || __PLATFORM__ === 'h5') {
if (
__PLATFORM__ === 'app' ||
__PLATFORM__ === 'h5' ||
__PLATFORM__ === 'mp-weibo'
) {
width = number === includeWidth || width <= maxWidth ? width : baseWidth
}
let result = (number / BASE_DEVICE_WIDTH) * width
2 changes: 1 addition & 1 deletion packages/uni-api/src/service/context/canvas.ts
Original file line number Diff line number Diff line change
@@ -442,7 +442,7 @@ export class CanvasContext implements UniApp.CanvasContext {
measureText(text: string) {
const font = this.state.font
let width = 0
if (__PLATFORM__ === 'h5') {
if (__PLATFORM__ === 'h5' || __PLATFORM__ === 'mp-weibo') {
width = measureText(text, font)
} else {
const webview = plus.webview
36 changes: 36 additions & 0 deletions packages/uni-api/src/service/lifecycle/app.ts
Original file line number Diff line number Diff line change
@@ -75,26 +75,62 @@ export function offPageNotFound(hook: UniApp.OnPageNotFoundCallback) {
}

export function onError(hook: UniApp.OnAppErrorCallback) {
// @ts-ignore
if (window.weibo) {
// @ts-ignore
window.weibo.onError(hook)
return
}
onAppHook(ON_ERROR, hook)
}

export function offError(hook: UniApp.OnAppErrorCallback) {
// @ts-ignore
if (window.weibo) {
// @ts-ignore
window.weibo.offError(hook)
return
}
offAppHook(ON_ERROR, hook)
}

export function onAppShow(hook: AppShowHook) {
// @ts-ignore
if (window.weibo) {
// @ts-ignore
window.weibo.onAppShow(hook)
return
}
onAppHook(ON_SHOW, hook)
}

export function offAppShow(hook: AppShowHook) {
// @ts-ignore
if (window.weibo) {
// @ts-ignore
window.weibo.offAppShow(hook)
return
}
offAppHook(ON_SHOW, hook)
}

export function onAppHide(hook: AppHideHook) {
// @ts-ignore
if (window.weibo) {
// @ts-ignore
window.weibo.onAppHide(hook)
return
}
onAppHook(ON_HIDE, hook)
}

export function offAppHide(hook: AppHideHook) {
// @ts-ignore
if (window.weibo) {
// @ts-ignore
window.weibo.offAppHide(hook)
return
}
offAppHook(ON_HIDE, hook)
}

2 changes: 1 addition & 1 deletion packages/uni-api/src/service/ui/locale.ts
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@ export const setLocale = defineSyncApi<typeof uni.setLocale>(
})
weex.requireModule('plus').setLanguage(locale)
}
if (__PLATFORM__ === 'h5') {
if (__PLATFORM__ === 'h5' || __PLATFORM__ === 'mp-weibo') {
navigator.cookieEnabled &&
window.localStorage &&
(localStorage[UNI_STORAGE_LOCALE] = locale)
3 changes: 2 additions & 1 deletion packages/uni-automator/package.json
Original file line number Diff line number Diff line change
@@ -22,7 +22,8 @@
"apply": [
"app",
"h5",
"mp-weixin"
"mp-weixin",
"mp-weibo"
],
"uvue": true
},
4 changes: 4 additions & 0 deletions packages/uni-cli-shared/src/constants.ts
Original file line number Diff line number Diff line change
@@ -42,6 +42,10 @@ export const H5_API_STYLE_PATH = '@dcloudio/uni-h5/style/api/'
export const H5_FRAMEWORK_STYLE_PATH = '@dcloudio/uni-h5/style/framework/'
export const H5_COMPONENTS_STYLE_PATH = '@dcloudio/uni-h5/style/'
export const BASE_COMPONENTS_STYLE_PATH = '@dcloudio/uni-components/style/'
export const MP_WEIBO_API_STYLE_PATH = '@dcloudio/uni-mp-weibo/style/api/'
export const MP_WEIBO_FRAMEWORK_STYLE_PATH =
'@dcloudio/uni-mp-weibo/style/framework/'
export const MP_WEIBO_COMPONENTS_STYLE_PATH = '@dcloudio/uni-mp-weibo/style/'

export const COMMON_EXCLUDE = [
/\/pages\.json\.js$/,
22 changes: 22 additions & 0 deletions packages/uni-cli-shared/src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
BASE_COMPONENTS_STYLE_PATH,
H5_COMPONENTS_STYLE_PATH,
MP_WEIBO_COMPONENTS_STYLE_PATH,
H5_API_STYLE_PATH,
MP_WEIBO_API_STYLE_PATH,
} from './constants'

const RESIZE_SENSOR_CSS = BASE_COMPONENTS_STYLE_PATH + 'resize-sensor.css'
@@ -41,3 +43,23 @@ export const COMPONENT_DEPS_CSS = {
`${BASE_COMPONENTS_STYLE_PATH}picker-view-column.css`,
],
}

export const WEIBO_API_DEPS_CSS = {
showModal: [`${MP_WEIBO_API_STYLE_PATH}modal.css`],
showToast: [`${MP_WEIBO_API_STYLE_PATH}toast.css`],
showActionSheet: [`${MP_WEIBO_API_STYLE_PATH}action-sheet.css`],
previewImage: [
RESIZE_SENSOR_CSS,
`${BASE_COMPONENTS_STYLE_PATH}swiper.css`,
`${BASE_COMPONENTS_STYLE_PATH}swiper-item.css`,
`${BASE_COMPONENTS_STYLE_PATH}movable-area.css`,
`${BASE_COMPONENTS_STYLE_PATH}movable-view.css`,
],
openLocation: [`${MP_WEIBO_API_STYLE_PATH}location-view.css`],
chooseLocation: [
`${MP_WEIBO_API_STYLE_PATH}/location-picker.css`,
`${BASE_COMPONENTS_STYLE_PATH}/input.css`,
`${MP_WEIBO_COMPONENTS_STYLE_PATH}/map.css`,
`${BASE_COMPONENTS_STYLE_PATH}/scroll-view.css`,
],
}
2 changes: 1 addition & 1 deletion packages/uni-cli-shared/src/json/manifest.ts
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ export const parseManifestJsonOnce = once(parseManifestJson)
export const parseRpx2UnitOnce = once(
(inputDir: string, platform: UniApp.PLATFORM = 'h5') => {
const rpx2unit =
platform === 'h5' || platform === 'app'
platform === 'h5' || platform === 'app' || platform === 'mp-weibo'
? defaultRpx2Unit
: defaultMiniProgramRpx2Unit
const platformOptions = parseManifestJsonOnce(inputDir)[platform]
4 changes: 2 additions & 2 deletions packages/uni-cli-shared/src/json/pages.ts
Original file line number Diff line number Diff line change
@@ -259,15 +259,15 @@ function normalizePageStyle(
}

if (pageStyle) {
if (platform === 'h5') {
if (platform === 'h5' || platform === 'mp-weibo') {
extend(pageStyle, pageStyle['app'] || pageStyle['app-plus'])
}
if (platform === 'app') {
extend(pageStyle, pageStyle['app'] || pageStyle['app-plus'])
} else {
extend(pageStyle, pageStyle[platform])
}
if (['h5', 'app'].includes(platform)) {
if (['h5', 'app', 'mp-weibo'].includes(platform)) {
pageStyle.navigationBar = normalizeNavigationBar(pageStyle)
if (isEnablePullDownRefresh(pageStyle)) {
pageStyle.enablePullDownRefresh = true
1 change: 1 addition & 0 deletions packages/uni-cli-shared/src/messages/zh_CN.ts
Original file line number Diff line number Diff line change
@@ -43,6 +43,7 @@ export default {
'prompt.run.devtools.mp-qq': 'QQ小程序开发者工具',
'prompt.run.devtools.mp-toutiao': '字节跳动开发者工具',
'prompt.run.devtools.mp-weixin': '微信开发者工具',
'prompt.run.devtools.mp-weibo': '微博开发者工具',
'prompt.run.devtools.quickapp-webview':
'快应用联盟开发者工具 | 华为快应用开发者工具',
'prompt.run.devtools.quickapp-webview-huawei': '华为快应用开发者工具',
1 change: 1 addition & 0 deletions packages/uni-cli-shared/src/postcss/plugins/uniapp.ts
Original file line number Diff line number Diff line change
@@ -128,6 +128,7 @@ const transforms: Record<string, RewriteTag | undefined> = {
h5: rewriteUniH5Tags,
app: rewriteUniAppTags,
'mp-baidu': rewriteBaiduTags,
'mp-weibo': rewriteUniH5Tags,
}

const uniapp = (opts?: UniAppCssProcessorOptions) => {
5 changes: 3 additions & 2 deletions packages/uni-cli-shared/src/preprocess/context.ts
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ const DEFAULT_KEYS = [
'MP_WEIXIN',
'MP_KUAISHOU',
'MP_JD',
'MP_WEIBO',
'QUICKAPP_NATIVE',
'QUICKAPP_WEBVIEW',
'QUICKAPP_WEBVIEW_HUAWEI',
@@ -79,11 +80,11 @@ export function initPreContext(
} else if (utsPlatform === 'app-ios') {
uvueContext.APP_IOS = true
}
} else if (platform.startsWith('mp-')) {
} else if (platform.startsWith('mp-') && platform !== 'mp-weibo') {
defaultContext.MP = true
} else if (platform.startsWith('quickapp-webview')) {
defaultContext.QUICKAPP_WEBVIEW = true
} else if (platform === 'h5') {
} else if (platform === 'h5' || platform === 'mp-weibo') {
defaultContext.WEB = true
}

Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ let disabled
const lastAction = {}

export function disableScrollBounce({ disable }) {
if (__PLATFORM__ === 'h5') {
if (__PLATFORM__ === 'h5' || __PLATFORM__ === 'mp-weibo') {
// H5 平台空方法
return
}
2 changes: 1 addition & 1 deletion packages/uni-components/src/helpers/useField.ts
Original file line number Diff line number Diff line change
@@ -300,7 +300,7 @@ function useAutoFocus(props: Props, fieldRef: Ref<HTMLFieldElement | null>) {
setTimeout(focus, 100)
return
}
if (__PLATFORM__ === 'h5') {
if (__PLATFORM__ === 'h5' || __PLATFORM__ === 'mp-weibo') {
field.focus()
} else {
const timeout = FOCUS_DELAY - (Date.now() - startTime)
10 changes: 10 additions & 0 deletions packages/uni-components/src/vue/button/index.tsx
Original file line number Diff line number Diff line change
@@ -48,6 +48,16 @@ export default /*#__PURE__*/ defineBuiltInComponent({
t('uni.button.feedback.send')
)
}
if (
// @ts-ignore
window.weibo &&
// @ts-ignore
typeof window.weibo.share === 'function' &&
props.openType === 'share'
) {
// @ts-ignore
window.weibo.share()
}
})

const uniLabel = inject<UniLabelCtx>(
14 changes: 13 additions & 1 deletion packages/uni-components/src/vue/image/index.tsx
Original file line number Diff line number Diff line change
@@ -99,13 +99,25 @@ function useImageState(rootRef: Ref<HTMLElement | null>, props: ImageProps) {
opts[0] && (position = opts[0])
opts[1] && (size = opts[1])
}
// @ts-ignore
if (window.weibo && props.src.startsWith('Temp')) {
// @ts-ignore
imgSrc.value = `../../${props.src}`
}
return `background-image:${
imgSrc.value ? 'url("' + imgSrc.value + '")' : 'none'
};background-position:${position};background-size:${size};`
})
const state = reactive({
rootEl: rootRef,
src: computed(() => (props.src ? getRealPath(props.src) : '')),
src: computed(function () {
// @ts-ignore
if (window.weibo && props.src.startsWith('Temp')) {
// @ts-ignore
return `../../${props.src}`
}
return props.src ? getRealPath(props.src) : ''
}),
origWidth: 0,
origHeight: 0,
origStyle: { width: '', height: '' },
2 changes: 1 addition & 1 deletion packages/uni-components/src/vue/swiper/index.tsx
Original file line number Diff line number Diff line change
@@ -724,7 +724,7 @@ export default /*#__PURE__*/ defineBuiltInComponent({
)

let createNavigationTsx: () => JSX.Element | null = () => null
if (__PLATFORM__ === 'h5') {
if (__PLATFORM__ === 'h5' || __PLATFORM__ === 'mp-weibo') {
createNavigationTsx = useSwiperNavigation(
rootRef,
props,
4 changes: 2 additions & 2 deletions packages/uni-core/src/helpers/page.ts
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ export function useCurrentPageId() {
return getCurrentPageId()
}
// 暂时仅在 h5 平台实现 $pageInstance,避免影响过大
if (__PLATFORM__ === 'h5') {
if (__PLATFORM__ === 'h5' || __PLATFORM__ === 'mp-weibo') {
const { $pageInstance } = getCurrentInstance()!
return $pageInstance && $pageInstance.proxy!.$page.id
}
@@ -44,7 +44,7 @@ export function getPageIdByVm(
return
}
// 暂时仅在 h5 平台实现 $pageInstance,避免影响过大
if (__PLATFORM__ === 'h5') {
if (__PLATFORM__ === 'h5' || __PLATFORM__ === 'mp-weibo') {
const { $pageInstance } = vm.$
return $pageInstance && $pageInstance.proxy!.$page.id
}
2 changes: 1 addition & 1 deletion packages/uni-core/src/i18n/useI18n.ts
Original file line number Diff line number Diff line change
@@ -88,7 +88,7 @@ export function defineI18nProperty(obj: Record<string, any>, names: string[]) {
export function useI18n() {
if (!i18n) {
let locale: BuiltInLocale
if (__PLATFORM__ === 'h5') {
if (__PLATFORM__ === 'h5' || __PLATFORM__ === 'mp-weibo') {
if (__NODE_JS__) {
locale = getEnvLocale() as BuiltInLocale
} else {
2 changes: 1 addition & 1 deletion packages/uni-core/src/service/plugin/componentInstance.ts
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import { ComponentPublicInstance } from 'vue'
export function getOpenerEventChannel(
this: ComponentPublicInstance
): EventChannel | undefined {
if (__PLATFORM__ === 'h5') {
if (__PLATFORM__ === 'h5' || __PLATFORM__ === 'mp-weibo') {
if (this.$route) {
const meta = this.$route.meta
if (!meta.eventChannel) {
Loading