1.注册微信小程序
2.安装小程序开发工具
3.实例(显示微信运动步数)
4.后端处理(c#)
一 注册微信小程序
注册地址:https://mp.weixin.qq.com/cgi-bin/registermidpage?action=index&lang=zh_CN&token=
二 安装小程序开发工具
三 实例(显示微信运动步数)
1.app.js 修改
wx.login({
success(res) {
if (res.code) {
// 发起网络请求
wx.request({
url: 'https://localhost:44395/api/WxApiTest/Login',
data: {
code: res.code
},
success:function(re){
wx.setStorageSync('openid', re.data.openid)
wx.setStorageSync('session_key', re.data.session_key)
},
fail:function(re){
console.log(re)
}
})
} else {
console.log('登录失败!' + res.errMsg)
}
}
})
2.读取微信运动步数
wx.getWeRunData({ success(res) { const encryptedData = res.encryptedData; wx.request({ url: 'https://localhost:44395/api/WxApiTest/Decrypt', method: "Post", header: { "content-type": "application/x-www-form-urlencoded" }, data: { encryptedData: res.encryptedData, iv: res.iv, session_key: wx.getStorageSync("session_key") }, success: function(json) { console.log(json.data.stepInfoList) if (json.data.stepInfoList) { json.data.stepInfoList = json.data.stepInfoList.reverse() for (var i = 0; i < json.data.stepInfoList.length; i++) { console.log('日期' + new Date(json.data.stepInfoList[i].timestamp*1000).toLocaleString()); console.log('步数' + json.data.stepInfoList[i].step); } } } }) } })
四 后端处理(c#)
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace WebApplicationVS2019Study.Controllers { [Route("api/[controller]")] [ApiController] public class WxApiTestController : ControllerBase { /// <summary> /// 微信登录:获取appId、session_key /// </summary> /// <returns></returns> [HttpGet] [Route("Login")] public string Login(string code) { var appId = "xxx"; //这里是真实的appId var secret = "xxx"; //这里是真实的secret var grant_type = "authorization_code"; var url = $"https://api.weixin.qq.com/sns/jscode2session?appid=" + appId + "&secret=" + secret + "&js_code=" + code + "&grant_type=" + grant_type; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8"; request.Timeout = 5000; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; } [Route("Decrypt")] [HttpPost] public string Decrypt() { var encryptedData = Request.Form["encryptedData"]; var iv = Request.Form["iv"]; //var openid = Request.Form["openid"]; var session_key = Request.Form["session_key"]; return Decrypto(encryptedData, session_key, iv); } /// <summary> /// 解密方法 /// </summary> private string Decrypto(string encryptedData, string key, string iv) { byte[] iv2 = Convert.FromBase64String(iv); if (string.IsNullOrEmpty(encryptedData)) return ""; Byte[] toEncryptArray = Convert.FromBase64String(encryptedData); System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged { Key = Convert.FromBase64String(key), IV = iv2, Mode = System.Security.Cryptography.CipherMode.CBC, Padding = System.Security.Cryptography.PaddingMode.PKCS7 }; System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor(); Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); var res = Encoding.UTF8.GetString(resultArray); return res; } } }