|
|
@@ -10,6 +10,7 @@ use App\Model\UserInfo;
|
|
|
use App\Model\UserLogin;
|
|
|
use App\Model\WebsiteGroup;
|
|
|
use App\Model\Wechat;
|
|
|
+use App\Model\UserImp;
|
|
|
use App\Tools\Result;
|
|
|
use Hamcrest\Arrays\IsArray;
|
|
|
use Hyperf\DbConnection\Db;
|
|
|
@@ -26,6 +27,54 @@ class UserService implements UserServiceInterface
|
|
|
|
|
|
public function createUser(array $data): array
|
|
|
{
|
|
|
+ //同步imp的用戶信息
|
|
|
+ if (isset($data['type']) && $data['type'] == 'imp') {
|
|
|
+
|
|
|
+ //獲取user的id,user_name,avatar,nickname,組成數據,批量插入imp users中,如果已經存在則替換,密碼用111111加密,加密算法是bcrypt
|
|
|
+ // 获取所有用户数据
|
|
|
+ $users = User::query()->select('id', 'user_name', 'avatar', 'nickname')->get()->toArray();
|
|
|
+
|
|
|
+ if (!empty($users)) {
|
|
|
+ $insertData = [];
|
|
|
+ $now = date('Y-m-d H:i:s');
|
|
|
+
|
|
|
+ // 准备插入数据
|
|
|
+ foreach ($users as $user) {
|
|
|
+ $insertData[] = [
|
|
|
+ 'id' => $user['id'],
|
|
|
+ 'mobile' => $user['user_name'], // user_name映射到mobile
|
|
|
+ 'nickname' => $user['nickname'] ?? $user['user_name'], // 如果nickname为空则使用user_name
|
|
|
+ 'avatar' => $user['avatar'] ?? '',
|
|
|
+ 'password' => password_hash('111111', PASSWORD_BCRYPT), // 使用bcrypt加密密码
|
|
|
+ 'gender' => 3, // 默认未知性别
|
|
|
+ 'motto' => '',
|
|
|
+ 'email' => $user['email'] ?? '',
|
|
|
+ 'birthday' => '',
|
|
|
+ 'is_robot' => 2, // 非机器人
|
|
|
+ 'status' => 1, // 正常状态
|
|
|
+ 'created_at' => $now,
|
|
|
+ 'updated_at' => $now,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ // 批量插入或替换
|
|
|
+ try {
|
|
|
+ // 使用replace into语法,如果主键存在则替换
|
|
|
+ $sql = "REPLACE INTO users (id, mobile, nickname, avatar, password, gender, motto, email, birthday, is_robot, status, created_at, updated_at) VALUES ";
|
|
|
+ $values = [];
|
|
|
+ foreach ($insertData as $row) {
|
|
|
+ $values[] = "('" . implode("', '", array_map(fn($v) => addslashes($v), $row)) . "')";
|
|
|
+ }
|
|
|
+ $sql .= implode(', ', $values);
|
|
|
+
|
|
|
+ // 使用imp数据库连接执行SQL
|
|
|
+ Db::connection('imp')->statement($sql);
|
|
|
+ } catch (\Throwable $e) {
|
|
|
+ // 记录错误但不中断主流程
|
|
|
+ var_dump('同步imp用户失败: ' . $e->getMessage());
|
|
|
+ }
|
|
|
+ return Result::success(['同步imp用户成功']);
|
|
|
+ }
|
|
|
+ }
|
|
|
Db::beginTransaction();
|
|
|
try {
|
|
|
$dataUserReq = [
|
|
|
@@ -99,6 +148,29 @@ class UserService implements UserServiceInterface
|
|
|
];
|
|
|
RoleUser::insert($roleUserData);
|
|
|
var_dump("userInfo:", $userInfoId);
|
|
|
+
|
|
|
+ //处理imp
|
|
|
+ $impUserData = [
|
|
|
+ 'id' => $userid, // 将在后面获取
|
|
|
+ 'mobile' => $data['user_name'],
|
|
|
+ 'nickname' => $data['nickname'] ?? $data['user_name'],
|
|
|
+ 'avatar' => $data['avatar'] ?? 'https://img.bjzxtw.org.cn/master/image/userDefault.jpg',
|
|
|
+ 'gender' => 3,
|
|
|
+ 'password' => password_hash('111111', PASSWORD_BCRYPT),
|
|
|
+ 'motto' => '',
|
|
|
+ 'email' => $data['email'] ?? '',
|
|
|
+ 'birthday' => '',
|
|
|
+ 'is_robot' => 2,
|
|
|
+ 'status' => $data['status'] ?? 1,
|
|
|
+ 'created_at' => date('Y-m-d H:i:s'),
|
|
|
+ 'updated_at' => date('Y-m-d H:i:s')
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 使用 REPLACE INTO 实现存在则替换的逻辑
|
|
|
+ Db::connection('imp')->table('users')->updateOrInsert(
|
|
|
+ ['mobile' => $data['user_name']],
|
|
|
+ $impUserData
|
|
|
+ );
|
|
|
Db::commit();
|
|
|
} catch (\Throwable $ex) {
|
|
|
Db::rollBack();
|
|
|
@@ -222,9 +294,13 @@ class UserService implements UserServiceInterface
|
|
|
|
|
|
)
|
|
|
->where('user.id', '=', $id)->first();
|
|
|
+ $userInfoImp = UserImp::where(['id' => $id])->first();
|
|
|
+ // $impUsers = Db::connection('imp')->select('SHOW TABLES');
|
|
|
+ // var_dump($impUsers);
|
|
|
if (empty($userInfo)) {
|
|
|
return Result::error("找不到用户", 0, []);
|
|
|
}
|
|
|
+ // $userInfo = array_merge($userInfo, $impUsers);
|
|
|
|
|
|
return Result::success($userInfo);
|
|
|
}
|