コード例 #1
0
    /// <summary>
    /// 获取 Token 方法
    /// </summary>
    /// <param name="session3rd">string 三方标识</param>
    /// <returns>Hash 返回结果</returns>
    public static Hash Token(string session3rd)
    {
        //  如果 session3rd 为空,则跳出
        if (Genre.IsNull(session3rd))
        {
            return(new Hash((int)CodeType.Session3rdRequired, "session3rd 为空"));
        }

        //  查询客户端编号是否存在
        int clientId = ClientData.GetClientIdBySession3rd(session3rd);

        //  如果客户端存在
        if (clientId > 0)
        {
            //  获取用户信息
            Hash data    = ClientData.GetByClientId(clientId);
            Hash setting = SettingData.Detail();

            //  附加 APP 配置信息
            foreach (string key in setting.Keys)
            {
                if (key.StartsWith("page"))
                {
                    data[key] = setting[key];
                }
            }

            //  返回成功结果
            return(new Hash((int)CodeType.OK, "成功", data));
        }
        return(new Hash((int)CodeType.ClientNotExists, "session3rd 无效"));
    }
コード例 #2
0
    /// <summary>
    /// 获取小程序访问 access_token
    /// </summary>
    /// <returns>string access_token</returns>
    public static string AccessToken()
    {
        Hash setting = SettingData.Detail();

        if (setting.IsNull("accessToken") || setting.ToDateTime("accessTokenExpireIn") < DateTime.Now)
        {
            Hash accessToken = API.GetAccessToken(setting.ToString("appKey"), setting.ToString("appSecret"));
            SettingData.SetItem("accessToken", accessToken.ToString("access_token"));
            SettingData.SetItem("accessTokenExpireIn", DateTime.Now.AddSeconds(accessToken.ToInt("expires_in")).ToString());
            return(accessToken.ToString("access_token"));
        }
        return(setting.ToString("accessToken"));
    }
コード例 #3
0
    /// <summary>
    /// 登录方法
    /// </summary>
    /// <param name="code">string 微信登录凭证</param>
    /// <returns>Hash 返回结果</returns>
    public static Hash Login(string code)
    {
        //  如果 code 为空,则跳出
        if (Genre.IsNull(code))
        {
            return(new Hash((int)CodeType.CodeRequired, "code 为空"));
        }

        //  用 code 换取微信客户端信息
        Hash setting       = SettingData.Detail();
        Hash wechatSession = API.Code2Session(setting.ToString("appKey"), setting.ToString("appSecret"), code);

        if (wechatSession.ToInt("errcode") != 0)
        {
            return(new Hash((int)CodeType.CodeInvalid, wechatSession.ToString("errmsg")));
        }

        //  初始化新客户端信息
        int    clientId   = ClientData.GetClientIdForCreate();
        string openId     = wechatSession.ToString("openid");
        string sessionKey = wechatSession.ToString("session_key");

        //  创建新客户端
        if (ClientData.Create(clientId, openId, sessionKey, Guid.NewGuid().ToString().Replace("-", "")) > 0)
        {
            //  重新获取客户端编号
            clientId = ClientData.GetClientIdByOpenId(openId);

            //  查询客户端信息
            Hash data = ClientData.GetByClientId(clientId);

            //  附加 APP 配置信息
            foreach (string key in setting.Keys)
            {
                if (key.StartsWith("page"))
                {
                    data[key] = setting[key];
                }
            }

            //  返回成功结果
            return(new Hash((int)CodeType.OK, "成功", data));
        }
        return(new Hash((int)CodeType.DataBaseUnknonw, "数据库操作失败"));
    }
コード例 #4
0
    /// <summary>
    /// 登录方法
    /// </summary>
    /// <param name="code">string 微信登录凭证</param>
    /// <returns>Hash 返回结果</returns>
    public static Hash Login(string code)
    {
        if (Genre.IsNull(code))
        {
            return(new Hash((int)CodeType.CodeRequired, "code 为空"));
        }
        Hash setting       = SettingData.Detail();
        Hash wechatSession = API.Code2Session(setting.ToString("appKey"), setting.ToString("appSecret"), code);

        if (wechatSession.ToInt("errcode") != 0)
        {
            return(new Hash((int)CodeType.CodeInvalid, wechatSession.ToString("errmsg")));
        }
        int    clientId   = ClientData.GetNewClientId();
        string openId     = wechatSession.ToString("openid");
        string sessionKey = wechatSession.ToString("session_key");

        if (ClientData.Create(clientId, openId, sessionKey, Guid.NewGuid().ToString().Replace("-", "")) > 0)
        {
            return(new Hash((int)CodeType.OK, "成功", ClientData.GetByOpenId(openId)));
        }
        return(new Hash((int)CodeType.DataBaseUnknonw, "数据库操作失败"));
    }