Common.php
8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
class Common
{
//检测表达式
private static function check_condition($condition)
{
return !!preg_match('/^(\S+--\S+--\S+--[\S ]+__)*(\S+--\S+--\S+--[\S ]+)$/', $condition);
}
//字符串转条件
public static function str2where($condition)
{
if(self::check_condition($condition))
{
$condition = preg_replace('/^(and|or)/i', '', $condition);
$condition = str_replace(array('--','__'), array(' ',"' "), $condition);
$old_char=array(' ne ',' eq ',' lt ',' gt ',' le ',' ge ',' ct ',' nct ');
$new_char=array(" != '"," = '"," < '"," > '"," <= '"," >= '", " like '%"," not like '%");
$condition = str_replace($old_char, $new_char, $condition);
$condition = preg_replace("/\s+(like\s+'[^']+)('|$)/i", " $1%$2", $condition);
if($condition!='') $condition .= "'";
return $condition;
}
return null;
}
//生成订单编号
public static function createOrderNo()
{
return date('YmdHis').rand(100000,999999);
}
//发放代金券
public static function paymentVoucher($voucherTemplate,$userID=null)
{
$model = new Model("voucher");
do{
$account = strtoupper(CHash::random(10,'char'));
$password = strtoupper(CHash::random(10,'char'));
$obj = $model->where("account = '$account'")->find();
}while($obj);
$start_time = date('Y-m-d H:i:s');
$end_time = date('Y-m-d H:i:s', strtotime('+'.$voucherTemplate['valid_days'].' days'));
$model->data(array('account'=>$account,'password'=>$password,'name'=>$voucherTemplate['name'],'value'=>$voucherTemplate['value'],'start_time'=>$start_time,'end_time'=>$end_time,'money'=>$voucherTemplate['money'],'is_send'=>1,'user_id'=>$userID))->insert();
}
//分类的树状化数组
public static function treeArray($datas){
$result = array();
$I = array();
foreach($datas as $val) {
$sort = intval($val['sort']);
if($val['parent_id'] == 0)
{
if(isset($result[$val['sort']])) $i = count($result[$val['sort']]);
else $i = 0;
$result[$sort][$i] = $val;
$I[$val['id']] = &$result[$sort][$i];
krsort($result);
} else
{
if(isset($I[$val['parent_id']]['child'][$sort])) $i = count($I[$val['parent_id']]['child'][$sort]);
else $i = 0;
$I[$val['parent_id']]['child'][$sort][$i] = $val;
krsort($I[$val['parent_id']]['child']);
$I[$val['id']] = &$I[$val['parent_id']]['child'][$sort][$i];
}
}
return self::parseTree($result);
}
//递归树状数组
public static function parseTree($result,&$tree = array())
{
foreach ($result as $items)
{
if(is_array($items))
{
foreach ($items as $item)
{
$tem = $item;
if(isset($item['child'])) unset($tem['child']);
$tree[] = $tem;
if(isset($item['child']))self::parseTree($item['child'],$tree);
}
}
}
return $tree;
}
//价格区间计算
static function priceRange($range)
{
$d0 = intval($range['min'],-2);
$d1 = intval(($range['min']+$range['avg'])/2);
$d2 = intval($range['avg']);
$d3 = intval(($range['max']+$range['avg'])/2);
$d4 = intval($range['max']);
if($d4>$d3 && $d3>$d2 && $d2>$d1 && $d1>$d0){
$d1 = self::formatInt($d1);
$d2 = self::formatInt($d2);
$d3 = self::formatInt($d3);
$d4 = self::formatInt($d4);
$price_range[0] = '0-'.$d1;
if($d2-$d1>2) $price_range[1] = $d1.'-'.($d2-1);
else $price_range[1] = $d1.'-'.$d2;
if($d3-$d2>2) $price_range[2] = $d2.'-'.($d3-1);
else $price_range[2] = $d2.'-'.$d3;
if($d4-$d3>2) $price_range[3] = $d3.'-'.($d4-1);
else $price_range[3] = $d3.'-'.$d4;
$price_range[4] = "$d4";
return $price_range;
}else{
if($d2!=0){
$d2 = self::formatInt($d2);
if($d2>1)return array(0=>('0-'.($d2-1)),1=>"$d2");
else return array(0=>('0-'.($d2)),1=>"$d2");
}else if($range['min']!=0){
return array(0=>('0-'.($range['min'])),1=>"$range[min]");
}
else return array();
}
}
static function formatInt($value)
{
$len = strlen($value);
switch ($len) {
case 1:
break;
case 2:
$value = round($value,-1);
break;
case 3:
case 4:
$value = round($value,-2);
break;
default:
$value = round($value,2-$len);
break;
}
return $value;
}
//thumb
static function thumb($image_url,$w=200,$h=200)
{
if(preg_match('@http://@i',$image_url)) return $image_url;
$access_image_size = array(
'220_220'=>true,
'100_100'=>true,
'367_367'=>true
);
$theme_config = Tiny::app()->getTheme()->getConfigInfo();
if($theme_config!=null && isset($theme_config['access_image_size'])){
$access_image_size = array_merge($access_image_size,$theme_config['access_image_size']);
}
if(func_num_args()==2)$h = $w;
if($image_url == '') return '';
if(isset($access_image_size[$w.'_'.$h])){
$ext = strtolower(strrchr($image_url,'.'));
$result_url = $image_url.'__'.$w.'_'.$h.$ext;
if(!file_exists(APP_ROOT.$result_url)){
$image = new Image();
$image->suffix = 'f_w_h';
$result_url = $image->thumb(APP_ROOT.$image_url,$w,$h);
$result_url = str_replace(APP_ROOT, '', $result_url);
}
return Url::urlFormat('@'.$result_url);
}else{
return Url::urlFormat('@'.$image_url);
}
}
//自动登录时的用户信息
static function autoLoginUserInfo()
{
$cookie = new Cookie();
$cookie->setSafeCode(Tiny::app()->getSafeCode());
$autologin = $cookie->get('autologin');
$obj = null;
if($autologin!=null){
$account = Filter::sql($autologin['account']);
$password = $autologin['password'];
$model = new Model("user as us");
$obj = $model->join("left join customer as cu on us.id = cu.user_id")->fields("us.*,cu.group_id,cu.user_id,cu.login_time,cu.mobile")->where("us.email='$account' or us.name='$account' or cu.mobile='$account'")->find();
if($obj['password'] != $password){
$obj = null;
}
}else{
$weixin_openid = Cookie::get('weixin_openid');
if($weixin_openid != null){
$model = new Model('oauth_user');
$oauth_user = $model->where("oauth_type='WeixinOAuth' and open_id='$weixin_openid'")->find();
if($oauth_user && $oauth_user['user_id']!=''){
$model = new Model("user as us");
$obj = $model->join("left join customer as cu on us.id = cu.user_id")->fields("us.*,cu.group_id,cu.user_id,cu.login_time,cu.mobile")->where("us.id=".$oauth_user['user_id'])->find();
}
}
}
return $obj;
}
//取得支付方式信息
static function getPaymentInfo($id)
{
$model = new Model('payment as pa');
$payment = $model->join('left join pay_plugin as pp on pa.plugin_id = pp.id')->where("pa.id = ".$id)->find();
return $payment;
}
}