Commit b9f7427a authored by wykolor's avatar wykolor

Merge branch 'feature/2021_03_12_bonus' of dev.jiuaoedu.com:wykolor/GeekBonusH5…

Merge branch 'feature/2021_03_12_bonus' of dev.jiuaoedu.com:wykolor/GeekBonusH5 into feature/2021_03_12_bonus
parents 43a49ec5 e7253e60
import { createApp } from "vue"; /* eslint-disable @typescript-eslint/no-var-requires */
import App from "./App.vue"; import { createApp } from "vue";
import router from "./router"; import App from "./App.vue";
import store from "./store"; import router from "./router";
import "./assets/style/normalize.css"; import store from "./store";
import "amfe-flexible"; import "./assets/style/normalize.css";
import VConsole from "vconsole"; import "amfe-flexible";
new VConsole();
createApp(App) if (process.env.NODE_ENV !== "production") {
.use(store) const VConsole = require("vconsole");
.use(router) new VConsole();
.mount("#app"); }
createApp(App)
.use(store)
.use(router)
.mount("#app");
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router"; import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import Bonus from "../views/bonus/index.vue"; import Bonus from "../views/bonus/index.vue";
const routes: Array<RouteRecordRaw> = [ const routes: Array<RouteRecordRaw> = [
{ {
path: "/", path: "/",
name: "Bonus", name: "Bonus",
component: Bonus component: Bonus
}, },
{ {
path: "/bonusDetail", path: "/bonusDetail",
name: "bonusDetail", name: "bonusDetail",
component: () => import("../views/bonus/bonus-detail/index.vue") component: () => import("../views/bonus/bonus-detail/index.vue")
}, },
{ {
path: "/withdraw", path: "/withdraw",
name: "withdraw", name: "withdraw",
component: () => import("../views/bonus/withdraw/index.vue") component: () => import("../views/bonus/withdraw/index.vue")
}, },
{ {
path: "/test", path: "/test",
name: "test", name: "test",
component: () => import("../views/test/index.vue") component: () => import("../views/test/index.vue")
} },
]; {
path: "/test/paging",
const router = createRouter({ name: "test-paging",
history: createWebHashHistory(), component: () => import("../views/test/paging.vue")
routes }
}); ];
export default router; const router = createRouter({
history: createWebHashHistory(),
routes
});
export default router;
/* eslint-disable @typescript-eslint/no-use-before-define */
/* eslint-disable @typescript-eslint/camelcase */ /* eslint-disable @typescript-eslint/camelcase */
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
import { Toast } from "vant";
import { onBeforeUnmount, onMounted, getCurrentInstance, ComponentInternalInstance } from "vue"; import { onBeforeUnmount, onMounted, getCurrentInstance, ComponentInternalInstance } from "vue";
import mitt from "./mitt"; import mitt from "./mitt";
const eventBus = mitt(); const eventBus = mitt();
const GEEK_NATVIE_NAME = "GEEK_NATVIE_NAME"; const GEEK_NATIVE_NAME = "GEEK_NATIVE_NAME";
interface NativeInvokeFuncParams { interface NativeInvokeFuncParams {
method: string; method: string;
data: any; data: any;
error?: string | null;
} }
const isAndroid = () => { const isAndroid = () => {
...@@ -21,13 +22,13 @@ const isAndroid = () => { ...@@ -21,13 +22,13 @@ const isAndroid = () => {
window.$geek_excuteJS = function (args) { window.$geek_excuteJS = function (args) {
try { try {
const params = JSON.parse(args); const params = JSON.parse(args);
eventBus.emit<NativeInvokeFuncParams>(GEEK_NATVIE_NAME, params); eventBus.emit<NativeInvokeFuncParams>(GEEK_NATIVE_NAME, params);
} catch (error) { } catch (error) {
eventBus.emit<NativeInvokeFuncParams>(GEEK_NATVIE_NAME, args); eventBus.emit<NativeInvokeFuncParams>(GEEK_NATIVE_NAME, args);
} }
}; };
const sendNativeMessage = (data: { type: string; data: any }) => { const sendNativeMessage = (data: { type: string; data?: any }) => {
if (isAndroid()) { if (isAndroid()) {
window.ReactNativeWebView && window.ReactNativeWebView &&
window.ReactNativeWebView.onMessage && window.ReactNativeWebView.onMessage &&
...@@ -71,12 +72,12 @@ function useNativeBridge( ...@@ -71,12 +72,12 @@ function useNativeBridge(
onMounted(() => { onMounted(() => {
fn = _listener.bind(getCurrentInstance()); fn = _listener.bind(getCurrentInstance());
eventBus.on<NativeInvokeFuncParams>(GEEK_NATVIE_NAME, fn); eventBus.on<NativeInvokeFuncParams>(GEEK_NATIVE_NAME, fn);
}); });
onBeforeUnmount(() => { onBeforeUnmount(() => {
if (fn) { if (fn) {
eventBus.off<NativeInvokeFuncParams>(GEEK_NATVIE_NAME, fn); eventBus.off<NativeInvokeFuncParams>(GEEK_NATIVE_NAME, fn);
} }
}); });
...@@ -84,3 +85,46 @@ function useNativeBridge( ...@@ -84,3 +85,46 @@ function useNativeBridge(
} }
export { useNativeBridge }; export { useNativeBridge };
const INVOKE_NATIVE_METHOD_TIMEOUT = 10000;
function invokeNativeMethod<T = any, P = any>(arg: { type: string; data?: P }): Promise<T> {
return new Promise((resolve, reject) => {
const callbackName = `${arg.type}_callback`;
const timer = setTimeout(() => {
reject(new Error(`invoke ${arg.type} timeout`));
eventBus.off(GEEK_NATIVE_NAME, onCallback);
}, INVOKE_NATIVE_METHOD_TIMEOUT);
eventBus.on(GEEK_NATIVE_NAME, onCallback);
function onCallback(params?: NativeInvokeFuncParams) {
if (params?.method === callbackName) {
clearTimeout(timer);
if (params.error) {
reject(new Error(params.error));
} else {
resolve(params.data);
}
eventBus.off(GEEK_NATIVE_NAME, onCallback);
}
}
sendNativeMessage(arg);
});
}
/**
* 微信登录
*/
export function bindWechat() {
return invokeNativeMethod<{ code: string }>({ type: "bindWechat" });
}
/**
* 打电话
*/
export function makecall(phone: string) {
return invokeNativeMethod<null, string>({ type: "makecall", data: phone });
}
/* eslint-disable @typescript-eslint/no-explicit-any */
import { reactive, UnwrapRef } from "vue";
type PagingFuncRet<T = object> = {
list: T[] | null;
hasNextPage: boolean;
total: number;
};
type BasePagingParams = {
pageNum: number;
pageSize: number;
};
interface Paging<T = object> extends BasePagingParams {
list: T[];
isLoad: boolean;
loading: boolean;
refreshing: boolean;
hasNextPage: boolean;
error: null | string;
total: number;
}
export type PagingFunc<T = object, P = any> = (
arg: Partial<P> & BasePagingParams
) => Promise<PagingFuncRet<T>>;
function usePaging<T = object, P = any>(
fn: PagingFunc<T, P>,
paramsFn?: () => Partial<P>
): [UnwrapRef<Paging<T>>, (arg?: Partial<P>) => void, (arg?: Partial<P>) => void] {
const paging = reactive<Paging<T>>({
list: [],
isLoad: false,
pageNum: 1,
pageSize: 15,
loading: false,
refreshing: false,
hasNextPage: false,
error: null,
total: 0
});
const request = (isRefresh: boolean) => {
return async (arg: Partial<P> = {}) => {
if (paging.loading || paging.refreshing) return;
if (!isRefresh && !paging.hasNextPage) return;
if (isRefresh) {
paging.refreshing = true;
} else {
paging.loading = true;
}
const currentPage = isRefresh ? 1 : paging.pageNum + 1;
try {
const params = paramsFn ? paramsFn() : {};
const ret = await fn({
...arg,
...params,
pageNum: currentPage,
pageSize: paging.pageSize
});
if (Array.isArray(ret.list)) {
if (isRefresh) {
paging.list = ret.list as UnwrapRef<T[]>;
} else {
paging.list = paging.list.concat(ret.list as UnwrapRef<T[]>);
}
}
paging.total = ret.total;
paging.hasNextPage = ret.hasNextPage;
paging.pageNum = currentPage;
} catch (error) {
paging.error = error.message;
} finally {
if (isRefresh) {
paging.refreshing = false;
} else {
paging.loading = false;
}
paging.isLoad = true;
}
};
};
const loadMore = request(false);
const refresh = request(true);
return [paging, loadMore, refresh];
}
export { usePaging };
<template>
<div>
<div v-if="paging.refreshing">refreshing</div>
<PullRefresh :modelValue="paging.refreshing" @refresh="onRefresh">
<List @load="loadMore" :modelValue="paging.loading" :finished="!paging.hasNextPage">
<div v-for="item in paging.list" class="item" :key="item.index">
{{ item.a }}
</div>
</List>
</PullRefresh>
<div v-if="paging.loading">loading</div>
<div v-if="paging.isLoad && !paging.hasNextPage">没有更多了</div>
</div>
</template>
<script lang="ts" setup>
import type { PagingFunc } from '@/util/use-paging';
import { usePaging } from "@/util/use-paging";
import { onMounted } from "vue";
import { PullRefresh, List } from 'vant';
type ListType = {
a: number;
index: number;
};
const fn: PagingFunc<ListType, { test: number }> = async (arg) => {
await new Promise(resolve => setTimeout(resolve, 1000));
console.log(arg)
return {
hasNextPage: arg.pageNum < 4,
total: 54,
list: new Array(20).fill(0).map((_, index) => ({
a: Math.random(),
index
}))
};
};
const [paging, loadMore, onRefresh] = usePaging(fn, () => ({ test: 1 }));
onMounted(() => {
onRefresh();
});
</script>
<style lang="scss">
.item {
height: 80px;
}
</style>
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