
REST API命名規范的終極指南:清晰度和一致性的最佳實踐
商業模式:
功能模塊 | 工具/平臺 |
---|---|
在線開發環境 | Replit |
AI 引擎 | OpenAI API |
前端框架 | 微信小程序原生 / WePY, 支付寶小程序原生 |
后端語言 | Node.js / Python |
Web 框架 | Express (Express.js) / FastAPI (FastAPI) |
數據存儲 | MongoDB Atlas / Firebase |
支付對接 | 微信支付 / 支付寶開放平臺 |
部署平臺 | Replit 內置部署 / Vercel / Railway |
日志與監控 | Sentry / Logflare |
提示:初期可省略專用數據庫,使用 Replit 提供的文件存儲;隨著用戶量增長,再遷移至云端數據庫。
以「智能對話問答+文本生成」為核心,搭建一款面向 校園課業答疑 或 興趣愛好問答 的小程序。主要功能:
以下示例以 Node.js + Express + OpenAI 為后端,微信小程序為前端,演示核心流程。支付寶小程序同理,API 名稱有所差異,文末附對接要點。
在項目根目錄新建 .env
,添加:
OPENAI_API_KEY=你的_openai_api_key
WECHAT_APPID=你的小程序 AppID
WECHAT_MCHID=你的商戶號
WECHAT_APIKEY=你的支付密鑰
安裝依賴:
npm install express openai weixin-pay body-parser cors
// index.js
import express from 'express';
import 'dotenv/config';
import bodyParser from 'body-parser';
import cors from 'cors';
import { Configuration, OpenAIApi } from 'openai';
import { WechatPay } from 'weixin-pay';
const app = express();
app.use(cors(), bodyParser.json());
/** OpenAI 配置 */
const openai = new OpenAIApi(new Configuration({
apiKey: process.env.OPENAI_API_KEY,
}));
/** 微信支付配置 */
const wxpay = new WechatPay({
appid: process.env.WECHAT_APPID,
mch_id: process.env.WECHAT_MCHID,
partner_key: process.env.WECHAT_APIKEY,
});
/** AI 問答接口 */
app.post('/api/ask', async (req, res) = > {
const { messages } = req.body;
try {
const rsp = await openai.createChatCompletion({
model: 'gpt-3.5-turbo',
messages,
temperature: 0.6,
max_tokens: 800,
});
res.json({ reply: rsp.data.choices[0].message.content });
} catch (e) {
console.error(e);
res.status(500).json({ error: 'AI 服務異常' });
}
});
/** 發起支付接口 */
app.post('/api/pay', (req, res) = > {
const { openid, description, amount } = req.body;
const params = {
body: description,
out_trade_no: Date.now().toString(),
total_fee: amount, // 分為單位
spbill_create_ip: req.ip,
notify_url: 'https://你的域名/api/pay/notify',
trade_type: 'JSAPI',
openid,
};
wxpay.getBrandWCPayRequestParams(params, (err, payargs) = > {
if (err) return res.status(500).json({ error: err });
res.json(payargs);
});
});
/** 支付回調通知 */
app.post('/api/pay/notify', bodyParser.xml(), (req, res) = > {
const notifyData = req.body.xml;
// TODO: 驗簽、訂單狀態更新
res.send({ xml: { return_code: 'SUCCESS', return_msg: 'OK' } });
});
const port = process.env.PORT || 3000;
app.listen(port, () = > console.log(后端服務已啟動,端口 ${port}
));
使用微信開發者工具,新建小程序項目,AppID 填寫你的 WECHAT_APPID。
app.js
App({
globalData: {
apiUrl: 'https://你的 Replit 域名',
history: [],
}
});
pages/index/index.wxml
< view class="container" >
< textarea placeholder="輸入你的問題..." bindinput="onInput" value="{{input}}"/ >
< button bindtap="askAI" > 提問 AI < /button >
< view wx:for="{{history}}" wx:key="index" >
< text > {{item.role}}: {{item.content}} < /text >
< /view >
< button bindtap="toPay" > 購買會員 < /button >
< /view >
pages/index/index.js
const app = getApp();
Page({
data: {
input: '',
history: [],
},
onInput(e) {
this.setData({ input: e.detail.value });
},
async askAI() {
const { input, history } = this.data;
const messages = [...history, { role: 'user', content: input }];
const res = await wx.request({
url: ${app.globalData.apiUrl}/api/ask
,
method: 'POST',
data: { messages },
});
const reply = res.data.reply;
const newHistory = [...messages, { role: 'assistant', content: reply }];
this.setData({ history: newHistory, input: '' });
},
async toPay() {
const session = await wx.login();
const res = await wx.request({
url: ${app.globalData.apiUrl}/api/pay
,
method: 'POST',
data: { openid: session.code, description: 'AI 會員', amount: 100 },
});
wx.requestPayment(res.data);
},
});
index.wxss
.container { padding: 20px; }
textarea { width: 100%; height: 100px; border: 1px solid #ccc; }
button { margin-top: 10px; }
https://your-domain.com
。alipay-trade-sdk
或直接調用 REST 接口。支付流程:
alipay.trade.create
;my.tradePay
接口。notify_id
與簽名,確保支付安全。示例后端代碼略與微信一致,僅需替換支付 SDK 與接口參數。
如何控制 API 成本?
max_tokens
;如何提升響應速度?
如何保障穩定運營?
收益來源 | 收費標準 | 預估用戶量 | 月收入(RMB) |
---|---|---|---|
按次付費 | 3 元/次,平均每日 200 次 | 6000 次 | 18,000 |
會員訂閱 | 50 元/月 × 100 人 | 100 人 | 5,000 |
廣告分成 | 0.2 元/次 × 10,000 次 | 10,000 次 | 2,000 |
合計 | — | — | 25,000+ |
即使保守估計,僅憑 200 人日常使用,也能實現日入 500+ 的目標。
推廣啟動:制定校園和線上推廣計劃,實現穩定引流與變現。
行動提示:現在就打開你的瀏覽器,登錄 Replit,創建第一個 AI 小程序項目,2 小時后,你就能擁有第一筆小程序收入!
本文提到的平臺與文檔:
- Replit:https://replit.com/
- OpenAI API:https://platform.openai.com/
- 微信支付:https://pay.weixin.qq.com/
- 支付寶開放平臺:https://open.alipay.com/
- Express.js:https://expressjs.com/
- FastAPI:https://fastapi.tiangolo.com/
祝你在 2025 年的副業道路上捷報頻傳,開啟 AI 小程序賺錢快車道!