专业微网站营销,多用户网站建设,中国工商注册网查询登记,东莞网站排名优化一、技术实现核心前置条件在进行技术开发前#xff0c;需先完成以下准备工作#xff0c;确保后续开发顺畅#xff1a;已拥有认证通过的微信服务号#xff0c;获取到核心凭证#xff1a;AppID#xff08;应用唯一标识#xff09;、AppSecret#xff08;应用密钥#xf…一、技术实现核心前置条件在进行技术开发前需先完成以下准备工作确保后续开发顺畅已拥有认证通过的微信服务号获取到核心凭证AppID应用唯一标识、AppSecret应用密钥需保密不可明文暴露在前端。已搭建基础技术架构后端服务Java/SpringBoot、Python/Flask/Django、PHP 等均可、数据库MySQL 推荐用于存储绑定关系、云存储阿里云 OSS / 腾讯云 COS用于存放孩子照片避免本地存储容量不足。已完成「家长手机号 ↔ 公众号 OpenID」的绑定关联关系存入 MySQL 数据库核心字段id、parent_mobile家长手机号、wechat_openid微信唯一标识、student_id学生 ID、create_time绑定时间。已完成「学生信息 ↔ 照片信息」的关联数据库存储字段student_id、student_name学生姓名、photo_url照片云存储地址、photo_update_time照片更新时间。二、核心技术方案选型2 种主流实现根据需求场景优先推荐「模板消息推送」支持批量精准推送不限用户触发适合定期发送学习照片其次是「客服消息推送」适用于用户主动查询场景以下分别详细讲解技术实现。方案一模板消息推送推荐批量定向发送核心方案1. 实现流程概述申请微信模板消息权限及符合需求的消息模板获取微信接口调用凭证access_token所有微信接口的通用凭证从数据库批量查询家长 OpenID、对应学生照片 URL 等信息调用微信模板消息接口分批次推送消息给家长记录推送结果便于后续统计排查。2. 关键步骤技术实现步骤 1申请模板消息模板登录微信公众平台 → 「功能」→ 「模板消息」→ 「模板库」→ 申请符合需求的模板示例模板内容plaintext【XX学校/机构】您的孩子{{studentName.DATA}}近期学习照片已更新点击下方链接即可查看高清照片 {{photoUrl.DATA}} 温馨提示照片仅用于家校沟通请勿随意转发。申请通过后获取模板 IDtemplate_id后续接口调用需使用。步骤 2获取微信接口凭证access_token微信所有接口调用均需携带access_token有效期 2 小时7200 秒需实现缓存机制避免频繁调用接口导致限流。接口信息接口地址https://api.weixin.qq.com/cgi-bin/token请求方式GET请求参数参数名说明grant_type固定值client_credentialappid服务号的 AppIDsecret服务号的 AppSecret代码示例Python/Flaskpython运行import requests import redis import json from datetime import datetime, timedelta # 初始化Redis用于缓存access_token无Redis可使用本地缓存/数据库缓存 redis_client redis.Redis(host127.0.0.1, port6379, db0, password你的Redis密码) # 配置信息 APPID 你的服务号AppID APPSECRET 你的服务号AppSecret ACCESS_TOKEN_KEY wechat_access_token def get_wechat_access_token(): # 先从Redis获取缓存的access_token cached_token redis_client.get(ACCESS_TOKEN_KEY) if cached_token: return cached_token.decode(utf-8) # 缓存不存在调用接口获取 url fhttps://api.weixin.qq.com/cgi-bin/token?grant_typeclient_credentialappid{APPID}secret{APPSECRET} response requests.get(url) result json.loads(response.text) if access_token in result: access_token result[access_token] expires_in result[expires_in] # 7200秒 # 缓存到Redis过期时间比接口返回少300秒避免过期 redis_client.setex(ACCESS_TOKEN_KEY, expires_in - 300, access_token) return access_token else: # 抛出异常或记录错误如AppID/AppSecret错误 raise Exception(f获取access_token失败{result})步骤 3批量调用模板消息接口推送通知接口信息接口地址https://api.weixin.qq.com/cgi-bin/message/template/send请求方式POST请求头Content-Type: application/json请求参数JSON 格式json{ touser: 家长的OpenID, // 单个家长的OpenID template_id: 你的模板ID, // 申请到的模板消息ID url: 可选点击模板消息跳转的H5页面地址照片查看页面, topcolor: #FF0000, // 模板顶部颜色可选 data: { studentName: { value: 张三, // 学生姓名 color: #173177 }, photoUrl: { value: https://xxx.oss-cn-beijing.aliyuncs.com/photos/zhangsan.jpg, // 照片URL color: #173177 } } }代码示例Python/Flask批量分批次推送python运行import pymysql import time # 数据库配置 DB_CONFIG { host: 127.0.0.1, user: 数据库用户名, password: 数据库密码, database: 你的数据库名, charset: utf8mb4 } # 分批次配置避免接口限流每批次500人间隔15分钟 BATCH_SIZE 500 BATCH_INTERVAL 15 * 60 # 秒 def get_parent_student_photo_data(): 从数据库查询家长OpenID、学生姓名、照片URL关联数据 conn pymysql.connect(**DB_CONFIG) cursor conn.cursor(pymysql.cursors.DictCursor) # 关联家长表和学生照片表查询有效数据 sql SELECT p.wechat_openid, s.student_name, s.photo_url FROM parent_wechat_bind p LEFT JOIN student_photo s ON p.student_id s.student_id WHERE p.wechat_openid IS NOT NULL AND s.photo_url IS NOT NULL cursor.execute(sql) data_list cursor.fetchall() cursor.close() conn.close() return data_list def send_template_message(access_token, openid, template_id, student_name, photo_url): 发送单个模板消息 url fhttps://api.weixin.qq.com/cgi-bin/message/template/send?access_token{access_token} payload { touser: openid, template_id: template_id, url: photo_url, # 点击跳转至照片页面 topcolor: #333333, data: { studentName: { value: student_name, color: #173177 }, photoUrl: { value: f点击查看{photo_url}, color: #0088ff } } } try: response requests.post(url, jsonpayload) result json.loads(response.text) if result.get(errcode) 0: print(f推送成功OpenID{openid}学生{student_name}) return True else: print(f推送失败OpenID{openid}错误信息{result}) return False except Exception as e: print(f推送异常OpenID{openid}异常信息{str(e)}) return False def batch_send_template_messages(): 批量分批次发送模板消息 # 1. 获取access_token try: access_token get_wechat_access_token() except Exception as e: print(f获取access_token失败{str(e)}) return # 2. 获取待推送数据 data_list get_parent_student_photo_data() if not data_list: print(暂无待推送的家长数据) return # 3. 分批次推送 total_count len(data_list) template_id 你的模板ID # 替换为实际模板ID for i in range(0, total_count, BATCH_SIZE): batch_data data_list[i:iBATCH_SIZE] batch_num (i // BATCH_SIZE) 1 print(f开始推送第{batch_num}批次共{len(batch_data)}条数据) for item in batch_data: openid item[wechat_openid] student_name item[student_name] photo_url item[photo_url] send_template_message(access_token, openid, template_id, student_name, photo_url) time.sleep(0.5) # 单个推送间隔0.5秒避免瞬时请求过多 # 最后一批次无需等待 if i BATCH_SIZE total_count: print(f第{batch_num}批次推送完成等待{BATCH_INTERVAL/60}分钟后推送下一批次) time.sleep(BATCH_INTERVAL) print(f所有批次推送完成总计推送{total_count}条数据) # 执行批量推送 if __name__ __main__: batch_send_template_messages()步骤 4关键注意事项分批次推送3000 位家长建议分 6 批次每批次 500 人间隔 10-30 分钟避免触发微信接口限流微信默认单个公众号模板消息接口日调用限额为 10 万次单批次请求不宜过多。照片权限控制跳转的 H5 照片页面需增加验证如手机号验证码、OpenID 校验避免非对应家长查看照片保障隐私。异常处理对推送失败的家长如 OpenID 无效、用户已取消关注需记录日志后续手动排查补推。方案二客服消息推送用户主动触发场景1. 实现流程概述配置公众号服务器回调接收用户消息用户在公众号对话框发送关键词如 “我的孩子照片”后端接收回调消息通过 OpenID 查询对应学生照片调用客服消息接口给用户发送照片或照片链接。2. 关键步骤技术实现步骤 1配置公众号消息推送回调登录微信公众平台 → 「设置与开发」→ 「基本配置」→ 「服务器配置」→ 开启并填写以下信息服务器地址URL你的后端接口地址需为 HTTPS 协议微信要求用于接收微信推送的用户消息Token自定义字符串如 “wechat_token_123”后端需用该 Token 验证消息合法性EncodingAESKey可选用于消息加密建议填写并使用安全模式。步骤 2后端接收用户消息并验证合法性代码示例Python/Flask接收并验证微信消息python运行from flask import Flask, request, abort import hashlib import xml.etree.ElementTree as ET app Flask(__name__) # 公众号配置的Token WECHAT_TOKEN 你的微信Token def validate_wechat_signature(signature, timestamp, nonce): 验证微信消息签名确保消息来自微信服务器 # 1. 将token、timestamp、nonce按字典序排序 params [WECHAT_TOKEN, timestamp, nonce] params.sort() # 2. 拼接为字符串并进行sha1加密 sign_str .join(params).encode(utf-8) sha1_sign hashlib.sha1(sign_str).hexdigest() # 3. 对比加密结果与签名是否一致 return sha1_sign signature app.route(/wechat/callback, methods[GET, POST]) def wechat_callback(): # GET请求微信验证服务器有效性 if request.method GET: signature request.args.get(signature) timestamp request.args.get(timestamp) nonce request.args.get(nonce) echostr request.args.get(echostr) if validate_wechat_signature(signature, timestamp, nonce): return echostr else: abort(403) # POST请求接收用户发送的消息 elif request.method POST: # 解析XML格式消息 xml_data request.data root ET.fromstring(xml_data) # 提取消息核心字段 to_user_name root.find(ToUserName).text # 公众号ID from_user_name root.find(FromUserName).text # 用户OpenID msg_type root.find(MsgType).text # 消息类型text文本消息 content root.find(Content).text if msg_type text else # 用户发送的文本内容 # 处理用户查询照片请求 if msg_type text and content in [照片, 我的孩子照片, 查询照片]: # 异步处理消息推送避免同步请求超时 from threading import Thread Thread(targetsend_custom_photo_message, args(from_user_name,)).start() # 微信要求返回空XML否则会持续推送消息 return xml ToUserName![CDATA[{from_user}]]/ToUserName FromUserName![CDATA[{to_user}]]/FromUserName CreateTime{time}/CreateTime MsgType![CDATA[text]]/MsgType Content![CDATA[正在为您查询孩子照片请稍候...]]/Content /xml.format( from_userfrom_user_name, to_userto_user_name, timeint(time.time()) ) if __name__ __main__: app.run(host0.0.0.0, port443, ssl_context(你的证书文件.pem, 你的私钥文件.key))步骤 3调用客服消息接口发送照片客服消息支持直接发送图片单张≤5M或图片链接以下是发送图片的代码示例python运行def send_custom_photo_message(openid): 给用户发送客服消息图片 # 1. 获取access_token try: access_token get_wechat_access_token() except Exception as e: print(f获取access_token失败{str(e)}) return # 2. 从数据库查询对应学生照片先获取学生ID再查询照片 conn pymysql.connect(**DB_CONFIG) cursor conn.cursor(pymysql.cursors.DictCursor) # 查询学生ID sql SELECT student_id FROM parent_wechat_bind WHERE wechat_openid %s cursor.execute(sql, (openid,)) parent_data cursor.fetchone() if not parent_data: print(f未查询到该用户绑定信息OpenID{openid}) cursor.close() conn.close() return student_id parent_data[student_id] # 查询照片信息优先获取最新照片 sql SELECT photo_url FROM student_photo WHERE student_id %s ORDER BY photo_update_time DESC LIMIT 1 cursor.execute(sql, (student_id,)) photo_data cursor.fetchone() cursor.close() conn.close() if not photo_data: # 发送无照片提示 send_custom_text_message(openid, access_token, 暂未查询到您孩子的学习照片请稍后再试~) return photo_url photo_data[photo_url] # 3. 调用客服消息接口发送图片需先将图片上传到微信临时素材库获取media_id media_id upload_photo_to_wechat(access_token, photo_url) if not media_id: send_custom_text_message(openid, access_token, 照片上传失败请稍后再试~) return # 客服消息接口地址 url fhttps://api.weixin.qq.com/cgi-bin/message/custom/send?access_token{access_token} payload { touser: openid, msgtype: image, image: { media_id: media_id } } try: response requests.post(url, jsonpayload) result json.loads(response.text) if result.get(errcode) 0: print(f客服消息图片推送成功OpenID{openid}) else: print(f客服消息推送失败OpenID{openid}错误信息{result}) except Exception as e: print(f客服消息推送异常OpenID{openid}异常信息{str(e)}) def upload_photo_to_wechat(access_token, photo_url): 将照片上传到微信临时素材库获取media_id有效期3天 # 先下载照片到本地或直接从云存储读取二进制流 try: photo_response requests.get(photo_url) photo_content photo_response.content except Exception as e: print(f下载照片失败{photo_url}异常{str(e)}) return None # 上传到微信临时素材库 url fhttps://api.weixin.qq.com/cgi-bin/media/upload?access_token{access_token}typeimage files { media: (photo.jpg, photo_content, image/jpeg) } try: response requests.post(url, filesfiles) result json.loads(response.text) if media_id in result: return result[media_id] else: print(f照片上传到微信素材库失败{result}) return None except Exception as e: print(f上传素材异常{str(e)}) return None def send_custom_text_message(openid, access_token, content): 发送文本类型客服消息 url fhttps://api.weixin.qq.com/cgi-bin/message/custom/send?access_token{access_token} payload { touser: openid, msgtype: text, text: { content: content } } requests.post(url, jsonpayload)步骤 4关键注意事项触发限制仅在用户主动发送消息后 48 小时内可推送客服消息超出时间无法主动推送。素材有效期临时素材库的media_id有效期为 3 天若需长期使用可上传到永久素材库需符合微信永久素材规范。图片大小单张图片不超过 5M支持 JPG/PNG 格式建议压缩后再上传。三、核心技术架构总结数据层MySQL 存储「家长 - 微信 OpenID - 学生」绑定关系、学生照片信息Redis 缓存access_token提升接口调用效率。存储层阿里云 OSS / 腾讯云 COS 存储孩子学习照片提供稳定的 HTTPS 访问链接。应用层后端服务Python/Java/PHP实现凭证获取、消息推送、数据库操作、回调处理等核心逻辑。接口层调用微信开放平台接口access_token接口、模板消息接口、客服消息接口、素材上传接口完成消息推送。四、无开发能力的替代方案若你暂无技术团队可直接使用微信第三方家校服务平台如「校宝在线」「微校通」「腾讯智慧校园」操作流程如下将认证后的服务号授权给第三方平台批量导入家长手机号、学生信息提前完成微信绑定上传孩子照片并关联对应学生使用平台现成的 “家校通知” 功能批量推送照片通知无需自行编写代码。总结优先选择「模板消息推送」实现批量精准通知核心是获取access_token、分批次调用微信模板消息接口关键技术点OpenID 与手机号绑定、access_token缓存、分批次推送避限流、照片隐私验证客服消息仅适用于用户主动查询场景需依赖消息回调和素材上传无开发能力可通过第三方家校平台快速落地降低技术门槛。