PHP 自动生成EAN码 barcode 代码

EAN 由三部分组成:国家代码+厂商代码+商品代码 组成,一共13位。最后一位为校验位,即校验码。
将最右边一个数位作为“奇数”位,从右向左为每个字符指定奇数/偶数位。 *

560405695210 5

1、对所有奇数位上的数值求和,
500651 => 17

2、对所有偶数位上的数值求和。将结果乘以3。
645920 => 26 *3 78

3、对第2步和第3步计算的结果求和。 95

4、校验位的数字加上用第3步计算的总和数应该能够被10整除。一般取个位数,即10-个位数=校验码

如果第4步计算的总和数能够被10整除,校验位就是“0”

国家码参考:
http://www.appsbarcode.com/sc20130113/EAN-country-code-cn.html

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/1/29
 * Time: 9:36
 */

namespace app\home\logic;


use app\common\ApiResponse;

class EanCreateLogic
{
    
    
    /**
     * @param $num
     * @return array
     */
    public static function createEanBarcode($num = 1)
    {
    
    
        IF ($num > 1000) {
    
    
            ApiResponse::error('最多生成1000个');
        }

        $Country_arr = static::getCountryCode();
        $Country_count = count($Country_arr);
        $country_code = $Country_arr[rand(0, $Country_count - 1)];

        $EanBarcode_arr = [];
        for ($g = 0; $g < $num; $g++) {
    
    
            $vendor_code = static::getVendorCode();
            $goods_code = static::getGoodsCode();
            $code_str = strval($country_code . $vendor_code . $goods_code);
            $strlen_code = strlen($code_str);
            if ($strlen_code != 12) {
    
    
                ApiResponse::error("长度错误");
            }

            $qi = $ou = 0;
            for ($i = 0; $i < $strlen_code; $i++) {
    
    
                if (boolval($i & 1)) {
    
    
                    $ou += $code_str[$i];
                } else {
    
    
                    $qi += $code_str[$i];
                }
            }

            $he = $ou * 3 + $qi;
            $ge = substr($he, -1);
            $Check_code = 10 - $ge;
            if ($Check_code == 10) {
    
    
                $Check_code = 0;
            }
            $EanBarcode = $code_str . $Check_code;
            db('ean_code')->insertGetId(['code' => $EanBarcode]);
            $EanBarcode_arr[$g] = $EanBarcode;
        }

        return $EanBarcode_arr;


    }


    /**
     * 厂商代码
     * @return int
     */
    public static function getVendorCode()
    {
    
    
        $VendorCode = getRandomStrStr(5);
        return $VendorCode;
    }


    /**
     * 商品代码
     * @return int
     */
    public static function getGoodsCode()
    {
    
    
        $GoodsCode = getRandomStrStr(4);
        return $GoodsCode;
    }

    /**
     * 获取国家代码
     * @return array
     */
    public static function getCountryCode()
    {
    
    
        $code_info = db('ean_country')->column('code');
        $new_arr = [];
        foreach ($code_info as $ke => &$val) {
    
    
            $val = str_replace(' ', '', trim($val));
            if (strstr($val, '-')) {
    
    
                if ($val == '060-139') {
    
    
                    $val = '060-060';
                }
                list($a, $b) = explode('-', $val);
                for ($i = $a; $i < $b + 1; $i++) {
    
    
                    if (strlen(strval($i)) != 3) {
    
    
                        if (strlen(strval($i)) == 1) {
    
    
                            $new_arr[$ke][] = '00' . $i;
                        } else {
    
    
                            $new_arr[$ke][] = '0' . $i;
                        }
                    } else {
    
    
                        $new_arr[$ke][] = $i;
                    }
                }
            } else {
    
    
                $new_arr[$ke] = $val;
            }
        }
        $Country_arr = [];
        foreach ($new_arr as $ne => $ar) {
    
    
            if (is_array($ar)) {
    
    
                foreach ($ar as $a) {
    
    
                    $Country_arr[] = $a;
                }
            } else {
    
    
                $Country_arr[] = $ar;
            }

        }
        #排除某些代号
        unset($Country_arr[array_search('000', $Country_arr)]);
        $Country_arr = array_values($Country_arr);

        return $Country_arr;

    }

}

猜你喜欢

转载自blog.csdn.net/weixin_42433970/article/details/113725527