Community编程与开发github.com

tencentcloudbase/auth-wechat-miniprogram

CloudBase 微信小程序原生认证指南。适用于需要小程序身份处理、获取 OPENID/UNIONID 或管理 `wx.cloud` 认证行为的项目。

auth-wechat-miniprogram 是什么?

此技能为开发者提供在微信小程序中集成 CloudBase 原生认证的完整指南。它涵盖了自动获取用户身份(如 OPENID 和 UNIONID)的流程,以及如何通过 `wx.cloud` 方法处理认证行为。该指南适用于需要无感登录、自动身份验证的小程序项目。支持在微信开发者工具及腾讯云 CloudBase 环境中使用。核心机制利用微信小程序的 `wx.login` 与 CloudBase SDK 协同工作,实现后端的安全用户标识提取与传递。无需配置额外认证页面,即可实现开箱即用的身份管理。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/tencentcloudbase/skills/tree/main/skills/auth-wechat-miniprogram

Installed? Explore more 编程与开发 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

auth-wechat-miniprogram 是做什么的?

CloudBase 微信小程序原生认证指南。此技能适用于用户需要小程序身份处理、获取 OPENID/UNIONID 或在项目中使用 wx.cloud 认证行为的场景,其中登录方式是原生且自动的。

概述

在微信小程序开发中,通常需要获取用户的唯一标识(如 OPENID 或 UNIONID)以进行业务逻辑处理。此技能基于腾讯云 CloudBase 的 wx.cloud 能力,提供了一种原生、自动的认证方案。

核心功能

  • 自动获取身份:通过 wx.login 接口自动获取临时 code,并换取 OPENID/UNIONID。
  • 集成 CloudBase:利用 wx.cloud.callFunction 调用云函数,在后端安全处理用户身份。
  • 无感认证:用户无需手动登录或授权页面,应用即可自动完成身份绑定。

使用步骤

1. 初始化 CloudBase SDK

在项目的 app.js 或页面入口文件进行初始化:

const cloud = require('wx-server-sdk')
cloud.init({
  env: 'your-env-id'
})

2. 获取用户身份

在小程序启动时或需要身份的时刻调用:

wx.login({
  success: res => {
    if (res.code) {
      // 将 code 传递给云函数
      wx.cloud.callFunction({
        name: 'login',
        data: {
          code: res.code
        },
        success: cloudRes => {
          const { openid, unionid } = cloudRes.result
          // 处理用户身份
        },
        fail: err => {
          console.error('认证失败:', err)
        }
      })
    } else {
      console.error('获取 code 失败:', res.errMsg)
    }
  }
})

3. 云函数处理

创建云函数 login 并编写处理逻辑:

const cloud = require('wx-server-sdk')
cloud.init()
exports.main = async (event, context) => {
  const { code } = event
  try {
    const result = await cloud.getOpenData({
      code
    })
    return {
      openid: result.openid,
      unionid: result.unionid
    }
  } catch (err) {
    return {
      error: err
    }
  }
}

注意事项

  • 确保已开通 CloudBase 环境并启用微信登录能力。
  • 生产环境建议将用户身份存储在云端数据库中以供后续查询。
  • 此方案适用于后端无额外认证服务器的场景。

相关资源

Individual skills in this repo

This repo contains 8 individual skills — each has its own dedicated page.

tencentcloudbase/ai-model-wechat

Use this skill for WeChat Mini Program AI via wx.cloud.extend.AI (小程序, 企业微信小程序, wx.cloud apps). Features generateText and streamText with callbacks (onText, onEvent, onFinish). Models via wx.cloud.extend.AI.createModel with groups hunyuan-exp (小程序成长计划), cloudbase (main managed), or custom-*. Model IDs (deepseek-v4-flash, deepseek-v3.2, hunyuan-2.0-instruct-20251111, glm-5, kimi-k2.6) go in the data wrapper model field. API differs from JS/Node SDK — streamText needs data wrapper, generateText returns raw response. MUST run two-step preflight before code — see body. Keywords: Mini Program AI, wx.cloud.extend.AI, 小程序成长计划, ai_miniprogram_inspire_plan, Token Credits 资源包, generateText, streamText, createModel, hunyuan-exp, TokenHub, Hunyuan, DeepSeek, GLM, Kimi, MiniMax. NOT for browser/Web (use ai-model-web), Node.js backend (use ai-model-nodejs), or image generation (use ai-model-nodejs).

tencentcloudbase/cloudbase-document-database-in-wechat-miniprog

Use CloudBase document database WeChat MiniProgram SDK to query, create, update, and delete data. Supports complex queries, pagination, aggregation, and geolocation queries.

tencentcloudbase/cloudbase-platform

CloudBase platform overview and routing guide. This skill should be used when users need high-level capability selection, platform concepts, console navigation, or cross-platform best practices before choosing a more specific implementation skill.

tencentcloudbase/cloud-functions

CloudBase function runtime guide for building, deploying, and debugging your own Event Functions or HTTP Functions. This skill should be used when users need application runtime code on CloudBase, not when they are merely calling CloudBase official platform APIs.

tencentcloudbase/miniprogram-development

WeChat Mini Program development skill for building, debugging, previewing, testing, publishing, and optimizing mini program projects. This skill should be used when users ask to create, develop, modify, debug, preview, test, deploy, publish, launch, review, or optimize WeChat Mini Programs, mini program pages, components, `tabBar`, routing, navigation, icon assets, project structure, project configuration, `project.config.json`, `appid` setup, device preview, real-device validation, WeChat Developer Tools workflows, `miniprogram-ci` preview/upload flows, or mini program release processes. It should also be used when users explicitly mention CloudBase, `wx.cloud`, Tencent CloudBase, 腾讯云开发, or 云开发 in a mini program project.

tencentcloudbase/spec-workflow

Use when medium-to-large changes need explicit requirements, technical design, and task planning before implementation, especially for multi-module work, unclear acceptance criteria, or architecture-heavy requests.

tencentcloudbase/ui-design

Use when users need visual direction, interface hierarchy, layout decisions, design specifications, or prototypes before implementing a Web or mini program UI.

tencentcloudbase/web-development

Use when users need to implement, integrate, debug, build, deploy, or validate a Web frontend after the product direction is already clear, especially for React, Vue, Vite, browser flows, or CloudBase Web integration.

相关技能