Commit 81e6ab5b by chunhong.mu

feat(*): 新增 @common/sdk 包和 qqqf-mp 项目,更新 commitlint scope 白名单

parent 4612dd33
export default { export default {
extends: ['@commitlint/config-conventional'], extends: ['@commitlint/config-conventional'],
rules: { rules: {
'scope-enum': [2, 'always', ["root", "*", "@common/utils", "@common/vue-kit", "official-site-web", "partner-mp", "test-demo-web"]], 'scope-enum': [2, 'always', ["root", "*", "@common/sdk", "@common/utils", "@common/vue-kit", "official-site-web", "partner-mp", "qqqf-mp", "test-demo-web"]],
'scope-empty': [2, 'never'], 'scope-empty': [2, 'never'],
}, },
} }
{
"name": "@common/sdk",
"version": "1.0.0",
"private": true,
"main": "src/index.ts",
"dependencies": {
"axios": "^0.27.2",
"lodash": "^4.17.21",
"uniplat-sdk": "^0.1.734-private"
}
}
export interface AuthHandlers {
getDeviceInfoWidthCache: () => void
clearLoginMsg: () => void
logout: () => void
afterSdkLogin: () => Promise<any>
}
export interface AuthHandlerConfig {
noNeedAuthPages: string[]
}
import { decodeJwt } from "uniplat-sdk"
export function decodeToken<T>(token: string): T {
return decodeJwt<T>(token)
}
export interface TokenInfo {
uid: number
orgId: number
token: string
}
export class PassportTokenController {
static hasToken(): boolean {
try {
const token = uni.getStorageSync("token")
return !!token
} catch {
return false
}
}
static getUid(): number {
try {
const token = uni.getStorageSync("token")
if (!token) return 0
const decoded = decodeToken<TokenInfo>(token)
return decoded.uid || 0
} catch {
return 0
}
}
static getToken(): string {
try {
return uni.getStorageSync("token") || ""
} catch {
return ""
}
}
static setToken(token: string): void {
uni.setStorageSync("token", token)
}
static clearToken(): void {
uni.removeStorageSync("token")
}
}
import type { AxiosPromise, AxiosRequestConfig, AxiosResponse } from "axios"
import { AxiosError } from "axios"
// @ts-ignore
import buildFullPath from "axios/lib/core/buildFullPath"
// @ts-ignore
import settle from "axios/lib/core/settle"
// @ts-ignore
import buildURL from "axios/lib/helpers/buildURL"
import { forEach } from "lodash"
function isJSONstr(str: string | any[]) {
try {
return (
typeof str === "string" &&
str.length &&
(str = JSON.parse(str)) &&
Object.prototype.toString.call(str) === "[object Object]"
)
} catch (error) {
return false
}
}
function getResponse(
res: Partial<
UniApp.RequestSuccessCallbackResult &
UniApp.GeneralCallbackResult &
AxiosResponse
>,
config: AxiosRequestConfig,
uniConfig: UniApp.RequestOptions,
) {
const headers = res.header || res.headers
const status = res.statusCode || res.status
let statusText = ""
if (status === 200) {
statusText = "OK"
} else if (status === 400) {
statusText = "Bad Request"
}
return {
...res,
statusText,
headers,
status,
config,
request: uniConfig,
} as any
}
function transformError(
error: UniApp.GeneralCallbackResult,
reject: (reason: any) => void,
config: AxiosRequestConfig,
) {
if (error.errMsg.indexOf("request:fail abort") !== -1) {
reject(new AxiosError("Request aborted", "ECONNABORTED", config, ""))
} else if (error.errMsg.indexOf("timeout") !== -1) {
reject(
new AxiosError(
`timeout of ${config.timeout}ms exceeded`,
"ECONNABORTED",
config,
"",
),
)
} else {
reject(new AxiosError("Network Error", null, config, ""))
}
}
export function uniAdapter(config: AxiosRequestConfig): AxiosPromise {
if (!uni) {
throw new Error("please use this in uni-app project!")
}
return new Promise((resolve, reject) => {
let requestTask: void | UniApp.RequestTask
let requestData = config.data
const uniConfig = {
method: config.method || "GET",
timeout: config.timeout || 20e3,
url: buildURL(
buildFullPath(config.baseURL, config.url),
config.params,
config.paramsSerializer,
),
} as UniApp.RequestOptions
const uniHeader: Record<string, string> = {}
forEach(config.headers, (val, key) => {
const _header = key.toLowerCase()
if (
(typeof requestData === "undefined" &&
_header === "content-type") ||
_header === "referer"
) {
} else {
uniHeader[key] = val
}
})
uniConfig.header = uniHeader
if (isJSONstr(requestData)) {
requestData = JSON.parse(requestData)
}
if (requestData !== undefined) {
uniConfig.data = requestData
}
if (config.responseType) {
uniConfig.responseType = config.responseType as
| "text"
| "arraybuffer"
}
if (config.cancelToken) {
config.cancelToken.promise.then((cancel) => {
if (!requestTask) {
return
}
requestTask.abort()
reject(cancel)
requestTask = undefined
})
}
requestTask = uni.request({
...uniConfig,
success(res) {
const response = getResponse(res, config, uniConfig)
settle(resolve, reject, response)
},
fail(error) {
transformError(error, reject, config)
},
complete() {
requestTask = undefined
},
})
})
}
export type { AuthHandlerConfig, AuthHandlers } from "./auth/auth-handlers"
export { decodeToken, PassportTokenController } from "./auth/token-manager"
export { uniAdapter } from "./http/adapter"
export { SdkCore } from "./sdk-core"
export type { SdkConfig, SdkInitOptions } from "./sdk-types"
export { buildFilePath, buildImage } from "./utils/image-builder"
import type {
metaRow,
SdkListRowPredict,
SdkListRowPredictObject,
} from "uniplat-sdk"
import type { AxiosAdapter } from "uniplat-sdk/build/main/helpers/axios"
import type { SdkConfig } from "./sdk-types"
import {
decodeJwt,
UniplatSdk,
UniplatSdkExtender,
} from "uniplat-sdk"
import { Tree } from "uniplat-sdk/build/main/model/tree/tree"
import { uniAdapter } from "./http/adapter"
import { buildFilePath, buildImage } from "./utils/image-builder"
export class SdkCore {
private uniplatSdk!: UniplatSdk
private readonly handler = new UniplatSdkExtender()
private token!: string
public orgId!: number
constructor(config?: SdkConfig) {
if (config) {
this.init(config)
}
}
public init(config: SdkConfig) {
const baseUrl = config.uniplatApi
this.uniplatSdk = new UniplatSdk({ sse: false })
this.uniplatSdk.global.baseUrl = baseUrl
let adapter: AxiosAdapter | undefined
// #ifdef MP
adapter = uniAdapter
// #endif
this.uniplatSdk.connect({
baseUrl,
axiosAdapter: adapter,
axiosTimeout: 10e3,
refreshInterval: 10 * 60 * 1000,
})
// #ifndef MP
this.uniplatSdk.getAxios().defaults.timeout = 10e3
// #endif
this.uniplatSdk.global.rootEntrance = config.rootEntrance
}
public setupEventHandlers(handlers: {
onTokenExpiring: () => void
onUniversalError: (error: any) => void
}) {
this.uniplatSdk.events.addUniversalErrorResponseCallback(handlers.onUniversalError)
this.uniplatSdk.events.addTokenExpiring(handlers.onTokenExpiring)
}
public get core() {
return this.uniplatSdk
}
public domainServiceGet<ParamsType, ReturnType>(
apiName: string,
params?: ParamsType,
isAnonymous?: boolean,
subProjectName?: string,
serviceName?: string,
config?: SdkConfig,
) {
return this.uniplatSdk
.domainService(
subProjectName || config?.domainService.subProjectName || "",
(isAnonymous ? "anonymous/" : "") +
(serviceName || config?.domainService.serviceName || ""),
apiName,
)
.request<ParamsType, unknown, ReturnType>("get", {
params,
})
}
public domainServicePost<ParamsType, DataType, ReturnType>(
apiName: string,
params?: {
data?: DataType
params?: ParamsType
},
isAnonymous?: boolean,
subProjectName?: string,
serviceName?: string,
config?: SdkConfig,
) {
return this.uniplatSdk
.domainService(
subProjectName || config?.domainService.subProjectName || "",
(isAnonymous ? "anonymous/" : "") +
(serviceName || config?.domainService.serviceName || ""),
apiName,
)
.request<ParamsType, DataType, ReturnType>("post", params)
}
public buildRows<T>(
rows: metaRow[],
predicts: SdkListRowPredict[] | SdkListRowPredictObject,
) {
return this.handler.buildRows<T>(rows, predicts)
}
public buildRow<T>(
item: metaRow,
predicts: SdkListRowPredict[] | SdkListRowPredictObject,
) {
return this.handler.buildRow<T>(item, predicts)
}
public buildActionParameter(parameter: { [key: string]: any }) {
return this.handler.buildActionParameter(parameter)
}
public buildImageUrl(url?: string, w?: number, h?: number, view?: boolean) {
return buildImage(this.uniplatSdk, url, w, h, view)
}
public buildFileUrl(url: string, notForceDownload?: boolean) {
return buildFilePath(this.uniplatSdk, url, notForceDownload)
}
public tree(modelName: string) {
return new Tree(modelName)
}
public decodeToken<T>(token: string) {
return decodeJwt<T>(token)
}
}
export interface SdkConfig {
uniplatApi: string
rootEntrance: string
logEnv: string
clientId: string
domainService: {
subProjectName: string
serviceName: string
}
}
export interface SdkInitOptions {
baseUrl: string
sse?: boolean
axiosTimeout?: number
refreshInterval?: number
}
import type { UniplatSdk } from "uniplat-sdk"
export function buildImage(
sdk: UniplatSdk,
url?: string,
w?: number,
h?: number,
view?: boolean,
): string {
if (!url) {
return ""
}
if (url.includes(".png")) {
w = 0
h = 0
}
const imageConfig = sdk.global.imageConfig
if (imageConfig) {
const { domain, protocol } = imageConfig
const width = w !== undefined ? w : imageConfig.defaultWidth
const height = h !== undefined ? h : imageConfig.defaultHeight
if (view) {
return `${protocol}://${domain}/view/${url}?w=${width}&h=${height}`
}
return `${protocol}://${domain}/image/${url}?w=${width}&h=${height}`
}
return url
}
export function buildFilePath(
sdk: UniplatSdk,
url: string,
notForceDownload?: boolean,
): string {
if (!url) return ""
const fileConfig = sdk.global.fileConfig
if (fileConfig && !notForceDownload) {
const { domain, protocol } = fileConfig
return `${protocol}://${domain}/download/${url}`
}
return url
}
...@@ -39,6 +39,18 @@ importers: ...@@ -39,6 +39,18 @@ importers:
specifier: ^5.6.3 specifier: ^5.6.3
version: 5.9.3 version: 5.9.3
common/sdk:
dependencies:
axios:
specifier: ^0.27.2
version: 0.27.2
lodash:
specifier: ^4.17.21
version: 4.18.1
uniplat-sdk:
specifier: ^0.1.734-private
version: 0.1.734-private
common/utils: {} common/utils: {}
common/vue-kit: common/vue-kit:
...@@ -83,6 +95,9 @@ importers: ...@@ -83,6 +95,9 @@ importers:
packages/partner-mp: packages/partner-mp:
dependencies: dependencies:
'@common/sdk':
specifier: workspace:*
version: link:../../common/sdk
'@common/utils': '@common/utils':
specifier: workspace:* specifier: workspace:*
version: link:../../common/utils version: link:../../common/utils
...@@ -313,6 +328,70 @@ importers: ...@@ -313,6 +328,70 @@ importers:
specifier: ^2.2.8 specifier: ^2.2.8
version: 2.2.12(typescript@5.9.3) version: 2.2.12(typescript@5.9.3)
packages/qqqf-mp:
dependencies:
'@common/sdk':
specifier: workspace:*
version: link:../../common/sdk
'@common/utils':
specifier: workspace:*
version: link:../../common/utils
'@dcloudio/uni-app':
specifier: 3.0.0-4070520250711001
version: 3.0.0-4070520250711001(@dcloudio/types@3.4.31)(@nuxt/kit@3.21.8(magicast@0.5.3))(postcss@8.5.15)(rollup@4.62.0)(vue@3.5.38(typescript@5.9.3))
'@dcloudio/uni-components':
specifier: 3.0.0-4070520250711001
version: 3.0.0-4070520250711001(@nuxt/kit@3.21.8(magicast@0.5.3))(postcss@8.5.15)(rollup@4.62.0)(vue@3.5.38(typescript@5.9.3))
'@dcloudio/uni-h5':
specifier: 3.0.0-4070520250711001
version: 3.0.0-4070520250711001(@nuxt/kit@3.21.8(magicast@0.5.3))(postcss@8.5.15)(rollup@4.62.0)(vue@3.5.38(typescript@5.9.3))
'@dcloudio/uni-mp-weixin':
specifier: 3.0.0-4070520250711001
version: 3.0.0-4070520250711001(@nuxt/kit@3.21.8(magicast@0.5.3))(postcss@8.5.15)(rollup@4.62.0)(vue@3.5.38(typescript@5.9.3))
pinia:
specifier: ^3.0.1
version: 3.0.4(typescript@5.9.3)(vue@3.5.38(typescript@5.9.3))
pinia-plugin-persistedstate:
specifier: ^4.2.0
version: 4.7.1(@nuxt/kit@3.21.8(magicast@0.5.3))(pinia@3.0.4(typescript@5.9.3)(vue@3.5.38(typescript@5.9.3)))
vue:
specifier: ^3.4.21
version: 3.5.38(typescript@5.9.3)
devDependencies:
'@dcloudio/types':
specifier: ^3.4.8
version: 3.4.31
'@dcloudio/uni-cli-shared':
specifier: 3.0.0-4070520250711001
version: 3.0.0-4070520250711001(@nuxt/kit@3.21.8(magicast@0.5.3))(postcss@8.5.15)(rollup@4.62.0)(vue@3.5.38(typescript@5.9.3))
'@dcloudio/vite-plugin-uni':
specifier: 3.0.0-4070520250711001
version: 3.0.0-4070520250711001(@nuxt/kit@3.21.8(magicast@0.5.3))(postcss@8.5.15)(rollup@4.62.0)(vite@5.2.8(@types/node@22.20.0)(sass@1.101.0)(terser@5.48.0))(vue@3.5.38(typescript@5.9.3))
'@types/node':
specifier: ^22.13.9
version: 22.20.0
'@vue/runtime-core':
specifier: ^3.4.21
version: 3.5.38
'@vue/tsconfig':
specifier: ^0.7.0
version: 0.7.0(typescript@5.9.3)(vue@3.5.38(typescript@5.9.3))
miniprogram-api-typings:
specifier: ^4.0.7
version: 4.1.3
sass:
specifier: ^1.85.1
version: 1.101.0
typescript:
specifier: ^5.5.4
version: 5.9.3
vite:
specifier: 5.2.8
version: 5.2.8(@types/node@22.20.0)(sass@1.101.0)(terser@5.48.0)
vue-tsc:
specifier: ^2.2.8
version: 2.2.12(typescript@5.9.3)
packages/test-demo-web: packages/test-demo-web:
dependencies: dependencies:
ant-design-vue: ant-design-vue:
...@@ -14450,7 +14529,7 @@ snapshots: ...@@ -14450,7 +14529,7 @@ snapshots:
graceful-fs: 4.2.11 graceful-fs: 4.2.11
is-stream: 2.0.1 is-stream: 2.0.1
lazystream: 1.0.1 lazystream: 1.0.1
lodash: 4.17.19 lodash: 4.18.1
normalize-path: 3.0.0 normalize-path: 3.0.0
readable-stream: 4.7.0 readable-stream: 4.7.0
...@@ -15655,7 +15734,7 @@ snapshots: ...@@ -15655,7 +15734,7 @@ snapshots:
debug: 4.4.3 debug: 4.4.3
eslint: 9.39.4(jiti@2.7.0) eslint: 9.39.4(jiti@2.7.0)
eslint-compat-utils: 0.6.5(eslint@9.39.4(jiti@2.7.0)) eslint-compat-utils: 0.6.5(eslint@9.39.4(jiti@2.7.0))
lodash: 4.17.19 lodash: 4.18.1
toml-eslint-parser: 0.10.1 toml-eslint-parser: 0.10.1
transitivePeerDependencies: transitivePeerDependencies:
- supports-color - supports-color
...@@ -20187,7 +20266,7 @@ snapshots: ...@@ -20187,7 +20266,7 @@ snapshots:
whatwg-url@8.7.0: whatwg-url@8.7.0:
dependencies: dependencies:
lodash: 4.17.19 lodash: 4.18.1
tr46: 2.1.0 tr46: 2.1.0
webidl-conversions: 6.1.0 webidl-conversions: 6.1.0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment