User.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. use DB;
  10. class User extends BaseModel
  11. {
  12. protected $appends = ['age'];
  13. protected $table = 'users';
  14. //从生日转成年龄
  15. public function getAgeAttribute()
  16. {
  17. return birthday_to_age($this->birthday);
  18. }
  19. public function getCouponNumAttribute()
  20. {
  21. return UserCoupon::where('user_id', $this->id)->where('status', 1)->where('expire_time', '>', time())->count();
  22. }
  23. public function patients()
  24. {
  25. return $this->hasMany(Patient::class);
  26. }
  27. public function order()
  28. {
  29. return $this->hasMany(Order::class);
  30. }
  31. //通过token获取用户信息
  32. public static function getUserByToken($is_exit = true)
  33. {
  34. $auth = request()->header('token');
  35. if (empty($auth)) {
  36. if (!$is_exit) {
  37. return [];
  38. }
  39. exit_out(null, 401, '验证失效,请重新登录');
  40. }
  41. $arr = aes_decrypt($auth);
  42. // if (empty($arr['id'])) {
  43. // if (!$is_exit) {
  44. // return [];
  45. // }
  46. // exit_out(null, 401, '账号验证失效,请重新登录');
  47. // }
  48. if (isset($arr['doctor_id'])){
  49. // 说明是医生登陆!
  50. if (empty($arr['doctor_id'])) {
  51. exit_out(null, 401, '医生验证失效,请重新登录');
  52. }
  53. $user = DB::table('docters')->where('id', $arr['doctor_id'])->first();
  54. // $user = Docter::where('id', $arr['doctor_id'])->first();
  55. if (empty($user)){
  56. exit_out(null, 601, '该账号已被删除');
  57. }
  58. $user = object_array($user);
  59. // if ($user['status'] == 0){
  60. // exit_out(null, 602, '该账号已被冻结');
  61. // }
  62. return $user;
  63. }else{
  64. if (empty($arr['id'])) {
  65. exit_out(null, 401, '账号验证失效,请重新登录');
  66. }
  67. $user = User::where('id', $arr['id'])->first();
  68. if (empty($user)){
  69. exit_out(null, 601, '该账号已被删除');
  70. }
  71. $user = $user->toArray();
  72. if ($user['status'] == 0){
  73. exit_out(null, 602, '该账号已被冻结');
  74. }
  75. return $user;
  76. }
  77. }
  78. /**
  79. * 用户医生关注表
  80. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  81. * @author Liu-Yh
  82. * Create By 2020/11/18 11:06
  83. */
  84. public function userDorter(){
  85. return $this->hasOne(UserDocter::class,'user_id','id')->select(['id', 'remark']);;
  86. }
  87. //改变用户余额
  88. public static function changeBalance($user_id, $change_balance, $type, $relation_id, $remark, $admin_user_id = 0)
  89. {
  90. $user = User::select(['balance'])->where('id', $user_id)->first();
  91. if ($type == 2) {
  92. User::where('id', $user_id)->update([
  93. 'balance' => DB::raw('balance + '.$change_balance),
  94. 'topup_balance' => DB::raw('topup_balance + '.$change_balance),
  95. ]);
  96. }
  97. elseif ($type == 3) {
  98. User::where('id', $user_id)->update([
  99. 'balance' => DB::raw('balance + '.$change_balance),
  100. 'giving_balance' => DB::raw('giving_balance + '.$change_balance),
  101. ]);
  102. }
  103. else {
  104. User::where('id', $user_id)->increment('balance', $change_balance);
  105. }
  106. UserBalanceLog::create([
  107. 'user_id' => $user_id,
  108. 'admin_user_id' => $admin_user_id,
  109. 'type' => $type,
  110. 'relation_id' => $relation_id,
  111. 'before_balance' => $user['balance'],
  112. 'change_balance' => $change_balance,
  113. 'after_balance' => $user['balance'] + $change_balance,
  114. 'remark' => $remark,
  115. ]);
  116. return true;
  117. }
  118. public function UserBalanceLog(){
  119. return $this->hasMany(UserBalanceLog::class);
  120. }
  121. public function patient(){
  122. return $this->hasMany(Patient::class,'user_id','id');
  123. }
  124. }