2.1.1 • Published 5 years ago

tmmtmm-js-sdk v2.1.1

Weekly downloads
2
License
ISC
Repository
-
Last release
5 years ago

tmmtmm-js-sdk

install

use npm

    npm install tmmtmm-js-sdk

use yarn

    yarn add tmmtmm-js-sdk

Get SDK instance

Self instantiation

    import SDK from 'tmmtmm-js-sdk';
    const sdk = new SDK();
    // This operation is necessary because subsequent callbacks will use it
    window.TMMTMM_JS_SDK = sdk;

instantiation by hooks in react-app

    import React from 'react';
    import {useSDK} from 'tmmtmm-js-sdk';
    const Page = ()=>{
        const sdk = useSDK();
        return <div>...</div>
    };

Unified call interface

    sdk.trigger(methodName: TriggerType, options: {
        callback?: CallbackHandler,
        [key: string]: any,
    });

example authorization

    sdk.trigger('authorization', {
        callback: (code:number, msg: string, data: any)=>{
            console.log(code, msg, data);
        }
    });

example recharge

    sdk.trigger("recharge", {
        orderId: "123456",
        callback: (code, msg, data) => {
            console.log(code, msg, data);
        },
    });

example share

    sdk.trigger("share", {
        title: "test",
        content:
            "this is a test content.",
        url: "https://www.github.com",
        callback: (code, msg, data) => {
            console.log(code, msg, data);
        },
    });

example checkSession

    sdk.trigger("checkSession", {
        callback: (code, msg, data) => {
            // data is like {"isAuthorize":true}
            console.log(code, msg, data);
            // Follow up according to the status
        },
    });

example openLink

    sdk.trigger("openLink", {
        type: 1,
        subType:101,
        url: "https://www.github.com",
        callback: (code, msg, data) => {
            console.log(code, msg, data);
        },
    });

Complete demo example of react app

index.ts

    import React, { CSSProperties, useState, useEffect } from "react";
    import { render } from "react-dom";
    import { useSDK } from "tmmtmm-js-sdk";
    const Demo = () => {
    const sdk = useSDK();
    const [a, setA] = useState<string>();
    const [b, setB] = useState<string>();
    const [c, setC] = useState<string>();
    const authorization = () => {
        sdk &&
        sdk.trigger("authorization", {
            callback: (code, msg, data) => {
            console.log(code, msg, data);
            setA(`${code}  ${msg}  ${JSON.stringify(data)}`);
            },
        });
    };
    const recharge = () => {
        sdk &&
        sdk.trigger("recharge", {
            orderId: "123456",
            callback: (code, msg, data) => {
            console.log(code, msg, data);
            setB(`${code}  ${msg}  ${JSON.stringify(data)}`);
            },
        });
    };
    const share = () => {
        sdk &&
        sdk.trigger("share", {
            title: "test",
            content:
            "this is a test content.",
            url: "http://www.github.com",
            callback: (code, msg, data) => {
            console.log(code, msg, data);
            setC(`${code}  ${msg}  ${JSON.stringify(data)}`);
            },
        });
    };
    const openLink = () => {
        sdk &&
        sdk.trigger("openLink", {
            type: 1,
            subType:101,
            url: "http://www.github.com",
            callback: (code, msg, data) => {
            console.log(code, msg, data);
            setC(`${code}  ${msg}  ${JSON.stringify(data)}`);
            },
        });
    };
    const buttonStyle: CSSProperties = {
        width: 120,
        backgroundColor: "#fff",
        border: "1px #000 solid",
        lineHeight: "60px",
        fontWeight: "bold",
        fontSize: 20,
        color: "#258",
    };
    return (
        <div>
        <button style={buttonStyle} onClick={authorization}>
            authorization
        </button>
        <pre>{a}</pre>
        <br />
        <br />
        <br />
        <br />
        <button style={buttonStyle} onClick={recharge}>
            recharge
        </button>
        <pre>{b}</pre>
        <br />
        <br />
        <br />
        <br />
        <button style={buttonStyle} onClick={share}>
            share
        </button>
        <pre>{c}</pre>
        </div>
    );
    };

    render(<Demo />, document.getElementById("root"));

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>demo</title>
    <!-- you can add debug facility when the project is in the development phase, don't forget to remove it in the production phase  -->
    <script src="https://cdn.bootcss.com/vConsole/3.3.4/vconsole.min.js"></script>
    <script>
      var vConsole = new VConsole();
    </script>
</head>
<body>
    <!-- Products packaged by webpack -->
    <script src="./dist/bundle.js"></script>
    <div id="root"></div>
</body>
</html>
2.1.1

5 years ago

2.1.0

5 years ago

2.0.9

5 years ago

2.0.8

5 years ago

2.0.7

5 years ago

2.0.6

5 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.0

5 years ago