laravel5 加密 解密 (這裏注意這個寫法set+字段名+Attribute,還有的就是使用駝峯法。)

<?php

namespace App\Models\User;

use App\Models\Distribution\Counselor\Counselor;
use App\Models\Distribution\Counselor\UserFan;
use App\Models\Distribution\Facilitator\Facilitator;
use App\Models\Distribution\Subsidiary\Subsidiary;
use App\Models\Goods\GoodsComment;
use App\Models\Order\Order;
use App\Models\Store\StoreUser;
use App\Models\Permission\AdminUser;
use App\Models\Warehouse\WarehouseUser;
use App\Models\Goods\Goods;
use App\Traits\User\UserHelper;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;


class User extends Authenticatable
{
   use HasRoles, UserHelper;
   
   /**
    * The attributes that are mass assignable.
    *
    * @var array
    */
   protected $fillable = [
      'aite_id', 'nickname', 'phone', 'email', 'password', 'user_status', 'sex', 'birthday', 'address_id', 'alias', 'is_validated', 'remember_token'
   ];
   
   /**
    *  The attributes that should be hidden for arrays.
    *
    * @var array
    */
   protected $hidden = [
      'password', 'remember_token',
   ];
   
   /**
    * 密碼加密
    *  
    * @param $password
    */
   public function setPasswordAttribute($password)
   {
      $this->attributes['password'] = bcrypt($password);
   }
   
   /**
    *  賬戶餘額加密
    *
    * @param $userMoney
    */
   public function setUserMoneyAttribute($userMoney)
   {
      $this->attributes['user_money'] = encrypt($userMoney);
   }
   
   /**
    *  賬戶餘額解密
    *
    * @return int|string
    */
   public function getUserMoneyAttribute()
   {
      return $this->attributes['user_money'] ? decrypt($this->attributes['user_money']) : 0;
   }
   
   /**
    *  賬戶積分加密
    *
    * @param $payPoints
    */
   public function setUserPointsAttribute($payPoints)
   {
      $this->attributes['user_points'] = encrypt($payPoints);
   }
   
   /**
    *  賬戶積分解密
    *
    * @return int|string
    */
   public function getUserPointsAttribute()
   {
      return $this->attributes['user_points'] ? decrypt($this->attributes['user_points']) : 0;
   }
   
   /**
    *  賬戶凍結餘額加密
    *
    * @param $frozenMoney
    */
   public function setFrozenMoneyAttribute($frozenMoney)
   {
      $this->attributes['frozen_money'] = encrypt($frozenMoney);
   }
   
   /**
    *  賬戶凍結餘額解密
    *
    * @return int|string
    */
   public function getFrozenMoneyAttribute()
   {
      return $this->attributes['frozen_money'] ? decrypt($this->attributes['frozen_money']) : 0;
   }
   
   
}
--------------------- 
作者:phpAndyChen 
來源:CSDN 
原文:https://blog.csdn.net/qq_39188306/article/details/80487004 
版權聲明:本文爲博主原創文章,轉載請附上博文鏈接!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章