with("parent_type")->where('id', "=", $typeId) ->first()->toArray(); $params['type_id'] = $typeId; $params['goods_code'] = static::getChCode($goodsType['id']); if (!empty($goodsType['parent_type']['id'])) { $params['goods_code'] .= "|" . static::getChCode($goodsType['parent_type']['id']); } $saveData = $params; $saveData['title'] = $goodsType['name'] . " " . $goodsType['parent_type']['name'] ?? ''; return Goods::query()->firstOrCreate($params, $saveData); } public function getRandomCode() { $time = time(); return substr($time, -7) . static::randomString(3); } public static function randomString($len = 32) { $chars = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ]; // 将数组打乱 shuffle($chars); $charsLen = count($chars) - 1; $output = ""; for ($i = 0; $i < $len; $i++) { $output .= $chars[mt_rand(0, $charsLen)]; } return $output; } public function getChCode($intValue) { $ch = range('A', 'Z'); $result = ''; while ($intValue) { $result = $ch[$intValue % 26] . $result; $intValue = intval($intValue / 26); } return $result; } }