Files
qlg.tsgz.moe/hyhproject/common/common/function.php
2020-09-21 08:15:51 +08:00

2099 lines
65 KiB
PHP
Executable File

<?php
/**
* ============================================================================
*/
use think\Db;
use wstmart\app\model\Base;
use wstmart\common\model\Aliyunoss;
const WST_ADDON_PATH = './addons/';
/******************自定义函数 start*************************/
function getAdminPhone(){
return '13336336128';
}
/**
* 获取惠宝奖励模式,0分期奖励,1是一次性奖励 mark 20170914
*/
function GetRewardScoreType(){
return 0;
}
/**
* 惠宝抵用比例,默认20% mark 20170914
*/
function HuiScale(){
return 0;//0.2;
}
/**
* 批发时惠宝抵用比例,默认10% mark 20171118
*/
function HuiWhsleScale(){
return 0.1;
}
/**
* 获取联盟id
*/
function GetLMID($userId){
return (int)Db::name('users')->where(['userId'=>$userId])->value('hyhlm_id');
}
/**
* 获取商城id,根据联盟id
*/
function GetShopID($user_id){
return (int)Db::table('rd_users')->where(['user_id'=>$user_id])->value('shop_id');
}
/**
* 获取userId,根据shopId
*/
function GetShopInfo($shopId,$field='userId'){
return Db::name('shops')->where(['shopId'=>$shopId])->field($field)->find();
}
/** [getLMUserByID 根据联盟id返回]
* @param [type] $user_id [description]
* @param string $field [description]
* @return [type] [description]
*/
function getLMUserByID($user_id,$field='name'){
$rs = Db::table('rd_users')->where(['user_id'=>$user_id])->field($field)->find();
return $rs;
}
/**
* [getLMUserByName 根据联盟名字返回]
* @param [type] $name [description]
* @param string $field [description]
* @return [type] [description]
*/
function getLMUserByName($name,$field='user_id'){
$rs = Db::table('rd_users')->where(['name|mobile'=>$name,'is_lock'=>0])->field($field)->find();
return $rs;
}
/**
* [getUserByName 根据名字返回]
* @param [type] $name [description]
* @param string $field [description]
* @return [type] [description]
*/
function getUserByName($name,$field='userId'){
$rs = Db::name('users')->where(["loginName|userPhone"=>['=',$name],'dataFlag'=>1])->field($field)->find();
return $rs;
}
/**
* [getUserByName 根据名字返回]
* @param [type] $name [description]
* @param string $field [description]
* @return [type] [description]
*/
function getUserInfo($where,$field='userId'){
$where['dataFlag']=1;
$rs = Db::name('users')->where($where)->field($field)->find();
return $rs;
}
/** [getUserByID 根据id返回]
* @param [type] $userId [description]
* @param string $field [description]
* @return [type] [description]
*/
function getUserByID($userId,$field='loginName'){
$rs = Db::name('users')->where(['userId'=>$userId])->field($field)->find();
return $rs;
}
function getUserByToken($token,$field='*'){
$rs = Db::name('users')->where(['token'=>$token])->field($field)->find();
return $rs;
}
function errLog($e){
$content = date('Y-m-d H:i:s').'-errMessage:'.$e->getMessage().',errLine:'.$e->getLine().',errInfo:'.$e->getTraceAsString().PHP_EOL.PHP_EOL;
\think\Log::write($content);
// $errAction = request()->module() . '/' . request()->controller() . '/' . request()->action();
// \think\Log::write($errAction.'-error:'.$e->getMessage().',errAction:'.$errAction.',errFile:'.$e->getFile().',errLine:'.$e->getLine());
}
/**
* 添加源宝记录
* @param [type] $userId [会员ID]
* @param [type] $dataId [数据记录ID]
* @param [type] $dataSrc [流水来源 1:交易订单 2:订单结算 3:提现申请 4.退款订单 20:抽奖所得]
* @param [type] $chargeMoney [变动金额]
* @param [type] $remark [备注]
* @param int $moneyType [0:支出 1:收入]
* @param int $targetType [0:会员 1:商家]
* @param string $payFrom [支付类型 支付来源代码,和支付方式对应代码]
* @param string $trade_no [外部流水号 例如微信支付,支付宝支付的交易流水号]
*/
function addMoneyLog($userId,$dataId,$dataSrc,$chargeMoney,$moneyType,$remark='',$targetType=0,$payFrom='',$trade_no=''){
$lm = [];
$lm['targetType'] = $targetType;
$lm['targetId'] = $userId;
$lm['dataId'] = $dataId;
$lm['dataSrc'] = $dataSrc;//抽奖获得
$lm['remark'] = $remark;
$lm['moneyType'] = $moneyType;
$lm['money'] = $chargeMoney;
$lm['payType'] = $payFrom;
$lm['tradeNo'] = $trade_no;
$lm['createTime'] = date('Y-m-d H:i:s');
if(Db::name('log_moneys')->insert($lm)){
return Db::name('users')->where(['userId'=>$userId])->setField('userMoney',$chargeMoney);
}
}
/**
* 添加惠宝记录
* @param [type] $userId [会员ID]
* @param [type] $dataId [数据记录ID]
* @param [type] $dataSrc [流水来源 1:订单 2:评价 3:订单取消返还 4:拒收返还 20:抽奖所得]
* @param [type] $chargeMoney [变动金额]
* @param [type] $remark [备注]
* @param int $moneyType [0:支出 1:收入]
*/
function addScoreLog($userId,$dataId,$dataSrc,$chargeMoney,$moneyType,$remark=''){
$uscore = [];
$uscore['userId'] = $userId;
$uscore['score'] = $chargeMoney;
$uscore['dataSrc'] = $dataSrc;
$uscore['dataId'] = $dataId;
$uscore['dataRemarks'] = $remark;
$uscore['scoreType'] = $moneyType;
model('common/UserScores')->add($uscore,true);
return Db::name('users')->where(['userId'=>$userId])->setField('userScore',$chargeMoney);
}
/**
* 获取广告列表
* 如 listAds('ads-hyhchosen',6,86400);
*/
function listAds($positionCode,$num,$cache = 0){
$cacheData = cache('TAG_ADS_APP'.$positionCode);
if($cacheData)return $cacheData;
$today = date('Y-m-d');
$rs = Db::name("ads")->alias('a')->join('__AD_POSITIONS__ ap','a.adPositionId= ap.positionId and ap.dataFlag=1','left')
->where("a.dataFlag=1 and ap.positionCode='".$positionCode."' and adStartDate<= '$today' and adEndDate>='$today'")
->field('adId,adName,adURL,targetType,adFile,positionWidth,positionHeight')
->order('adSort DESC')->limit($num)->select();
foreach ($rs as &$v) {
$v['adFile'] = WSTImg($v['adFile'],2);
}
// if(count($rs)>0){
// foreach ($rs as $key => $v) {
// $rs[$key]['isOpen'] = false;
// if(stripos($v['adURL'],'http:')!== false || stripos($v['adURL'],'https:')!== false){
// $rs[$key]['isOpen'] = true;
// }
// }
// }
cache('TAG_ADS_APP'.$positionCode,$rs,$cache);
return $rs;
}
function getImgUrl(){
return 'http://img.zgqlg.com.cn/';
}
/**
* [file_exists_oss [判断文件是否存在oss上]
* @param [type] $object [description]
* @return [type] [description]
*/
function file_exists_oss($object){
$imgurl = getImgUrl().$object;
$ch = curl_init();
$timeout = 10;
curl_setopt ($ch, CURLOPT_URL, $imgurl);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
if (preg_match("/404/", $contents)){
return false;
}else{
return true;
}
//说是在windows下可以,LINUX下无论图片在不在都返加TRUE
// if(file_get_contents($imgurl,0,null,0,1)){
// return true;
// }else{
// return false;
// }
// if(@fopen( $imgurl, 'r' )){
// return true;
// }else{
// return false;
// }
}
/******************自定义函数 end*************************/
/*******************ect社区自己定义函数********************/
/**
* [ectLog ect记录]
* @param [type] $userId [用户id]
* @param [type] $ectNum [ect数目]
* @param [type] $dataSrc [数据来源]
* @param [type] $remark [备注]
* @param [type] $update [更新表达式:['userECT'=>['exp','userECT+'.$recomNum]]]
* @return [type] [bool]
*/
function ectLog($userId,$ectNum,$dataSrc,$remark,$update,$ectType=1){
if(!$ectNum || ($ectNum > 0 && $ectNum < 0.00001)) return;
// if($ectNum < 0){
// $ectType = 2;
// }else{
// $ectType = 1;
// }
$data['userId'] = $userId;
$data['ectNum'] = $ectNum;
$data['dataSrc'] = $dataSrc;
$data['dataRemarks'] = $remark;
$data['ectType'] = $ectType;
$data['createTime'] = time();
if(false !== Db::name('user_ect_log')->insert($data)){
Db::name('users')->where(['userId'=>$userId])->update($update);
return true;
}
return false;
}
/**
* [create_tree 用户表树]
* @param [type] $uid [用户id]
* @param [type] $pid [推荐人id]
* @return [type] [description]
*/
function create_tree($uid,$pid){
if(!$uid) return;
if(Db::name('user_trees')->where(array('uid'=>$uid))->find()) return;//树里有
if($pid){
$p_info = Db::name('user_trees')->where(array('uid'=>$pid))->field('bid,t_level')->find();
if($p_info['t_level']){
$t_level = $p_info['t_level'] + 1;
}else{
$t_level = 2;
}
if($p_info['bid']){
$bid = $p_info['bid'];
}else{
$bid = $pid;
}
}else{
$bid = $uid;
$t_level = 1;
}
$set = array(
'uid'=>$uid,
'pid'=>$pid,
'bid'=>$bid,
't_level'=>$t_level,
'update_time'=>time()
);
Db::name('user_trees')->insert($set);
//获取家族树最顶级节点,重建树
// rebuild_tree($bid,1);//重建树,建立有左右值的数据表,对整个结构重新进行一次编号
}
function rebuild_tree($root, $left) {
// the right value of this node is the left value + 1
$right = $left+1;
// get all children of this node
$trees = Db::name('user_trees')->where(array('pid'=>$root))->field('uid')->select();
if($trees){
foreach ($trees as $k => $v) {
$right = rebuild_tree($v['uid'], $right);
}
}
$set = array(
'lft' => $left,
'rgt' => $right,
);
Db::name('user_trees')->where(array('uid'=>$root))->update($set);
// return the right value of this node + 1
return $right + 1;
}
/******************自定义函数 end*************************/
/**
* 获取分类的佣金原函数
*/
// function WSTGoodsCommissionRate($goodsCatId){
// $cats = Db::name('goods_cats')->where('catId',$goodsCatId)->field('parentId,commissionRate')->find();
// if(empty($cats)){
// return 0;
// }else{
// if((float)$cats['commissionRate']>=0)return (float)$cats['commissionRate'];
// return WSTGoodsCommissionRate($cats['parentId']);
// }
// }
/**
* 获取分类的佣金 mark 修改一下,商家批量购买佣金5% 20170914
*/
function WSTGoodsCommissionRate($goodsCatId,$isWhsle = 0){
$cats = Db::name('goods_cats')->where('catId',$goodsCatId)->field('parentId,commissionRate')->find();
if(empty($cats)){
return 0;
}else{
if((float)$cats['commissionRate']>=0){
if($isWhsle){
return (float)($cats['commissionRate']/2);
}else{
return (float)$cats['commissionRate'];
}
}
return WSTGoodsCommissionRate($cats['parentId'],$isWhsle);//递归获取分类的佣金,因为-1是继承父级的分类佣金
}
}
/**
* 检测登录账号是否可用 原函数
* @param $key 要检测的内容
*/
// function WSTCheckLoginKey($val,$userId = 0){
// if($val=='')return WSTReturn("登录账号不能为空");
// if(!WSTCheckFilterWords($val,WSTConf("CONF.registerLimitWords"))){
// return WSTReturn("登录账号包含非法字符");
// }
// $dbo = Db::name('users')->where(["loginName|userEmail|userPhone"=>['=',$val],'dataFlag'=>1]);
// if($userId>0){
// $dbo->where("userId", "<>", $userId);
// }
// $rs = $dbo->count();
// if($rs==0){
// return WSTReturn("该登录账号可用",1);
// }
// return WSTReturn("对不起,登录账号已存在");
// }
/**
* 检测登录账号是否可用
* @param $key 要检测的内容
*/
function WSTCheckLoginKey($val,$userId = 0){
if($val=='')return WSTReturn("登录账号不能为空");
if(!WSTCheckFilterWords($val,WSTConf("CONF.registerLimitWords"))){
return WSTReturn("登录账号包含非法字符");
}
$dbo = Db::name('users')->where(["loginName|userPhone"=>['=',$val]]);
if($userId>0){
$dbo->where("userId", "<>", $userId);
}
if($dbo->find()){
return WSTReturn("友情提示:您好:该用户名已注册");
}
return WSTReturn("该登录账号可用",1);
// $rs = $dbo->count();
// if($rs==0){
// $lm_info = getLMUserByName($val);//获取联盟会员信息
// if($lm_info){
// return WSTReturn("该登录账号已注册联盟用户");
// }
// return WSTReturn("该登录账号可用",1);
// }
// return WSTReturn("对不起,登录账号已存在");
}
/***************************以上都是为修改内容*****************************/
/**
* 生成验证码
*/
function WSTVerify(){
ob_clean();
$Verify = new \verify\Verify();
$Verify->length = 4;
$Verify->entry();
}
/**
* 核对验证码
*/
function WSTVerifyCheck($code){
$verify = new \verify\Verify();
return $verify->check($code);
}
/**
* 生成数据返回值
*/
function WSTReturn($msg,$status = -1,$data = []){
$rs = ['status'=>$status,'msg'=>$msg];
if(!empty($data))$rs['data'] = $data;
return $rs;
}
/**
* 生成数据返回值
*/
function jsonReturn($msg,$status = -1,$data = []){
//if(isset($data['status']))return json_encode($data);
$rs = ['status'=>$status,'msg'=>$msg];
if(!empty($data))$rs['data'] = $data;
return json_encode($rs);
}
/**
* 检测字符串不否包含
* @param $srcword 被检测的字符串
* @param $filterWords 禁用使用的字符串列表
* @return boolean true-检测到,false-未检测到
*/
function WSTCheckFilterWords($srcword,$filterWords){
$flag = true;
if($filterWords!=""){
$filterWords = str_replace("",",",$filterWords);
$words = explode(",",$filterWords);
for($i=0;$i<count($words);$i++){
if(strpos($srcword,$words[$i]) !== false){
$flag = false;
break;
}
}
}
return $flag;
}
function dataConf($key){
$rs = cache('DATA_CONFIG_'.$key);
if(!$rs){
$rs = Db::name('data_configs')->where(['fieldCode'=>$key])->value('fieldValue');
cache('DATA_CONFIG_'.$key,$rs,0);
}
return $rs;
}
function dataConfList($field,$order='id ASC'){
return Db::name('data_configs')->field($field)->order($order)->select();
}
/**
* 获取指定的全局配置
*/
function WSTConf($key,$v = ''){
if(is_null($v)){
if(array_key_exists('WSTMARTCONF',$GLOBALS) && array_key_exists($key,$GLOBALS['WSTMARTCONF'])){
unset($GLOBALS['WSTMARTCONF'][$key]);
}
}else if($v === ''){
if(array_key_exists('WSTMARTCONF',$GLOBALS)){
$conf = $GLOBALS['WSTMARTCONF'];
$ks = explode(".",$key);
for($i=0,$k=count($ks);$i<$k;$i++){
if(array_key_exists($ks[$i],$conf)){
$conf = $conf[$ks[$i]];
}else{
return null;
}
}
return $conf;
}
}else{
return $GLOBALS['WSTMARTCONF'][$key] = $v;
}
return null;
}
//php获取中文字符拼音首字母
function WSTGetFirstCharter($str){
if(empty($str)){
return '';
}
$fchar=ord($str{0});
if($fchar>=ord('A')&&$fchar<=ord('z')) return strtoupper($str{0});
$s1=iconv('UTF-8','gb2312',$str);
$s2=iconv('gb2312','UTF-8',$s1);
$s=$s2==$str?$s1:$str;
if(empty($s{1})){
return '';
}
$asc=ord($s{0})*256+ord($s{1})-65536;
if($asc>=-20319 && $asc<=-20284) return 'A';
if($asc>=-20283 && $asc<=-19776) return 'B';
if($asc>=-19775 && $asc<=-19219) return 'C';
if($asc>=-19218 && $asc<=-18711) return 'D';
if($asc>=-18710 && $asc<=-18527) return 'E';
if($asc>=-18526 && $asc<=-18240) return 'F';
if($asc>=-18239 && $asc<=-17923) return 'G';
if($asc>=-17922 && $asc<=-17418) return 'H';
if($asc>=-17417 && $asc<=-16475) return 'J';
if($asc>=-16474 && $asc<=-16213) return 'K';
if($asc>=-16212 && $asc<=-15641) return 'L';
if($asc>=-15640 && $asc<=-15166) return 'M';
if($asc>=-15165 && $asc<=-14923) return 'N';
if($asc>=-14922 && $asc<=-14915) return 'O';
if($asc>=-14914 && $asc<=-14631) return 'P';
if($asc>=-14630 && $asc<=-14150) return 'Q';
if($asc>=-14149 && $asc<=-14091) return 'R';
if($asc>=-14090 && $asc<=-13319) return 'S';
if($asc>=-13318 && $asc<=-12839) return 'T';
if($asc>=-12838 && $asc<=-12557) return 'W';
if($asc>=-12556 && $asc<=-11848) return 'X';
if($asc>=-11847 && $asc<=-11056) return 'Y';
if($asc>=-11055 && $asc<=-10247) return 'Z';
return null;
}
/**
* 设置当前页面对象
* @param int 0-用户 1-商家
*/
function WSTLoginTarget($target = 0){
$WST_USER = session('WST_USER');
$WST_USER['loginTarget'] = $target;
session('WST_USER',$WST_USER);
}
/**
* 邮件发送函数
* @param string to 要发送的邮箱地址
* @param string subject 邮件标题
* @param string content 邮件内容
* @return array
*/
function WSTSendMail($to, $subject, $content) {
$mail = new \phpmailer\phpmailer();
// 装配邮件服务器
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->Host = WSTConf("CONF.mailSmtp");
$mail->SMTPAuth = WSTConf("CONF.mailAuth");
$mail->Username = WSTConf("CONF.mailUserName");
$mail->Password = WSTConf("CONF.mailPassword");
$mail->CharSet = 'utf-8';
// 装配邮件头信息
$mail->From = WSTConf("CONF.mailAddress");
$mail->AddAddress($to);
$mail->FromName = WSTConf("CONF.mailSendTitle");
$mail->IsHTML(true);
// 装配邮件正文信息
$mail->Subject = $subject;
$mail->Body = $content;
// 发送邮件
$rs =array();
if (!$mail->Send()) {
$rs['status'] = 0;
$rs['msg'] = $mail->ErrorInfo;
return $rs;
} else {
$rs['status'] = 1;
return $rs;
}
}
/**
* 获取系统配置数据
*/
function WSTConfig(){
$rs = cache('WST_CONF');
if(!$rs){
$rv = Db::name('sys_configs')->field('fieldCode,fieldValue')->select();
$rs = [];
foreach ($rv as $v){
$rs[$v['fieldCode']] = $v['fieldValue'];
}
//获取风格
$styles = Db::name('styles')->where(['isUse'=>1])->field('styleSys,stylePath,id')->select();
if(!empty($styles)){
foreach ($styles as $key => $v) {
$rs['wst'.$v['styleSys'].'Style'] = $v['stylePath'];
$rs['wst'.$v['styleSys'].'StyleId'] = $v['id'];
}
}
//获取上传文件目录配置
$data = Db::name('datas')->where('catId',3)->column('dataVal');
foreach ($data as $key => $v){
$data[$key] = str_replace('_','',$v);
}
$rs['wstUploads'] = $data;
if($rs['mallLicense']=='')$rs['mallSlogan'] = $rs['mallSlogan']." ".base64_decode('UG93ZXJlZCBCeSBXU1RNYXJ0');
cache('WST_CONF',$rs,31536000);
}
return $rs;
}
/**
* 判断手机号格式是否正确
*/
function WSTIsPhone($phoneNo){
//$reg = "/^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$/";
$reg = "/^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[\d]{9}$|^18[\d]{9}$|^16[\d]{9}$|^19[\d]{9}$/";
$rs = \think\Validate::regex($phoneNo,$reg);
return $rs;
}
// /**
// * 检测登录账号是否可用
// * @param $key 要检测的内容
// */
// function WSTCheckLoginKey($val,$userId = 0){
// if($val=='')return WSTReturn("登录账号不能为空");
// if(!WSTCheckFilterWords($val,WSTConf("CONF.registerLimitWords"))){
// return WSTReturn("登录账号包含非法字符");
// }
// $dbo = Db::name('users')->where(["loginName|userEmail|userPhone"=>['=',$val],'dataFlag'=>1]);
// if($userId>0){
// $dbo->where("userId", "<>", $userId);
// }
// $rs = $dbo->count();
// if($rs==0){
// return WSTReturn("该登录账号可用",1);
// }
// return WSTReturn("对不起,登录账号已存在");
// }
/**
* 生成随机数账号
*/
function WSTRandomLoginName($loginName){
$chars = array("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");
//简单的派字母
foreach ($chars as $key =>$c){
$crs = WSTCheckLoginKey($loginName."_".$c);
if($crs['status']==1)return $loginName."_".$c;
}
//随机派三位数值
for($i=0;$i<1000;$i++){
$crs = WSTCheckLoginKey($loginName."_".$i);
if($crs['status']==1)return $loginName."_".$i;
}
return '';
}
/**
* 删除一维数组里的多个key
*/
function WSTUnset(&$data,$keys){
if($keys!='' && is_array($data)){
$key = explode(',',$keys);
foreach ($key as $v)unset($data[$v]);
}
}
/**
* 只允许一维数组里的某些key通过
*/
function WSTAllow(&$data,$keys){
if($keys!='' && is_array($data)){
$key = explode(',',$keys);
foreach ($data as $vkeys =>$v)if(!in_array($vkeys,$key))unset($data[$vkeys]);
}
}
/**
* 字符串替换
* @param string $str 要替换的字符串
* @param string $repStr 即将被替换的字符串
* @param int $start 要替换的起始位置,从0开始
* @param string $splilt 遇到这个指定的字符串就停止替换
*/
function WSTStrReplace($str,$repStr,$start,$splilt = ''){
$newStr = substr(utf8_encode($str),0,$start);
$breakNum = -1;
for ($i=$start;$i<strlen($str);$i++){
$char = substr($str,$i,1);
if($char==$splilt){
$breakNum = $i;
break;
}
$newStr.=$repStr;
}
if($splilt!='' && $breakNum>-1){
for ($i=$breakNum;$i<strlen($str);$i++){
$char = substr($str,$i,1);
$newStr.=$char;
}
}
return $newStr;
}
/**
* 获取指定商品分类的子分类列表/获取指定的商品分类,靠$isSelf=-1判断
*/
function WSTGoodsCats($parentId = 0,$isFloor = -1,$isSelf = 0){
if($isSelf==1){
return Db::name('goods_cats')->where(['dataFlag'=>1, 'isShow' => 1,'catId'=>$parentId])
->field("catName,catId,parentId")->order('catSort asc')->find();
}else{
$dbo = Db::name('goods_cats')->where(['dataFlag'=>1, 'isShow' => 1,'parentId'=>$parentId]);
if($isFloor!=-1)$dbo->where('isFloor',$isFloor);
return $dbo->field("catName,catId")->order('catSort asc')->select();
}
}
// /**
// * 上传图片
// * 需要生成缩略图: isThumb=1
// * 需要加水印:isWatermark=1
// * pc版缩略图: width height
// * 手机版原图:mWidth mHeight
// * 缩略图:mTWidth mTHeight
// * 判断图片来源:fromType 0:商家/用户 1:平台管理员
// */
// function WSTUploadPic($fromType=0){
// $fileKey = key($_FILES);
// $dir = Input('param.dir');
// if($dir=='')return json_encode(['msg'=>'没有指定文件目录!','status'=>-1]);
// $dirs = WSTConf("CONF.wstUploads");
// if(!in_array($dir, $dirs)){
// return json_encode(['msg'=>'非法文件目录!','status'=>-1]);
// }
// // 上传文件
// $file = request()->file($fileKey);
// if($file===null){
// return json_encode(['msg'=>'上传文件不存在或超过服务器限制','status'=>-1]);
// }
// $validate = new \think\Validate([
// ['fileMime','fileMime:image/png,image/gif,image/jpeg,image/x-ms-bmp','只允许上传jpg,gif,png,bmp类型的文件'],
// ['fileExt','fileExt:jpg,jpeg,gif,png,bmp','只允许上传后缀为jpg,gif,png,bmp的文件'],
// ['fileSize','fileSize:2097152','文件大小超出限制'],//最大2M
// ]);
// $data = ['fileMime' => $file,
// 'fileSize' => $file,
// 'fileExt'=> $file
// ];
// if (!$validate->check($data)) {
// return json_encode(['msg'=>$validate->getError(),'status'=>-1]);
// }
// $info = $file->rule('uniqid')->move(ROOT_PATH.'/upload/'.$dir."/".date('Y-m'));
// if($info){
// $filePath = $info->getPathname();
// $filePath = str_replace(ROOT_PATH,'',$filePath);
// $filePath = str_replace('\\','/',$filePath);
// $name = $info->getFilename();
// $filePath = str_replace($name,'',$filePath);
// //原图路径
// $imageSrc = trim($filePath.$name,'/');
// //图片记录
// WSTRecordImages($imageSrc, (int)$fromType);
// //打开原图
// $image = \image\Image::open($imageSrc);
// //缩略图路径 手机版原图路径 手机版缩略图路径
// $thumbSrc = $mSrc = $mThumb = null;
// //手机版原图宽高
// $mWidth = min($image->width(),(int)input('mWidth',700));
// $mHeight = min($image->height(),(int)input('mHeight',700));
// //手机版缩略图宽高
// $mTWidth = min($image->width(),(int)input('mTWidth',250));
// $mTHeight = min($image->height(),(int)input('mTHeight',250));
// /****************************** 生成缩略图 *********************************/
// $isThumb = (int)input('isThumb');
// if($isThumb==1){
// //缩略图路径
// $thumbSrc = str_replace('.', '_thumb.', $imageSrc);
// $image->thumb((int)input('width',min(300,$image->width())), (int)input('height',min(300,$image->height())),2)->save($thumbSrc,$image->type(),90);
// //是否需要生成移动版的缩略图
// $suffix = WSTConf("CONF.wstMobileImgSuffix");
// if(!empty($suffix)){
// $image = \image\Image::open($imageSrc);
// $mSrc = str_replace('.',"$suffix.",$imageSrc);
// $mThumb = str_replace('.', '_thumb.',$mSrc);
// $image->thumb($mWidth, $mHeight)->save($mSrc,$image->type(),90);
// $image->thumb($mTWidth, $mTHeight, 2)->save($mThumb,$image->type(),90);
// }
// }
// /***************************** 添加水印 ***********************************/
// $isWatermark=(int)input('isWatermark');
// if($isWatermark==1 && (int)WSTConf('CONF.watermarkPosition')!==0){
// //取出水印配置
// $wmWord = WSTConf('CONF.watermarkWord');//文字
// $wmFile = trim(WSTConf('CONF.watermarkFile'),'/');//水印文件
// $wmPosition = (int)WSTConf('CONF.watermarkPosition');//水印位置
// $wmSize = ((int)WSTConf('CONF.watermarkSize')!=0)?WSTConf('CONF.watermarkSize'):'20';//大小
// $wmColor = (WSTConf('CONF.watermarkColor')!='')?WSTConf('CONF.watermarkColor'):'#000000';//颜色必须是16进制的
// $wmOpacity = ((int)WSTConf('CONF.watermarkOpacity')!=0)?WSTConf('CONF.watermarkOpacity'):'100';//水印透明度
// //是否有自定义字体文件
// $customTtf = $_SERVER['DOCUMENT_ROOT'].WSTConf('CONF.watermarkTtf');
// $ttf = is_file($customTtf)?$customTtf:EXTEND_PATH.'/verify/verify/ttfs/3.ttf';
// $image = \image\Image::open($imageSrc);
// if(!empty($wmWord)){//当设置了文字水印 就一定会执行文字水印,不管是否设置了文件水印
// //执行文字水印
// $image->text($wmWord, $ttf, $wmSize, $wmColor, $wmPosition)->save($imageSrc);
// if($thumbSrc!==null){
// $image->thumb((int)input('width',min(300,$image->width())), (int)input('height',min(300,$image->height())),2)->save($thumbSrc,$image->type(),90);
// }
// //如果有生成手机版原图
// if(!empty($mSrc)){
// $image = \image\Image::open($imageSrc);
// $image->thumb($mWidth, $mHeight)->save($mSrc,$image->type(),90);
// $image->thumb($mTWidth, $mTHeight, 2)->save($mThumb,$image->type(),90);
// }
// }elseif(!empty($wmFile)){//设置了文件水印,并且没有设置文字水印
// //执行图片水印
// $image->water($wmFile, $wmPosition, $wmOpacity)->save($imageSrc);
// if($thumbSrc!==null){
// $image->thumb((int)input('width',min(300,$image->width())), (int)input('height',min(300,$image->height())),2)->save($thumbSrc,$image->type(),90);
// }
// //如果有生成手机版原图
// if($mSrc!==null){
// $image = \image\Image::open($imageSrc);
// $image->thumb($mWidth, $mHeight)->save($mSrc,$image->type(),90);
// $image->thumb($mTWidth, $mTHeight,2)->save($mThumb,$image->type(),90);
// }
// }
// }
// //判断是否有生成缩略图
// $thumbSrc = ($thumbSrc==null)?$info->getFilename():str_replace('.','_thumb.', $info->getFilename());
// $filePath = ltrim($filePath,'/');
// // 用户头像上传宽高限制
// $isCut = (int)input('isCut');
// if($isCut){
// $imgSrc = $filePath.$info->getFilename();
// $image = \image\Image::open($imgSrc);
// $size = $image->size();//原图宽高
// $w = $size[0];
// $h = $size[1];
// $rate = $w/$h;
// if($w>$h && $w>500){
// $newH = 500/$rate;
// $image->thumb(500, $newH)->save($imgSrc,$image->type(),90);
// }elseif($h>$w && $h>500){
// $newW = 500*$rate;
// $image->thumb($newW, 500)->save($imgSrc,$image->type(),90);
// }
// }
// return json_encode(['status'=>1,'savePath'=>$filePath,'name'=>$info->getFilename(),'thumb'=>$thumbSrc]);
// }else{
// //上传失败获取错误信息
// return $file->getError();
// }
// }
/**
* 上传图片 修改为上传至oss mark 20180609
* 需要生成缩略图: isThumb=1
* 需要加水印:isWatermark=1
* pc版缩略图: width height
* 手机版原图:mWidth mHeight
* 缩略图:mTWidth mTHeight
* 判断图片来源:fromType 0:商家/用户 1:平台管理员
*/
function WSTUploadPic($fromType=0){
$fileKey = key($_FILES);
$dir = Input('param.dir');
if($dir=='')return json_encode(['msg'=>'没有指定文件目录!','status'=>-1]);
$dirs = WSTConf("CONF.wstUploads");
if(!in_array($dir, $dirs)){
return json_encode(['msg'=>'非法文件目录!','status'=>-1]);
}
// 上传文件
$file = request()->file($fileKey);
if($file===null){
return json_encode(['msg'=>'上传文件不存在或超过服务器限制','status'=>-1]);
}
$validate = new \think\Validate([
['fileMime','fileMime:image/png,image/gif,image/jpeg,image/x-ms-bmp','只允许上传jpg,gif,png,bmp类型的文件'],
['fileExt','fileExt:jpg,jpeg,gif,png,bmp','只允许上传后缀为jpg,gif,png,bmp的文件'],
['fileSize','fileSize:2097152','文件大小超出限制'],//最大2M
]);
$data = ['fileMime' => $file,
'fileSize' => $file,
'fileExt'=> $file
];
if (!$validate->check($data)) {
return json_encode(['msg'=>$validate->getError(),'status'=>-1]);
}
//原文件名
$file_name = $_FILES['file']['name'];
//文件大小
$file_size = $_FILES['file']['size'];
//获得文件扩展名
$temp_arr = explode(".", $file_name);
$file_ext = array_pop($temp_arr);
$file_ext = trim($file_ext);
$file_ext = strtolower($file_ext);
//服务器上临时文件
$filePath = $_FILES['file']['tmp_name'];
//存储在oss上的文件名
$new_path = 'upload/'.$dir.'/'.date('Y-m').'/';
$new_name = uniqid(). '.' . $file_ext;
$new_file_name = $new_path.$new_name;
$object = $new_file_name;
$ossClient = new Aliyunoss();
//上传
$res = $ossClient->uploadFile($object, $filePath);
if($res){
//图片记录
WSTRecordImages($object,(int)$fromType,$file_size);
//缩略图路径 手机版原图路径 手机版缩略图路径
$thumbSrc = $mSrc = $mThumb = null;
//获取原图信息
$imgInfo = $ossClient->getInfo($object);
//原图高宽
$height = (int)$imgInfo['ImageHeight']['value'];
$width = (int)$imgInfo['ImageWidth']['value'];
//pc版缩略图
$tWidth = min($width,300);
$tHeight = min($height,300);
//手机版原图宽高
$mWidth = min($height,(int)input('mWidth',700));
$mHeight = min($height,(int)input('mHeight',700));
//手机版缩略图宽高
$mTWidth = min($width,(int)input('mTWidth',250));
$mTHeight = min($height,(int)input('mTHeight',250));
/****************************** 生成缩略图 *********************************/
$isThumb = (int)input('isThumb');
if($isThumb==1){
//缩略图路径
$thumbSrc =$new_name.'?x-oss-process=image/resize,m_pad,h_'.$tHeight.',w_'.$tWidth;
// $thumbSrc =$new_name.'?x-oss-process=image/resize,m_pad,h_300,w_300';
}
// 用户头像上传宽高限制
$isCut = (int)input('isCut');
if($isCut){
// $imgSrc = $filePath.$info->getFilename();
// $image = \image\Image::open($imgSrc);
// $size = $image->size();//原图宽高
// $w = $size[0];
// $h = $size[1];
// $rate = $w/$h;
$rate = $width/$height;
if($width>$height && $width>500){
$newH = (int)(500/$rate);
$new_name = $new_name.'?x-oss-process=image/resize,w_500,h_'.$newH;
// $image->thumb(500, $newH)->save($imgSrc,$image->type(),90);
}elseif($height>$width && $height>500){
$newW = (int)(500*$rate);
$new_name = $new_name.'?x-oss-process=image/resize,h_500,w_'.$newW;
}
}
$thumbSrc = ($thumbSrc==null)?$new_name:$thumbSrc;
return json_encode(['status'=>1,'savePath'=>$new_path,'name'=>$new_name,'thumb'=>$thumbSrc]);
}else{
return WSTReturn('上传失败',-1);
}
}
/**
* 上传文件
*/
function WSTUploadFile(){
$fileKey = key($_FILES);
$dir = Input('post.dir');
if($dir=='')return json_encode(['msg'=>'没有指定文件目录!','status'=>-1]);
$dirs = WSTConf("CONF.wstUploads");
if(!in_array($dir, $dirs)){
return json_encode(['msg'=>'非法文件目录!','status'=>-1]);
}
//上传文件
$file = request()->file($fileKey);
if($file===null){
return json_encode(['msg'=>'上传文件不存在或超过服务器限制','status'=>-1]);
}
$validate = new \think\Validate([
['fileExt','fileExt:xls,xlsx,xlsm','只允许上传后缀为xls,xlsx,xlsm的文件']
]);
$data = ['fileExt'=> $file];
if (!$validate->check($data)) {
return json_encode(['msg'=>$validate->getError(),'status'=>-1]);
}
$info = $file->rule('uniqid')->move(ROOT_PATH.'/upload/'.$dir."/".date('Y-m'));
//保存路径
$filePath = $info->getPathname();
$filePath = str_replace(ROOT_PATH,'',$filePath);
$filePath = str_replace('\\','/',$filePath);
$name = $info->getFilename();
$filePath = str_replace($name,'',$filePath);
if($info){
return json_encode(['status'=>1,'name'=>$info->getFilename(),'route'=>$filePath]);
}else{
//上传失败获取错误信息
return $file->getError();
}
}
/**
* 生成默认商品编号/货号
*/
function WSTGoodsNo($pref = ''){
return $pref.(round(microtime(true),4)*10000).mt_rand(0,9);
}
/**
* 获取订单统一流水号
*/
function WSTOrderQnique(){
return (round(microtime(true),4)*10000).mt_rand(1000,9999);
}
/**
* 图片管理
* @param $imgPath 图片路径
* @param $fromType 0:用户/商家 1:平台管理员
*
*/
// function WSTRecordImages($imgPath, $fromType){
// $data = [];
// $data['imgPath'] = $imgPath;
// if(file_exists($imgPath)){
// $data['imgSize'] = filesize($imgPath); //返回字节数 imgsize/1024 k imgsize/1024/1024 m
// }
// //获取表名
// $table = explode('/',$imgPath);
// $data['fromTable'] = $table[1];
// $data['fromType'] = (int)$fromType;
// //根据类型判断所有者
// $data['ownId'] = ((int)$fromType==0)?(int)session('WST_USER.userId'):(int)session('WST_STAFF.staffId');
// $data['isUse'] = 0; //默认不使用
// $data['createTime'] = date('Y-m-d H:i:s');
// //保存记录
// Db::name('images')->insert($data);
// }
/**
* 图片管理 mark 20180308 添加图片大小字段(主要是为了整合oss图片)
* @param $imgPath 图片路径
* @param $fromType 0:用户/商家 1:平台管理员
* @param $file_size 图片大小
*/
function WSTRecordImages($imgPath, $fromType,$file_size=0){
$data = [];
$data['imgPath'] = $imgPath;
//更改为可以直接给图片大小
if(file_exists($imgPath)){
$data['imgSize'] = filesize($imgPath); //返回字节数 imgsize/1024 k imgsize/1024/1024 m
}else{
$data['imgSize'] = $file_size;
}
//获取表名
$table = explode('/',$imgPath);
$data['fromTable'] = $table[1];
$data['fromType'] = (int)$fromType;
//根据类型判断所有者
$data['ownId'] = ((int)$fromType==0)?(int)session('WST_USER.userId'):(int)session('WST_STAFF.staffId');
$data['isUse'] = 0; //默认不使用
$data['createTime'] = date('Y-m-d H:i:s');
//保存记录
Db::name('images')->insert($data);
}
// /**
// * 启用图片
// * @param $fromType 0: 用户/商家 1:平台管理员
// * @param $dataId 来源记录id
// * @param $imgPath 图片路径,要处理多张图片时请传入一位数组,或用","连接图片路径
// * @param $fromTable 该记录来自哪张表
// * @param $imgFieldName 表中的图片字段名称
// */
// function WSTUseImages($fromType, $dataId, $imgPath, $fromTable='', $imgFieldName=''){
// if(empty($imgPath))return;
// $image['fromType'] = (int)$fromType;
// //根据类型判断所有者
// $image['ownId'] = ((int)$fromType==0)?(int)session('WST_USER.userId'):(int)session('WST_STAFF.staffId');
// $image['dataId'] = (int)$dataId;
// $image['isUse'] = 1;//标记为启用
// if($fromTable!=''){
// $tmp = ['',''];
// if(strpos($fromTable,'-')!==false){
// $tmp = explode('-',$fromTable);
// $fromTable = str_replace('-'.$tmp[1],'',$fromTable);
// }
// $image['fromTable'] = str_replace('_','',$fromTable.$tmp[1]);
// }
// $imgPath = is_array($imgPath)?$imgPath:explode(',',$imgPath);//转数组
// //用于与旧图比较
// $newImage = $imgPath;
// // 不为空说明执行修改
// if($imgFieldName!=''){
// //要操作的表名 $fromTable;
// // 获取`$fromTable`表的主键
// $prefix = config('database.prefix');
// $tableName = $prefix.$fromTable;
// $pk = Db::getTableInfo("$tableName", 'pk');
// // 取出旧图
// $oldImgPath = model("$fromTable")->where("$pk",$dataId)->value("$imgFieldName");
// // 转数组
// $oldImgPath = explode(',', $oldImgPath);
// // 1.要设置为启用的文件
// $newImage = array_diff($imgPath, $oldImgPath);
// // 2.要标记为删除的文件
// $oldImgPath = array_diff($oldImgPath, $imgPath);
// //旧图数组跟新图数组相同则不需要继续执行
// if($newImage!=$oldImgPath)WSTUnuseImage($oldImgPath);
// }
// if(!empty($newImage)){
// Db::name('images')->where(['imgPath'=>['in',$newImage]])->update($image);
// }
// }
/**
* 启用图片 修改 适应oss
* @param $fromType 0: 用户/商家 1:平台管理员
* @param $dataId 来源记录id
* @param $imgPath 图片路径,要处理多张图片时请传入一位数组,或用","连接图片路径
* @param $fromTable 该记录来自哪张表
* @param $imgFieldName 表中的图片字段名称
*/
function WSTUseImages($fromType, $dataId, $imgPath, $fromTable='', $imgFieldName=''){
if(empty($imgPath))return;
$image['fromType'] = (int)$fromType;
//根据类型判断所有者
$image['ownId'] = ((int)$fromType==0)?(int)session('WST_USER.userId'):(int)session('WST_STAFF.staffId');
$image['dataId'] = (int)$dataId;
$image['isUse'] = 1;//标记为启用
if($fromTable!=''){
$tmp = ['',''];
if(strpos($fromTable,'-')!==false){
$tmp = explode('-',$fromTable);
$fromTable = str_replace('-'.$tmp[1],'',$fromTable);
}
$image['fromTable'] = str_replace('_','',$fromTable.$tmp[1]);
}
// $imgPath = is_array($imgPath)?$imgPath:explode(',',$imgPath);//转数组
//修改 去除oss图片后面的参数
//先判断是不是数组 mark 20180612
if(is_array($imgPath)){
foreach ($imgPath as $key => $value) {
if(stripos($value,'?')){
$imgPath[$key] = strstr($imgPath,'?',true);
}
}
}else{
if(stripos($imgPath,'?'))$imgPath = strstr($imgPath,'?',true);
}
$imgPath = is_array($imgPath)?$imgPath:explode(',',$imgPath);
//用于与旧图比较
$newImage = $imgPath;
// 不为空说明执行修改
if($imgFieldName!=''){
//要操作的表名 $fromTable;
// 获取`$fromTable`表的主键
$prefix = config('database.prefix');
$tableName = $prefix.$fromTable;
$pk = Db::getTableInfo("$tableName", 'pk');
// 取出旧图
$oldImgPath = model("$fromTable")->where("$pk",$dataId)->value("$imgFieldName");
// 转数组
// $oldImgPath = explode(',', $oldImgPath);
// 目前似乎数据库中似乎只有头像有参数 mark 20180612
if(stripos($oldImgPath,'?')){
$oldImgPath = strstr($oldImgPath,'?',true);
}
$oldImgPath = explode(',', $oldImgPath);
// 1.要设置为启用的文件
$newImage = array_diff($imgPath, $oldImgPath);
// 2.要标记为删除的文件
$oldImgPath = array_diff($oldImgPath, $imgPath);
//旧图数组跟新图数组相同则不需要继续执行
if($newImage!=$oldImgPath)WSTUnuseImage($oldImgPath);
}
if(!empty($newImage)){
$rs = Db::name('images')->where(['imgPath'=>['in',$newImage]])->update($image);
}
}
/**
* 编辑器图片记录
* @param $fromType 0: 用户/商家 1:平台管理员
* @param $dataId 来源记录id
* @param $oldDesc 旧商品描述
* @param $newDesc 新商品描述
*/
function WSTEditorImageRocord($fromType, $dataId, $oldDesc, $newDesc){
// 解义
$oldDesc = htmlspecialchars_decode($oldDesc);
$newDesc = htmlspecialchars_decode($newDesc);
//编辑器里的图片
$rule = '/src=".*?\/(upload.*?)"/';
// 获取旧的src数组
preg_match_all($rule,$oldDesc,$images);
$oldImgPath = $images[1];
preg_match_all($rule,$newDesc,$images);
// 获取新的src数组
$imgPath = $images[1];
// 1.要设置为启用的文件
$newImage = array_diff($imgPath, $oldImgPath);
// 2.要标记为删除的文件
$oldImgPath = array_diff($oldImgPath, $imgPath);
//旧图数组跟新图数组相同则不需要继续执行
if($newImage!=$oldImgPath){
//标记新图启用
WSTUseImages($fromType, $dataId, $newImage);
//标记旧图删除
WSTUnuseImage($oldImgPath);
}
}
/**
* 标记删除图片
*/
function WSTUnuseImage($fromTable, $field = '' , $dataId = 0){
if($fromTable=='')return;
$imgPath = $fromTable;
if($field!=''){
$prefix = config('database.prefix');
$tableName = $prefix.$fromTable;
$pk = Db::getTableInfo("$tableName", 'pk');
// 取出旧图
$imgPath = model("$fromTable")->where("$pk",$dataId)->value("$field");
}
if(!empty($imgPath)){
$imgPath = is_array($imgPath)?$imgPath:explode(',',$imgPath);//转数组
Db::name('images')->where(['imgPath'=>['in',$imgPath]])->setField('isUse',0);
}
}
/**
* 获取系统根目录
*/
function WSTRootPath(){
return dirname(dirname(dirname(dirname(__File__))));
}
/**
* 切换图片
* @param $imgurl 图片路径
* @param $imgType 图片类型 0:PC版大图 1:PC版缩略图 2:移动版大图 3:移动版缩略图
* 图片规则
* PC版版大图 :201635459344.jpg
* PC版版缩略图 :201635459344_thumb.jpg
* 移动版大图 :201635459344_m.jpg
* 移动版缩略图 :201635459344_m_thumb.jpg
*/
// function WSTImg($imgurl,$imgType = 1){
// $m = WSTConf('CONF.wstMobileImgSuffix');
// $imgurl = str_replace($m.'.','.',$imgurl);
// $imgurl = str_replace($m.'_thumb.','.',$imgurl);
// $imgurl = str_replace('_thumb.','.',$imgurl);
// $img = '';
// switch ($imgType){
// case 0:$img = $imgurl;break;
// case 1:$img = str_replace('.','_thumb.',$imgurl);break;
// case 2:$img = str_replace('.',$m.'.',$imgurl);break;
// case 3:$img = str_replace('.',$m.'_thumb.',$imgurl);break;
// }
// return ((file_exists(WSTRootPath()."/".$img))?$img:$imgurl);
// }
/**
* [WSTImg 切换图片]
* @param [type] $imgurl [图片路径]
* @param integer $imgType [图片类型 0:PC版大图 1:PC版缩略图 2:移动版大图 3:移动版缩略图]
*/
function WSTImg($imgurl,$imgType = 1){
$oss_imgurl = getImgUrl();
$m = WSTConf('CONF.wstMobileImgSuffix');
$imgurl = str_replace($m.'.','.',$imgurl);
$imgurl = str_replace($m.'_thumb.','.',$imgurl);
$imgurl = str_replace('_thumb.','.',$imgurl);
$img = '';
switch($imgType){
case 0:$img = $imgurl;break;
case 1:$img = $imgurl.'?x-oss-process=image/resize,m_pad,h_300,w_300';break;
case 2:$img = $imgurl.'?x-oss-process=image/resize,h_750,w_750';break;
case 3:$img = $imgurl.'?x-oss-process=image/resize,h_250,w_250';break;
}
return $img;
}
/**
* 根据送货城市获取运费
* @param $cityId 送货城市Id
* @param @shopIds 店铺ID
*/
function WSTOrderFreight($shopId,$cityId){
$goodsFreight = ['total'=>0,'shops'=>[]];
$rs = Db::name('shops')->alias('s')->join('__SHOP_FREIGHTS__ sf','s.shopId=sf.shopId and sf.areaId2='.$cityId,'left')
->where(['s.shopId'=>$shopId])->field('s.freight,sf.freightId,sf.freight freight2')->find();
if(empty($rs))return 0;
if((int)$rs['freight']<0)$rs['freight'] = 0;
if((int)$rs['freight2']<0)$rs['freight2'] = 0;
return (int)((int)$rs['freightId']>0)?$rs['freight2']:$rs['freight'];
}
/**
* 生成订单号
*/
function WSTOrderNo(){
$orderId = Db::name('orderids')->insertGetId(['rnd'=>time()]);
return $orderId.(fmod($orderId,7));
}
/**
* 高精度数字相加
* @param $num
* @param number $i 保留小数位
*/
function WSTBCMoney($num1,$num2,$i=2){
$num = bcadd($num1, $num2, $i);
return (float)$num;
}
/**
* 获取支付方式
*/
function WSTLangPayType($v){
switch($v){
case 0:return '货到付款';
case 1:return '在线支付';
}
}
/**
* 收货方式
*/
function WSTLangDeliverType($v){
switch ($v) {
case 2:return "顺带件";
case 1:return "自提";
case 0:return "送货上门";
}
}
/**
* 订单状态
*/
function WSTLangOrderStatus($v){
switch($v){
case -3:return '用户拒收';
case -2:return '待支付';
case -1:return '已取消';
case 0:return '待发货';
case 1:return '待收货';
case 2:return '已收货';
}
}
/**
* 惠宝来源
*/
function WSTLangScore($v){
switch($v){
case 1:return '商品订单';
case 2:return '评价订单';
case 4:return '退款订单';
case 5:return '惠宝签到';
case 10001:return '管理员';
default:return '其他';
}
}
/**
* 资金来源
*/
function WSTLangMoneySrc($v){
switch($v){
case 1:return '商品订单';
case 2:return '订单结算';
case 3:return '提现申请';
case 4:return '钱包充值';
default:return '其他';
}
}
/**
* 投诉状态
*/
function WSTLangComplainStatus($v){
switch($v){
case 0:return '等待处理';
case 1:return '等待应诉人应诉';
case 2:return '应诉人已应诉';
case 3:return '等待仲裁';
case 4:return '已仲裁';
}
}
/**
* 支付来源
*/
function WSTLangPayFrom($pkey){
$paySrc = cache('WST_PAY_SRC');
if(!$paySrc){
$paySrc = Db::name('payments')->order('payOrder asc')->select();
cache('WST_PAY_SRC',$paySrc,31622400);
}
foreach($paySrc as $v){
if($pkey==$v['payCode'])return $v['payName'];
}
return '其他';
}
/**
* 插件状态
*/
function WSTLangAddonStatus($v){
switch($v){
case 0:return '未安装';
case 1:return '启用';
case 2:return '禁用';
}
}
/**
* 获取业务数据内容【根据catCode获取】
*/
function WSTDatas($catCode,$id = 0){
$catId = (int)Db::name('data_cats')->where(['catCode'=>$catCode])->value('catId');
$data = cache('WST_DATAS');
if(!$data){
$rs = Db::name('datas')->where(['dataFlag'=>1])->order('catId asc,dataSort asc,id asc')->select();
$data = [];
foreach ($rs as $key =>$v){
$data[$v['catId']][$v['dataVal']] = $v;
}
cache('WST_DATAS',$data,378432000);
}
if(isset($data[$catId])){
if($id==0)return $data[$catId];
return isset($data[$catId][$id])?$data[$catId][$id]:'';
}
return [];
}
/**
* 获取消息模板
*/
function WSTMsgTemplates($tplCode){
$data = cache('WST_MSG_TEMPLATES');
if(!$data){
$rs = Db::name('template_msgs')->order('id asc')->select();
$data = [];
foreach ($rs as $key =>$v){
if($v['tplType']==3 && (int)WSTConf('CONF.wxenabled')==1){
$ps = Db::name('wx_template_params')->where('parentId',$v['id'])->select();
$v['params'] = $ps;
}
if($v['tplContent']==''){
$data[$v['tplCode']] = $v;
}else{
$v['content'] = htmlspecialchars_decode($v['tplContent']);
$v['tplContent'] = strip_tags(htmlspecialchars_decode($v['tplContent']));
$data[$v['tplCode']] = $v;
}
}
cache('WST_MSG_TEMPLATES',$data,378432000);
}
return (isset($data[$tplCode]))?$data[$tplCode]:null;
}
/**
* 发送微信消息
*/
function WSTWxMessage($params){
$tpl = WSTMsgTemplates($params['CODE']);
if($tpl && file_exists('wstmart'.DS.'wechat'.DS.'behavior'.DS.'InitWechatMessges.php')){
\think\Hook::exec('wstmart\\wechat\\behavior\\InitWechatMessges','run',$params);
}
}
/**
* 批量发送微信消息
*/
function WSTWxBatchMessage($params){
$tpl = WSTMsgTemplates($params['CODE']);
if($tpl && file_exists('wstmart'.DS.'wechat'.DS.'behavior'.DS.'InitWechatMessges.php')){
\think\Hook::exec('wstmart\\wechat\\behavior\\InitWechatMessges','batchRun',$params);
}
}
/**
* 截取字符串
*/
function WSTMSubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = false){
$newStr = '';
if (function_exists ( "mb_substr" )) {
$newStr = mb_substr ( $str, $start, $length, $charset );
if ($suffix && (mb_strlen($str,$charset)>$length))$newStr .= "...";
} elseif (function_exists ( 'iconv_substr' )) {
$newStr = iconv_substr( $str, $start, $length, $charset );
if ($suffix && (mb_strlen($str,$charset)>$length))$newStr .= "...";
}
if($newStr==''){
$re ['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re ['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re ['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re ['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all ( $re [$charset], $str, $match );
$slice = join ( "", array_slice ( $match [0], $start, $length ) );
if ($suffix)
$newStr = $slice;
}
return $newStr;
}
function WSTScore($score,$users,$type = 5,$len = 0,$total = 1){
if((int)$score==0)return $type;
switch($type){
case 5:return round($score/$total/$users,0);
case 10:return round($score/$total*2/$users,$len);
case 100:return round($score/$total*2/$users,$len);
}
}
function WSTShopEncrypt($shopId){
return md5(base64_encode("wstmart".date("Y-m-d").$shopId));
}
/**
* 根据子分类循环获取其父级分类
*/
function WSTGoodsCatPath($catId, $data = []){
if($catId==0)return $data;
$data[] = $catId;
$parentId = Db::name('goods_cats')->where('catId',$catId)->value('parentId');
if($parentId==0){
krsort($data);
return $data;
}else{
return WSTGoodsCatPath($parentId, $data);
}
}
/**
* 提供原生分页处理
*/
function WSTPager($total,$rs,$page,$size = 0){
$pageSize = ($size>0)?$size:config('paginate.list_rows');
$totalPage = ($total%$pageSize==0)?($total/$pageSize):(intval($total/$pageSize)+1);
return ['Total'=>$total,'PerPage'=>$pageSize,'CurrentPage'=>$page,'TotalPage'=>$totalPage,'Rows'=>$rs];
}
/**
* 编辑器上传图片
*/
// function WSTEditUpload($fromType){
// $root = str_replace('/index.php','',\think\Request::instance()->root());
// //PHP上传失败
// if (!empty($_FILES['imgFile']['error'])) {
// switch($_FILES['imgFile']['error']){
// case '1':
// $error = '超过php.ini允许的大小。';
// break;
// case '2':
// $error = '超过表单允许的大小。';
// break;
// case '3':
// $error = '图片只有部分被上传。';
// break;
// case '4':
// $error = '请选择图片。';
// break;
// case '6':
// $error = '找不到临时目录。';
// break;
// case '7':
// $error = '写文件到硬盘出错。';
// break;
// case '8':
// $error = 'File upload stopped by extension。';
// break;
// case '999':
// default:
// $error = '未知错误。';
// }
// return WSTReturn(1,$error);
// }
// $fileKey = key($_FILES);
// $dir = 'image'; // 编辑器上传图片目录
// $dirs = WSTConf("CONF.wstUploads");
// if(!in_array($dir, $dirs)){
// return json_encode(['error'=>1,'message'=>'非法文件目录!']);
// }
// // 上传文件
// $file = request()->file($fileKey);
// if($file===null){
// return json_encode(["error"=>1,"message"=>'上传文件不存在或超过服务器限制']);
// }
// $validate = new \think\Validate([
// ['fileMime','fileMime:image/png,image/gif,image/jpeg,image/x-ms-bmp','只允许上传jpg,gif,png,bmp类型的文件'],
// ['fileExt','fileExt:jpg,jpeg,gif,png,bmp','只允许上传后缀为jpg,gif,png,bmp的文件'],
// ['fileSize','fileSize:2097152','文件大小超出限制'],//最大2M
// ]);
// $data = ['fileMime' => $file,
// 'fileSize' => $file,
// 'fileExt'=> $file
// ];
// if (!$validate->check($data)) {
// return json_encode(['message'=>$validate->getError(),'error'=>1]);
// }
// $info = $file->rule('uniqid')->move(ROOT_PATH.'/upload/'.$dir."/".date('Y-m'));
// if($info){
// $filePath = $info->getPathname();
// $filePath = str_replace(ROOT_PATH,'',$filePath);
// $filePath = str_replace('\\','/',$filePath);
// $name = $info->getFilename();
// $imageSrc = trim($filePath,'/');
// //图片记录
// WSTRecordImages($imageSrc, (int)$fromType);
// return json_encode(array('error' => 0, 'url' => $root.$filePath));
// }
// }
/**
* 编辑器上传图片 修改成上传到oss mark 20180607
*/
function WSTEditUpload($fromType){
//PHP上传失败
if (!empty($_FILES['imgFile']['error'])) {
switch($_FILES['imgFile']['error']){
case '1':
$error = '超过php.ini允许的大小。';
break;
case '2':
$error = '超过表单允许的大小。';
break;
case '3':
$error = '图片只有部分被上传。';
break;
case '4':
$error = '请选择图片。';
break;
case '6':
$error = '找不到临时目录。';
break;
case '7':
$error = '写文件到硬盘出错。';
break;
case '8':
$error = 'File upload stopped by extension。';
break;
case '999':
default:
$error = '未知错误。';
}
return WSTReturn(1,$error);
}
$fileKey = key($_FILES);
// $dir = 'image'; // 编辑器上传图片目录
$dir = empty($_GET['dir']) ? 'image' : trim($_GET['dir']);
$dirs = WSTConf("CONF.wstUploads");
if(!in_array($dir, $dirs)){
return json_encode(['error'=>1,'message'=>'非法文件目录!']);
}
// 上传文件
$file = request()->file($fileKey);
if($file===null){
return json_encode(["error"=>1,"message"=>'上传文件不存在或超过服务器限制']);
}
$validate = new \think\Validate([
['fileMime','fileMime:image/png,image/gif,image/jpeg,image/x-ms-bmp','只允许上传jpg,gif,png,bmp类型的文件'],
['fileExt','fileExt:jpg,jpeg,gif,png,bmp','只允许上传后缀为jpg,gif,png,bmp的文件'],
['fileSize','fileSize:2097152','文件大小超出限制'],//最大2M
]);
$data = ['fileMime' => $file,
'fileSize' => $file,
'fileExt'=> $file
];
if (!$validate->check($data)) {
return json_encode(['message'=>$validate->getError(),'error'=>1]);
}
//原文件名
$file_name = $_FILES['imgFile']['name'];
//文件大小
$file_size = $_FILES['imgFile']['size'];
//获得文件扩展名
$temp_arr = explode(".", $file_name);
$file_ext = array_pop($temp_arr);
$file_ext = trim($file_ext);
$file_ext = strtolower($file_ext);
//服务器上临时文件
$filePath = $_FILES['imgFile']['tmp_name'];
//存储在oss上的文件名
$new_file_name = 'upload/'.$dir.'/'.date('Y-m').'/'.uniqid(). '.' . $file_ext;
$object = $new_file_name;
try{
$ossClient = new Aliyunoss();
//上传
$ossClient->uploadFile($object, $filePath);
$file_url = getImgUrl().$object;
WSTRecordImages($object, (int)$fromType,$file_size);
return json_encode(array('error'=>0,'url'=>$file_url));
} catch(Exception $e) {
printf($e->getMessage() . "\n");
return;
}
}
/**
* 转义单引号
*/
function WSTHtmlspecialchars($v){
return htmlspecialchars($v,ENT_QUOTES);
}
/**
* 发送商城消息
* @param int $to 接受者d
* @param string $content 内容
* @param array $msgJson 存放json数据
*/
function WSTSendMsg($to,$content,$msgJson=[],$msgType = 1){
$message = [];
$message['msgType'] = $msgType;
$message['sendUserId'] = 1;
$message['createTime'] = date('Y-m-d H:i:s');
$message['msgStatus'] = 0;
$message['dataFlag'] = 1;
$message['from'] = (int)$msgJson['from'];//添加指定消息来源 mark hsf 20180117
$message['receiveUserId'] = $to;
$message['msgContent'] = $content;
$message['msgJson'] = json_encode($msgJson);
Db::name('messages')->insert($message);
}
/**
* 获取分类的佣金
function WSTGoodsCommissionRate($goodsCatId){
$cats = Db::name('goods_cats')->where('catId',$goodsCatId)->field('parentId,commissionRate')->find();
if(empty($cats)){
return 0;
}else{
if((float)$cats['commissionRate']>=0)return (float)$cats['commissionRate'];
return WSTGoodsCommissionRate($cats['parentId']);
}
}
*/
function WSTFormatIn($split,$str){
$strdatas = explode($split,$str);
$data = array();
for($i=0;$i<count($strdatas);$i++){
$data[] = (int)$strdatas[$i];
}
$data = array_unique($data);
return implode($split,$data);
}
function WSTRandStr($len = 6){
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$tmp = '';
for($i=0;$i<$len;$i++){
$tmp.=$str[rand(0,35)];
}
return $tmp;
}
/**
* 金额兑惠宝
*/
function WSTMoneyGiftScore($money){
$moneyToScore = (float)WSTConf('CONF.moneyToScore');
return intval($money*$moneyToScore);
}
/**
* 惠宝兑金额
* $isBack=true则$score实际上传入金额,通过金额反推需要兑换的惠宝
*/
function WSTScoreToMoney($score,$isBack = false){
$scoreToMoney = (int)WSTConf('CONF.scoreToMoney');
if($scoreToMoney<=0)return 0;
if($isBack){
return round(strval($score*$scoreToMoney),2);
}else{
return round($score/$scoreToMoney,2);
}
}
/**
* 头像处理
*/
// function WSTUserPhoto($userPhoto=''){
// if(substr($userPhoto,0,4)!='http' && $userPhoto){
// $userPhoto = '__ROOT__/'.$userPhoto;
// }else if(!$userPhoto){
// $userPhoto = '__ROOT__/'.WSTConf('CONF.userLogo');
// }
// return $userPhoto;
// }
/**
* 头像处理 去除 mark
*/
function WSTUserPhoto($userPhoto=''){
if(substr($userPhoto,0,4)!='http' && $userPhoto){
$userPhoto = '/'.$userPhoto;
}else if(!$userPhoto){
$userPhoto = '/'.WSTConf('CONF.userLogo');
}
return $userPhoto;
}
function WSTClearHookCache(){
WSTConf('listenUrl',null);
$STAFF = session('WST_STAFF');
if(!empty($STAFF)){
//获取角色权限
$STAFF['privileges'] = Db::name('privileges')->where(['dataFlag'=>1])->column('privilegeCode');
$STAFF['menuIds'] = Db::name('menus')->where('dataFlag',1)->column('menuId');
session('WST_STAFF',$STAFF);
}
WSTConf('protectedUrl',null);
cache('WST_HOME_MENUS',null);
cache('WST_PRO_MENUS',null);
cache('WST_MOBILE_BTN',null);
cache('hooks',null);
cache('WST_ADDONS',null);
WSTConf('WST_ADDONS',null);
}
/**
* 获取移动端首页按钮
*/
function WSTMobileBtns($src){
$data = cache('WST_MOBILE_BTN');
if(!$data){
$rs = Db::name('mobile_btns')->order('btnSort asc')->select();
$data = [];
foreach ($rs as $key => $v) {
$data[$v['btnSrc']][] = $v;
}
cache('WST_MOBILE_BTN',$data,31536000);
}
return $data[$src];
}
/**
* 获取星期几
*/
function WSTgetWeek($date){
//强制转换日期格式
$date_str=date('Y-m-d',strtotime($date));
$number_wk=date("w",strtotime($date));
$weekArr=array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
return $weekArr[$number_wk];
}
/**
* 获取路由规则
*/
function WSTRoute(){
$data = cache('WST_ROUTES');
if(!$data){
$routes = \think\Route::rules('*');
$data = [];
foreach ($routes as $key => $v) {
if($v['rule']=='addon/:route')continue;
$data[$v['route']] = $v['rule'];
}
cache('WST_ROUTES',$data,31536000);
}
return json_encode($data);
}
/**
* 获取项目根路径
*/
function WSTRoot(){
$url = request()->root(true);
$data = explode("/index.php",$url);
return $data[0];
}
/**
* URL 64位加密处理
* @param string $data 字符串内容
* @param boolean $isEncode true:编码 false:解码
*/
function WSTBase64url($data,$isEncode = true) {
return ($isEncode)?rtrim(strtr(base64_encode($data), '+/', '-_'), '='):base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
}
/**
* 将空内容设置为特定内容
* @param [type] $[name] [description]
*/
function WSTBlank($v,$defaultValue = ''){
if($v=='')return $defaultValue;
if($v=='0000-00-00')return $defaultValue;
if($v=='0000-00-00 00:00:00')return $defaultValue;
}
/**
* 判断访问端来源
*/
function WSTVisitModule(){
$request = request();
if($request->isMobile()){
return (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false)?'wechat':'mobile';
}
return 'home';
}
/**
* 获取图片颜色
* @imgUrl 图片地址
*/
function WSTImgColor($imgUrl){
$imageInfo = getimagesize($imgUrl);
//图片类型
$imgType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));
//对应函数
$imageFun = 'imagecreatefrom' . ($imgType == 'jpg' ? 'jpeg' : $imgType);
$im = $imageFun($imgUrl);
$rgb = imagecolorat ( $im , 10 , 15 );
$arr = array();
$arr['r'] = ( $rgb >> 16 ) & 0xFF ;
$arr['g'] = ( $rgb >> 8 ) & 0xFF ;
$arr['b'] = $rgb & 0xFF ;
return implode(',',$arr);
}
/**
* 获取用户等级
*/
function WSTUserRank($userScore){
$data = cache('WST_USER_RANK');
if(!$data){
$data = Db::name('user_ranks')->where('dataFlag',1)->order('startScore asc,rankId desc')->select();
cache('WST_USER_RANK',$data,2592000);
}
if(!$data)$data = [];
foreach ($data as $key => $v) {
if($userScore>=$v['startScore'] && $userScore<=$v['endScore'])return $v;
}
return ['rankName'=>'','rankId'=>0,'userrankImg'=>''];
}
/**
* 获取购物车数量
*/
function WSTCartNum(){
$userId = session('WST_USER.userId');
$cartNum = Db::name('carts')->where(['userId'=>$userId])->field('cartId')->select();
$count = count($cartNum);
return $count;
}
/**
* 增加文章访问数
*/
function WSTArticleVisitorNum($id){
Db::name('articles')->where(['articleId'=>$id])->setInc('visitorNum',1);
}
/**
* 保持数值为大于0的数值
*/
function WSTPositiveNum($num){
return ($num>0)?$num:0;
}
/**
* 将字符串转换为时间戳,解决部分服务器时间不能超过2038的问题
*/
function WSTStrToTime($str){
if(strtotime('2099-09-09 23:59:59')){
return strtotime($str);
}else{
$date = new DateTime($str);
return $date->format('U');
}
}
/**
* 计算剩余时间
*/
function WSTTimeToStr($second){
$day = floor($second/(3600*24));
$second = $second%(3600*24);//除去整天之后剩余的时间
$hour = floor($second/3600);
$second = $second%3600;//除去整小时之后剩余的时间
$minute = floor($second/60);
$second = $second%60;//除去整分钟之后剩余的时间
//返回字符串
return (($day>0)?($day.'天'):"").($hour<10?"0".$hour:$hour).':'.($minute<10?"0".$minute:$minute).':'.($second<10?"0".$second:$second);
}
/**
* 适应mmgrid的表格返回结构
*/
function WSTGrid($page){
if(!is_array($page))$page = $page->toArray();
$rs = ['status'=>1,'msg'=>'','items'=>$page['Rows'],'totalCount'=>$page['Total']];
return $rs;
}
/**
* RSA解密
*/
function WSTRSA($hex_encrypt_data){
$hex_encrypt_data = trim($hex_encrypt_data);
$isCrypt = WSTConf('CONF.isCryptPwd');
if($isCrypt==0)return WSTReturn('success',1,$hex_encrypt_data);
$private_key = WSTConf('CONF.pwdPrivateKey');
if($private_key=='')return WSTReturn('fail');
try{
$encrypt_data = pack("H*", $hex_encrypt_data); //对十六进制数据进行转换
openssl_private_decrypt($encrypt_data, $decrypt_data, $private_key); //解密数据
return WSTReturn('success',1,$decrypt_data);
}catch(\Exception $e){
return WSTReturn('fail');
}
}
/**
* 获取订单来源提示
*/
function WSTOrderCodeTitle($orderCode){
$addonMaps = model("common/addons")->getAddonsMaps();
$title = array_key_exists($orderCode,$addonMaps)?$addonMaps[$orderCode]:"普通订单";
return $title;
}
/**
* 循环删除指定目录下的文件及文件夹
* @param string $dirpath 文件夹路径
*/
function WSTDelDir($dirpath){
$dh=opendir($dirpath);
while (($file=readdir($dh))!==false) {
if($file!="." && $file!="..") {
$fullpath=$dirpath."/".$file;
if(!is_dir($fullpath)) {
unlink($fullpath);
} else {
WSTDelDir($fullpath);
@rmdir($fullpath);
}
}
}
closedir($dh);
$isEmpty = true;
$dh=opendir($dirpath);
while (($file=readdir($dh))!== false) {
if($file!="." && $file!="..") {
$isEmpty = false;
break;
}
}
return $isEmpty;
}
/**
* 清除整个所有缓存
* 注意:此函数非迫不得己不要调用。能删除指定缓存的就尽量删除指定缓存。尽量只在后台管理员才做时调用,前台用户操作就不要调用了
*/
function WSTClearAllCache(){
cache(null);
}
function WSTShopOrderMenus(){
$wst_user = session('WST_USER');
$orderMenus = array("waitPay"=>"home/orders/waituserPay",
"waitDeliver"=>"home/orders/waitdelivery",
"waitReceive"=>"home/orders/delivered",
"abnormal"=>"home/orders/failure",
"finish"=>"home/orders/finished");
if(!empty($wst_user)){
$roleId = isset($wst_user["roleId"])?(int)$wst_user["roleId"]:0;
if($roleId>0){
$shopMenuUrls = model("common/HomeMenus")->getShopMenuUrls();
foreach ($orderMenus as $key => $menuUrl) {
if(!in_array($menuUrl,$shopMenuUrls)){
unset($orderMenus[$key]);
}
}
}
}
if(count($orderMenus)==5){
$orderMenus["all"] = "";
}
return $orderMenus;
}
function get_my_id(){
return (int)session('WST_USER.userId');
}
function pd(...$data){
echo "<pre>";
if(empty($data)){
print_r(1);
}else{
foreach ($data as $datum){
print_r($datum);
}
}
die;
}