User.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. protected $table = 'users';
  12. public function patients()
  13. {
  14. return $this->hasMany(Patient::class);
  15. }
  16. public function order()
  17. {
  18. return $this->hasMany(Order::class);
  19. }
  20. //通过token获取用户信息
  21. public static function getUserByToken()
  22. {
  23. $auth = request()->header('token');
  24. if (empty($auth)) {
  25. exit_out(null, 401, '认证失效,请重新登录');
  26. }
  27. $arr = aes_decrypt($auth);
  28. if (isset($arr['doctor_id'])){
  29. // 说明是医生登陆!
  30. if (empty($arr['doctor_id'])) {
  31. exit_out(null, 401, '认证失效,请重新登录');
  32. }
  33. $user = Docter::where('id', $arr['doctor_id'])->first();
  34. if (empty($user)){
  35. exit_out(null, 601, '该账号已被删除');
  36. }
  37. $user = $user->toArray();
  38. // if ($user['status'] == 0){
  39. // exit_out(null, 602, '该账号已被冻结');
  40. // }
  41. return $user;
  42. }else{
  43. if (empty($arr['id'])) {
  44. exit_out(null, 401, '认证失效,请重新登录');
  45. }
  46. $user = User::where('id', $arr['id'])->first();
  47. if (empty($user)){
  48. exit_out(null, 601, '该账号已被删除');
  49. }
  50. $user = $user->toArray();
  51. if ($user['status'] == 0){
  52. exit_out(null, 602, '该账号已被冻结');
  53. }
  54. return $user;
  55. }
  56. }
  57. /**
  58. * 用户医生关注表
  59. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  60. * @author Liu-Yh
  61. * Create By 2020/11/18 11:06
  62. */
  63. public function userDorter(){
  64. return $this->hasOne(UserDocter::class,'user_id','id')->select(['id', 'remark']);;
  65. }
  66. //改变用户余额
  67. public static function changeBalance($user_id, $change_balance, $type, $relation_id, $remark, $admin_user_id = 0)
  68. {
  69. $user = User::select(['balance'])->where('id', $user_id)->first();
  70. User::where('id', $user_id)->increment('balance', $change_balance);
  71. UserBalanceLog::create([
  72. 'user_id' => $user_id,
  73. 'admin_user_id' => $admin_user_id,
  74. 'type' => $type,
  75. 'relation_id' => $relation_id,
  76. 'before_balance' => $user['balance'],
  77. 'change_balance' => $change_balance,
  78. 'after_balance' => $user['balance'] + $change_balance,
  79. 'remark' => $remark,
  80. ]);
  81. return true;
  82. }
  83. }