https://github.com/chunbo007/wechat-mp
微信開放平臺管理工具
微信開放平臺服務(wù)商一般有多套小程序需要管理,幫人代開發(fā)時需要客戶不停掃碼授權(quán),體驗極其不好。對于SAAS小程序服務(wù)商也可以在運維平臺中嘗試二開對接開放平臺的api,但是項目多了以后每個運營平臺都需要重復(fù)造輪子,而且維護(hù)成本也高,所以萌生了寫一個通用管理微信開放平臺的工具。
微信官方其實有提供“第三方平臺云服務(wù)”,但需要付費使用或者下載他們的源碼本地部署,可惜官方只提供了GO語言的版本,所以只能自己寫一個了。
目前打算先實現(xiàn)以下功能
下載代碼
// github
git clone https://github.com/chunbo007/wechat-mp.git
// gitee
git clone https://gitee.com/chunboli/wechat-mp.git
安裝相關(guān)依賴
// 安裝php相關(guān)依賴
cd wechat-mp
composer install
// 安裝前端依賴
cd front
yarn install --production(推薦) 或 npm install --production
配置文件
本地運行
以上根據(jù)自己實際情況選擇,然后運行前端項目
之后打開瀏覽器訪問 http://localhost:8001 就可以了,默認(rèn)用戶名/密碼都是 admin
打包上線
location /admin {
proxy_pass http://127.0.0.1:8789/admin;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
}
location /wechat {
proxy_pass http://127.0.0.1:8789/wechat;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
}
location /openapi {
proxy_pass http://127.0.0.1:8789/openapi;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
}
location / {
try_files $uri $uri/ /index.html;
}
如果你需要的不只是小程序版本管理相關(guān)的功能,需要自己實現(xiàn)其他功能,可能需要用到
component_access_token、component_appid、authorizer_appid、authorizer_access_token
等參數(shù),由于你在自己的項目中刷新token,可能會讓wechat-mp的token失效,或者wechat-mp會讓你項目的token失效
所以留了一個開放接口,供其他項目獲取相關(guān)token,具體調(diào)用方式如下
// 生成簽名
function generateSign($params, $secretKey): string
{
// 將參數(shù)按照鍵名進(jìn)行字典排序
ksort($params);
// 拼接參數(shù)和對應(yīng)的值
$signStr = '';
foreach ($params as $key => $value) {
$signStr .= $key . '=' . $value . '&';
}
// 拼接密鑰
$signStr .= 'key=' . $secretKey;
// 使用哈希函數(shù)計算簽名,這里使用 MD5 作為示例
return strtoupper(md5($signStr));
}
$params = [
'platform_appid' => '', // 開放平臺的appid
'appid' => '', // 小程序的appid
'time' => time(),
];
$secretKey = '';
$params['sign'] = generateSign($params,$secretKey);
$url = 'https://xxxxxx.com/openapi/getToken?' . http_build_query($params);
$res = file_get_contents($url);
//var_dump(json_decode($res,true));
如有不明白的可以留言,歡迎各位提pr
如果對你的項目有幫助,歡迎star,謝謝