api文档:云通信号码认证服务_云产品主页-阿里云OpenAPI开发者门户 (aliyun.com)
reg.vue
<template>
<div>
<input class="sl-input" v-model="phone" type="number" maxlength="11" placeholder="手机号" />
<button @click="sendSms">短信注册</button>
</div>
</template>
<script>
export default {
data() {
return {
phone: '', // 用于绑定输入框的值
};
},
methods: {
sendSms() {
uni.request({
url: 'http://localhost:3000/valisms', // 你的后端服务地址
method: 'POST',
data: {
phoneNumbers: this.phone,
},
success: (res) => {
if (res.data.success) {
console.log('短信发送成功:', res.data.response);
uni.showToast({
title: '短信发送成功',
icon: 'success'
});
} else {
console.log('短信发送失败:', res.data.message);
uni.showToast({
title: '短信发送失败',
icon: 'none'
});
}
},
fail: (err) => {
console.error('发送请求失败:', err);
uni.showToast({
title: '请求失败',
icon: 'none'
});
}
});
},
},
};
</script>
<style scoped>
</style>
sendSms() {
console.log('电话号:',this.phone);
uni.request({
url: 'http://localhost:3000/valisms', // 你的后端服务地址
method: 'POST',
data: {
phoneNumbers:this.phone ,
},
success: (res) => {
if (res.data.success) {
console.log('短信发送成功:', res.data.response);
uni.showToast({
title: '短信发送成功',
icon: 'success'
});
} else {
console.log('短信发送失败:', res.data.message);
uni.showToast({
title: '短信发送失败',
icon: 'none'
});
}
},
fail: (err) => {
console.error('发送请求失败:', err);
uni.showToast({
title: '请求失败',
icon: 'none'
});
}
});
},
cloudsms.js:
'use strict';
const Dypnsapi20170525 = require('@alicloud/dypnsapi20170525');
const OpenApi = require('@alicloud/openapi-client');
const Console = require('@alicloud/tea-console');
const Util = require('@alicloud/tea-util');
const Tea = require('@alicloud/tea-typescript');class Client {
static createClient() {
let config = new OpenApi.Config({
accessKeyId: 'xxxxxxxx',
accessKeySecret: 'xxxxxxxx',
});
config.endpoint = `dypnsapi.aliyuncs.com`;
return new Dypnsapi20170525.default(config);
}
static async cloudSms(phoneNumbers, code) {
let client = Client.createClient();
console.log('验证码是:',code);
let sendSmsVerifyCodeRequest = new Dypnsapi20170525.SendSmsVerifyCodeRequest({
phoneNumber: phoneNumbers,
signName: '',
templateCode: '',
templateParam: JSON.stringify({ code:code }),
});
let runtime = new Util.RuntimeOptions({ });
try {
let resp = await client.sendSmsVerifyCodeWithOptions(sendSmsVerifyCodeRequest, runtime);
Console.default.log(Util.default.toJSONString(resp));
} catch (error) {
console.log(error.message);
console.log(error.data["Recommend"]);
Util.default.assertAsString(error.message);
}
}
}
module.exports = Client;
// exports.Client = Client;
// Client.smscloud(process.argv.slice(2));
cloudserver.js:
const express = require('express');
const bodyParser = require('body-parser');
const ClientSms = require('./cloudsms.js');const app = express();
const port = 3000;
app.use(bodyParser.json());
app.post('/valisms', async (req, res) => {
const { phoneNumbers} = req.body;
console.log('电话号:',phoneNumbers);
const code = Math.floor(100000 + Math.random() * 900000).toString();
console.log('验证码是:',code);
try {
const response = await ClientSms.cloudSms(phoneNumbers, code);
res.json({ success: true, message: '短信发送成功', response });
} catch (error) {
console.error('短信发送失败的具体原因:', error);
res.status(500).json({ success: false, message: '短信发送失败' });
}
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});