帝国cms 微信小程序用户注册的api接口
也做了好几个项目了,都是关于帝国cms 微信小程序,所有接口都是自己写,还是挺好用的。今天分享一个帝国cms 微信小程序用户注册的api接口
<?php
// ini_set('display_errors', 'On');
// error_reporting(E_ALL);
defined('ECMSAPI_MOD') or exit;
date_default_timezone_set("PRC"); // 系统使用北京时间
$qianming = $public_r['add_login'];
require_once(ECMS_PATH . "ecmsapi/_mod/wechat/vendor/autoload.php");
use \Firebase\JWT\JWT;
define('KEY', $qianming);
require_once(ECMS_PATH . "e/extend/mp/wxtokenvalid.php");
// 过滤非POST方式请求
if (!$api->isPost()) {
// 使用fun类的json方法快速输出JSON结构数据
$api->load('fun')->json(0, '非法提交');
}
// 加载user核心类,方便后续调用
$userapi = $api->load('user');
$username = $api->post('username');
$wxopenid = $api->post('wxopenid');
$unionid = $api->post('unionid');
$checkcode = $api->post('checkcode', 0, 'intval');
$checked = $api->post('checked', 1, 'intval');
$userpic = $api->post('userpic');
$phone = $api->post('phone');
$qqopenid = "oHDMG7nf3H57OA8QOtx2S3-lOB0k";
// 用户名过滤函数
function filterEmoji($str) {
$str = preg_replace('/[^\p{Han}\p{L}\p{N}\-\_\s]+/u', '', $str);
return $str;
}
$password = 'ailu'.time();
$password2=$password ;
$username = filterEmoji($username);
if($username=='' || $username=='微信用户'){
$ord = $empire->fetch1("SELECT MAX(userid) AS maxid FROM phome_enewsmember");
// 如果没有记录,从1开始;否则+1
$maxId = $ord['maxuserid'] ? intval($ord['maxuserid']) + 1 : 1;
// 保证为8位,前面补0
$sequence = str_pad($maxId, 6, '0', STR_PAD_LEFT);
$username='alchat-' . $sequence;
}
// print_r($username);
// 如果开启了验证码,先验证验证码是否正确
if ($checkcode) {
// 获取验证码并用RepPostVar函数过滤
$code = $api->post('code', '', 'RepPostVar');
// 验证码为空直接返回错误提示
if ($code === '') {
$api->load('fun')->json(0, '验证码不能为空');
}
// 验证码是否正确
$isok = $userapi->code('register', $code);
// 若没有通过验证将返回错误提示
if ($isok !== 1) {
$api->load('fun')->json(0, $isok < 0 ? '验证码超时' : '验证码不正确');
}
$userapi->code('register'); // 清理掉验证码
}
// 验证用户名与密码是否为空
if ($username === '') {
$api->load('fun')->json(0, '用户名不能为空');
}
if ($username === '游客') {
$username = 'wx' . time();
}
if ($password === '') {
$api->load('fun')->json(0, '密码不能为空');
}
if ($password !== $password2) {
$api->load('fun')->json(0, '两次输入的密码不一致');
}
if ($wxopenid === '' || $qqopenid === '') {
$api->load('fun')->json(0, $wxopenid . $qqopenid);
}
// 定义要写入数据库的字段信息
$data = [
'username' => $username,
'password' => $password,
'wxopenid' => $wxopenid,
'unionid' => $unionid,
'checked' => $checked,
'phone' => $phone,
];
// 写入数据,写入成功后返回用户ID
$userid = $userapi->insert($data);
if ($userid != '') {
$api->load('db')->update('[!db.pre!]enewsmemberadd', ['userpic' => $userpic], 'userid=' . $userid);
}
if (false === $userid) {
// 注册失败返回错误信息
$api->load('fun')->json(0, $userapi->getError());
}
// 根据实际情况选择是否将用户设置为登录状态
$isok = $userapi->setSession($userid);
// 注册成功后发送注册到微信公众号
if (false === $isok) {
// 设置失败后返回错误信息
$api->load('fun')->json(0, $userapi->getError());
} else {
// 设置成功后获取用户数据
$user = $userapi->one($username);
$nowtime = time();
$token = [
'iss' => 'https://www.abc.com', // 签发者
'aud' => 'https://www.abc.com', // jwt所面向的用户
'iat' => $nowtime, // 签发时间
'nbf' => $nowtime + 10, // 在什么时间之后该jwt才可用
'exp' => $nowtime + 604800, // 过期时间-10min
'data' => [
'userid' => $user['userid'],
'username' => $user['username'],
]
];
$jwt = JWT::encode($token, KEY);
$res['result'] = 'success';
$res['jwt'] = 'bearer' . $jwt;
$user = [
'token' => $res['jwt'],
'username' => $user['username'],
'userid' => $user['userid'],
'groupid' => $user['groupid'],
'email' => $user['email'],
'unionid' => $user['unionid'],
];
// 消息模板通知注册成功
$openids = $public_r['add_openids'];
$retime = date("Y-m-d H:i:s", time());
$weChatAPI = new WeChatAPI();
$template_id = 'RWaywhQQ7V0';
$tzurl = 'https://www.abc.com/e/extend/mp/reginfo.php?userid='.$user['userid'];
// 消息内容数据
$wxdt = [
'phrase2' => ['value' => $user['username']],
'time6' => ['value' => $retime],
];
// 将字符串转换为数组
$openidList = explode(',', $openids);
$wx_send_result = []; // 用于记录发送结果
// 循环发送给每个用户
foreach ($openidList as $openid) {
$openid = trim($openid);
if (!empty($openid)) {
$result = $weChatAPI->send($openid, $template_id, $tzurl, $wxdt);
$wx_send_result[] = [
'openid' => $openid,
'status' => is_array($result) && isset($result['errcode']) && $result['errcode'] == 0 ? 'success' : 'OK',
'raw' => $result
];
}
}
// 把微信发送结果附加到用户数据中
$user['wx_template_send_result'] = $wx_send_result;
// 输出JSON数据
$api->load('fun')->json(1, $user);
}
本文分享了一个基于帝国CMS的微信小程序用户注册API接口实现。该接口主要功能包括:接收用户信息参数,验证用户名、密码及验证码,过滤特殊字符,自动生成默认用户名,将用户数据写入数据库,并生成JWT令牌实现用户认证。接口还包含微信公众号通知功能,在用户注册成功后向管理员发送模板消息提醒。核心代码展示了用户注册的完整流程,包括参数处理、数据验证、数据库操作和结果返回等环节,适用于微信小程序与帝国CMS系统的集成开发。
更多推荐


所有评论(0)