User.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zilongs
  5. * Date: 20-9-23
  6. * Time: 上午11:04
  7. */
  8. namespace App\Models;
  9. class User extends BaseModel
  10. {
  11. public static function getUserByToken()
  12. {
  13. $auth = request()->header('token');
  14. if (empty($auth)) {
  15. exit_out(null, 401, '认证失效,请重新登录');
  16. }
  17. $arr = aes_decrypt($auth);
  18. if (empty($arr['id'])) {
  19. exit_out(null, 401, '认证失效,请重新登录');
  20. }
  21. $user = User::where('id', $arr['id'])->first();
  22. if (empty($user)){
  23. exit_out(null, 601, '该账号已被删除');
  24. }
  25. $user = $user->toArray();
  26. if ($user['status'] == 0){
  27. exit_out(null, 602, '该账号已被冻结');
  28. }
  29. return $user;
  30. }
  31. public static function changeBalance($user_id, $change_balance, $type, $relation_id, $remark, $admin_user_id = 0)
  32. {
  33. $user = User::select(['balance'])->where('id', $user_id)->first();
  34. User::where('id', $user_id)->increment('balance', $change_balance);
  35. UserBalanceLog::create([
  36. 'user_id' => $user_id,
  37. 'admin_user_id' => $admin_user_id,
  38. 'type' => $type,
  39. 'relation_id' => $relation_id,
  40. 'before_balance' => $user['balance'],
  41. 'change_balance' => $change_balance,
  42. 'after_balance' => $user['balance'] + $change_balance,
  43. 'remark' => $remark,
  44. ]);
  45. return true;
  46. }
  47. }