代码提交
This commit is contained in:
@@ -2,71 +2,33 @@
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\common\model\UserModel;
|
||||
use app\common\model\UserTokenModel;
|
||||
use think\Controller;
|
||||
use think\facade\Env;
|
||||
|
||||
class BaseController extends Controller {
|
||||
|
||||
|
||||
/**
|
||||
* 令牌
|
||||
*
|
||||
* @var null
|
||||
* @var string
|
||||
*/
|
||||
protected $token = NULL;
|
||||
protected $token = '';
|
||||
|
||||
protected function initialize() {
|
||||
parent::initialize();
|
||||
protected $baseUrl;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
$this->baseUrl = Env::get('api.wechat_url');
|
||||
// 允许跨域
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: *');
|
||||
header('Access-Control-Allow-Headers: *');
|
||||
}
|
||||
|
||||
/**
|
||||
* 接口调用成功 JSON
|
||||
*
|
||||
* @param null $data
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
protected function jsonSucc($data = NULL) {
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '操作成功',
|
||||
'data' => $data,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 接口调用错误 JSON
|
||||
*
|
||||
* @param $error
|
||||
* @param int $code
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
protected function jsonFail($error, $code = 500) {
|
||||
return json([
|
||||
'code' => $code,
|
||||
'msg' => $error,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取URL
|
||||
*
|
||||
* @param $url
|
||||
* @return string
|
||||
*/
|
||||
protected function absoluteUrl($url) {
|
||||
return (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . ($url{0} === '/' ? $url : '/' . $url);
|
||||
}
|
||||
|
||||
/**
|
||||
* 小数格式化
|
||||
*
|
||||
* @param $float
|
||||
* @return float
|
||||
*/
|
||||
protected function floatFormat($float) {
|
||||
return floatval($float);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
class BaseLoginController extends BaseController {
|
||||
|
||||
protected function initialize() {
|
||||
parent::initialize();
|
||||
}
|
||||
}
|
||||
75
Server/application/api/controller/StatsController.php
Normal file
75
Server/application/api/controller/StatsController.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
/**
|
||||
* 统计控制器
|
||||
* Class StatsController
|
||||
* @package app\frontend\controller
|
||||
*/
|
||||
class StatsController extends BaseController
|
||||
{
|
||||
/**
|
||||
* API客户端类型
|
||||
*/
|
||||
const CLIENT_TYPE = 'system';
|
||||
|
||||
/**
|
||||
* 账号基本信息
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function basicData()
|
||||
{
|
||||
$authorization = trim($this->request->header('authorization', ''));
|
||||
if (empty($authorization)) {
|
||||
return errorJson('缺少授权信息');
|
||||
}
|
||||
|
||||
$headerData = ['client:' . self::CLIENT_TYPE];
|
||||
$header = setHeader($headerData, $authorization, 'plain');
|
||||
|
||||
try {
|
||||
$result = requestCurl($this->baseUrl . '/api/DashBoard/ListHomePageStatistics', ['refresh' => 10000], 'GET', $header);
|
||||
return successJson($result);
|
||||
} catch (\Exception $e) {
|
||||
return errorJson('获取基础数据失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 好友统计
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function FansStatistics(){
|
||||
/* 参数说明
|
||||
lidu 数据搜索类型 0 小时 1 天 2月
|
||||
from to 时间 当lidu为 0时(2025-03-12 09:54:42) 当lidu为 1时(2025-03-12) 当lidu为 2时(2025-03)
|
||||
*/
|
||||
|
||||
$authorization = trim($this->request->header('authorization', ''));
|
||||
$lidu = trim($this->request->param('lidu', ''));
|
||||
$from = trim($this->request->param('from', ''));
|
||||
$to = trim($this->request->param('to', ''));
|
||||
|
||||
if (empty($authorization)) {
|
||||
return errorJson('缺少授权信息');
|
||||
}
|
||||
|
||||
$params = [
|
||||
'lidu' => $lidu,
|
||||
'from' => $from,
|
||||
'to' => $to,
|
||||
];
|
||||
|
||||
$headerData = ['client:' . self::CLIENT_TYPE];
|
||||
$header = setHeader($headerData, $authorization, 'plain');
|
||||
|
||||
try {
|
||||
$result = requestCurl($this->baseUrl . 'api/DashBoard/listStatisticsCountDTOByCreateTimeAsync', $params, 'GET', $header);
|
||||
return successJson($result);
|
||||
} catch (\Exception $e) {
|
||||
return errorJson('获取粉丝统计数据失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
374
Server/application/api/controller/UserController.php
Normal file
374
Server/application/api/controller/UserController.php
Normal file
@@ -0,0 +1,374 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\common\model\CompanyAccountModel;
|
||||
use think\facade\Env;
|
||||
use think\Response;
|
||||
|
||||
/**
|
||||
* 用户控制器
|
||||
* Class UserController
|
||||
* @package app\frontend\controller
|
||||
*/
|
||||
class UserController extends BaseController
|
||||
{
|
||||
/**
|
||||
* API客户端类型
|
||||
*/
|
||||
const CLIENT_TYPE = 'system';
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function login()
|
||||
{
|
||||
// 获取并验证参数
|
||||
$params = $this->validateLoginParams();
|
||||
if (!is_array($params)) {
|
||||
return $params;
|
||||
}
|
||||
|
||||
// 验证账号是否存在
|
||||
$existingAccount = CompanyAccountModel::where('userName', $params['username'])->find();
|
||||
if (empty($existingAccount)) {
|
||||
// 记录登录失败日志
|
||||
recordUserLog(0, $params['username'], 'LOGIN', '账号不存在', $params, 500, '账号不存在');
|
||||
return errorJson('账号不存在');
|
||||
}
|
||||
|
||||
// 获取验证码会话ID和用户输入的验证码
|
||||
$verifySessionId = $this->request->param('verifySessionId', '');
|
||||
$verifyCode = $this->request->param('verifyCode', '');
|
||||
|
||||
// 设置请求头
|
||||
$headerData = ['client:' . self::CLIENT_TYPE];
|
||||
|
||||
// 如果存在验证码信息,添加到请求头
|
||||
if (!empty($verifySessionId) && !empty($verifyCode)) {
|
||||
$headerData[] = 'verifysessionid:' . $verifySessionId;
|
||||
$headerData[] = 'verifycode:' . $verifyCode;
|
||||
}
|
||||
|
||||
$header = setHeader($headerData, '', 'plain');
|
||||
|
||||
try {
|
||||
// 请求登录接口
|
||||
$result = requestCurl($this->baseUrl . 'token', $params, 'POST', $header);
|
||||
$result_array = handleApiResponse($result);
|
||||
|
||||
if (is_array($result_array) && isset($result_array['error'])) {
|
||||
// 记录登录失败日志
|
||||
recordUserLog(0, $params['username'], 'LOGIN', '登录失败', $params, 500, $result_array['error_description']);
|
||||
return errorJson($result_array['error_description']);
|
||||
}
|
||||
|
||||
// 获取客户端IP地址
|
||||
$ip = $this->request->ip();
|
||||
|
||||
// 登录成功,更新密码信息和登录信息
|
||||
$updateData = [
|
||||
'passwordMd5' => md5($params['password']),
|
||||
'passwordLocal' => localEncrypt($params['password']),
|
||||
'lastLoginIp' => $ip,
|
||||
'lastLoginTime' => time()
|
||||
];
|
||||
|
||||
// 更新密码信息
|
||||
CompanyAccountModel::where('userName', $params['username'])->update($updateData);
|
||||
|
||||
// 记录登录成功日志
|
||||
recordUserLog($existingAccount['id'], $params['username'], 'LOGIN', '登录成功', [], 200, '登录成功');
|
||||
|
||||
return successJson($result_array);
|
||||
} catch (\Exception $e) {
|
||||
// 记录登录异常日志
|
||||
recordUserLog(0, $params['username'], 'LOGIN', '登录请求失败', $params, 500, $e->getMessage());
|
||||
return errorJson('登录请求失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取新的token
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getNewToken()
|
||||
{
|
||||
$grant_type = $this->request->param('grant_type', 'refresh_token');
|
||||
$refresh_token = $this->request->param('refresh_token', '');
|
||||
$authorization = $this->request->header('authorization', '');
|
||||
|
||||
if (empty($grant_type) || empty($authorization)) {
|
||||
return errorJson('参数错误');
|
||||
}
|
||||
|
||||
$params = [
|
||||
'grant_type' => $grant_type,
|
||||
'refresh_token' => $refresh_token,
|
||||
];
|
||||
|
||||
|
||||
|
||||
$headerData = ['client:' . self::CLIENT_TYPE];
|
||||
$header = setHeader($headerData, $authorization, 'system');
|
||||
|
||||
try {
|
||||
$result = requestCurl($this->baseUrl . 'token', $params, 'POST', $header);
|
||||
$result_array = handleApiResponse($result);
|
||||
|
||||
if (is_array($result_array) && isset($result_array['error'])) {
|
||||
recordUserLog(0, '', 'REFRESH_TOKEN', '刷新token失败', $params, 500, $result_array['error_description']);
|
||||
return errorJson($result_array['error_description']);
|
||||
}
|
||||
|
||||
recordUserLog(0, '', 'REFRESH_TOKEN', '刷新token成功', $params, 200, '刷新成功');
|
||||
return successJson($result_array);
|
||||
} catch (\Exception $e) {
|
||||
recordUserLog(0, '', 'REFRESH_TOKEN', '刷新token异常', $params, 500, $e->getMessage());
|
||||
return errorJson('获取新token失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商户基本信息
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getAccountInfo()
|
||||
{
|
||||
$authorization = trim($this->request->header('authorization', ''));
|
||||
if (empty($authorization)) {
|
||||
return errorJson('缺少授权信息');
|
||||
}
|
||||
|
||||
$headerData = ['client:' . self::CLIENT_TYPE];
|
||||
$header = setHeader($headerData, $authorization, 'plain');
|
||||
|
||||
try {
|
||||
$result = requestCurl($this->baseUrl . 'api/Account/self', [], 'GET', $header);
|
||||
$response = handleApiResponse($result);
|
||||
|
||||
if (!empty($response['account'])) {
|
||||
$accountData = $response['account'];
|
||||
|
||||
// 准备数据库字段映射,保持驼峰命名
|
||||
$dbData = [
|
||||
'accountId' => $accountData['id'],
|
||||
'realName' => $accountData['realName'],
|
||||
'nickname' => $accountData['nickname'],
|
||||
'memo' => $accountData['memo'],
|
||||
'avatar' => $accountData['avatar'],
|
||||
'userName' => $accountData['userName'],
|
||||
'secret' => $accountData['secret'],
|
||||
'accountType' => $accountData['accountType'],
|
||||
'departmentId' => $accountData['departmentId'],
|
||||
'useGoogleSecretKey' => $accountData['useGoogleSecretKey'],
|
||||
'hasVerifyGoogleSecret' => $accountData['hasVerifyGoogleSecret'],
|
||||
'updateTime' => time()
|
||||
];
|
||||
|
||||
|
||||
// 查找是否存在该账户
|
||||
$existingAccount = CompanyAccountModel::where('userName', $accountData['userName'])->find();
|
||||
if ($existingAccount) {
|
||||
// 更新现有记录
|
||||
CompanyAccountModel::where('userName', $accountData['userName'])->update($dbData);
|
||||
} else {
|
||||
// 创建新记录
|
||||
$dbData['createTime'] = time();
|
||||
CompanyAccountModel::create($dbData);
|
||||
}
|
||||
return successJson($response['account']);
|
||||
}else{
|
||||
return successJson($response);
|
||||
}
|
||||
|
||||
|
||||
} catch (\Exception $e) {
|
||||
recordUserLog(0, '', 'GET_ACCOUNT_INFO', '获取账户信息异常', [], 500, $e->getMessage());
|
||||
return errorJson('获取账户信息失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改密码
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function modifyPwd()
|
||||
{
|
||||
// 获取并验证参数
|
||||
$params = $this->validateModifyPwdParams();
|
||||
if (!is_array($params)) {
|
||||
return $params;
|
||||
}
|
||||
|
||||
$authorization = trim($this->request->header('authorization', ''));
|
||||
if (empty($authorization)) {
|
||||
return errorJson('缺少授权信息');
|
||||
}
|
||||
|
||||
$headerData = ['client:' . self::CLIENT_TYPE];
|
||||
$header = setHeader($headerData, $authorization, 'plain');
|
||||
|
||||
try {
|
||||
$result = requestCurl($this->baseUrl . 'api/Account/self', $params, 'PUT', $header);
|
||||
$response = handleApiResponse($result);
|
||||
|
||||
if (empty($response)) {
|
||||
// 获取当前用户信息
|
||||
$currentUser = CompanyAccountModel::where('token', $authorization)->find();
|
||||
if ($currentUser) {
|
||||
recordUserLog($currentUser['id'], $currentUser['userName'], 'MODIFY_PASSWORD', '修改密码成功', [], 200, '修改成功');
|
||||
}
|
||||
return successJson(['message' => '修改成功']);
|
||||
}
|
||||
|
||||
recordUserLog(0, '', 'MODIFY_PASSWORD', '修改密码失败', $params, 500, $response);
|
||||
return errorJson($response);
|
||||
} catch (\Exception $e) {
|
||||
recordUserLog(0, '', 'MODIFY_PASSWORD', '修改密码异常', $params, 500, $e->getMessage());
|
||||
return errorJson('修改密码失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 登出
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function logout()
|
||||
{
|
||||
$authorization = trim($this->request->header('authorization', ''));
|
||||
if (empty($authorization)) {
|
||||
return errorJson('缺少授权信息');
|
||||
}
|
||||
|
||||
$headerData = ['client:' . self::CLIENT_TYPE];
|
||||
$header = setHeader($headerData, $authorization, 'plain');
|
||||
|
||||
try {
|
||||
// 获取当前用户信息
|
||||
$currentUser = CompanyAccountModel::where('token', $authorization)->find();
|
||||
|
||||
// 调用外部退出登录接口
|
||||
$result = requestCurl($this->baseUrl . 'api/Account/SignOut', [], 'POST', $header);
|
||||
|
||||
if ($currentUser) {
|
||||
recordUserLog($currentUser['id'], $currentUser['userName'], 'LOGOUT', '退出登录成功', [], 200, '退出成功');
|
||||
}
|
||||
|
||||
return successJson([] , '退出成功');
|
||||
} catch (\Exception $e) {
|
||||
recordUserLog(0, '', 'LOGOUT', '退出登录异常', [], 500, $e->getMessage());
|
||||
return errorJson('退出登录失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取验证码
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getVerifyCode()
|
||||
{
|
||||
$headerData = ['client:' . self::CLIENT_TYPE];
|
||||
$header = setHeader($headerData, '', 'plain');
|
||||
|
||||
try {
|
||||
$result = requestCurl($this->baseUrl . 'api/Account/getVerifyCode', [], 'GET', $header);
|
||||
$response = handleApiResponse($result);
|
||||
|
||||
// 检查返回的数据格式
|
||||
if (is_array($response)) {
|
||||
// 如果verifyCodeImage和verifySessionId都不为null,返回它们
|
||||
if (!empty($response['verifyCodeImage']) && !empty($response['verifySessionId'])) {
|
||||
return successJson([
|
||||
'verifyCodeImage' => $response['verifyCodeImage'],
|
||||
'verifySessionId' => $response['verifySessionId']
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果不是预期的格式,返回原始数据
|
||||
return successJson($response);
|
||||
} catch (\Exception $e) {
|
||||
return errorJson('获取验证码失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证登录参数
|
||||
* @return array|\think\response\Json
|
||||
*/
|
||||
private function validateLoginParams()
|
||||
{
|
||||
$username = trim($this->request->param('username', ''));
|
||||
$password = trim($this->request->param('password', ''));
|
||||
$verifyCode = trim($this->request->param('verifyCode', ''));
|
||||
$verifySessionId = trim($this->request->param('verifySessionId', ''));
|
||||
|
||||
if (empty($username) || empty($password)) {
|
||||
return errorJson('用户名和密码不能为空');
|
||||
}
|
||||
|
||||
// 验证密码格式
|
||||
$passwordValidation = validateString($password, 'password',['max_length' => 20]);
|
||||
if (!$passwordValidation['status']) {
|
||||
return errorJson($passwordValidation['message']);
|
||||
}
|
||||
|
||||
// 如果提供了验证码,验证格式
|
||||
if (!empty($verifyCode)) {
|
||||
if (empty($verifySessionId)) {
|
||||
return errorJson('验证码会话ID不能为空');
|
||||
}
|
||||
// 验证码格式验证(假设是4位数字)
|
||||
if (!preg_match('/^\d{4}$/', $verifyCode)) {
|
||||
return errorJson('验证码格式不正确');
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'grant_type' => 'password',
|
||||
'username' => $username,
|
||||
'password' => $password,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证修改密码参数
|
||||
* @return array|\think\response\Json
|
||||
*/
|
||||
private function validateModifyPwdParams()
|
||||
{
|
||||
$cPw = trim($this->request->param('cPw', ''));
|
||||
$newPw = trim($this->request->param('newPw', ''));
|
||||
$oldPw = trim($this->request->param('oldPw', ''));
|
||||
|
||||
if (empty($cPw) || empty($newPw) || empty($oldPw)) {
|
||||
return errorJson('密码参数不完整');
|
||||
}
|
||||
|
||||
if ($newPw !== $cPw) {
|
||||
return errorJson('两次输入的新密码不一致');
|
||||
}
|
||||
|
||||
// 验证新密码格式
|
||||
$passwordValidation = validateString($newPw, 'password');
|
||||
if (!$passwordValidation['status']) {
|
||||
return errorJson($passwordValidation['message']);
|
||||
}
|
||||
|
||||
return [
|
||||
'cPw' => $cPw,
|
||||
'newPw' => $newPw,
|
||||
'oldPw' => $oldPw,
|
||||
];
|
||||
}
|
||||
}
|
||||
102
Server/application/api/controller/WechatController.php
Normal file
102
Server/application/api/controller/WechatController.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\common\model\WechatAccountModel;
|
||||
|
||||
class WechatController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 保存微信账号数据到数据库
|
||||
* @param array $item 微信账号数据
|
||||
*/
|
||||
private function saveWechatAccount($item)
|
||||
{
|
||||
$data = [
|
||||
'wechatId' => $item['wechatId'],
|
||||
'deviceAccountId' => $item['deviceAccountId'],
|
||||
'imei' => $item['imei'],
|
||||
'deviceMemo' => $item['deviceMemo'],
|
||||
'accountUserName' => $item['accountUserName'],
|
||||
'accountRealName' => $item['accountRealName'],
|
||||
'accountNickname' => $item['accountNickname'],
|
||||
'keFuAlive' => $item['keFuAlive'],
|
||||
'deviceAlive' => $item['deviceAlive'],
|
||||
'wechatAlive' => $item['wechatAlive'],
|
||||
'yesterdayMsgCount' => $item['yesterdayMsgCount'],
|
||||
'sevenDayMsgCount' => $item['sevenDayMsgCount'],
|
||||
'thirtyDayMsgCount' => $item['thirtyDayMsgCount'],
|
||||
'totalFriend' => $item['totalFriend'],
|
||||
'maleFriend' => $item['maleFriend'],
|
||||
'femaleFriend' => $item['femaleFriend'],
|
||||
'wechatGroupName' => $item['wechatGroupName'],
|
||||
'tenantId' => $item['tenantId'],
|
||||
'nickname' => $item['nickname'],
|
||||
'alias' => $item['alias'],
|
||||
'avatar' => $item['avatar'],
|
||||
'gender' => $item['gender'],
|
||||
'region' => $item['region'],
|
||||
'signature' => $item['signature'],
|
||||
'bindQQ' => $item['bindQQ'],
|
||||
'bindEmail' => $item['bindEmail'],
|
||||
'bindMobile' => $item['bindMobile'],
|
||||
'currentDeviceId' => $item['currentDeviceId'],
|
||||
'isDeleted' => $item['isDeleted'],
|
||||
'deleteTime' => $item['deleteTime'],
|
||||
'groupId' => $item['groupId'],
|
||||
'memo' => $item['memo'],
|
||||
'wechatVersion' => $item['wechatVersion'],
|
||||
'labels' => $item['labels']
|
||||
];
|
||||
|
||||
$account = WechatAccountModel::where('wechatId', $item['wechatId'])->find();
|
||||
if ($account) {
|
||||
$account->save($data);
|
||||
} else {
|
||||
WechatAccountModel::create($data);
|
||||
}
|
||||
}
|
||||
|
||||
public function getWechatAccountList()
|
||||
{
|
||||
// 获取授权token
|
||||
$authorization = trim($this->request->header('authorization', ''));
|
||||
if (empty($authorization)) {
|
||||
return errorJson('缺少授权信息');
|
||||
}
|
||||
|
||||
try {
|
||||
// 构建请求参数
|
||||
$params = [
|
||||
'wechatAlive' => input('wechatAlive', ''),
|
||||
'keyword' => input('keyword', ''),
|
||||
'groupId' => input('groupId', ''),
|
||||
'departmentId' => input('departmentId', ''),
|
||||
'hasDevice' => input('hasDevice', ''),
|
||||
'deviceGroupId' => input('deviceGroupId', ''),
|
||||
'containSubDepartment' => input('containSubDepartment', 'false'),
|
||||
'pageIndex' => input('pageIndex', 0),
|
||||
'pageSize' => input('pageSize', 10)
|
||||
];
|
||||
|
||||
// 设置请求头
|
||||
$headerData = ['client:system'];
|
||||
$header = setHeader($headerData, $authorization, 'plain');
|
||||
|
||||
// 发送请求
|
||||
$result = requestCurl($this->baseUrl . 'api/WechatAccount/list', $params, 'GET', $header);
|
||||
$response = handleApiResponse($result);
|
||||
|
||||
// 保存数据到数据库
|
||||
if (!empty($response['results'])) {
|
||||
foreach ($response['results'] as $item) {
|
||||
$this->saveWechatAccount($item);
|
||||
}
|
||||
}
|
||||
|
||||
return successJson($response);
|
||||
} catch (\Exception $e) {
|
||||
return errorJson('获取微信账号列表失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
127
Server/application/api/controller/WechatFriendController.php
Normal file
127
Server/application/api/controller/WechatFriendController.php
Normal file
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\common\model\WechatFriendModel;
|
||||
use think\facade\Request;
|
||||
|
||||
class WechatFriendController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 获取微信好友列表数据
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function friendlistData()
|
||||
{
|
||||
// 获取授权token
|
||||
$authorization = trim($this->request->header('authorization', ''));
|
||||
if (empty($authorization)) {
|
||||
return errorJson('缺少授权信息');
|
||||
}
|
||||
|
||||
try {
|
||||
// 构建请求参数
|
||||
$params = [
|
||||
'accountKeyword' => input('accountKeyword', ''),
|
||||
'addFrom' => input('addFrom', []),
|
||||
'allotAccountId' => input('allotAccountId', ''),
|
||||
'containAllLabel' => input('containAllLabel', false),
|
||||
'containSubDepartment' => input('containSubDepartment', false),
|
||||
'departmentId' => input('departmentId', ''),
|
||||
'extendFields' => input('extendFields', []),
|
||||
'friendKeyword' => input('friendKeyword', ''),
|
||||
'friendPhoneKeyword' => input('friendPhoneKeyword', ''),
|
||||
'friendPinYinKeyword' => input('friendPinYinKeyword', ''),
|
||||
'friendRegionKeyword' => input('friendRegionKeyword', ''),
|
||||
'friendRemarkKeyword' => input('friendRemarkKeyword', ''),
|
||||
'gender' => input('gender', ''),
|
||||
'groupId' => input('groupId', null),
|
||||
'isDeleted' => input('isDeleted', false),
|
||||
'isPass' => input('isPass', true),
|
||||
'keyword' => input('keyword', ''),
|
||||
'labels' => input('labels', []),
|
||||
'pageIndex' => input('pageIndex', 0),
|
||||
'pageSize' => input('pageSize', 20),
|
||||
'preFriendId' => input('preFriendId', ''),
|
||||
'wechatAccountKeyword' => input('wechatAccountKeyword', '')
|
||||
];
|
||||
|
||||
// 设置请求头
|
||||
$headerData = ['client:system'];
|
||||
$header = setHeader($headerData, $authorization, 'plain');
|
||||
|
||||
// 发送请求获取好友列表
|
||||
$result = requestCurl($this->baseUrl . 'api/WechatFriend/friendlistData', $params, 'GET', $header);
|
||||
$response = handleApiResponse($result);
|
||||
|
||||
// 保存数据到数据库
|
||||
if (!empty($response['results'])) {
|
||||
foreach ($response['results'] as $item) {
|
||||
$this->saveFriend($item);
|
||||
}
|
||||
}
|
||||
|
||||
return successJson($response);
|
||||
} catch (\Exception $e) {
|
||||
return errorJson('获取微信好友列表失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存微信好友数据到数据库
|
||||
* @param array $item 微信好友数据
|
||||
*/
|
||||
private function saveFriend($item)
|
||||
{
|
||||
$data = [
|
||||
'wechatAccountId' => $item['wechatAccountId'],
|
||||
'alias' => $item['alias'],
|
||||
'wechatId' => $item['wechatId'],
|
||||
'conRemark' => $item['conRemark'],
|
||||
'nickname' => $item['nickname'],
|
||||
'pyInitial' => $item['pyInitial'],
|
||||
'quanPin' => $item['quanPin'],
|
||||
'avatar' => $item['avatar'],
|
||||
'gender' => $item['gender'],
|
||||
'region' => $item['region'],
|
||||
'addFrom' => $item['addFrom'],
|
||||
'labels' => is_array($item['labels']) ? json_encode($item['labels']) : json_encode([]),
|
||||
'signature' => $item['signature'],
|
||||
'isDeleted' => $item['isDeleted'],
|
||||
'isPassed' => $item['isPassed'],
|
||||
'deleteTime' => $item['deleteTime'],
|
||||
'accountId' => $item['accountId'],
|
||||
'extendFields' => is_array($item['extendFields']) ? json_encode($item['extendFields']) : json_encode([]),
|
||||
'accountUserName' => $item['accountUserName'],
|
||||
'accountRealName' => $item['accountRealName'],
|
||||
'accountNickname' => $item['accountNickname'],
|
||||
'ownerAlias' => $item['ownerAlias'],
|
||||
'ownerWechatId' => $item['ownerWechatId'],
|
||||
'ownerNickname' => $item['ownerNickname'],
|
||||
'ownerAvatar' => $item['ownerAvatar'],
|
||||
'phone' => $item['phone'],
|
||||
'thirdParty' => is_array($item['thirdParty']) ? json_encode($item['thirdParty']) : json_encode([]),
|
||||
'groupId' => $item['groupId'],
|
||||
'passTime' => $item['passTime'],
|
||||
'additionalPicture' => $item['additionalPicture'],
|
||||
'desc' => $item['desc'],
|
||||
'country' => $item['country'],
|
||||
'province' => $item['province'],
|
||||
'city' => $item['city'],
|
||||
'createTime' => $item['createTime']
|
||||
];
|
||||
|
||||
// 使用三个字段的组合作为唯一性判断
|
||||
$friend = WechatFriendModel::where([
|
||||
['ownerWechatId', '=', $item['ownerWechatId']],
|
||||
['wechatId', '=', $item['wechatId']],
|
||||
['wechatAccountId', '=', $item['wechatAccountId']]
|
||||
])->find();
|
||||
|
||||
if ($friend) {
|
||||
$friend->save($data);
|
||||
} else {
|
||||
WechatFriendModel::create($data);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user