manghe/app/api/controller/Base.php
2025-03-21 17:30:31 +00:00

276 lines
8.3 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\api\controller;
use app\MyController;
use app\common\model\User;
use app\common\model\UserAccount;
use think\facade\Request;
use app\common\model\User as UserModel;
header('Access-Control-Allow-Origin: *');
/**
* 商户后台控制器基类
*/
class Base extends MyController
{
public $page = '10';
/**
* 获取当前用户信息
*/
protected function getUser()
{
$token = request()->header('token', '');
$user_account = UserAccount::getInfo(['account_token' => $token]);
if (!$user_account) {
$data['status'] = '-1';
$data['msg'] = "没有找到用户信息1";
exit(json_encode($data, 1));
}
$user_account_is = user_md5($user_account['user_id'] . $user_account['token_num'] . $user_account['token_time']);
if ($token !== $user_account_is) {
$data['status'] = '-1';
$data['msg'] = "没有找到用户信息2";
exit(json_encode($data, 1));
}
$user = User::where(['id' => $user_account['user_id']])->find();
if (!$user) {
$data['status'] = '-1';
$data['msg'] = "没有找到用户信息3";
exit(json_encode($data, 1));
}
if ($user['status'] != 1) {
$data['status'] = '-1';
$data['msg'] = "该账户已被封号,请联系客服解封";
exit(json_encode($data, 1));
}
// 检查用户是否有UID如果没有则生成
if (empty($user['uid'])) {
// 获取用户uid配置
$user_config = getConfig('user_config');
if (!empty($user_config) && isset($user_config['uid_type'])) {
// 生成UID
$uid = $this->generateUidForUser($user['id']);
if ($uid) {
// 更新用户UID
User::where('id', $user['id'])->update(['uid' => $uid]);
$user['uid'] = $uid;
}
}
}
return $user;
}
/**
* 为用户生成UID
*
* @param int $user_id 用户ID
* @return string 生成的UID
*/
protected function generateUidForUser($user_id)
{
// 获取用户uid配置
$user_config = getConfig('user_config');
if (empty($user_config) || !isset($user_config['uid_type'])) {
return (string)$user_id; // 默认使用真实ID
}
$uid_type = (int)$user_config['uid_type'];
$uid_length = isset($user_config['uid_length']) ? (int)$user_config['uid_length'] : 6;
// 限制长度范围
if ($uid_length < 4) {
$uid_length = 4;
} elseif ($uid_length > 16) {
$uid_length = 16;
}
// 根据不同类型生成UID
switch ($uid_type) {
case 0: // 真实ID
return (string)$user_id;
case 1: // 数字ID
// 生成不以0开头的随机数字
$max_attempts = 10;
$attempt = 0;
while ($attempt < $max_attempts) {
// 生成随机数字
$min = pow(10, $uid_length - 1);
$max = pow(10, $uid_length) - 1;
$uid = (string)mt_rand($min, $max);
// 检查是否唯一
$exists = User::where('uid', $uid)->count();
if ($exists == 0) {
return $uid;
}
$attempt++;
}
// 如果多次尝试后仍无法生成唯一ID则使用更可靠的方法
return $this->generateUniqueNumericId($uid_length);
case 2: // 随机字符和数字
$length = max(8, $uid_length); // 字母数字混合至少8位
$max_attempts = 10;
$attempt = 0;
while ($attempt < $max_attempts) {
// 生成随机字母数字
$characters = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ'; // 排除容易混淆的字符
$uid = '';
// 确保第一个字符不是数字
$uid .= $characters[mt_rand(8, strlen($characters) - 1)]; // 从字母开始
// 生成剩余字符
for ($i = 1; $i < $length; $i++) {
$uid .= $characters[mt_rand(0, strlen($characters) - 1)];
}
// 检查是否唯一
$exists = User::where('uid', $uid)->count();
if ($exists == 0) {
return $uid;
}
$attempt++;
}
// 如果多次尝试后仍无法生成唯一ID使用时间戳+随机数确保唯一性
return $this->generateUniqueAlphaNumId($length);
default:
return (string)$user_id;
}
}
/**
* 生成唯一的数字ID备用方法
*/
private function generateUniqueNumericId($length)
{
// 使用时间微秒 + 随机数的方式保证唯一性
$base = microtime(true) * 10000 . mt_rand(1000, 9999);
$uid = substr(preg_replace('/[^0-9]/', '', $base), 0, $length);
// 确保不以0开头且长度正确
while (strlen($uid) < $length || $uid[0] == '0') {
$uid = mt_rand(1, 9) . substr($uid, 1);
$uid = substr($uid, 0, $length);
}
return $uid;
}
/**
* 生成唯一的字母数字ID备用方法
*/
private function generateUniqueAlphaNumId($length)
{
// 使用时间戳 + 随机字符
$base = time() . mt_rand(1000, 9999);
$hash = md5($base);
// 转换为字母数字混合
$uid = '';
$chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'; // 排除容易混淆的字符
// 确保第一个字符是字母
$uid .= $chars[mt_rand(0, 25)]; // 前26个是字母
// 生成剩余字符
for ($i = 1; $i < $length; $i++) {
$uid .= $chars[mt_rand(0, strlen($chars) - 1)];
}
return $uid;
}
/**
*
* 是否H5客户端
* @return bool
*/
protected function ish5()
{
$client = request()->header('client', '');
if ($client == "h5") {
return true;
}
return false;
}
/**
* 获取当前用户信息
*/
protected function getuserid()
{
$user_id = 0;
$token = request()->header('token', '');
$user_account = UserAccount::getInfo(['account_token' => $token]);
if ($user_account) {
$user = User::where(['id' => $user_account['user_id']])->find();
if ($user && $user['status'] == 1) {
$user_id = $user['id'];
}
}
return $user_id;
}
/*
* 判断优惠券是否领过
*/
protected function getUserid1($is_force = true)
{
$header = Request::header();
if ($header['token'] == '') {
$token = $this->request->param('token');
} else {
$token = $header['token'];
}
$header['logintype'] = 1;
if (!isset($header['logintype']) or !in_array($header['logintype'], [1, 2])) {
return false;
}
if (!$token) {
if ($is_force) {
return false;
}
}
$user_account = UserAccount::getInfo(['account_token' => $token]);
if (empty($user_account)) {
if ($is_force) {
return false;
}
return false;
}
$user = UserModel::getInfo(['id' => $user_account['user_id']]);
if (empty($user)) {
return false;
}
if ($user['status'] != 1) {
return false;
}
#判断优惠券是否领过
if ($user['is_use_coupon'] == 1) {
return false;
} else {
return true;
}
}
}