// +---------------------------------------------------------------------- use think\facade\Env; use app\common\model\Order; // 应用公共文件 if (!function_exists('get_middle_str')) { /** * 获取指定字符串中间内容 * @param string $str 原字符串 * @param string $leftStr 左侧 * @param string $rightStr * @return bool|string */ function get_middle_str($str, $leftStr, $rightStr) { $left = strpos($str, $leftStr); $right = strpos($str, $rightStr, $left); if ($left < 0 || $right < $left) { return ''; } return substr($str, $left + strlen($leftStr), $right - $left - strlen($leftStr)); } } if (!function_exists('format_size')) { /** * 格式化文件大小单位 * @param $size * @param string $delimiter * @return string */ function format_size($size, $delimiter = '') { $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB'); for ($i = 0; $size >= 1024 && $i < 5; $i++) { $size /= 1024; } return round($size, 2) . $delimiter . $units[$i]; } } if (!function_exists('thumbnail_img')) { /** * 生成缩略图 * @param string $status 上传后图片信息 * @param string $width 宽 * @param string $heigh 高 */ function thumbnail_img($status, $width, $heigh, $folder_name = '') { $img = Env::get('root_path') . 'public/uploads/attachment/' . $status->getSaveName(); $image = \think\Image::open($img); $slt = substr($img, 0, strlen($img) - 4); $image->thumb($width, $heigh, \think\Image::THUMB_CENTER)->save($slt . '_' . $width . '_' . $heigh . '.' . $status->getExtension()); $img_url = $status->getSaveName(); $img_slt = '/uploads/' . $folder_name . '/' . substr($img_url, 0, strlen($img_url) - 4) . '_' . $width . '_' . $heigh . '.' . $status->getExtension(); return $img_slt; } } if (!function_exists('get_order_sn')) { /** * 获取得到新订单编号 */ function get_order_sn() { mt_srand((float) microtime() * 1000000); $order_sn = 'O' . date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT); $where = 'order_no="' . $order_sn . '"'; //开始编辑区域 $_OrderModel = new Order(); $count = $_OrderModel->where($where)->count(); //结束编辑区域 if ($count == 0) { /* 否则就使用这个订单号 */ return $order_sn; } return get_order_sn(); } } /** * Desc: 用时间生成订单编号 * @param $table * @param $field * @param string $prefix * @param int $rand_suffix_length * @param array $pool * @return string * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ function createSn($table, $field, $prefix = '', $rand_suffix_length = 4, $pool = []) { $suffix = ''; for ($i = 0; $i < $rand_suffix_length; $i++) { if (empty($pool)) { $suffix .= rand(0, 9); } else { $suffix .= $pool[array_rand($pool)]; } } $sn = $prefix . date('YmdHis') . $suffix; if (Db::name($table)->where($field, $sn)->find()) { return createSn($table, $field, $prefix, $rand_suffix_length, $pool); } return $sn; }