1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- /**
- * Created by PhpStorm.
- * User: zilongs
- * Date: 20-9-23
- * Time: 上午11:04
- */
- namespace App\Models;
- use DB;
- class User extends BaseModel
- {
- public function getCouponNumAttribute()
- {
- return UserCoupon::where('user_id', $this->id)->where('status', 1)->where('expire_time', '>', time())->count();
- }
- //通过token获取用户信息
- public static function getUserByToken($is_exit = true)
- {
- $auth = request()->header('token');
- if (empty($auth)) {
- if (!$is_exit) {
- return [];
- }
- exit_out(null, 401, '认证失效,请重新登录');
- }
- $arr = aes_decrypt($auth);
- if (empty($arr['id'])) {
- if (!$is_exit) {
- return [];
- }
- exit_out(null, 401, '认证失效,请重新登录');
- }
- $user = User::where('id', $arr['id'])->first();
- if (empty($user)){
- exit_out(null, 601, '该账号已被删除');
- }
- $user = $user->toArray();
- if ($user['status'] == 0){
- exit_out(null, 602, '该账号已被冻结');
- }
- return $user;
- }
- //改变用户余额
- public static function changeBalance($user_id, $change_balance, $type, $relation_id, $remark, $admin_user_id = 0)
- {
- $user = User::select(['balance'])->where('id', $user_id)->first();
- if ($type == 2) {
- User::where('id', $user_id)->update([
- 'balance' => DB::raw('balance + '.$change_balance),
- 'topup_balance' => DB::raw('topup_balance + '.$change_balance),
- ]);
- }
- elseif ($type == 3) {
- User::where('id', $user_id)->update([
- 'balance' => DB::raw('balance + '.$change_balance),
- 'giving_balance' => DB::raw('giving_balance + '.$change_balance),
- ]);
- }
- else {
- User::where('id', $user_id)->increment('balance', $change_balance);
- }
- UserBalanceLog::create([
- 'user_id' => $user_id,
- 'admin_user_id' => $admin_user_id,
- 'type' => $type,
- 'relation_id' => $relation_id,
- 'before_balance' => $user['balance'],
- 'change_balance' => $change_balance,
- 'after_balance' => $user['balance'] + $change_balance,
- 'remark' => $remark,
- ]);
- return true;
- }
- }
|