UniApp 快速开始
- AI 快速开始
- 完整开发指引
准备工作
在开始之前,请确保您已完成以下准备:
- 开通云开发环境:开通云开发环境
- 安装 Node.js:下载 Node.js(建议 LTS 版本)
Client
配置你的 AI 工具以连接 CloudBase 能力 支持本地与托管两种连接方式,详见 连接方式。
步骤 1:安装 / 配置 CloudBase
步骤 2:和 AI 对话
在 AI 对话中依次输入以下内容:
prompt
安装 CloudBase Skills:命令 npx skills add tencentcloudbase/cloudbase-skills -y使用 CloudBase Skills: 使用 CloudBase Skills:在 UniApp 应用中集成 CloudBase Web SDK,包括认证和数据库功能准备工作
在开始之前,请确保您已完成以下准备:
- 开通云开发环境:开通云开发环境
- 安装 Node.js:下载 Node.js(建议 LTS 版本)
更多详情请参考:UniApp 完整文档
安装 SDK
@cloudbase/js-sdk 配合 @cloudbase/adapter-uni-app 可以让您在 uni-app 项目中使用 JavaScript 访问 CloudBase 服务和资源。
npm
npm i @cloudbase/js-sdk @cloudbase/adapter-uni-app
yarn
yarn add @cloudbase/js-sdk @cloudbase/adapter-uni-app
pnpm
pnpm add @cloudbase/js-sdk @cloudbase/adapter-uni-app
初始化 SDK
新增如下代码到您的 uni-app 项目
./utils/cloudbase.js
import cloudbaseSDK from "@cloudbase/js-sdk";
import adapter from "@cloudbase/adapter-uni-app";
// 传入配置选项
const options = {
uni: uni, // 传入 uni 对象,用于图形验证码功能
};
cloudbaseSDK.useAdapters(adapter, options);
const cloudbase = cloudbaseSDK.init({
// 环境 ID
env: "{%ENV_ID%}",
// 地域
region: "{%REGION%}",
// 匿名访问令牌
accessKey: "{%PUBLISHABLE_KEY%}",
});
export default cloudbase;
身份认证
- 短信验证码注册
- 邮箱验证码注册
- 账号密码登录
- 短信验证码登录
- 邮箱验证码登录
- 小程序手机号登录
- 微信 OpenID 登录
使用 短信验证码注册 请先前往 身份认证/登录方式 开启 短信验证码
调用方式:
import cloudbase from "./utils/cloudbase";
const auth = cloudbase.auth();
// 发送验证码
const res = await auth.getVerification({ phone_number: phone });
// 验证验证码
const verifyRes = await auth.verify({
verification_id: verificationId,
verification_code: code,
});
// 注册(如用户已存在则自动登录)
await auth.signUp({
phone_number: `+86 ${phone}`,
verification_code: code,
verification_token: verifyRes.verification_token,
name: `user_${phone.slice(-4)}`,
password: "admin@123",
});
完整示例:
<template>
<view>
<view>
<text>手机号:</text>
<input v-model="phone" placeholder="13800000000" />
</view>
<view>
<text>验证码:</text>
<input v-model="code" placeholder="验证码" />
<button :disabled="!phone" @click="sendCode">发送验证码</button>
</view>
<button :disabled="!verificationId || !code" @click="register">注册</button>
<text>{{ message }}</text>
</view>
</template>
<script>
import cloudbase from "./utils/cloudbase";
export default {
data() {
return {
phone: "",
code: "",
verificationId: "",
message: "",
};
},
methods: {
// 发送验证码
async sendCode() {
try {
const auth = cloudbase.auth();
const res = await auth.getVerification({ phone_number: this.phone });
this.verificationId = res.verification_id;
this.message = "验证码已发送!";
} catch (error) {
this.message = "发送失败:" + error.message;
}
},
// 注册
async register() {
try {
const auth = cloudbase.auth();
// 验证验证码
const verifyRes = await auth.verify({
verification_id: this.verificationId,
verification_code: this.code,
});
// 注册(如用户已存在则自动登录)
await auth.signUp({
phone_number: `+86 ${this.phone}`,
verification_code: this.code,
verification_token: verifyRes.verification_token,
name: `user_${this.phone.slice(-4)}`,
password: "admin@123",
});
this.message = "注册成功!";
} catch (error) {
this.message = "注册失败:" + error.message;
}
},
},
};
</script>
使用 邮箱验证码注册 请先前往 身份认证/登录方式 开启 邮箱验证码
调用方式:
import cloudbase from "./utils/cloudbase";
const auth = cloudbase.auth();
// 发送验证码
const res = await auth.getVerification({ email });
// 验证验证码
const verifyRes = await auth.verify({
verification_id: verificationId,
verification_code: code,
});
// 注册(如用户已存在则自动登录)
await auth.signUp({
email,
verification_code: code,
verification_token: verifyRes.verification_token,
name: `user_${email.slice(-4)}`,
password: "admin@123",
});
完整示例:
<template>
<view>
<view>
<text>邮箱:</text>
<input v-model="email" placeholder="example@email.com" />
</view>
<view>
<text>验证码:</text>
<input v-model="code" placeholder="验证码" />
<button :disabled="!email" @click="sendCode">发送验证码</button>
</view>
<button :disabled="!verificationId || !code" @click="register">注册</button>
<text>{{ message }}</text>
</view>
</template>
<script>
import cloudbase from "./utils/cloudbase";
export default {
data() {
return {
email: "",
code: "",
verificationId: "",
message: "",
};
},
methods: {
// 发送验证码
async sendCode() {
try {
const auth = cloudbase.auth();
const res = await auth.getVerification({ email: this.email });
this.verificationId = res.verification_id;
this.message = "验证码已发送!";
} catch (error) {
this.message = "发送失败:" + error.message;
}
},
// 注册
async register() {
try {
const auth = cloudbase.auth();
// 验证验证码
const verifyRes = await auth.verify({
verification_id: this.verificationId,
verification_code: this.code,
});
// 注册(如用户已存在则自动登录)
await auth.signUp({
email: this.email,
verification_code: this.code,
verification_token: verifyRes.verification_token,
name: `user_${this.email.slice(-4)}`,
password: "admin@123",
});
this.message = "注册成功!";
} catch (error) {
this.message = "注册失败:" + error.message;
}
},
},
};
</script>
使用 账号密码登录 请先前往 身份认证/登录方式 开启 用户名密码登录
调用方式:
import cloudbase from "./utils/cloudbase";
const auth = cloudbase.auth();
await auth.signIn({
username, // 可以是用户名、手机号或邮箱
password,
});
完整示例:
<template>
<view>
<view>
<text>账号:</text>
<input v-model="username" placeholder="用户名/手机号/邮箱" />
<text>注:手机号登录请添加区号 +86</text>
</view>
<view>
<text>密码:</text>
<input type="password" v-model="password" placeholder="请输入密码" />
</view>
<button :disabled="!username || !password" @click="login">登录</button>
<text>{{ message }}</text>
</view>
</template>
<script>
import cloudbase from "./utils/cloudbase";
export default {
data() {
return {
username: "",
password: "",
message: "",
};
},
methods: {
// 登录
async login() {
try {
const auth = cloudbase.auth();
await auth.signIn({
username: this.username, // 可以是用户名、手机号或邮箱
password: this.password,
});
this.message = "登录成功!";
} catch (error) {
this.message = "登录失败:" + error.message;
}
},
},
};
</script>
使用 短信验证码登录 请先前往 身份认证/登录方式 开启 短信验证码登录
调 用方式:
import cloudbase from "./utils/cloudbase";
const auth = cloudbase.auth();
// 发送验证码
const res = await auth.getVerification({ phone_number: `+86 ${phone}` });
// 登录
await auth.signInWithSms({
verificationInfo: res,
verificationCode: code,
phoneNum: `+86 ${phone}`,
});
完整示例:
<template>
<view>
<view>
<text>手机号:</text>
<input v-model="phone" placeholder="13800000000" />
</view>
<view>
<text>验证码:</text>
<input v-model="code" placeholder="验证码" />
<button @click="sendCode" :disabled="!phone">发送验证码</button>
</view>
<button @click="login" :disabled="!verificationInfo || !code">登录</button>
<text>{{ message }}</text>
</view>
</template>
<script>
import cloudbase from "./utils/cloudbase";
export default {
data() {
return {
phone: "",
code: "",
verificationInfo: null,
message: "",
};
},
methods: {
// 发送验证码
async sendCode() {
try {
const auth = cloudbase.auth();
const res = await auth.getVerification({
phone_number: `+86 ${this.phone}`,
});
this.verificationInfo = res;
this.message = "验证码已发送!";
} catch (error) {
this.message = "发送失败:" + error.message;
}
},
// 登录
async login() {
try {
const auth = cloudbase.auth();
await auth.signInWithSms({
verificationInfo: this.verificationInfo,
verificationCode: this.code,
phoneNum: `+86 ${this.phone}`,
});
this.message = "登录成功!";
} catch (error) {
this.message = "登录失败:" + error.message;
}
},
},
};
</script>
使用 邮箱验证码登录 请先前往 身份认证/登录方式 开启