LiuJ hai 4 meses
pai
achega
0ec648f547
Modificáronse 3 ficheiros con 139 adicións e 0 borrados
  1. 76 0
      app/JsonRpc/UserService.php
  2. 33 0
      app/Model/UserImp.php
  3. 30 0
      config/autoload/databases.php

+ 76 - 0
app/JsonRpc/UserService.php

@@ -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);
     }

+ 33 - 0
app/Model/UserImp.php

@@ -0,0 +1,33 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Model;
+
+use Hyperf\DbConnection\Model\Model;
+
+/**
+ */
+class UserImp extends Model
+{
+    /**
+     * The table associated with the model.
+     */
+    /**
+     * The connection name for the model.
+     *
+     * @var string
+     */
+    protected ?string $connection = 'imp';
+    protected ?string $table = 'users';
+
+    /**
+     * The attributes that are mass assignable.
+     */
+    protected array $fillable = [];
+    protected array $hidden = [];
+    /**
+     * The attributes that should be cast to native types.
+     */
+    protected array $casts = [];
+}

+ 30 - 0
config/autoload/databases.php

@@ -9,6 +9,7 @@ declare(strict_types=1);
  * @contact  group@hyperf.io
  * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
  */
+
 use function Hyperf\Support\env;
 
 return [
@@ -41,4 +42,33 @@ return [
             ],
         ],
     ],
+    'imp' => [
+        'driver' => env('DB_DRIVER', 'mysql'),
+        'host' => env('DB_HOST', 'localhost'),
+        'database' => 'imp',
+        'port' => env('DB_PORT', 3306),
+        'username' => env('DB_USERNAME', 'root'),
+        'password' => env('DB_PASSWORD', ''),
+        'charset' => env('DB_CHARSET', 'utf8'),
+        'collation' => env('DB_COLLATION', 'utf8_unicode_ci'),
+        'prefix' => env('DB_PREFIX', ''),
+        'pool' => [
+            'min_connections' => 1,
+            'max_connections' => 10,
+            'connect_timeout' => 10.0,
+            'wait_timeout' => 3.0,
+            'heartbeat' => -1,
+            'max_idle_time' => (float) env('DB_MAX_IDLE_TIME', 60),
+        ],
+        'variables' => [
+            'time_zone' => 'Asia/Shanghai', // 设置MySQL连接的时区
+        ],
+        'commands' => [
+            'gen:model' => [
+                'path' => 'app/Model',
+                'force_casts' => true,
+                'inheritance' => 'Model',
+            ],
+        ],
+    ],
 ];