Commit f70e03e3 authored by haiyoucuv's avatar haiyoucuv

整理一下代码

parent 1d22ae14
......@@ -3,6 +3,7 @@
<component name="Encoding">
<file url="file://$PROJECT_DIR$/../../../../../Cocos/editors/Creator/3.8.3/resources/resources/3d/engine/bin/.declarations/cc.d.ts" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/@types/cc.d.ts" charset="GBK" />
<file url="file://$PROJECT_DIR$/assets/AppTool.ts" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/assets/Component/Svga" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/assets/Module/hooks" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/assets/plugin/SVGA.Lite.v2.1.1.js" charset="UTF-8" />
......
import { showToast } from "./Module/UIFast";
import { sendWebNet } from "./Scripts/Utils/WebNet/WebNet";
import { ajax } from "./Scripts/Utils/WebNet/web/ajax";
export function isIos() {
return navigator.userAgent.match(/iphone|ipod|ipad/gi)
}
export function popPage(){
window["MarsJSBridge"].invoke("popPage", {
index: -1,
data: {},
});
}
export function disableGestureReturn() {
try {
if (isIos()) {
window["MarsJSBridge"].invoke("setGestureHandle", {
enable: 0,
});
} else {
window["MarsJSBridge"].invoke("setInterceptBack", {
enableIntercept: true,
function(res) {
// console.log("截断物理返回");
},
});
}
} catch (e) {
console.log("disableGestureReturn error", e);
}
}
export function isLogin() {
return new Promise((resolve) => {
window["MarsJSBridge"].invoke("isLogin", {},
function (res) {
resolve(res?.result?.isLogin);
});
});
}
export function appJump(url) {
sendWebNet("/autoLogin/tempSaveCookie", null, null, true)
.then((res) => {
res?.data && localStorage.setItem("db_zheshang_cookie", res.data);
})
.finally(() => {
window["MarsJSBridge"].invoke("pushPage", {
uri: url,
});
})
}
export function resetBackCookie(duibaTempCookieId) {
return new Promise(async (resolve) => {
ajax({
url: "/autoLogin/resetCookie", //请求地址
type: 'GET', //请求方式
data: {duibaTempCookieId}, //请求参数
dataType: "json", // 返回值类型的设定,暂时只有json
async: true, //是否异步
success: ()=>{
resolve('success');
},
error: (e)=>{
resolve(e);
},
});
});
}
// 接口请求
export function appRequest(url, params = {}) {
const options = Object.assign({
url
}, params);
console.log('接口请求options: ', options);
return new Promise((resolve, reject) => {
window["MarsJSBridge"]?.invoke('httpRequest', options, res => {
console.log('接口请求res: ', res);
// 如果调用失败则返回失败结果
if (res && res.code && res.code !== '0') {
reject(res);
} else {
if (res.result.code === "0") {
// 如果请求成功
resolve(res.result);
} else {
// 如果成功获得返回但是近回的状态码异常则返回请求结果
reject(res);
}
}
});
});
}
export function saveFunc(imageUrl) {
window["MarsJSBridge"]?.invoke(
"jsToWeChatShare", {
saveAlbum: "1", // 11保存相册 1打开保存相册入口 0关闭入口
weChatMoments: "0", // 分享到朋友圈 1-打开分享到朋友圈入口 0-关闭入口
friendList: "0", // 分享给好友 1-打开分享给好友入口 0-关闭入口
url: "xxx", // 链接地址
image: imageUrl, // 保存分享图片
simplelmage: "xx", // 分享链接缩略图 缩略图大小不超过64K
description: "xx", // 内容简介
},
function (res) {
console.log(res);
}
);
}
let appSystemInfo = null;
// 获取系统信息
export async function getSystemInfo() {
if (!appSystemInfo) {
await new Promise<void>((resolve) => {
window["MarsJSBridge"]?.invoke('getSystemInfo', {}, res => {
appSystemInfo = res.result;
resolve();
});
});
}
return appSystemInfo;
}
// 分享 拉起面板的版本
export async function appShareWx(options) {
const {
saveAlbum = '0',
weChatMoments = '1',
friendList = '1',
url = '',
imageBase64 = '',
simpleImage = '',
title = '',
description = '',
} = options || {};
const appSystemInfo = await getSystemInfo();
if (compareVersion(appSystemInfo?.version, '5.0.12') != -1) {
window["MarsJSBridge"]?.invoke("jsToWeChatShare", {
saveAlbum: saveAlbum, // 保存相册 1 打开保存相册入口 0 关闭入口
weChatMoments: weChatMoments, // 分享到朋友圈 1 打开分享到朋友圈入口 0 关闭入口
friendList: friendList, // 分享给好友 1 打开分享给好友入口 0 关闭入口
url: url, // 链接地址
image: imageBase64, // 保存分享图片 base64
simpleImage: simpleImage, // 分享链接缩略图 备:图大小不超过64K
title: title, // 标题
description: description, // 内容简介
}, res => {
console.log('weChat', res);
if (res.code === '0') {
console.log('分享成功');
} else if (res.code === '1') {
showToast(res.result);
}
});
} else {
showToast('当前版本暂未支持,请升级最新版本。');
}
}
/**
* 版本比较方法
* 1: V1 > V2
* 0: V1 = V2
* -1: v1 < V2
*/
export function compareVersion(v1, v2) {
console.log('compareVersion v1===> ', v1);
console.log('compareVersion v2===> ', v2);
v1 = v1?.split(".") || [];
v2 = v2.split(".");
const len = Math.max(v1.length, v2.length);
// 调整两版本号位数相同
while (v1.length < len) {
v1.push('0');
}
while (v2.length < len) {
v2.push('0');
}
// 遍历每位大小
for (let i = 0; i < len; i++) {
const num1 = parseInt(v1[i]);
const num2 = parseInt(v2[i]);
if (num1 > num2) {
return 1;
} else if (num1 < num2) {
return -1;
}
}
return 0;
}
// 分享
export function shareFunc(type, imageUrl) {
window["MarsJSBridge"]?.invoke(
"jsToSaveAlbumOrShareWeChat", {
type: type, // 0-保存相册 1-微信好友 2-微信朋友圈
image: imageUrl, // base64后的数据 不超过64k
// url, // 分享链接
// title, // 链接标题
// description, // 简介内容
},
(res) => {
// 返回值:{code:"0",message:"",result:""}
// code:0 成功
// code:1 失败, message:1(取图片失败)/2(未安装微信)/3(没有相册读取权限),result:失败原因;
// 注意事项:
// 1.type='0' 保存相册 除了相册权限问题message为3,其它情况message和result都为空
// 2.type='1' 微信分享 除了微信未安装message为2,其它情况message为空,取result值结果
// 3.解析imagebase64数据失败 message为1
console.log('jsToSaveAlbumOrShareWeChat res ===> ', res);
if (typeof res === 'string') {
res = res ? JSON.parse(res) : {}
}
const {
code,
result
} = res
if (type == '0') { // 保存
if (code == '0') {
showToast("已保存到本地照片")
} else {
showToast(result || "保存失败")
}
} else { // 分享
if (code == '1') {
showToast(result || "分享失败")
}
}
}
);
}
// 获取登录状态
export function getLoginStatus() {
return new Promise((resolve) => {
window["MarsJSBridge"]?.invoke('isLogin', {}, res => {
resolve(res.result)
})
})
}
export const appShare = (shareParams) => {
return new Promise((r) => {
window["MarsJSBridge"].invoke("jsToWeChatShare", {
saveAlbum: '0', // 保存相册 1 打开保存相册入口 0 关闭入口
weChatMoments: '1', // 分享到朋友圈 1 打开分享到朋友圈入口 0 关闭入口
friendList: '1', // 分享给好友 1 打开分享给好友入口 0 关闭入口
url: shareParams.url, // 链接地址
image: '', // 保存分享图片
simpleImage: shareParams.thumbnail, // 分享链接缩略图 备:缩略图大小不超过64K
title: shareParams.title, // 标题
description: shareParams.content, // 内容简介
}, function (res) {
console.log(11111, res);
r(res);
// 返回值: {code:'0',message:'', result:'xxx'}
// {
// code: '1',
// message: '1',
// result: '读取图片失败'
// } {
// code: '1',
// message: '2',
// result: '手机未安装微信'
// } {
// code: '1',
// message: '3',
// result: '没有相册权限'
// }
});
})
}
......@@ -2,6 +2,7 @@ import { ajax, jsonp } from "./web/ajax";
import { getUrlParams } from "./web/webTools";
import { showToast } from "db://assets/Module/UIFast";
import { PREVIEW } from 'cc/env';
import { resetBackCookie } from "../../../AppTool";
// import { isFromShare, newUser } from 'duiba-utils';
......@@ -10,6 +11,7 @@ import { PREVIEW } from 'cc/env';
// is_from_share: isFromShare ? '0' : '1',
// }
export const DEFAULF_ERROR = '网络异常,请稍后重试';
//////////////星速台接口方法集成
/**
* web接口枚举,mock 文件名类似aaa/homeInfo.do
......@@ -138,12 +140,12 @@ export enum WebNetName {
/**
* 完成行方浏览任务
*/
finishBrowseTask ="task_1/finishBrowseTask.do",
finishBrowseTask = "task_1/finishBrowseTask.do",
/**
* 领取行方浏览任务
*/
claimThirdTask ="task_1/claimThirdTask.do"
claimThirdTask = "task_1/claimThirdTask.do"
}
export const ERR_MESSAGE = {
......@@ -184,7 +186,7 @@ let dataRecord: {
* @param headers
*/
export function sendWebNet(
netName: WebNetName,
netName: WebNetName | string,
parameter?: any,
callback?: (success: boolean, res?: dataOut) => void,
hideMsg: boolean = false,
......@@ -197,11 +199,11 @@ export function sendWebNet(
if (channel) {
parameter = {
...(parameter || {}),
...{ channel }
...{channel}
};
}
return new Promise((resolve, reject) => {
return new Promise(async (resolve, reject) => {
const success = (data) => {
//发现有些接口成功了,但是response为空
......@@ -245,7 +247,19 @@ export function sendWebNet(
const url = "mock/" + path + ".json";
fetchAsync(url)
.then(success, fail)
return
return;
}
const duibaTempCookieId = localStorage.getItem("db_zheshang_cookie");
if (duibaTempCookieId) {
localStorage.removeItem("db_zheshang_cookie");
const res = await sendWebNet("userLogin.check")
.catch(async () => {
await resetBackCookie(duibaTempCookieId);
});
if (!res || !res.success) {
await resetBackCookie(duibaTempCookieId);
}
}
//网络请求
......@@ -275,10 +289,10 @@ export function sendWebNetWithToken(
// getPxToken(async (msg, token) => {
if (!token) {
showToast(DEFAULF_ERROR);
r({ success: false })
r({success: false})
return;
}
const res = await sendWebNet(netName, { token, ...parameter }, callback, hideMsg, isGet, headers);
const res = await sendWebNet(netName, {token, ...parameter}, callback, hideMsg, isGet, headers);
r(res);
// })
})
......@@ -287,7 +301,6 @@ export function sendWebNetWithToken(
}
/**
* 获取数据
* @param netName
......@@ -309,6 +322,7 @@ async function fetchAsync(url: string): Promise<any> {
const projectxString = "projectx/";
let projectId: string;
/**
* 获取链接上的projectId
*/
......@@ -352,6 +366,7 @@ export function refreshPxTokenKey(callback?: (success: boolean) => void) {
callback && callback(true)
}
}
//执行一次
refreshPxTokenKey();
......@@ -382,6 +397,7 @@ export function getPxTokenSave() {
})
})
}
/**
* 获取星速台token
* @param callback
......@@ -403,8 +419,7 @@ export function getPxToken(callback: (msg: string, token?: string) => void) {
if (e.success) {
window.eval(e.data);
callback(null, window["ohjaiohdf"]());
}
else {
} else {
var msg = (() => {
switch (e.code) {
case "100001":
......@@ -467,7 +482,11 @@ export function sendLog(type: LOG_TYPE | 'exposure' | 'click', area: number, dpm
// console.log('try log', {type, ...params});
}
export const sendLogList = (type: LOG_TYPE | 'exposure' | 'click', areaList: (number | { area: number, dpm_d: number, dcm_c: number })[]) => {
export const sendLogList = (type: LOG_TYPE | 'exposure' | 'click', areaList: (number | {
area: number,
dpm_d: number,
dcm_c: number
})[]) => {
areaList.forEach(v => {
if (typeof v === "object") {
sendLog(type, v.area, v.dpm_d, v?.dcm_c || 0)
......@@ -509,14 +528,14 @@ export function queryPrizeList(strategyId: string, optionId?: string): Promise<d
ajax({
url,
type: 'GET',
data: optionId ? { optionId } : {},
data: optionId ? {optionId} : {},
dataType: "json",
async: true,
success: function (response) {
resolve(response)
},
error: function () {
resolve({ success: false })
resolve({success: false})
}
})
}
......@@ -524,13 +543,13 @@ export function queryPrizeList(strategyId: string, optionId?: string): Promise<d
}
/**
* 扣积分流程,带轮询
* @param toPlaywayId
* @param toActionId
* @param credits
* @param desc?
* @return {Promise<{ success: boolean, ticket?: any,pollingData }>}
*/
* 扣积分流程,带轮询
* @param toPlaywayId
* @param toActionId
* @param credits
* @param desc?
* @return {Promise<{ success: boolean, ticket?: any,pollingData }>}
*/
export async function creditsCost(toPlaywayId, toActionId, desc, credits = "") {
// 预扣积分
const param = {
......@@ -540,19 +559,19 @@ export async function creditsCost(toPlaywayId, toActionId, desc, credits = "") {
};
//@ts-ignore
desc && (param.desc = desc);
const { success, data: ticket } = await sendWebNet(WebNetName.creditsCost, param);
const {success, data: ticket} = await sendWebNet(WebNetName.creditsCost, param);
if (!success) return { success: false };
if (!success) return {success: false};
// 轮询
const pollingData = await pollingWebNet(
{ ticketNum: ticket },
{ticketNum: ticket},
(success, res) => {
return res.data != 0; // 0 是处理中
}
);
//@ts-ignore
return { success: pollingData.data == 1, ticket, pollingData };
return {success: pollingData.data == 1, ticket, pollingData};
}
/**
......
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 to comment