zoukankan      html  css  js  c++  java
  • 20171106

    App_Data/MsgTemple.xml

    <?xml version="1.0" encoding="utf-8"?>
    <xml>
    <!--<temp>
    <tempid>sendWhenApp</tempid>
    <tempcontent>{ "touser":"$$0$$","template_id":"$$1$$","url":"http://weixin.deqiaohr.com.cn/EmploUpdatePwd.aspx", "data":{"first":{"value":"恭喜您!你的查询密码已重置成功!","color":"#173177"},"keyword1":{"value":"$$2$$","color":"#173177"},"keyword2":{"value":"NULL","color":"#173177"},"keyword3":{"value":"$$3$$","color":"#173177"},"remark":{"value":"如需再次修改密码,请点击进入!!","color":"#173177"}} }</tempcontent>
    </temp>-->
    <temp>
    <tempid>sendWhenApptem</tempid>
    <tempcontent>{ "touser":"$$0$$","template_id":"$$1$$", "data":{"first":{"value":"$$2$$","color":"#173177"}} }</tempcontent><!--恭喜您!你的查询密码已重置成功!-->
    </temp>
    <temp>
    <tempid>sendWhenApp</tempid>
    <tempcontent>{ "touser":"$$0$$","template_id":"$$1$$", "data":{"first":{"value":"$$2$$","color":"#173177"}} }</tempcontent>
    </temp>
    <temp>
    <tempid>queue</tempid>
    <tempcontent>{ "touser":"$$0$$","template_id":"$$1$$", "data":{"first":{"value":"$$2$$","color":"#173177"}} }</tempcontent>
    </temp>
    <temp>
    <tempid>queuefinish</tempid>
    <tempcontent>{ "touser":"$$0$$","template_id":"$$1$$", "data":{"first":{"value":"$$2$$","color":"#173177"}} }</tempcontent>
    </temp>
    <temp>
    <tempid>pay</tempid>
    <tempcontent>{ "touser":"$$0$$","template_id":"$$1$$","url":"http://weixin.deqiaohr.com.cn/EmploUpdatePwd.aspx", "data":{"first":{"value":"服务完成,请点击链接进行支付!","color":"#173177"},"keyword1":{"value":"$$2$$","color":"#173177"},"keyword2":{"value":"$$3$$","color":"#173177"},"keyword3":{"value":"$$4$$","color":"#173177"},"keyword4":{"value":"$$5$$","color":"#173177"},"keyword5":{"value":"$$6$$","color":"#173177"},"remark":{"value":"请点击链接完成本次服务付款!","color":"#173177"}} }</tempcontent>
    </temp>
    </xml>

    Class/GetAccess_token.cs

    using AppWeb.Models;
    using Common;
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Xml;

    namespace AppWeb.Class
    {
    public class GetAccess_token
    {
    public static Access_token GetAccess_token_Fun()
    {
    string appid = ConfigurationManager.AppSettings["appid"];
    string secret = ConfigurationManager.AppSettings["secret"];
    Access_token mode = new Access_token();

    var GetTakenStr = HttpHelper.HttpGet("https://api.weixin.qq.com/cgi-bin/token", "grant_type=client_credential&appid=" + appid + "&secret=" + secret);//get
    var jd = JsonConvert.DeserializeObject<Access_token>(GetTakenStr);
    mode.access_token = (String)jd.access_token;
    mode.expires_in = (String)jd.expires_in;
    return mode;
    }

    /// <summary>
    /// 根据当前日期 判断Access_Token 是否超期 如果超期返回新的Access_Token 否则返回之前的Access_Token
    /// </summary>
    /// <param name="datetime"></param>
    /// <returns></returns>
    public static string IsExistAccess_Token()
    {

    string Token = string.Empty;
    DateTime YouXRQ;
    // 读取XML文件中的数据,并显示出来 ,注意文件路径
    string filepath = AppDomain.CurrentDomain.BaseDirectory + "/App_Data/XMLFile.xml";

    StreamReader str = new StreamReader(filepath, System.Text.Encoding.UTF8);
    XmlDocument xml = new XmlDocument();
    xml.Load(str);
    str.Close();
    str.Dispose();
    Token = xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText;
    YouXRQ = Convert.ToDateTime(xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText);

    if (DateTime.Now > YouXRQ)
    {
    DateTime _youxrq = DateTime.Now;
    Access_token mode = GetAccess_token_Fun();
    xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText = mode.access_token;
    _youxrq = _youxrq.AddSeconds(int.Parse(mode.expires_in));
    xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText = _youxrq.ToString();
    xml.Save(filepath);
    Token = mode.access_token;
    }
    return Token;
    }

    public static string ForceRecreate()
    {
    string Token = string.Empty;
    // 读取XML文件中的数据,并显示出来 ,注意文件路径
    string filepath = AppDomain.CurrentDomain.BaseDirectory + "/App_Data/XMLFile.xml";

    StreamReader str = new StreamReader(filepath, System.Text.Encoding.UTF8);
    XmlDocument xml = new XmlDocument();
    xml.Load(str);
    DateTime _youxrq = DateTime.Now;
    Access_token mode = GetAccess_token_Fun();
    xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText = mode.access_token;
    _youxrq = _youxrq.AddSeconds(int.Parse(mode.expires_in));
    xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText = _youxrq.ToString();
    xml.Save(filepath);
    Token = mode.access_token;
    return Token;
    }
    }
    }

    Class/WXUtility.cs

    using AppWeb.Class;
    using AppWeb.Models;
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data.Entity;
    using System.Linq;
    using System.Text;

    namespace Common
    {
    public class WXUtility
    {
    #region 静默获取用户OpenID
    public static void ReGetOpenId(string returnUrl)//"/shoplist/index"
    {
    string url = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;//获取当前url
    //if (System.Web.HttpContext.Current.Session["openid"] == null||System.Web.HttpContext.Current.Session["openid"].ToString() == "")
    //{
    //先要判断是否是获取code后跳转过来的
    if (System.Web.HttpContext.Current.Request.QueryString["code"] == "" || System.Web.HttpContext.Current.Request.QueryString["code"] == null)
    {
    //Code为空时,先获取Code
    string GetCodeUrls = GetCodeUrl(url);
    System.Web.HttpContext.Current.Response.Redirect(GetCodeUrls);//先跳转到微信的服务器,取得code后会跳回来这页面的
    }
    else
    {
    //Code非空,已经获取了code后跳回来啦,现在重新获取openid
    // Log log = new Log(AppDomain.CurrentDomain.BaseDirectory + @"/log/Log.txt");
    string openid = "";
    openid = GetOauthAccessOpenId(System.Web.HttpContext.Current.Request.QueryString["Code"]);//重新取得用户的openid
    System.Web.HttpContext.Current.Session["openid"] = openid;
    wxWebEntities bll = new wxWebEntities();
    var temModel= bll.UserInfo.Where(x => x.Openid == openid).First();
    if (temModel != null && temModel.Tel!=null&&!string.IsNullOrEmpty(temModel.Tel))
    {
    System.Web.HttpContext.Current.Session["tel"] = temModel.Tel;
    System.Web.HttpContext.Current.Session["UserType"] = temModel.UserType;
    System.Web.HttpContext.Current.Session["UserID"] = temModel.ID;
    if (temModel.UserType == 2)
    {
    var styleModel= bll.Stylist.Where(x => x.OpenID == openid).First();
    if (styleModel != null)
    {
    System.Web.HttpContext.Current.Session["StyleID"] = styleModel.Guid;
    }
    }
    }
    System.Web.HttpContext.Current.Response.Redirect(returnUrl);
    }
    //}
    }

    #region 重新获取Code的跳转链接(没有用户授权的,只能获取基本信息)
    /// <summary>重新获取Code,以后面实现带着Code重新跳回目标页面(没有用户授权的,只能获取基本信息(openid))</summary>
    /// <param name="url">目标页面</param>
    /// <returns></returns>
    public static string GetCodeUrl(string url)
    {
    string CodeUrl = "";
    //对url进行编码
    url = System.Web.HttpUtility.UrlEncode(url);
    CodeUrl = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + ConfigurationManager.AppSettings["appid"] + "&redirect_uri=" + url + "?action=viewtest&response_type=code&scope=snsapi_base&state=1#wechat_redirect");

    return CodeUrl;

    }
    #endregion

    #region 以Code换取用户的openid、access_token
    /// <summary>根据Code获取用户的openid、access_token</summary>
    public static string GetOauthAccessOpenId(string code)
    {
    Log log = new Log(AppDomain.CurrentDomain.BaseDirectory + @"/log/Log.txt");
    string Openid = "";
    string gethtml = HttpHelper.HttpGet("https://api.weixin.qq.com/sns/oauth2/access_token", "appid=" + ConfigurationManager.AppSettings["appid"] + "&secret=" + ConfigurationManager.AppSettings["secret"] + "&code=" + code + "&grant_type=authorization_code");
    log.log("拿到的url是:" + "https://api.weixin.qq.com/sns/oauth2/access_token" + "appid=" + ConfigurationManager.AppSettings["appid"] + "&secret=" + ConfigurationManager.AppSettings["secret"] + "&code=" + code + "&grant_type=authorization_code");
    log.log("获取到的gethtml是" + gethtml);
    OAuth_Token ac = new OAuth_Token();
    ac = JsonConvert.DeserializeObject<OAuth_Token>(gethtml);
    log.log("能否从html里拿到openid=" + ac.openid);
    Openid = ac.openid;

    #region 判断用户是否存在在数据库 不存在的话新增
    wxWebEntities bll = new wxWebEntities();
    var selTem = bll.UserInfo.Where(x => x.Openid == ac.openid).FirstOrDefault();
    if (selTem == null)
    {
    var getUserInfo = HttpHelper.HttpGet("https://api.weixin.qq.com/cgi-bin/user/info", "access_token=" + GetAccess_token.IsExistAccess_Token() + "&openid=" + ac.openid + "&lang=zh_CN");
    if (!string.IsNullOrEmpty(getUserInfo))
    {
    WxUserInfo model = JsonConvert.DeserializeObject<WxUserInfo>(getUserInfo);
    UserInfo AddModel = new UserInfo();
    AddModel.ID = Guid.NewGuid().ToString();
    AddModel.Openid = model.openid;
    AddModel.Nickname = model.nickname;
    AddModel.Sex = Convert.ToByte(model.sex);
    AddModel.Language = model.language;
    AddModel.City = model.city;
    AddModel.Province = model.province;
    AddModel.Country = model.country;
    AddModel.Headimgurl = model.headimgurl;
    AddModel.AddTime = DateTime.Now;
    AddModel.Tel = "";
    AddModel.Status = 1;
    AddModel.IsDelete = 0;
    AddModel.LastLoginTime = DateTime.Now;
    AddModel.UserType = 1;

    bll.UserInfo.Add(AddModel);
    bll.SaveChanges();
    }
    }
    else// 更新登录信息
    {
    selTem.LastLoginTime = DateTime.Now;
    bll.UserInfo.Attach(selTem);
    bll.Entry(selTem).State = EntityState.Modified;
    bll.SaveChanges();

    //将数据存到session
    System.Web.HttpContext.Current.Session["tel"] = selTem.Tel;
    System.Web.HttpContext.Current.Session["UserType"] = selTem.UserType;
    System.Web.HttpContext.Current.Session["UserID"] = selTem.ID;
    if (selTem.UserType == 2)
    {
    var styleModel = bll.Stylist.Where(x => x.OpenID == ac.openid).First();
    if (styleModel != null)
    {
    System.Web.HttpContext.Current.Session["StyleID"] = styleModel.Guid;
    }
    }
    }
    #endregion
    return Openid;
    }
    #endregion

    #endregion
    }
    }

    Controllers/ShopDetailsController.cs

  • 相关阅读:
    Redis与Redis 伪集群环境的搭建
    github的基本使用
    使用七牛云存储图片或文件并回显
    阿里云搭建wordpress博客教程
    判断是否同一天 同一月
    Python学习笔记之 并发编程
    Python学习笔记之 日志模块logging使用详解
    Python学习笔记之 网络编程(socket套接字编程)
    Python实现TCP文件传输
    实例:Python实现聊天室
  • 原文地址:https://www.cnblogs.com/muxueyuan/p/7794395.html
Copyright © 2011-2022 走看看