轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 终极进阶 查看主题

微信小程序获取openid小程序端及服务器端代码

发布者: lhpwz | 发布时间: 2018-6-3 14:58| 查看数: 6660| 评论数: 1|帖子模式

作者:奋斗放个,原文地址

一:客户端

  1. var that = this
  2. wx.login({
  3. success: function (res) {
  4. var appId = ' ';//微信公众号平台申请的appid
  5. var appSecret = ' ';//微信公众号平台申请的app secret
  6. var js_code = res.code;//调用登录接口获得的用户的登录凭证code
  7. wx.request({
  8. url: ' + appId + '&secret=' + appSecret + '&js_code=' + js_code + '&grant_type=authorization_code',
  9. data: {},
  10. method: 'GET',
  11. success: function (res) {
  12. var openid = res.data.openid //返回的用户唯一标识符openid
  13. console.log(openid)
  14. console.log("试试吧上面就是获得的openid")
  15. }
  16. })
  17. }
  18. })
  19. //试验自己的服务器获取openId
  20. //调用登录接口
  21. wx.login({
  22. success: function (res) {
  23. //console.log(res);
  24. that.globalData.loginCode = res.code
  25. wx.getUserInfo({
  26. success: function (res) {
  27. that.globalData.userInfo = res.userInfo
  28. that.globalData.iv = res.iv
  29. that.globalData.encryptedData = res.encryptedData
  30. typeof cb == "function" && cb(that.globalData.userInfo)
  31. that.req(
  32. 'https://lifar网址.aspx?Action=ActionLogin',
  33. {
  34. encryptedData: that.globalData.encryptedData,
  35. iv: that.globalData.iv,
  36. code: that.globalData.loginCode
  37. },
  38. 'GET',
  39. function (res) {
  40. console.log(res)
  41. if (res.data.success) {
  42. console.log('试验自己的服务器获取openId:')
  43. console.log(res.data)
  44. var sessionId = res.data.result;
  45. wx.setStorageSync('sessionId', sessionId)
  46. console.log(sessionId)
  47. }
  48. },
  49. function (res) {
  50. console.log(res)
  51. }
  52. );
  53. }
  54. })
  55. }
  56. })
  57. //试验自己的服务器获取openId结束

方法:

  1. req: function (url, data, method, success, fail) {
  2. var mydata = data || {};
  3. //mydata['appId'] = app.globalData.appId;
  4. wx.request({
  5. url: url,
  6. data: mydata,
  7. method: method,
  8. success: success,
  9. fail: fail,
  10. complete: function () {
  11. // complete
  12. }
  13. })
  14. }

二:服务端

  1. string JsCode2SessionUrl = "";
  2. protected void Page_Load(object sender, EventArgs e)
  3. {
  4. JsCode2SessionUrl = "{0}&secret={1}&js_code={2}&grant_type=authorization_code";
  5. if (Request["Action"] == "ActionLogin")
  6. {
  7. string str_encryptedData = Request["encryptedData"];
  8. string iv = Request["iv"];
  9. //小程序appid和appsecret配置
  10. string appid = Request["appid"];
  11. string secret = Request["secret"];
  12. //如果不传过来可以在web.config里配置
  13. appid = ConfigurationManager.AppSettings["wx_appid"];
  14. secret = ConfigurationManager.AppSettings["wx_key"];
  15. string code = Request["code"];
  16. string sessionId = JsCode2Session(appid, secret, code);
  17. //Session["sessionId"] = sessionId;
  18. if (sessionId == "")
  19. {
  20. Response.Write("{\"result\":\"session_key和openid取不到\",\"success\":false}");
  21. Response.End();
  22. }
  23. string session_key = sessionId.Split(new Char[] { '#' })[0];
  24. string openid = sessionId.Split(new Char[] { '#' })[1];
  25. //Response.Write(sessionId);
  26. Response.Write("{\"result\":\"" + openid + "\",\"success\":true}");
  27. Response.End();
  28. }
  29. }
  30. //code换取session_key,openid
  31. //<summary>
  32. //code换取session_key,openid
  33. //</summary>
  34. //<param name="appid"></param>
  35. //<param name="secret"></param>
  36. //<param name="code"></param>
  37. //<returns></returns>
  38. public string JsCode2Session(string appid, string secret, string code)
  39. {
  40. var url = string.Format(JsCode2SessionUrl, appid, secret, code);
  41. var str = GetFunction(url);
  42. try
  43. {
  44. JsonData jo = JsonMapper.ToObject(str);
  45. string session_key = jo["session_key"].ToString();
  46. string weixinID = jo["openid"].ToString();
  47. return session_key + "#" + weixinID;
  48. }
  49. catch (Exception ex)
  50. {
  51. return "";
  52. }
  53. }
  54. public string GetFunction(string url)
  55. {
  56. string serviceAddress = url;
  57. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
  58. request.Method = "GET";
  59. request.ContentType = "textml;charset=UTF-8";
  60. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  61. Stream myResponseStream = response.GetResponseStream();
  62. StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
  63. string retString = myStreamReader.ReadToEnd();
  64. myStreamReader.Close();
  65. myResponseStream.Close();
  66. //Response.Write(retString);
  67. return retString;
  68. }

最新评论

King火山 发表于 2022-6-21 23:43
音乐编程代码

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表