一: 登錄微信公眾平台:進行注冊
(注意:請通讀整篇文章后,再進行實操,免走彎路)
https://mp.weixin.qq.com/ ,點擊立即注冊,選擇你需要的賬號類型,
這個是各種類型的區分:https://kf.qq.com/faq/170815aUZjeQ170815mU7bI7.htm,
大致總結:訂閱號沒有服務號權限多,類型個人沒有類型企業注冊麻煩,個人無法認證,企業可認證
注冊過程中,可分為:個人/企業等(企業,自媒體等注冊時,需要組織代號,經營許可證,對公賬戶等),這塊我選擇注冊的是個人賬號(需要上傳手持身份證的照片)
二:微信公眾號基礎操作
1:首先,了解一下整個工作流程(開發-->接口權限,可看見開發者的接口權限)
2:開發前的准備
開發前,必須設置好 開發-->基礎配置-->【服務器配置】(不配置的情況下,所有操作對測試公眾號/公眾號,都是不起作用的)
如上圖所示:何進行服務器驗證呢?
前提:
a: 一個對外的接口(微信服務器會通過該接口,傳遞給你4個參數:加密簽名,時間戳,隨機數,隨機字符串)
b:頁面填寫時,還會有個:Token
接口處理邏輯:
wechat server ---> url : 傳入【加密簽名,時間戳,隨機數,隨機字符串】--> 將【Token,時間戳,隨機數】排序並連接成一個字符串
進行sha1加密,得到的字符串 與 【加密簽名】進行對比,一致則表示通過,並返回【隨機字符串】
代碼:

1 [Route("wechat")] 2 [HttpGet] 3 public ActionResult WechatValidation(string echoStr,string signature,string timestamp,string nonce) 4 { 5 string filename = "1.text"; 6 string webRootPath = _hostingEnvironment.WebRootPath; 7 string contentRootPath = _hostingEnvironment.ContentRootPath; 8 string fullpath = Path.Combine(webRootPath, filename); 9 if (!System.IO.File.Exists(fullpath)) 10 { 11 System.IO.File.Create(fullpath).Close(); 12 } 13 14 System.IO.File.AppendAllText(fullpath, $"\r\nTime:{DateTime.Now},string echoStr:{echoStr},string signature:{signature},string timestamp:{timestamp},string nonce:{nonce}"); 15 16 var token = "wechat"; 17 var checkResult = CheckSignature(token, signature, timestamp, nonce); 18 return Content(echoStr); 19 } 20 21 22 private bool CheckSignature(string token, string signature, string timestamp, string nonce) 23 { 24 25 //創建數組,將 Token, timestamp, nonce 三個參數加入數組 26 string[] array = { token, timestamp, nonce }; 27 //進行排序 28 Array.Sort(array); 29 //拼接為一個字符串 30 var tempStr = String.Join("", array); 31 //對字符串進行 SHA1加密 32 tempStr = Get_SHA1_Method2(tempStr); 33 //判斷signature 是否正確 34 if (tempStr.Equals(signature)) 35 { 36 return true; 37 } 38 else 39 { 40 return false; 41 } 42 } 43 44 public string Get_SHA1_Method2(string strSource) 45 { 46 string strResult = ""; 47 48 //Create 49 System.Security.Cryptography.SHA1 md5 = System.Security.Cryptography.SHA1.Create(); 50 51 //注意編碼UTF8、UTF7、Unicode等的選擇 52 byte[] bytResult = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strSource)); 53 54 //字節類型的數組轉換為字符串 55 for (int i = 0; i < bytResult.Length; i++) 56 { 57 //16進制轉換 58 strResult = strResult + bytResult[i].ToString("X"); 59 } 60 return strResult.ToLower(); 61 }
c:接口寫完,即可部署服務器
d:回到微信服務器,將url api填好, token和代碼里面的token值保持一致,點擊:提交(驗證要求在5秒內有響應,如果失敗,多點幾次提交)
三:測試賬號開發
開發-->開發者工具--->公眾平台測試賬號
a:可獲得測試用的:appid, appscrect
b:填入接口配置信息: url API , token
c: 掃一掃測試號碼二維碼(即:測試用的測試公眾號)
d: 可接入模板消息,如:(json單引號會出錯)
訂單編號:{{orderid.DATA}}
親愛的,{{name.DATA}},
於{{time.DATA}},發貨
點擊查看詳情
沒有對外服務器的時候,如何自己申請(買)一個服務器域名,進行配置
這邊我使用的是騰訊雲產品:
https://cloud.tencent.com/guide