package com.ym.unityandroid;

import android.app.Activity;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class UnityWxLoginUtil {


    private static volatile UnityWxLoginUtil sInstance;
    private UnityWxLoginUtil() {

    }

    public static UnityWxLoginUtil getInstance() {
        if (sInstance == null) {
            synchronized (UnityWxLoginUtil.class) {
                if (sInstance == null) {
                    sInstance = new UnityWxLoginUtil();
                }
            }
        }
        return sInstance;
    }
    // APP_ID 替换为你的应用从官方网站申请到的合法appID
    public static String WxAPP_ID = "";
    private IWXAPI api;

    public void InitWxLogin(Activity activity, String WxAppId){
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                WxAPP_ID = WxAppId;
                // 通过WXAPIFactory工厂，获取IWXAPI的实例
                api = WXAPIFactory.createWXAPI(activity, WxAPP_ID, true);
                // 将应用的appId注册到微信
                api.registerApp(WxAPP_ID);
            }
        });
    }

    public void wxLogin(Activity activity){
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                final SendAuth.Req req = new SendAuth.Req();
                req.scope = "snsapi_userinfo";
                req.state = "wechat_sdk";
                if (api!=null){
                    api.sendReq(req);
                }
            }
        });

    }



}
