refactor: 优化平台适配器命名规范和登录逻辑
- 将平台标识符从简写改为语义化名称(tt->bytedance, weapp->wechat) - 登录接口动态获取平台类型,提升代码可维护性 - 优化代码格式,统一条件判断样式
This commit is contained in:
parent
f7073fb4f7
commit
be71df2ecd
|
|
@ -15,7 +15,7 @@ declare const qq: any;
|
||||||
* 支持的平台类型
|
* 支持的平台类型
|
||||||
* 目前支持:字节跳动小程序(tt)
|
* 目前支持:字节跳动小程序(tt)
|
||||||
*/
|
*/
|
||||||
export type Platform = 'tt' | 'weapp' | 'alipay' | 'swan' | 'qq';
|
export type Platform = 'bytedance' | 'wechat' | 'alipay' | 'swan' | 'qq';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 平台适配器工厂类
|
* 平台适配器工厂类
|
||||||
|
|
@ -40,11 +40,11 @@ export class PlatformFactory {
|
||||||
*/
|
*/
|
||||||
createRewardedVideoAd(): RewardedVideoAd {
|
createRewardedVideoAd(): RewardedVideoAd {
|
||||||
switch (this.platform) {
|
switch (this.platform) {
|
||||||
case 'tt':
|
case 'bytedance':
|
||||||
return new RewardedVideoAdTT({
|
return new RewardedVideoAdTT({
|
||||||
adUnitId: 'gncb4kr2b6kwp0uacr'
|
adUnitId: 'gncb4kr2b6kwp0uacr'
|
||||||
});
|
});
|
||||||
case 'weapp':
|
case 'wechat':
|
||||||
return new RewardedVideoAdWeApp({
|
return new RewardedVideoAdWeApp({
|
||||||
adUnitId: 'adunit-xxxxxxxxxxxxxxxx' // 微信广告单元ID,需要开发者配置
|
adUnitId: 'adunit-xxxxxxxxxxxxxxxx' // 微信广告单元ID,需要开发者配置
|
||||||
});
|
});
|
||||||
|
|
@ -62,9 +62,9 @@ export class PlatformFactory {
|
||||||
|
|
||||||
createMedia(): Media {
|
createMedia(): Media {
|
||||||
switch (this.platform) {
|
switch (this.platform) {
|
||||||
case 'tt':
|
case 'bytedance':
|
||||||
return new MediaTT();
|
return new MediaTT();
|
||||||
case 'weapp':
|
case 'wechat':
|
||||||
return new MediaWeApp();
|
return new MediaWeApp();
|
||||||
case 'alipay':
|
case 'alipay':
|
||||||
throw new Error(`支付宝小程序平台暂未实现`);
|
throw new Error(`支付宝小程序平台暂未实现`);
|
||||||
|
|
@ -80,9 +80,9 @@ export class PlatformFactory {
|
||||||
|
|
||||||
createUserInfo(): UserInfo {
|
createUserInfo(): UserInfo {
|
||||||
switch (this.platform) {
|
switch (this.platform) {
|
||||||
case 'tt':
|
case 'bytedance':
|
||||||
return new UserInfoTT();
|
return new UserInfoTT();
|
||||||
case 'weapp':
|
case 'wechat':
|
||||||
return new UserInfoWeApp();
|
return new UserInfoWeApp();
|
||||||
case 'alipay':
|
case 'alipay':
|
||||||
throw new Error(`支付宝小程序平台暂未实现`);
|
throw new Error(`支付宝小程序平台暂未实现`);
|
||||||
|
|
@ -119,10 +119,10 @@ export class PlatformFactory {
|
||||||
*/
|
*/
|
||||||
static detectPlatform(): Platform {
|
static detectPlatform(): Platform {
|
||||||
if (typeof tt !== 'undefined') {
|
if (typeof tt !== 'undefined') {
|
||||||
return 'tt';
|
return 'bytedance';
|
||||||
}
|
}
|
||||||
if (typeof wx !== 'undefined') {
|
if (typeof wx !== 'undefined') {
|
||||||
return 'weapp';
|
return 'wechat';
|
||||||
}
|
}
|
||||||
if (typeof my !== 'undefined') {
|
if (typeof my !== 'undefined') {
|
||||||
return 'alipay';
|
return 'alipay';
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
|
|
||||||
import Taro from "@tarojs/taro"
|
import Taro from "@tarojs/taro"
|
||||||
import { IToken, IUser } from "./types";
|
import { IToken, IUser } from "./types";
|
||||||
|
import { createPlatformFactory } from "../platforms";
|
||||||
|
|
||||||
const TOKEN_STORAGE_KEY = 'access_token';
|
const TOKEN_STORAGE_KEY = 'access_token';
|
||||||
const TOKEN_UID_KEY = 'user_uid';
|
const TOKEN_UID_KEY = 'user_uid';
|
||||||
|
|
@ -193,7 +194,7 @@ export class SdkServer {
|
||||||
imageUrl: params.imageUrl,
|
imageUrl: params.imageUrl,
|
||||||
});
|
});
|
||||||
const data = response.data;
|
const data = response.data;
|
||||||
if(typeof data === 'string'){
|
if (typeof data === 'string') {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
return data.executionId
|
return data.executionId
|
||||||
|
|
@ -209,8 +210,9 @@ export class SdkServer {
|
||||||
*/
|
*/
|
||||||
async login(params: any): Promise<{ tokens: IToken, user: IUser }> {
|
async login(params: any): Promise<{ tokens: IToken, user: IUser }> {
|
||||||
try {
|
try {
|
||||||
|
const platform = createPlatformFactory()
|
||||||
const response = await this.request<{ tokens: IToken, user: IUser }>(`/api/v1/users/login`, 'POST', {
|
const response = await this.request<{ tokens: IToken, user: IUser }>(`/api/v1/users/login`, 'POST', {
|
||||||
platform: `bytedance`,
|
platform: platform.getPlatform(),
|
||||||
code: params.code,
|
code: params.code,
|
||||||
encryptedData: params.encryptedData,
|
encryptedData: params.encryptedData,
|
||||||
iv: params.iv,
|
iv: params.iv,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue