165 lines
4.8 KiB
PHP
Executable File
165 lines
4.8 KiB
PHP
Executable File
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
|
// +----------------------------------------------------------------------
|
|
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
|
|
// +----------------------------------------------------------------------
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
|
// +----------------------------------------------------------------------
|
|
// | Author: 流年 <liu21st@gmail.com>
|
|
// +----------------------------------------------------------------------
|
|
use think\facade\Env;
|
|
use app\common\model\Order;
|
|
use think\db;
|
|
|
|
// 应用公共文件
|
|
|
|
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
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* 生成会员码
|
|
* @return 会员码
|
|
*/
|
|
function create_user_sn($prefix = '', $length = 8)
|
|
{
|
|
$rand_str = '';
|
|
for ($i = 0; $i < $length; $i++) {
|
|
$rand_str .= mt_rand(0, 9);
|
|
}
|
|
$sn = $prefix . $rand_str;
|
|
if (Db::name('user')->where(['sn' => $sn])->find()) {
|
|
return create_user_sn($prefix, $length);
|
|
}
|
|
return $sn;
|
|
}
|
|
|
|
//生成用户邀请码
|
|
function generate_invite_code()
|
|
{
|
|
|
|
$letter_all = range('A', 'Z');
|
|
shuffle($letter_all);
|
|
//排除I、O字母
|
|
$letter_array = array_diff($letter_all, ['I', 'O', 'D']);
|
|
//排除1、0
|
|
$num_array = range('2', '9');
|
|
shuffle($num_array);
|
|
|
|
$pattern = array_merge($num_array, $letter_array, $num_array);
|
|
shuffle($pattern);
|
|
$pattern = array_values($pattern);
|
|
|
|
$code = '';
|
|
for ($i = 0; $i < 6; $i++) {
|
|
$code .= $pattern[mt_rand(0, count($pattern) - 1)];
|
|
}
|
|
|
|
$code = strtoupper($code);
|
|
$check = Db::name('user')->where('distribution_code', $code)->find();
|
|
if ($check) {
|
|
return generate_invite_code();
|
|
}
|
|
return $code;
|
|
}
|