https://github.com/willin/mp-sdk
微信小程序服务器端SDK。Wechat Mini Program (mp) Serverside SDK
https://github.com/willin/mp-sdk
mini-program mp sdk wechat
Last synced: 10 months ago
JSON representation
微信小程序服务器端SDK。Wechat Mini Program (mp) Serverside SDK
- Host: GitHub
- URL: https://github.com/willin/mp-sdk
- Owner: willin
- License: apache-2.0
- Created: 2019-06-11T12:59:02.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-07-12T00:02:22.000Z (over 2 years ago)
- Last Synced: 2025-04-10T17:41:55.687Z (11 months ago)
- Topics: mini-program, mp, sdk, wechat
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/mp-sdk
- Size: 313 KB
- Stars: 11
- Watchers: 2
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mp-sdk
比官方SDK更好用的微信小程序服务器端SDK。
> 已经疯狂得不能用代码行数(总计`89`行,包含空行和debug)来衡量该项目了,代码仅有 `1,310`字节(净化后)。
[](https://github.com/willin) [](https://npmjs.org/package/mp-sdk) [](https://npmjs.org/package/mp-sdk) [](https://npmjs.org/package/mp-sdk) [](https://codebeat.co/projects/github-com-willin-mp-sdk-master)
Minimum, Flexible, Scalable.
支持Lazy Require。
- [安装使用](#%E5%AE%89%E8%A3%85%E4%BD%BF%E7%94%A8)
- [基本使用示例](#%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B)
- [二维码处理示例](#%E4%BA%8C%E7%BB%B4%E7%A0%81%E5%A4%84%E7%90%86%E7%A4%BA%E4%BE%8B)
- [解密示例](#%E8%A7%A3%E5%AF%86%E7%A4%BA%E4%BE%8B)
- [参考文档](#%E5%8F%82%E8%80%83%E6%96%87%E6%A1%A3)
- [相关项目推荐](#%E7%9B%B8%E5%85%B3%E9%A1%B9%E7%9B%AE%E6%8E%A8%E8%8D%90)
- [License](#license)
## 安装使用
```bash
yarn add mp-sdk
# 或
npm i --save mp-sdk
```
### 基本使用示例
```js
const sdk = require('mp-sdk');
const cloud = sdk('appid', 'secret');
// appid、secret、access_token、grant_type 4个字段可以忽略不写
cloud.auth.code2Session({
js_code: 'js_code'
}).then(result => {
// code here
});
```
### 二维码处理示例
```js
const sdk = require('mp-sdk');
const fs = require('fs');
const cloud = sdk('appid', 'secret');
cloud.wxacode.getUnlimited({
scene: 'test',
path: 'page/index?foo=bar'
}).then((d) => {
fs.writeFileSync('1.png', d);
});
```
### 解密示例
本SDK中加入解密方法 `.crypto.decryptData`。
传入一个对象,包含以下三个参数:
- sessionKey: 登录会话的凭证
- encryptedData: 密文数据
- iv:初始化向量
以上三个字段均为必须,在微信开发者文档中也有具体的说明。
```js
const sdk = require('mp-sdk');
const cloud = sdk('appid', 'secret');
// 注意: 该方法并不放回 Promise 而是直接返回解密结果。
const result = cloud.crypto.decryptData({
sessionKey: 'xxx',
encryptedData: 'xxx',
iv: 'xxx'
});
console.log(result);
// 可能结果如下: watermark 对象用作校验,具体请参考文档
// {
// phoneNumber: 'xxxx',
// purePhoneNumber: 'xxx',
// countryCode: 'xxxx',
// watermark: { timestamp: 1560502778, appid: 'wxc0783c8b8bfef8d3' }
// }
```
## 参考文档
- 接口文档: https://developers.weixin.qq.com/miniprogram/dev/api-backend/
- 登录(sessionKey获取): https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html
- 数据加密解密: https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html
- 小程序端授权: https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/authorize.html
## 相关项目推荐
- 阿里云SDK: https://github.com/willin/waliyun
- 腾讯云SDK: https://github.com/willin/wqcloud
- 网易云音乐SDK: https://github.com/willin/wnm
- Rescuetime SDK: https://github.com/willin/wrescuetime
## License
Apache 2.0