laravel+apiato日期字段和金额字段自动处理

本文介绍了在Laravel和Apiato框架下,如何处理日期字段(存储为timestamp,前端显示为Unix时间戳)和金额字段(数据库存储为整数,单位为分,前端显示为元)。在获取API数据时,日期转化为Unix时间戳乘以1000(毫秒),金额除以100展示为元。反之,设置数据时进行相反转换。文章提供了一个名为`AmountAndDateTrait`的 Trait 示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

程序开发中经常涉及到金额和日期的转换

1 日期在数据库中存储为timestamp类型,前端vue+element显示时使用Unix时间戳(Unix timestamp)*1000,单位毫秒

原理: get到api时 转为时间戳INT * 1000, set到数据库时 将int/1000 转为日期

2 金额字段,数据库类型为int类型单位分,前端转换为元显示

原理: get到API时/100, set到数据库时*100

参考自    hao-li/laravel-amount

 

 

一 首先laravel中新建Trait AmountAndDateTrait

 

 

<?php

namespace App\Ship\Parents\Models;


/**
 * Class AmountAndDateTrait
 *
 * 金额自动转换
 * get到API时/100,
 * set到数据库时*100
 *
 *  系统中涉及到金额的字段,View 层表现的时候一般都是以元为单位使用小数形式展示,
 * 不过 Domain 层存储时从空间、性能、容错角度出发,
 * 经常以分为单位,用整型来存储。
 *
 * 原理
 * 将转换逻辑封装在 AmountTrait 中,
 * 覆写 Model 类的 getMutatedAttributes, mutateAttributeForArray, getAttributeValue 及 setAttribute 方法,
 * 当访问相关字段时自动进行转换处理。
 *
 *
 *
 *  日期自动转换
 * get到api时 转为时间戳INT * 1000
 * set到数据库时  将int/1000 转为日期
 *
 *
 *
 *
 *
 *
 *
 *
 * 
 */
Trait AmountAndDateTrait
{


    public function __construct()
    {
    }

    public static $amountTimes = 100;



    /*Mutated是无引用时 原始调用 model时引用*/

    public function getMutatedAttributes()
    {
        $attributes = parent::getMutatedAttributes();

        if ($this->getAmountFields())  $attributes= array_merge($attributes, $this->getAmountFields());

        if ($this->getDates()) $attributes= array_merge($attributes, $this->getDates());

        //dump($attributes);
        return $attributes;

    }

    protected function mutateAttributeForArray($key, $value)
    {
        /*dump($key);
        dump($this->getDates());
        dump($this->getAmountFields())*/;

        if (in_array($key, $this->getAmountFields()) && is_int($value)) {
            $return = $value / self::$amountTimes;
        } else if (in_array($key, $this->getDates())) {
            $return = is_int($value) ? $value * 1000 : strtotime($value) * 1000;
        } else {
            $return = parent::mutateAttributeForArray($key, $value);
        }
        return $return;
    }


    /*以下的是在transform中引用时  引用*/
    //读取给API时
    public function getAttributeValue($key)
    {
        // dump($this);
/*        dump($key);
        dump($this->getDates());
        dump($this->getAmountFields());*/
        $value = parent::getAttributeValue($key);
        if (in_array($key, $this->getAmountFields()) && is_int($value)) {
            $value = $value / self::$amountTimes;
        } else if (in_array($key, $this->getDates())) {
            // 数据库存的是 timestamp, 给前端的是int...
            $value = is_int($value) ? $value * 1000 : strtotime($value) * 1000;
        }

        return $value;
    }


    //保存入数据库时设定
    public function setAttribute($key, $value)
    {
        if (in_array($key, $this->getAmountFields())) {
            $value = (int)($value * self::$amountTimes);
        }
        if (in_array($key, $this->getDates())) {
            if ($value != 0 && $value != '') {
                $value = is_int($value) ? date('Y-m-d H:i:s', ($value * 1000)) : $value;
            }
        }
        parent::setAttribute($key, $value);
    }


    //获取model金额字段的设置
    public function getAmountFields()
    {
        return (property_exists($this, 'amountFields')) ? $this->amountFields : [];
    }

    //获取model日期字段的设置
    public function getDates()
    {
        //$aaaa = $this->dates;
        return (property_exists($this, 'dates')) ? $this->dates : [];
    }

}

 

二 Model中引用

use App\Ship\Parents\Models\AmountAndDateTrait;//金额和日期自动转换



    use AmountAndDateTrait;
    protected $amountFields = ['money_total', 'money_pay'];//金额字段
    protected $dates = ['created_at', 'updated_at'];

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值