UserService.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\Role;
  4. use App\Model\RoleLog;
  5. use App\Model\RoleUser;
  6. use App\Model\User;
  7. use App\Model\UserInfo;
  8. use App\Model\UserLogin;
  9. use App\Model\WebsiteGroup;
  10. use App\Model\Wechat;
  11. use App\Model\UserImp;
  12. use App\Tools\Result;
  13. use Hamcrest\Arrays\IsArray;
  14. use Hyperf\DbConnection\Db;
  15. use Hyperf\RpcServer\Annotation\RpcService;
  16. #[RpcService(name: "UserService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  17. class UserService implements UserServiceInterface
  18. {
  19. /**
  20. * @param string $name
  21. * @param int $gender
  22. * @return string
  23. */
  24. public function createUser(array $data): array
  25. {
  26. //同步imp的用戶信息
  27. if (isset($data['type']) && $data['type'] == 'imp') {
  28. //獲取user的id,user_name,avatar,nickname,組成數據,批量插入imp users中,如果已經存在則替換,密碼用111111加密,加密算法是bcrypt
  29. // 获取所有用户数据
  30. $users = User::query()->select('id', 'user_name', 'avatar', 'nickname')->get()->toArray();
  31. if (!empty($users)) {
  32. $insertData = [];
  33. $now = date('Y-m-d H:i:s');
  34. // 准备插入数据
  35. foreach ($users as $user) {
  36. $insertData[] = [
  37. 'id' => $user['id'],
  38. 'mobile' => $user['user_name'], // user_name映射到mobile
  39. 'nickname' => $user['nickname'] ?? $user['user_name'], // 如果nickname为空则使用user_name
  40. 'avatar' => $user['avatar'] ?? '',
  41. 'password' => password_hash('111111', PASSWORD_BCRYPT), // 使用bcrypt加密密码
  42. 'gender' => 3, // 默认未知性别
  43. 'motto' => '',
  44. 'email' => $user['email'] ?? '',
  45. 'birthday' => '',
  46. 'is_robot' => 2, // 非机器人
  47. 'status' => 1, // 正常状态
  48. 'created_at' => $now,
  49. 'updated_at' => $now,
  50. ];
  51. }
  52. // 批量插入或替换
  53. try {
  54. // 使用replace into语法,如果主键存在则替换
  55. $sql = "REPLACE INTO users (id, mobile, nickname, avatar, password, gender, motto, email, birthday, is_robot, status, created_at, updated_at) VALUES ";
  56. $values = [];
  57. foreach ($insertData as $row) {
  58. $values[] = "('" . implode("', '", array_map(fn($v) => addslashes($v), $row)) . "')";
  59. }
  60. $sql .= implode(', ', $values);
  61. // 使用imp数据库连接执行SQL
  62. Db::connection('imp')->statement($sql);
  63. } catch (\Throwable $e) {
  64. // 记录错误但不中断主流程
  65. var_dump('同步imp用户失败: ' . $e->getMessage());
  66. }
  67. return Result::success(['同步imp用户成功']);
  68. }
  69. }
  70. Db::beginTransaction();
  71. try {
  72. $dataUserReq = [
  73. 'user_name' => $data['user_name'],
  74. 'password' => md5(md5($data['password']) . $data['salt']),
  75. 'avatar' => $data['avatar'] ?? 'https://img.bjzxtw.org.cn/master/image/userDefault.jpg',
  76. 'type_id' => $data['type_id'] ?? '20000',
  77. 'mobile' => $data['mobile'] ?? '', //手机号
  78. 'status' => $data['status'] ?? 1,
  79. 'email' => $data['email'] ?? '',
  80. 'salt' => $data['salt'],
  81. 'admin_id' => isset($data['admin_id']) && $data['admin_id'] != '' ? $data['admin_id'] : 0,
  82. // 'level_id'=>$data['level_id']??0, //会员等级
  83. 'nickname' => $data['nickname'] ?? $data['user_name'], //把账号同步到昵称里面
  84. 'last_login_ip' => $data['last_login_ip'] ?? '',
  85. 'sszq' => $data['sszq'] ?? '',
  86. ];
  87. $data['other'] = is_array($data['other']) ? json_encode($data['other'] ?? []) : $data['other'];
  88. $data['city_arr_id'] = is_array($data['city_arr_id']) ? json_encode($data['city_arr_id'] ?? []) : $data['city_arr_id'];
  89. $data['address_arr_id'] = is_array($data['address_arr_id']) ? json_encode($data['address_arr_id'] ?? []) : $data['address_arr_id'];
  90. var_dump("user:", $dataUserReq);
  91. $userid = User::query()->insertGetId($dataUserReq);
  92. // $data['administrative_unit_arr_id'] = is_array($data['administrative_unit_arr_id']) ? json_encode($data['administrative_unit_arr_id'] ?? []): $data['administrative_unit_arr_id'];
  93. $dataUserInfoReq = [
  94. 'user_id' => $userid,
  95. 'id_card' => $data['id_card'] ?? '',
  96. 'gender' => $data['gender'] ?? 0,
  97. 'real_name' => $data['real_name'] ?? '',
  98. 'business_name' => $data['business_name'] ?? '',
  99. 'job' => $data['job'] ?? '',
  100. 'city_id' => $data['city_id'] ?? 0,
  101. 'birthday' => $data['birthday'] ?? '',
  102. 'number' => $data['number'] ?? '',
  103. 'city_arr_id' => $data['city_arr_id'] ?? '',
  104. 'from_time' => $data['from_time'] ?? null,
  105. 'to_time' => $data['to_time'] ?? null,
  106. 'long_time' => $data['long_time'] ?? 0,
  107. 'native_place_id' => $data['native_place_id'] ?? 0,
  108. 'native_place_arr_id' => $data['native_place_arr_id'] ?? '',
  109. 'qq' => $data['qq'] ?? '',
  110. 'zip_code' => $data['zip_code'] ?? '',
  111. 'address_arr_id' => $data['address_arr_id'] ?? '',
  112. 'address_id' => $data['address_id'] ?? 0,
  113. 'address' => $data['address'] ?? '',
  114. 'other' => $data['other'] ?? '',
  115. 'remark' => $data['remark'] ?? '',
  116. 'fax' => $data['fax'] ?? '',
  117. 'position' => $data['position'] ?? '',
  118. 'legal_person_real_name' => $data['legal_person_real_name'] ?? '',
  119. 'legal_person_mobile' => $data['legal_person_mobile'] ?? '',
  120. 'legal_person_id_card' => $data['legal_person_id_card'] ?? '',
  121. 'administrative_unit_arr_id' => $data['administrative_unit_arr_id'] ?? '',
  122. 'administrative_unit_id' => $data['administrative_unit_id'] ?? 0,
  123. 'department_id' => $data['department_id'] ?? 0,
  124. 'department_arr_id' => $data['department_arr_id'] ?? '',
  125. // 企业会员 相关 公司信息
  126. 'company_hy_id' => $data['company_hy_id'] ?? 0,
  127. 'company_size' => $data['company_size'] ?? 0,
  128. 'company_nature' => $data['company_nature'] ?? 0,
  129. 'introduction' => $data['introduction'] ?? '',
  130. 'company_url' => $data['company_url'] ?? '',
  131. ];
  132. var_dump("UserINfo:::", $dataUserInfoReq);
  133. $userInfoId = UserInfo::query()->insertGetId($dataUserInfoReq);
  134. $roleUserData = [
  135. 'role_id' => isset($data['role_id']) && $data['role_id'] != '' ? $data['role_id'] : 0,
  136. 'user_id' => $userid,
  137. 'admin_user_id' => isset($data['admin_id']) && $data['admin_id'] != '' ? $data['admin_id'] : 0,
  138. ];
  139. RoleUser::insert($roleUserData);
  140. var_dump("userInfo:", $userInfoId);
  141. //处理imp
  142. $impUserData = [
  143. 'id' => $userid, // 将在后面获取
  144. 'mobile' => $data['user_name'],
  145. 'nickname' => $data['nickname'] ?? $data['user_name'],
  146. 'avatar' => $data['avatar'] ?? 'https://img.bjzxtw.org.cn/master/image/userDefault.jpg',
  147. 'gender' => $data['gender'] ?? 3,
  148. 'password' => password_hash('111111', PASSWORD_BCRYPT),
  149. 'motto' => '',
  150. 'email' => $data['email'] ?? '',
  151. 'birthday' => $data['birthday'] ?? '',
  152. 'is_robot' => 2,
  153. 'status' => isset($data['status']) && $data['status'] === 0 ? 2 : ($data['status'] ?? 1), // 当status为0时设置为2,其他情况保持正常
  154. 'created_at' => date('Y-m-d H:i:s'),
  155. 'updated_at' => date('Y-m-d H:i:s')
  156. ];
  157. // 使用 REPLACE INTO 实现存在则替换的逻辑
  158. Db::connection('imp')->table('users')->updateOrInsert(
  159. ['mobile' => $data['user_name']],
  160. $impUserData
  161. );
  162. Db::commit();
  163. } catch (\Throwable $ex) {
  164. Db::rollBack();
  165. var_dump($ex->getMessage());
  166. return Result::error("创建失败", 0);
  167. }
  168. return $userInfoId ? Result::success(["id" => $userid]) : Result::error("创建失败", 0);
  169. }
  170. /**
  171. * @return array
  172. */
  173. public function getUserList(array $data): array
  174. {
  175. $where = [];
  176. if (isset($data['keyword']) && $data['keyword']) {
  177. array_push($where, ['user.user_name', 'like', '%' . $data['keyword'] . '%']);
  178. }
  179. if (isset($data['type_id']) && $data['type_id']) {
  180. array_push($where, ['user.type_id', '=', $data['type_id']]);
  181. }
  182. if (isset($data['status']) && $data['status'] != '' && intval($data['status']) >= 0) {
  183. array_push($where, ['user.status', '=', $data['status']]);
  184. }
  185. $result = User::where($where)
  186. ->leftJoin('user_info', 'user.id', '=', 'user_info.user_id')
  187. ->leftJoin("user_info as user_infoA", 'user.admin_id', "user_infoA.user_id")
  188. ->leftJoin("role_user", 'role_user.user_id', "user.id")
  189. ->leftJoin("role", 'role.id', "role_user.role_id")
  190. ->orderBy("user.id", "desc")->paginate(
  191. intval($data['pageSize']),
  192. [
  193. 'user.id',
  194. 'user.admin_id',
  195. 'user.user_name',
  196. 'user_info.real_name',
  197. 'user.mobile',
  198. 'user.type_id',
  199. 'user.created_at',
  200. 'user.status',
  201. 'user_infoA.real_name as admin_real_name',
  202. 'role.role_name',
  203. ],
  204. 'page',
  205. intval($data['page'])
  206. );
  207. $count = $result->total();
  208. if (empty($result)) {
  209. return Result::error("没有数据", 0);
  210. }
  211. $rep = $result->items();
  212. //1:个人会员 2:政务会员 3:企业会员 4:调研员 10000:管理员 20000:游客
  213. $type = ['1' => "个人会员", '2' => "政务会员", '3' => "企业会员", '4' => '调研员', '10000' => '管理员', '20000' => '游客'];
  214. if ($rep) {
  215. foreach ($rep as $k => $v) {
  216. $rep[$k]['type_name'] = $type[$v['type_id']];
  217. // $rep[$k]['gender_name'] = $gender[$v['gender']];
  218. // $rep[$k]['status_name'] = $status[$v['status']];
  219. // $rep[$k]['city_id'] = $v['city_id']?json_decode($v['city_id']):[];
  220. }
  221. }
  222. $data = [
  223. 'rows' => $rep,
  224. 'count' => $count,
  225. ];
  226. return Result::success($data);
  227. }
  228. /**
  229. * @param int $id
  230. * @return array
  231. */
  232. public function getUserInfo(int $id): array
  233. {
  234. $userInfo = Db::table('user')
  235. ->leftJoin('user_info', 'user.id', '=', 'user_info.user_id')
  236. ->leftJoin('role_user', 'role_user.user_id', '=', 'user.id')
  237. ->select(
  238. 'user.*',
  239. 'user_info.id as user_info_id',
  240. 'user_info.real_name',
  241. 'user_info.id_card',
  242. 'user_info.birthday',
  243. 'user_info.gender',
  244. 'user_info.city_id',
  245. 'user_info.business_name',
  246. 'user_info.job',
  247. 'user_info.number',
  248. 'user_info.city_arr_id',
  249. 'user_info.from_time',
  250. 'user_info.to_time',
  251. 'user_info.long_time',
  252. 'user_info.native_place_id',
  253. 'user_info.native_place_arr_id',
  254. 'user_info.qq',
  255. 'user_info.zip_code',
  256. 'user_info.address_arr_id',
  257. 'user_info.address_id',
  258. 'user_info.address',
  259. 'user_info.other',
  260. 'user_info.remark',
  261. 'user_info.fax',
  262. 'user_info.position',
  263. 'user_info.legal_person_real_name',
  264. 'user_info.legal_person_mobile',
  265. 'user_info.legal_person_id_card',
  266. 'user_info.administrative_unit_arr_id',
  267. 'user_info.administrative_unit_id',
  268. // 企业会员 相关 公司信息
  269. 'user_info.company_hy_id',
  270. 'user_info.company_size',
  271. 'user_info.company_nature',
  272. 'user_info.introduction',
  273. 'user_info.company_url',
  274. 'role_user.role_id',
  275. 'user_info.department_id',
  276. 'user_info.department_arr_id',
  277. )
  278. ->where('user.id', '=', $id)->first();
  279. $userInfoImp = UserImp::where(['id' => $id])->first();
  280. // $impUsers = Db::connection('imp')->select('SHOW TABLES');
  281. // var_dump($impUsers);
  282. if (empty($userInfo)) {
  283. return Result::error("找不到用户", 0, []);
  284. }
  285. // $userInfo = array_merge($userInfo, $impUsers);
  286. return Result::success($userInfo);
  287. }
  288. /**
  289. * 检测用户是否存在
  290. * @param array $data
  291. * @return array
  292. */
  293. public function verifyUserInfo(array $data): array
  294. {
  295. var_dump("进不进来");
  296. if (isset($data['id'])) {
  297. $data[] = ['id', "!=", $data['id']];
  298. unset($data['id']);
  299. }
  300. $userInfo = User::query()->where($data)->first();
  301. if (empty($userInfo)) {
  302. return Result::error("找不到用户", 0);
  303. }
  304. return Result::success($userInfo->toArray());
  305. }
  306. /**
  307. * @param array $data
  308. * @return array
  309. */
  310. public function createUserLogin(array $data): array
  311. {
  312. $id = UserLogin::insertGetId($data);
  313. if (empty($id)) {
  314. return Result::error("创建登录日志失败", 0);
  315. }
  316. return Result::success(["id" => $id]);
  317. }
  318. /**
  319. * 更新用户信息和userInfo
  320. * @param array $data
  321. * @return array
  322. */
  323. public function updateUser(array $data): array
  324. {
  325. var_dump($data, "----------------------");
  326. Db::beginTransaction();
  327. try {
  328. $userInfos = User::where(['id' => $data['id']])->first(); //查询用户昵称
  329. $dataUserReq = [
  330. 'user_name' => $data['user_name'],
  331. 'avatar' => $data['avatar'] ?? '',
  332. 'type_id' => $data['type_id'] ?? '20000',
  333. 'mobile' => $data['mobile'] ?? '', //手机号
  334. 'status' => $data['status'] ?? 1,
  335. 'email' => $data['email'] ?? '',
  336. 'admin_id' => $data['admin_id'],
  337. 'nickname' => $userInfos['nickname'] ?? $data['user_name'], //存在昵称同步一下,不存在取账号
  338. 'last_login_ip' => $data['last_login_ip'] ?? '',
  339. 'sszq' => $data['sszq'] ?? '', //网站标识
  340. ];
  341. $userRep = User::where(['id' => $data['id']])->update($dataUserReq);
  342. //插入 imp users
  343. $dataImpUserReq = [
  344. 'mobile' => $data['user_name'],
  345. ];
  346. // 只有在数据存在时才设置字段,避免设置空值
  347. if (!empty($data['nickname'])) {
  348. $dataImpUserReq['nickname'] = $data['nickname'];
  349. }
  350. if (!empty($data['avatar'])) {
  351. $dataImpUserReq['avatar'] = $data['avatar'];
  352. }
  353. if (!empty($data['email'])) {
  354. $dataImpUserReq['email'] = $data['email'];
  355. }
  356. UserImp::where(['id' => $data['id']])->update($dataImpUserReq);
  357. $data['other'] = is_array($data['other']) ? json_encode($data['other'] ?? []) : $data['other'];
  358. $data['city_arr_id'] = is_array($data['city_arr_id']) ? json_encode($data['city_arr_id'] ?? []) : $data['city_arr_id'];
  359. $data['address_arr_id'] = is_array($data['address_arr_id']) ? json_encode($data['address_arr_id'] ?? []) : $data['address_arr_id'];
  360. var_dump("修改user:", $userRep);
  361. $dataUserInfoReq = [
  362. 'id_card' => $data['id_card'] ?? '',
  363. 'gender' => $data['gender'] ?? 0,
  364. 'real_name' => $data['real_name'] ?? '',
  365. 'job' => $data['job'] ?? '',
  366. 'city_id' => $data['city_id'] ?? 0,
  367. 'birthday' => $data['birthday'] ?? '',
  368. 'number' => $data['number'] ?? '',
  369. 'city_arr_id' => $data['city_arr_id'] ?? '',
  370. 'from_time' => $data['from_time'] ?? null,
  371. 'business_name' => $data['business_name'] ?? '',
  372. 'to_time' => $data['to_time'] ?? null,
  373. 'long_time' => $data['long_time'] ?? 0,
  374. 'native_place_id' => $data['native_place_id'] ?? 0,
  375. 'native_place_arr_id' => $data['native_place_arr_id'] ?? '',
  376. 'qq' => $data['qq'] ?? '',
  377. 'zip_code' => $data['zip_code'] ?? '',
  378. 'address_arr_id' => $data['address_arr_id'] ?? '',
  379. 'address_id' => $data['address_id'] ?? 0,
  380. 'address' => $data['address'] ?? '',
  381. 'other' => $data['other'] ?? '',
  382. 'remark' => $data['remark'] ?? '',
  383. 'fax' => $data['fax'] ?? '',
  384. 'position' => $data['position'] ?? '',
  385. 'legal_person_real_name' => $data['legal_person_real_name'] ?? '',
  386. 'legal_person_mobile' => $data['legal_person_mobile'] ?? '',
  387. 'legal_person_id_card' => $data['legal_person_id_card'] ?? '',
  388. 'administrative_unit_arr_id' => $data['administrative_unit_arr_id'] ?? '',
  389. 'administrative_unit_id' => $data['administrative_unit_id'] ?? 0,
  390. 'department_id' => $data['department_id'] ?? 0,
  391. 'department_arr_id' => $data['department_arr_id'] ?? '',
  392. // 企业会员 相关 公司信息
  393. 'company_hy_id' => $data['company_hy_id'] ?? 0,
  394. 'company_size' => $data['company_size'] ?? 0,
  395. 'company_nature' => $data['company_nature'] ?? 0,
  396. 'introduction' => $data['introduction'] ?? '',
  397. 'company_url' => $data['company_url'] ?? '',
  398. ];
  399. $userInfoRep = UserInfo::where(['user_id' => $data['id']])->update($dataUserInfoReq);
  400. var_dump("修改userInfo:", $userInfoRep);
  401. $roleUserData = [
  402. 'role_id' => $data['role_id'],
  403. 'admin_user_id' => $data['admin_id'],
  404. ];
  405. $resultRoleUserRep = RoleUser::where(['user_id' => $data['id']])->update($roleUserData);
  406. var_dump("修改用户角色权限:", $resultRoleUserRep);
  407. //处理imp
  408. Db::commit();
  409. } catch (\Throwable $ex) {
  410. Db::rollBack();
  411. var_dump($ex->getMessage());
  412. return Result::error("修改失败", 0);
  413. }
  414. $result = [
  415. 'user' => $userRep,
  416. 'userInfo' => $userInfoRep,
  417. ];
  418. return $result ? Result::success($result) : Result::error("修改失败", 0);
  419. }
  420. /**
  421. * @param array $data
  422. * @return array
  423. */
  424. public function updateUserInfo(array $data): array
  425. {
  426. return 1;
  427. }
  428. /**
  429. * @param array $data
  430. * @return array|int
  431. */
  432. public function resetPassword(array $data): array
  433. {
  434. $where = [
  435. 'id' => $data['id'],
  436. ];
  437. $data = [
  438. 'password' => md5(md5($data['password']) . $data['salt']),
  439. 'salt' => $data['salt'],
  440. ];
  441. $result = User::where($where)->update($data);
  442. if ($result) {
  443. return Result::error("修改失败", 0);
  444. } else {
  445. return Result::success([]);
  446. }
  447. }
  448. /**
  449. * @param int $id
  450. * @return array
  451. */
  452. public function delUser(int $id): array
  453. {
  454. Db::beginTransaction();
  455. try {
  456. User::where(['id' => $id])->delete();
  457. UserInfo::where(['user_id' => $id])->delete();
  458. Db::commit();
  459. } catch (\Throwable $ex) {
  460. Db::rollBack();
  461. return Result::error("删除失败", 0);
  462. }
  463. return Result::success([]);
  464. }
  465. /**
  466. * @param array $data
  467. * @return array
  468. */
  469. public function addRole(array $data): array
  470. {
  471. Db::beginTransaction();
  472. try {
  473. $data['sort'] = intval($data['sort']) ?? 0;
  474. Role::insertGetId($data);
  475. $logData = [
  476. 'user_id' => $data['user_id'],
  477. 'data' => json_encode($data),
  478. 'type' => 1,
  479. ];
  480. RoleLog::insertGetId($logData);
  481. Db::commit();
  482. } catch (\Throwable $ex) {
  483. Db::rollBack();
  484. return Result::error("新增失败", 0);
  485. }
  486. return Result::success([]);
  487. }
  488. /**
  489. * @param array $data
  490. * @return array
  491. */
  492. public function delRole(array $data): array
  493. {
  494. Db::beginTransaction();
  495. try {
  496. $roleInfo = Role::where(['id' => $data['id']])->first();
  497. $logData = [
  498. 'user_id' => $data['user_id'],
  499. 'data' => json_encode($roleInfo->toArray()),
  500. 'type' => 2,
  501. ];
  502. RoleLog::insertGetId($logData);
  503. RoleUser::where(['role_id' => $data['id']])->delete();
  504. $result = Role::where(['id' => $data['id']])->delete();
  505. Db::commit();
  506. } catch (\Throwable $ex) {
  507. Db::rollBack();
  508. return Result::error("删除失败", 0);
  509. }
  510. return Result::success($result);
  511. }
  512. /**
  513. * @param array $data
  514. * @return array
  515. */
  516. public function updateRole(array $data): array
  517. {
  518. Db::beginTransaction();
  519. try {
  520. $result = Role::where(['id' => $data['id']])->update($data);
  521. $logData = [
  522. 'user_id' => $data['user_id'],
  523. 'data' => json_encode($data),
  524. 'type' => 3,
  525. ];
  526. RoleLog::insertGetId($logData);
  527. Db::commit();
  528. } catch (\Throwable $ex) {
  529. Db::rollBack();
  530. return Result::error("更新失败");
  531. }
  532. return Result::success($result);
  533. }
  534. /**
  535. * @param array $data
  536. * @return array
  537. */
  538. public function roleInfo(array $data): array
  539. {
  540. $roleInfo = Role::where(['id' => $data['id']])->first();
  541. if ($roleInfo) {
  542. return Result::success($roleInfo->toArray());
  543. } else {
  544. return Result::error("没有数据");
  545. }
  546. }
  547. /**
  548. * @param array $data
  549. * @return array
  550. */
  551. public function roleList(array $data): array
  552. {
  553. $where = [];
  554. if (isset($data['keyword']) && $data['keyword']) {
  555. array_push($where, ['role.role_name', 'like', '%' . $data['keyword'] . '%']);
  556. }
  557. $result = Role::withCount('users')->where($where)->limit($data['pageSize'])->offset(($data['page'] - 1) * $data['pageSize'])->get();
  558. $count = Role::where($where)->count();
  559. if (empty($result)) {
  560. return Result::error("没有数据", 0);
  561. }
  562. $data = [
  563. 'rows' => $result->toArray(),
  564. 'count' => $count,
  565. ];
  566. return Result::success($data);
  567. }
  568. /**
  569. * 获取小程序注册信息
  570. * @param array $data
  571. * @return array
  572. */
  573. public function getWechatInfo(array $data): array
  574. {
  575. $where = [
  576. 'purePhoneNumber' => $data['purePhoneNumber'],
  577. 'openid' => $data['openid'],
  578. ];
  579. $result = Wechat::where($where)->first();
  580. var_dump($result);
  581. if ($result) {
  582. return Result::success($result);
  583. } else {
  584. return Result::error('没有数据');
  585. }
  586. }
  587. /**
  588. * 添加小程序 注册信息
  589. * @param array $data
  590. * @return array
  591. */
  592. public function addWechatInfo(array $data): array
  593. {
  594. $result = Wechat::insertGetId($data);
  595. if ($result) {
  596. return Result::success($result);
  597. } else {
  598. return Result::error('添加失败');
  599. }
  600. }
  601. /**
  602. * 修改密码
  603. * @param array $data
  604. * @return array
  605. */
  606. public function changePassword(array $data): array
  607. {
  608. Db::beginTransaction();
  609. $userInfo = User::where(['id' => $data['user_id']])->first();
  610. // return Result::success($userInfo);
  611. try {
  612. $dataUserReq = [
  613. 'password' => md5(md5($data['new_password1']) . $userInfo['salt']),
  614. ];
  615. if ($userInfo['password'] != md5(md5($data['password']) . $userInfo['salt'])) {
  616. Db::rollBack();
  617. return Result::error('您输入的密码错误');
  618. }
  619. $userRep = User::where(['id' => $data['user_id']])->update($dataUserReq);
  620. $dataImpUserReq = [
  621. password_hash($data['new_password1'], PASSWORD_BCRYPT), // 使用bcrypt加密密码
  622. ];
  623. UserImp::where(['id' => $data['id']])->update($dataImpUserReq);
  624. Db::commit();
  625. } catch (\Throwable $ex) {
  626. Db::rollBack();
  627. var_dump($ex->getMessage());
  628. return Result::error("创建失败", 0);
  629. }
  630. return Result::success([]);
  631. }
  632. /**
  633. * 修改用户状态
  634. * @param array $data
  635. * @return array
  636. */
  637. public function upUserStatus(array $data): array
  638. {
  639. $where = [
  640. 'id' => $data['id'],
  641. ];
  642. $result = User::where($where)->update($data);
  643. if ($result) {
  644. return Result::success($result);
  645. } else {
  646. return Result::error('修改失败');
  647. }
  648. }
  649. /**
  650. * 修改用户头像昵称
  651. * @param array $data
  652. * @return array
  653. */
  654. public function updateUserAvatarNickname(array $data): array
  655. {
  656. Db::beginTransaction();
  657. try {
  658. $where1 = [
  659. 'id' => $data['user_id'],
  660. ];
  661. $updateData = [];
  662. if (!empty($data['nickname'])) {
  663. $updateData['nickname'] = $data['nickname'];
  664. }
  665. if (!empty($data['avatar'])) {
  666. $updateData['avatar'] = $data['avatar'];
  667. }
  668. $result = User::where($where1)->update($updateData);
  669. //imp
  670. $dataImpUserReq = [
  671. 'nickname' => $data['nickname'], // 如果nickname为空则使用user_name
  672. 'avatar' => $data['avatar'] ?? '',
  673. ];
  674. if (empty($data['nickname'])) {
  675. unset($dataImpUserReq['nickname']);
  676. }
  677. if (empty($data['avatar'])) {
  678. unset($dataImpUserReq['avatar']);
  679. }
  680. UserImp::where(['id' => $data['user_id']])->update($dataImpUserReq);
  681. $updateData = [];
  682. $where = [
  683. 'user_id' => $data['user_id'],
  684. ];
  685. if (!empty($data['real_name'])) {
  686. $updateData = ['real_name' => $data['real_name']];
  687. }
  688. $result = UserInfo::where($where)->update($updateData);
  689. $result = User::where($where1)->first();
  690. Db::commit();
  691. return Result::success($result);
  692. } catch (\Throwable $ex) {
  693. Db::rollBack();
  694. var_dump($ex->getMessage());
  695. return Result::error("创建失败", 0);
  696. }
  697. }
  698. /**
  699. * 获取网站组信息
  700. * @param array $data
  701. * @return array
  702. */
  703. public function getWebsiteGroupInfo(array $data): array
  704. {
  705. $result = WebsiteGroup::where(['id' => $data['id']])->first();
  706. if ($result) {
  707. return Result::success($result);
  708. } else {
  709. return Result::error('查询失败');
  710. }
  711. }
  712. }