cus.php 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  1. <?php
  2. //搜索参数编码转换
  3. function search_data($str) {
  4. if (CHARSET=='utf-8') return $str;
  5. if (CHARSET=='gbk') return iconv("gb2312","utf-8",$str);
  6. }
  7. //ajax请求时候的编码转换
  8. function ajax_data($str) {
  9. if (CHARSET=='utf-8') return $str;
  10. if (CHARSET=='gbk') return iconv("utf-8","gb2312",$str);
  11. }
  12. //检查模型是否存在,不存在抛出404错误
  13. function check_record_exists($model)
  14. {
  15. if(empty($model))
  16. {
  17. if (Yii::$app->request->isAjax) {
  18. $msgdata = ['error' => 1,'msg' => Yii::t('admin','the record does not exist')];
  19. echo_json($msgdata);
  20. }
  21. else
  22. {
  23. throw new \Exception(Yii::t('admin','the record does not exist'),404);
  24. }
  25. }
  26. else
  27. {
  28. return true;
  29. }
  30. }
  31. //判断文档是否被删除
  32. function check_record_delete($model)
  33. {
  34. if($model->is_delete)
  35. {
  36. if (Yii::$app->request->isAjax) {
  37. $msgdata = ['error' => 1,'msg' => Yii::t('admin','the record does not exist')];
  38. echo_json($msgdata);
  39. }
  40. else
  41. {
  42. throw new \Exception(Yii::t('admin','the record does not exist'),404);
  43. }
  44. }
  45. else
  46. {
  47. return true;
  48. }
  49. }
  50. //判断文档是否包含敏感词
  51. function check_record_badwords($model)
  52. {
  53. //过滤时段设置
  54. $filter = true;
  55. $hournow = get_date(TIMESTAMP,'H');
  56. if(Yii::$app->controller->module->badwordconfig['hours'])
  57. {
  58. $hours = explode(",",Yii::$app->controller->module->badwordconfig['hours']);
  59. if(!in_array($hournow,$hours))
  60. {
  61. $filter = false;
  62. }
  63. }
  64. if($filter==true)
  65. {
  66. $badwords = getRecordBadWords();
  67. //设定过滤条件(爬虫过滤)
  68. if(isSpider()&&Yii::$app->controller->module->badwordconfig['open_spider'])
  69. {
  70. if(is_array($badwords))foreach($badwords as $badword)
  71. {
  72. if(strpos($model->title,$badword)!==false||strpos($model->description,$badword)!==false)
  73. {
  74. if (Yii::$app->request->isAjax) {
  75. $msgdata = ['error' => 1,'msg' => Yii::t('admin','the record does not exist')];
  76. echo_json($msgdata);
  77. }
  78. else
  79. {
  80. throw new \Exception(Yii::t('admin','the record does not exist'),404);
  81. }
  82. }
  83. }
  84. }
  85. //设定过滤条件(手机站)
  86. if(!isSpider()&&defined('IN_WAP')&&IN_WAP==TRUE&&Yii::$app->controller->module->badwordconfig['open_wap'])
  87. {
  88. if(is_array($badwords))foreach($badwords as $badword)
  89. {
  90. if(strpos($model->title,$badword)!==false||strpos($model->description,$badword)!==false)
  91. {
  92. if (Yii::$app->request->isAjax) {
  93. $msgdata = ['error' => 1,'msg' => Yii::t('admin','the record does not exist')];
  94. echo_json($msgdata);
  95. }
  96. else
  97. {
  98. throw new \Exception(Yii::t('admin','the record does not exist'),404);
  99. }
  100. }
  101. }
  102. }
  103. //设定过滤条件(PC站)
  104. if(!isSpider()&&defined('REQUEST_FROM')&&REQUEST_FROM==1&&Yii::$app->controller->module->badwordconfig['open_pc'])
  105. {
  106. if(is_array($badwords))foreach($badwords as $badword)
  107. {
  108. if(strpos($model->title,$badword)!==false||strpos($model->description,$badword)!==false)
  109. {
  110. if (Yii::$app->request->isAjax) {
  111. $msgdata = ['error' => 1,'msg' => Yii::t('admin','the record does not exist')];
  112. echo_json($msgdata);
  113. }
  114. else
  115. {
  116. throw new \Exception(Yii::t('admin','the record does not exist'),404);
  117. }
  118. }
  119. }
  120. }
  121. return true;
  122. }
  123. else
  124. {
  125. return true;
  126. }
  127. }
  128. //列表过滤敏感词
  129. function check_doclist_badwords()
  130. {
  131. $badwords1 = getBadWords(1);
  132. if(!empty($badwords1))
  133. {
  134. $units[] = " is_badcom=0 ";
  135. }
  136. if(!\app\common\helpers\Identify::hasLogined())
  137. {
  138. $badwords2 = getBadWords(2);
  139. if(!empty($badwords2))
  140. {
  141. $units[] = " is_badlogin=0 ";
  142. }
  143. }
  144. if(!\app\common\helpers\Identify::hasLogined()||(\app\common\helpers\Identify::hasLogined()&&!\app\common\helpers\Identify::getUserInfo(NULL,'vip_info')))
  145. {
  146. $badwords3 = getBadWords(3);
  147. if(!empty($badwords3))
  148. {
  149. $units[] = " is_badvip=0 ";
  150. }
  151. }
  152. if(!empty($units))
  153. {
  154. $sql = ' and '.join(' and ',$units);
  155. }
  156. else
  157. {
  158. $sql = '';
  159. }
  160. //过滤时段设置
  161. $filter = true;
  162. $hournow = get_date(TIMESTAMP,'H');
  163. if(Yii::$app->controller->module->badwordconfig['hours'])
  164. {
  165. $hours = explode(",",Yii::$app->controller->module->badwordconfig['hours']);
  166. if(!in_array($hournow,$hours))
  167. {
  168. $filter = false;
  169. }
  170. }
  171. if($filter==true)
  172. {
  173. //设定过滤条件(爬虫过滤)
  174. if(isSpider()&&Yii::$app->controller->module->badwordconfig['open_spider'])
  175. {
  176. return $sql;
  177. }
  178. //设定过滤条件(手机站)
  179. if(!isSpider()&&defined('IN_WAP')&&IN_WAP==TRUE&&Yii::$app->controller->module->badwordconfig['open_wap'])
  180. {
  181. return $sql;
  182. }
  183. //设定过滤条件(PC站)
  184. if(!isSpider()&&defined('REQUEST_FROM')&&REQUEST_FROM==1&&Yii::$app->controller->module->badwordconfig['open_pc'])
  185. {
  186. return $sql;
  187. }
  188. return '';
  189. }
  190. else
  191. {
  192. return '';
  193. }
  194. }
  195. //获取OSS访问绝对URL;
  196. function getOssUrl()
  197. {
  198. if(Yii::$app->params['oss']['OSS_DOMAIN'])
  199. {
  200. return SITE_PROTOCOL.Yii::$app->params['oss']['OSS_DOMAIN'].'/';
  201. }
  202. else
  203. {
  204. return SITE_PROTOCOL.Yii::$app->params['oss']['OSS_BUCKET'].'.'.Yii::$app->params['oss']['OSS_ENDPOINT'].'/';
  205. }
  206. }
  207. //获取OSS内网访问绝对URL;
  208. function getOssInterUrl()
  209. {
  210. return SITE_PROTOCOL.Yii::$app->params['oss']['OSS_BUCKET'].'.'.Yii::$app->params['oss']['OSS_INTERNAL_ENDPOINT'].'/';
  211. }
  212. /*
  213. * $type 1:封面 2:头像
  214. */
  215. function getThumb($filepath,$width,$height,$type=1)
  216. {
  217. $hash = md5($filepath);
  218. $img = \app\modules\admin\models\Attachment::find()->where("hash='".$hash."'")->limit(1)->one();
  219. if($type==1)
  220. {
  221. if(empty($img)) $hash = 'noimage';
  222. $flag = 'p';
  223. }
  224. if($type==2)
  225. {
  226. if(empty($img)) $hash = 'noavatar';
  227. $flag = 'avt';
  228. }
  229. return APP_URL.$flag.'/'.$width.'/'.$height.'/'.$hash.'.jpg';
  230. }
  231. //获取系统配置单个值
  232. function getSysconfigValue($name,$key=null)
  233. {
  234. $sysconfig = \app\modules\admin\models\Sysconfig::find()->where("name='".$name."'")->one();
  235. if($sysconfig->type==3)
  236. {
  237. return $sysconfig->value;
  238. }
  239. else
  240. {
  241. if($key===null)
  242. {
  243. return json_decode($sysconfig->value,true);
  244. }
  245. else
  246. {
  247. if($sysconfig->type==1)
  248. {
  249. $result = json_decode($sysconfig->value,true);
  250. return $result[$key];
  251. }
  252. else
  253. {
  254. return $key;
  255. }
  256. }
  257. }
  258. }
  259. //获取用户模型下的默认用户组
  260. function getDefaultUserGroup($contentModelId)
  261. {
  262. $userGroup = \app\modules\ucenter\models\UserGroup::find()->where("content_model_id='".$contentModelId."' and is_default=1")->orderBy(['group_id'=>SORT_ASC])->one();
  263. return $userGroup->group_id;
  264. }
  265. //获取用户模型下的默认用户等级
  266. function getDefaultUserGroupLevel($contentModelId)
  267. {
  268. $userGroup = \app\modules\ucenter\models\UserGroup::find()->where("content_model_id='".$contentModelId."' and is_default=1")->orderBy(['group_id'=>SORT_ASC])->one();
  269. if($userGroup->group_id)
  270. {
  271. $userGroupLevel = \app\modules\ucenter\models\UserGroupLevel::find()->where("user_group_id='".$userGroup->group_id."' and is_default=1")->orderBy(['id'=>SORT_ASC])->one();
  272. return $userGroupLevel->id;
  273. }
  274. }
  275. /**合并搜索参数*
  276. * $params 表单传递参数
  277. * $initParams 初始化参数
  278. * $otherParams 其他参数
  279. */
  280. function mergeParams($query,$params,$initParams=[],$otherParams=[])
  281. {
  282. $params = count($params)>0?array_merge($initParams,$params):$initParams;
  283. if (count($params) > 0) {
  284. $i=0;
  285. foreach ($params as $key => $value) {
  286. $value = trim($value);
  287. if ($value != null) {
  288. $flag = is_numeric($value)?'=':'like';
  289. if($i==0){
  290. $query = $query->where(array($flag, $key, $value));
  291. }else{
  292. $query = $query->andWhere(array($flag, $key, $value));
  293. }
  294. $i++;
  295. }
  296. }
  297. }
  298. if(count($otherParams)>0)
  299. {
  300. $k=0;
  301. foreach($otherParams as $v)
  302. {
  303. if(count($initParams)==0&&count($params)==0&&$k==0)
  304. {
  305. $query = $query->where($v);
  306. }
  307. else
  308. {
  309. $query = $query->andWhere($v);
  310. }
  311. $k++;
  312. }
  313. }
  314. return $query;
  315. }
  316. function getLastInsertId()
  317. {
  318. return Yii::$app->db->getLastInsertID();
  319. }
  320. //CMS权限检测
  321. function check_rights($id, $ids = '', $s = ',') {
  322. if(!$ids) return false;
  323. $ids = explode($s, $ids);
  324. return is_array($id) ? array_intersect($id, $ids) : in_array($id, $ids);
  325. }
  326. //获取用户模型附表名称
  327. function userDetailTable($content_model_id)
  328. {
  329. $contentModel = \app\models\ContentModel::findOne($content_model_id);
  330. $tablename = Yii::$app->db->tablePrefix.$contentModel->table_name;
  331. return $tablename;
  332. }
  333. //通过分享码获取父级用户
  334. function getReferIdsByShareNo($share_no)
  335. {
  336. $sharelogModel = \app\modules\ucenter\models\ShareLog::find()->where("share_no='".$share_no."'")->one();
  337. $referer_id = intval($sharelogModel->agent_id);
  338. $parentUser = \app\modules\ucenter\models\User::findOne($sharelogModel->agent_id);
  339. if(!empty($parentUser->referer_ids))
  340. {
  341. $referer_ids[] = $parentUser->user_id;
  342. $referer_tempids = explode(",",trim($parentUser->referer_ids,","));
  343. $referer_ids = array_merge($referer_ids,$referer_tempids);
  344. }
  345. else
  346. {
  347. $referer_ids[] = $parentUser->user_id;
  348. }
  349. return ['referer_id'=>$referer_id,'referer_ids'=>",".join(",",$referer_ids).","];
  350. }
  351. /**
  352. * @todo 敏感词过滤,返回结果
  353. * @param array $list 定义敏感词一维数组
  354. * @param string $string 要过滤的内容
  355. * @return string $log 处理结果
  356. */
  357. function sensitive($string){
  358. $badwordList = Yii::$app->db->createCommand("select * from {{%bad_word}}")->queryAll();
  359. if($badwordList)foreach($badwordList as $badword)
  360. {
  361. if($badword['type']==99) { $list1[] = $badword['bad_word'];$list1_all[] = $badword;}//全局替换
  362. if($badword['type']==4) $list2[] = $badword['bad_word'];//禁止搜索
  363. if($badword['type']==88) $list3[] = $badword['bad_word'];//禁止发布
  364. }
  365. $count1 = 0; //违规替换词的个数
  366. $stringAfter = $string; //替换后的内容
  367. if(!empty($list1))
  368. {
  369. $sensitiveWord1 = ''; //违规词
  370. $pattern = "/".implode("|",$list1)."/i"; //定义正则表达式
  371. if(preg_match_all($pattern, $string, $matches)){ //匹配到了结果
  372. $patternList = $matches[0]; //匹配到的数组
  373. $count1 = count($patternList);
  374. $sensitiveWord1 = implode(',', $patternList); //敏感词数组转字符串
  375. $replaceWords = [];
  376. if($patternList)foreach($patternList as $sesword)
  377. {
  378. if($list1_all)foreach($list1_all as $badword)
  379. {
  380. if($badword['bad_word']==$sesword)
  381. {
  382. $replaceWords[] = $badword['replace_word']?$badword['replace_word']:'*';
  383. continue;
  384. }
  385. }
  386. }
  387. if(!empty($replaceWords)&&count($patternList)==count($replaceWords))
  388. {
  389. $replaceArray = array_combine($patternList,$replaceWords); //把匹配到的数组进行合并,替换使用
  390. //$replaceArray = array_combine($patternList,array_fill(0,count($patternList),'*')); //把匹配到的数组进行合并,替换使用
  391. $stringAfter = strtr($string, $replaceArray); //结果替换
  392. }
  393. }
  394. }
  395. $count2 = 0; //违规禁止搜索词的个数
  396. if(!empty($list2))
  397. {
  398. $sensitiveWord2 = '';
  399. $pattern2 = "/".implode("|",$list2)."/i";
  400. if(preg_match_all($pattern2, $string, $matches)){
  401. $patternList2 = $matches[0];
  402. $count2 = count($patternList2);
  403. $sensitiveWord2 = implode(',', $patternList2);
  404. }
  405. }
  406. $count3 = 0; //违规禁止发布词的个数
  407. if(!empty($list3))
  408. {
  409. $sensitiveWord3 = '';
  410. $pattern3 = "/".implode("|",$list3)."/i";
  411. if(preg_match_all($pattern3, $string, $matches)){
  412. $patternList3 = $matches[0];
  413. $count3 = count($patternList3);
  414. $sensitiveWord3 = implode(',', $patternList3);
  415. }
  416. }
  417. if($count1==0&&$count2==0&&$count3==0)
  418. {
  419. $result = [];
  420. }
  421. else
  422. {
  423. $result['after'] = $stringAfter;
  424. if($count1) {
  425. $result['log1'] = "全局替换敏感词:[ {$sensitiveWord1} ]";
  426. }
  427. else
  428. {
  429. $result['log1'] = '';
  430. }
  431. if($count2) {
  432. $result['log2'] = "禁止搜索敏感词:[ {$sensitiveWord2} ]";
  433. }
  434. else
  435. {
  436. $result['log2'] = '';
  437. }
  438. if($count3) {
  439. $result['log3'] = "禁止发布敏感词:[ {$sensitiveWord3} ]";
  440. }
  441. else
  442. {
  443. $result['log3'] = '';
  444. }
  445. }
  446. return $result;
  447. }
  448. //通过数据库表名获得对应的模型名称
  449. function getModelName($table)
  450. {
  451. $modelName = str_replace('_', ' ', $table);
  452. $modelName = ucwords($modelName);
  453. $modelName = str_replace(' ', '', $modelName);
  454. return $modelName;
  455. }
  456. //本地化存储文件到临时目录
  457. function locateFile($file,$folder='temp')
  458. {
  459. $result = parse_url($file);
  460. $result['path'] = str_replace('/upload/','',$result['path']);
  461. $relativeFile = ltrim(str_replace('/',DIRECTORY_SEPARATOR,$result['path']),DIRECTORY_SEPARATOR);
  462. $targetFile = UPLOAD_PATH.$folder.DIRECTORY_SEPARATOR.$relativeFile;
  463. dir_create(dirname($targetFile));
  464. if(Yii::$app->params['oss']['OPEN_OSS']==1)
  465. {
  466. if(!file_exists($targetFile))
  467. {
  468. if(Yii::$app->params['oss']['OPEN_INTERNAL'])
  469. {
  470. file_put_contents($targetFile,file_get_contents(str_replace(Yii::$app->params['oss']['OSS_ENDPOINT'],Yii::$app->params['oss']['OSS_INTERNAL_ENDPOINT'],$file)));
  471. }
  472. else
  473. {
  474. file_put_contents($targetFile,file_get_contents($file));
  475. }
  476. }
  477. }
  478. else
  479. {
  480. if(!file_exists($targetFile))
  481. {
  482. file_put_contents($targetFile,file_get_contents($file));
  483. }
  484. }
  485. return $targetFile;
  486. }
  487. //合辑下载时候归档所有文档
  488. function locateColFile($col,$docList)
  489. {
  490. $targetPath = UPLOAD_PATH.'download'.DIRECTORY_SEPARATOR.md5($col['id']).DIRECTORY_SEPARATOR;
  491. dir_create($targetPath);
  492. if(is_array($docList))foreach($docList as $doc)
  493. {
  494. $file = getFileUrl($doc['file']);
  495. $targetFile = $targetPath.$doc['title'].'.'.$doc['ext'];
  496. if(Yii::$app->params['oss']['OPEN_OSS']==1)
  497. {
  498. if(!file_exists($targetFile))
  499. {
  500. if(Yii::$app->params['oss']['OPEN_INTERNAL'])
  501. {
  502. file_put_contents($targetFile,file_get_contents(str_replace(Yii::$app->params['oss']['OSS_ENDPOINT'],Yii::$app->params['oss']['OSS_INTERNAL_ENDPOINT'],$file)));
  503. }
  504. else
  505. {
  506. file_put_contents($targetFile,file_get_contents($file));
  507. }
  508. }
  509. }
  510. else
  511. {
  512. if(!file_exists($targetFile))
  513. {
  514. file_put_contents($targetFile,file_get_contents($file));
  515. }
  516. }
  517. }
  518. $zipFile = dirname($targetPath).DIRECTORY_SEPARATOR.md5($col['id']).'.zip';
  519. if(!file_exists($zipFile))
  520. {
  521. new \app\common\components\Dozip($targetPath,$zipFile);
  522. }
  523. return $zipFile;
  524. }
  525. //大文件生成下载说明
  526. function initBigfileTorent($bigfile,$doc,$docConfig)
  527. {
  528. $savePath = dirname(str_replace(UPLOAD_PATH,'',$bigfile)).DIRECTORY_SEPARATOR;
  529. $bigfileUrl = UPLOAD_URL.str_replace(DIRECTORY_SEPARATOR,'/',$savePath).basename($bigfile);
  530. $torenFile = UPLOAD_PATH.$savePath.$doc['title'].'【下载说明】.txt';
  531. $torenContents = "【下载地址】".$bigfileUrl.",该地址有效期仅".$docConfig['bigfilesize_time']."小时,请尽快下载!";
  532. file_put_contents($torenFile,$torenContents);
  533. $torenUrl = UPLOAD_URL.str_replace(DIRECTORY_SEPARATOR,'/',$savePath).basename($torenFile);
  534. return $torenUrl;
  535. }
  536. //生成网盘资源种子文件
  537. function initPanfile($doc,$docConfig,$folder='download')
  538. {
  539. $savePath = $folder.DIRECTORY_SEPARATOR.get_date(TIMESTAMP,'Ymd').DIRECTORY_SEPARATOR;
  540. $targetPath = UPLOAD_PATH.$savePath;
  541. dir_create($targetPath);
  542. $torenFile = $targetPath.$doc['title'].'【提取说明】.txt';
  543. $contents = "【提取地址】".$doc['pan_url']."\r\n";
  544. if($doc['pan_pwd']) $contents .= "【提取密码】".$doc['pan_pwd']."\r\n";
  545. $contents .= "该地址有效期仅".$docConfig['bigfilesize_time']."小时,请尽快下载!";
  546. file_put_contents($torenFile,$contents);
  547. $torenUrl = UPLOAD_URL.str_replace(DIRECTORY_SEPARATOR,'/',$savePath).basename($torenFile);
  548. return $torenUrl;
  549. }
  550. //自动根据文档标题和摘要返回标签(通用)
  551. function initTags($model)
  552. {
  553. $tags = [];
  554. $tagResultList = \app\models\Tag::find()->where("disabled=0")->all();
  555. if(is_array($tagResultList))foreach($tagResultList as $tagResult)
  556. {
  557. if(strpos($model->title,$tagResult->tag)!==false) $tags[] = $tagResult->tag;
  558. if(strpos($model->description,$tagResult->tag)!==false) $tags[] = $tagResult->tag;
  559. }
  560. if(!empty($tags))
  561. {
  562. return join(",",array_unique($tags));
  563. }
  564. else
  565. {
  566. return '';
  567. }
  568. }
  569. //更新标签
  570. function refreshTag($tags,$table_name,$data_id,$user_id)
  571. {
  572. //清空当前记录关联标签
  573. \app\models\TagData::deleteAll("table_name='".$table_name."' and data_id=".$data_id);
  574. $tags = str_replace(",",",",$tags);
  575. $tagList = explode(",",$tags);
  576. if(is_array($tagList))foreach($tagList as $tag)
  577. {
  578. //更新标签库
  579. $tagModel = \app\models\Tag::find()->where("tag='".$tag."'")->one();
  580. if(empty($tagModel))
  581. {
  582. $tagModel = new \app\models\Tag();
  583. $tagModel->tag = $tag;
  584. $pinyin = new \app\common\components\PinYin();
  585. $tagModel->pinyin = $pinyin->encode($tagModel->tag);
  586. $tagModel->create_time = TIMESTAMP;
  587. $tagModel->user_id = $user_id;
  588. $tagModel->save();
  589. }
  590. //记录关联标签
  591. $tagDataModel = \app\models\TagData::find()->where("tag_id='".$tagModel->id."' and table_name='".$table_name."' and data_id=".$data_id)->one();
  592. if(empty($tagDataModel))
  593. {
  594. $tagDataModel = new \app\models\TagData();
  595. $tagDataModel->tag_id = $tagModel->id;
  596. $tagDataModel->table_name = $table_name;
  597. $tagDataModel->data_id = $data_id;
  598. $tagDataModel->user_id = $user_id;
  599. $tagDataModel->create_time = TIMESTAMP;
  600. $tagDataModel->save();
  601. }
  602. //更新标签被引用数量
  603. $quote_num = \app\models\TagData::find()->where("tag_id='".$tagModel->id."'")->count();
  604. $tagModel->quote_num = $quote_num;
  605. $tagModel->save();
  606. }
  607. }
  608. //获取周期数据
  609. function getTimeList($stamp=0)
  610. {
  611. //昨天
  612. $predayStart = get_date(mktime(0,0,0,date('m'),date('d')-1,date('Y')),'Y-m-d H:i:s');
  613. $predayEnd = get_date(mktime(0,0,0,date('m'),date('d'),date('Y'))-1,'Y-m-d H:i:s');
  614. //今天
  615. $dayStart = get_date(mktime(0,0,0,date('m'),date('d'),date('Y')),'Y-m-d H:i:s');
  616. $dayEnd = get_date(mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1,'Y-m-d H:i:s');
  617. //上周
  618. $preweekStart = get_date(str_to_time('last week monday'),'Y-m-d 00:00:00');
  619. $preweekEnd = get_date( str_to_time('last week sunday'),'Y-m-d 23:59:59');
  620. //$preweekStart = get_date(mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y")),"Y-m-d H:i:s");
  621. //$preweekEnd = get_date(mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y")),"Y-m-d H:i:s");
  622. //本周
  623. $weekStart = get_date(str_to_time('this week monday'),'Y-m-d 00:00:00');
  624. $weekEnd = get_date( str_to_time('this week sunday'),'Y-m-d 23:59:59');
  625. //$weekStart = get_date(mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")),"Y-m-d H:i:s");
  626. //$weekEnd = get_date(mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")),"Y-m-d H:i:s");
  627. //上月
  628. $premonthStart = get_date(mktime(0, 0 , 0,date("m")-1,1,date("Y")),"Y-m-d H:i:s");
  629. $premonthEnd = get_date(mktime(23,59,59,date("m") ,0,date("Y")),"Y-m-d H:i:s");
  630. //本月
  631. $monthStart = get_date(mktime(0, 0 , 0,date("m"),1,date("Y")),"Y-m-d H:i:s");
  632. $monthEnd = get_date(mktime(23,59,59,date("m"),date("t"),date("Y")),"Y-m-d H:i:s");
  633. //上季度
  634. $season = ceil((date('n'))/3)-1;//上季度是第几季度
  635. $presesStart = get_date(mktime(0, 0, 0,$season*3-3+1,1,date('Y')),'Y-m-d H:i:s');
  636. $presesEnd = get_date(mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y')),'Y-m-d H:i:s');
  637. //本季度
  638. $season = ceil((date('n'))/3);//当月是第几季度
  639. $sesStart = get_date( mktime(0, 0, 0,$season*3-3+1,1,date('Y')),'Y-m-d H:i:s');
  640. $sesEnd = get_date( mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y')),'Y-m-d H:i:s');
  641. //去年
  642. $preyearStart = get_date(mktime(0,0,0,1,1,date("Y",time())-1),"Y-m-d H:i:s");
  643. $preyearEnd = get_date(mktime(23,59,59,12,31,date("Y",time())-1),"Y-m-d H:i:s");
  644. //今年
  645. $yearStart = get_date(mktime(0,0,0,1,1,date("Y",time())),"Y-m-d H:i:s");
  646. $yearEnd = get_date(mktime(23,59,59,12,31,date("Y",time())),"Y-m-d H:i:s");
  647. $stampList = array(
  648. 'predayStart'=>str_to_time($predayStart),
  649. 'predayEnd'=>str_to_time($predayEnd),
  650. 'dayStart'=>str_to_time($dayStart),
  651. 'dayEnd'=>str_to_time($dayEnd),
  652. 'preweekStart'=>str_to_time($preweekStart),
  653. 'preweekEnd'=>str_to_time($preweekEnd),
  654. 'weekStart'=>str_to_time($weekStart),
  655. 'weekEnd'=>str_to_time($weekEnd),
  656. 'premonthStart'=>str_to_time($premonthStart),
  657. 'premonthEnd'=>str_to_time($premonthEnd),
  658. 'monthStart'=>str_to_time($monthStart),
  659. 'monthEnd'=>str_to_time($monthEnd),
  660. 'presesStart'=>str_to_time($presesStart),
  661. 'presesEnd'=>str_to_time($presesEnd),
  662. 'sesStart'=>str_to_time($sesStart),
  663. 'sesEnd'=>str_to_time($sesEnd),
  664. 'preyearStart'=>str_to_time($preyearStart),
  665. 'preyearEnd'=>str_to_time($preyearEnd),
  666. 'yearStart'=>str_to_time($yearStart),
  667. 'yearEnd'=>str_to_time($yearEnd),
  668. );
  669. $list = array(
  670. 'predayStart'=>($predayStart),
  671. 'predayEnd'=>($predayEnd),
  672. 'dayStart'=>($dayStart),
  673. 'dayEnd'=>($dayEnd),
  674. 'preweekStart'=>($preweekStart),
  675. 'preweekEnd'=>($preweekEnd),
  676. 'weekStart'=>($weekStart),
  677. 'weekEnd'=>($weekEnd),
  678. 'premonthStart'=>($premonthStart),
  679. 'premonthEnd'=>($premonthEnd),
  680. 'monthStart'=>($monthStart),
  681. 'monthEnd'=>($monthEnd),
  682. 'presesStart'=>($presesStart),
  683. 'presesEnd'=>($presesEnd),
  684. 'sesStart'=>($sesStart),
  685. 'sesEnd'=>($sesEnd),
  686. 'preyearStart'=>($preyearStart),
  687. 'preyearEnd'=>($preyearEnd),
  688. 'yearStart'=>($yearStart),
  689. 'yearEnd'=>($yearEnd),
  690. );
  691. return $stamp==1?$stampList:$list;
  692. }
  693. /**
  694. * 获取指定月的前N个月数据
  695. * @param $number 前number个月
  696. * @param $time 指定的月份2021-05
  697. * @return array 结果数据 ["2021-01", "2021-02", "2021-03"]
  698. */
  699. function getLastAllMonthByNumber($number, $time) {
  700. $months = [$time];
  701. for ($i = 1; $i < $number; $i++) {
  702. $beforeMonth = get_date(strtotime( $time. '-01' . " -{$i} months"),"Y-m");
  703. array_unshift($months, $beforeMonth);
  704. }
  705. return array_reverse($months);
  706. }
  707. //获取当月所有日期
  708. function get_days( $date ,$rtype = '1')
  709. {
  710. $tem = explode('-' , $date); //切割日期 得到年份和月份
  711. $year = $tem['0'];
  712. $month = $tem['1'];
  713. if( in_array($month , array( 1,3 , 5 , 7 , 8 , '01' , '03' ,'05' , '07' ,'08' , 10 , 12)))
  714. {
  715. // $text = $year.'年的'.$month.'月有31天';
  716. $text = '31';
  717. }
  718. elseif( $month == 2 )
  719. {
  720. if ( $year%400 == 0 || ($year%4 == 0 && $year%100 !== 0) ) //判断是否是闰年
  721. {
  722. // $text = $year.'年的'.$month.'月有29天';
  723. $text = '29';
  724. }
  725. else{
  726. // $text = $year.'年的'.$month.'月有28天';
  727. $text = '28';
  728. }
  729. }
  730. else{
  731. // $text = $year.'年的'.$month.'月有30天';
  732. $text = '30';
  733. }
  734. if ($rtype == '2') {
  735. for ($i = 1; $i <= $text ; $i ++ ) {
  736. $r[] = $year."-".$month."-".$i;
  737. }
  738. } else {
  739. $r = $text;
  740. }
  741. return $r;
  742. }
  743. /**
  744. * @param int $num 要转换的阿拉伯数字
  745. * @return string 转换成的字符串
  746. */
  747. function docnumConvert($num)
  748. {
  749. if($num<=1000000)
  750. {
  751. return '共<b>'.$num.'</b>份';
  752. }
  753. if ($num >= 100000000) {
  754. $num = '共<b>'.round($num / 100000000, 1).'</b><i class="total">亿</i>份</span>';
  755. } else if ($num >= 10000000) {
  756. $num = '共<b>'.round($num / 10000000, 1).'</b><i class="total">千万</i>份</span>';
  757. }else if ($num >= 100000) {
  758. $num = '共<b>'.round($num / 10000, 1).'</b><i class="total">万</i>份</span>';
  759. }
  760. return $num;
  761. }
  762. //美化时长显示
  763. function secondTime($seconds=0){
  764. $duration = '';
  765. $seconds = (int) $seconds;
  766. if ($seconds <= 0) {
  767. return $duration.'0秒';
  768. }
  769. list($day, $hour, $minute, $second) = explode(' ', gmstrftime('%j %H %M %S', $seconds));
  770. $day -= 1;
  771. if ($day > 0) {
  772. $duration .= (int) $day.'天';
  773. }
  774. if ($hour > 0) {
  775. $duration .= (int) $hour.'小时';
  776. }
  777. if ($minute > 0) {
  778. $duration .= (int) $minute.'分钟';
  779. }
  780. if ($second > 0) {
  781. $duration .= (int) $second.'秒';
  782. }
  783. return $duration;
  784. }
  785. //生成下载码
  786. function getDowncode($id)
  787. {
  788. $code = rand(100000,999999);
  789. $exist = \app\modules\doc\models\DocDowncode::find()->where("downcode='".$code."' and doc_id=".$id)->one();
  790. if(!$exist)
  791. {
  792. return $code;
  793. }
  794. else
  795. {
  796. return getDowncode($id);
  797. }
  798. }
  799. function getMimes()
  800. {
  801. $mimes = array(
  802. 'hqx' => 'application/mac-binhex40',
  803. 'cpt' => 'application/mac-compactpro',
  804. 'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel'),
  805. 'bin' => 'application/macbinary',
  806. 'dms' => 'application/octet-stream',
  807. 'lha' => 'application/octet-stream',
  808. 'lzh' => 'application/octet-stream',
  809. 'exe' => array('application/octet-stream', 'application/x-msdownload'),
  810. 'class' => 'application/octet-stream',
  811. 'psd' => 'application/x-photoshop',
  812. 'so' => 'application/octet-stream',
  813. 'sea' => 'application/octet-stream',
  814. 'dll' => 'application/octet-stream',
  815. 'oda' => 'application/oda',
  816. 'pdf' => array('application/pdf', 'application/x-download'),
  817. 'ai' => 'application/postscript',
  818. 'eps' => 'application/postscript',
  819. 'ps' => 'application/postscript',
  820. 'smi' => 'application/smil',
  821. 'smil' => 'application/smil',
  822. 'mif' => 'application/vnd.mif',
  823. 'xls' => array('application/excel', 'application/vnd.ms-excel', 'application/msexcel'),
  824. 'ppt' => array('application/powerpoint', 'application/vnd.ms-powerpoint'),
  825. 'wbxml' => 'application/wbxml',
  826. 'wmlc' => 'application/wmlc',
  827. 'dcr' => 'application/x-director',
  828. 'dir' => 'application/x-director',
  829. 'dxr' => 'application/x-director',
  830. 'dvi' => 'application/x-dvi',
  831. 'gtar' => 'application/x-gtar',
  832. 'gz' => 'application/x-gzip',
  833. 'php' => 'application/x-httpd-php',
  834. 'php4' => 'application/x-httpd-php',
  835. 'php3' => 'application/x-httpd-php',
  836. 'phtml' => 'application/x-httpd-php',
  837. 'phps' => 'application/x-httpd-php-source',
  838. 'js' => 'application/x-javascript',
  839. 'swf' => 'application/x-shockwave-flash',
  840. 'sit' => 'application/x-stuffit',
  841. 'tar' => 'application/x-tar',
  842. 'tgz' => array('application/x-tar', 'application/x-gzip-compressed'),
  843. 'xhtml' => 'application/xhtml+xml',
  844. 'xht' => 'application/xhtml+xml',
  845. 'zip' => array('application/x-zip', 'application/zip', 'application/x-zip-compressed'),
  846. 'mid' => 'audio/midi',
  847. 'midi' => 'audio/midi',
  848. 'mpga' => 'audio/mpeg',
  849. 'mp2' => 'audio/mpeg',
  850. 'mp3' => array('audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'),
  851. 'aif' => 'audio/x-aiff',
  852. 'aiff' => 'audio/x-aiff',
  853. 'aifc' => 'audio/x-aiff',
  854. 'ram' => 'audio/x-pn-realaudio',
  855. 'rm' => 'audio/x-pn-realaudio',
  856. 'rpm' => 'audio/x-pn-realaudio-plugin',
  857. 'ra' => 'audio/x-realaudio',
  858. 'rv' => 'video/vnd.rn-realvideo',
  859. 'wav' => 'audio/x-wav',
  860. 'bmp' => 'image/bmp',
  861. 'gif' => 'image/gif',
  862. 'jpeg' => array('image/jpeg', 'image/pjpeg'),
  863. 'jpg' => array('image/jpeg', 'image/pjpeg'),
  864. 'jpe' => array('image/jpeg', 'image/pjpeg'),
  865. 'png' => array('image/png', 'image/x-png'),
  866. 'tiff' => 'image/tiff',
  867. 'tif' => 'image/tiff',
  868. 'css' => 'text/css',
  869. 'html' => 'text/html',
  870. 'htm' => 'text/html',
  871. 'shtml' => 'text/html',
  872. 'txt' => 'text/plain',
  873. 'text' => 'text/plain',
  874. 'log' => array('text/plain', 'text/x-log'),
  875. 'rtx' => 'text/richtext',
  876. 'rtf' => 'text/rtf',
  877. 'xml' => 'text/xml',
  878. 'xsl' => 'text/xml',
  879. 'mpeg' => 'video/mpeg',
  880. 'mpg' => 'video/mpeg',
  881. 'mp4' => 'video/mp4',
  882. 'mpe' => 'video/mpeg',
  883. 'qt' => 'video/quicktime',
  884. 'mov' => 'video/quicktime',
  885. 'avi' => 'video/x-msvideo',
  886. 'movie' => 'video/x-sgi-movie',
  887. 'doc' => 'application/msword',
  888. 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  889. 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  890. 'word' => array('application/msword', 'application/octet-stream'),
  891. 'xl' => 'application/excel',
  892. 'eml' => 'message/rfc822',
  893. 'json' => array('application/json', 'text/json')
  894. );
  895. return $mimes;
  896. }
  897. //获取数据库连接中的数据库名称
  898. function getDsnAttribute($name, $dsn)
  899. {
  900. if (preg_match('/' . $name . '=([^;]*)/', $dsn, $match)) {
  901. return $match[1];
  902. } else {
  903. return null;
  904. }
  905. }
  906. //根据表名(不含前缀名)获取主键名
  907. function getPrikeyByTableName($table_name)
  908. {
  909. $db = Yii::$app->getDb();
  910. $dbName = getDsnAttribute('dbname', $db->dsn);
  911. $sqlbuf ="SELECT cu.Column_Name FROM INFORMATION_SCHEMA.`KEY_COLUMN_USAGE` cu
  912. WHERE CONSTRAINT_NAME = 'PRIMARY' AND cu.Table_Name = '".Yii::$app->db->tablePrefix.$table_name."' AND CONSTRAINT_SCHEMA='".$dbName."';";
  913. $result = Yii::$app->db->createCommand($sqlbuf)->queryOne();
  914. return $result['Column_Name'];
  915. }
  916. //返回不同内容的详情页链接
  917. function getContentLink($result)
  918. {
  919. if($result['table_name']=='doc_real')
  920. {
  921. $url = $result['doc_type']==2?\app\common\components\SiteUrl::colDetail($result['data_id']):\app\common\components\SiteUrl::docDetail($result['data_id']);
  922. }
  923. return $url;
  924. }
  925. //返回文档详情页链接
  926. function getDocLink($result)
  927. {
  928. $url = $result['doc_type']==2?\app\common\components\SiteUrl::colDetail($result['id']):\app\common\components\SiteUrl::docDetail($result['id']);
  929. return $url;
  930. }
  931. //生成分享分享单号
  932. function getShareNo()
  933. {
  934. $share_no = getProUniOrderNo();
  935. $exist = \app\modules\ucenter\models\ShareLog::find()->where("share_no='".$share_no."'")->one();
  936. if(!$exist)
  937. {
  938. return $share_no;
  939. }
  940. else
  941. {
  942. return getShareNo();
  943. }
  944. }
  945. //返回用户提现账户
  946. function getWithdrawBank($userInfo,$k=0)
  947. {
  948. $bankTypes = [];
  949. if($userInfo['alipay_name']&&$userInfo['alipay_account'])
  950. {
  951. $bankTypes[1] = array('title'=>'支付宝','real_name'=>$userInfo['alipay_name'],'account'=>$userInfo['alipay_account'],'desc'=>'支付宝【'.$userInfo['alipay_name'].':'.$userInfo['alipay_account'].'】');
  952. }
  953. if($userInfo['bank_real_name']&&$userInfo['bank_name']&&$userInfo['bank_no'])
  954. {
  955. $bankTypes[2] = array('title'=>$userInfo['bank_name'],'real_name'=>$userInfo['bank_real_name'],'account'=>$userInfo['bank_no'],'desc'=>$userInfo['bank_name'].'【'.$userInfo['bank_real_name'].':'.$userInfo['bank_no'].'】');
  956. }
  957. return $k==0?$bankTypes:$bankTypes[$k];
  958. }
  959. //生成海报
  960. function createPoster($config=array(),$filename=""){
  961. //如果要看报什么错,可以先注释调这个header
  962. if(empty($filename)) header("content-type: image/png");
  963. $imageDefault = array(
  964. 'left'=>0,
  965. 'top'=>0,
  966. 'right'=>0,
  967. 'bottom'=>0,
  968. 'width'=>100,
  969. 'height'=>100,
  970. 'opacity'=>100
  971. );
  972. $textDefault = array(
  973. 'text'=>'',
  974. 'left'=>0,
  975. 'top'=>0,
  976. 'fontSize'=>32, //字号
  977. 'fontColor'=>'255,255,255', //字体颜色
  978. 'angle'=>0,
  979. );
  980. $background = $config['background'];//海报最底层得背景
  981. //背景方法
  982. $backgroundInfo = getimagesize($background);
  983. $backgroundFun = 'imagecreatefrom'.image_type_to_extension($backgroundInfo[2], false);
  984. $background = $backgroundFun($background);
  985. $backgroundWidth = imagesx($background); //背景宽度
  986. $backgroundHeight = imagesy($background); //背景高度
  987. $imageRes = imageCreatetruecolor($backgroundWidth,$backgroundHeight);
  988. $bg = imagecolorallocatealpha($imageRes, 255, 255, 255, 127);
  989. imagefill($imageRes, 0, 0, $bg);
  990. imagecopyresampled($imageRes,$background,0,0,0,0,imagesx($background),imagesy($background),imagesx($background),imagesy($background));
  991. //处理了图片
  992. if(!empty($config['image'])){
  993. foreach ($config['image'] as $key => $val) {
  994. $val = array_merge($imageDefault,$val);
  995. $info = getimagesize($val['url']);
  996. $function = 'imagecreatefrom'.image_type_to_extension($info[2], false);
  997. if($val['stream']){ //如果传的是字符串图像流
  998. $info = getimagesizefromstring($val['url']);
  999. $function = 'imagecreatefromstring';
  1000. }
  1001. $res = $function($val['url']);
  1002. $resWidth = $info[0];
  1003. $resHeight = $info[1];
  1004. //建立画板 ,缩放图片至指定尺寸
  1005. $canvas=imagecreatetruecolor($val['width'], $val['height']);
  1006. //imagefill($canvas, 0, 0, $color);
  1007. //关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)
  1008. imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'],$resWidth,$resHeight);
  1009. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']) - $val['width']:$val['left'];
  1010. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']) - $val['height']:$val['top'];
  1011. //放置图像
  1012. imagecopymerge($imageRes,$canvas, $val['left'],$val['top'],$val['right'],$val['bottom'],$val['width'],$val['height'],$val['opacity']);//左,上,右,下,宽度,高度,透明度
  1013. }
  1014. }
  1015. //处理头像
  1016. if(!empty($config['avatar'])){
  1017. foreach ($config['avatar'] as $key => $val) {
  1018. if(empty($val))continue;
  1019. $val = array_merge($imageDefault,$val);
  1020. list($res,$resWidth,$resHeight) = toCircleImg($val['url']);
  1021. $res = getNewImgSize($res,$val['width'],$val['height'],$resWidth);
  1022. //关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)
  1023. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']) - $val['width']:$val['left'];
  1024. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']) - $val['height']:$val['top'];
  1025. imageantialias($imageRes, true);
  1026. imagecopy($imageRes, $res, $val['left'], $val['top'],$val['right'],$val['bottom'], $val['width'], $val['height']);
  1027. }
  1028. }
  1029. //处理缩略图
  1030. if(!empty($config['thumb'])){
  1031. foreach ($config['thumb'] as $key => $val) {
  1032. $info = getimagesize($val['url']);
  1033. $function = 'imagecreatefrom'.image_type_to_extension($info[2], false);
  1034. if($val['stream']){ //如果传的是字符串图像流
  1035. $info = getimagesizefromstring($val['url']);
  1036. $function = 'imagecreatefromstring';
  1037. }
  1038. $res = $function($val['url']);
  1039. $info = getimagesize($val['url']);
  1040. $resWidth = $info[0];
  1041. $resHeight = $info[1];
  1042. $canvas=imagecreatetruecolor($val['width'], $val['height']);
  1043. //imagefill($canvas, 0, 0, $color);
  1044. //关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)
  1045. imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'],$resWidth,$resHeight);
  1046. imagecopy($imageRes, $canvas, $val['left'], $val['top'],$val['right'],$val['bottom'], $val['width'], $val['height']);
  1047. }
  1048. }
  1049. //处理文字
  1050. if(!empty($config['text'])){
  1051. foreach ($config['text'] as $key => $val) {
  1052. if(empty($val))continue;
  1053. $val = array_merge($textDefault,$val);
  1054. list($R,$G,$B) = explode(',', $val['fontColor']);
  1055. $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
  1056. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']):$val['left'];
  1057. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']):$val['top'];
  1058. imagettftext($imageRes,$val['fontSize'],$val['angle'],$val['left'],$val['top'],$fontColor,$val['fontPath'],$val['text']);
  1059. }
  1060. }
  1061. if(!empty($config['text1'])){
  1062. foreach ($config['text1'] as $key => $val) {
  1063. if(empty($val))continue;
  1064. $val = array_merge($textDefault,$val);
  1065. list($R,$G,$B) = explode(',', $val['fontColor']);
  1066. $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
  1067. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']):$val['left'];
  1068. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']):$val['top'];
  1069. imagettftext($imageRes,$val['fontSize'],$val['angle'],$val['left'],$val['top'],$fontColor,$val['fontPath'],$val['text']);
  1070. }
  1071. }
  1072. if(!empty($config['nickname'])){
  1073. foreach ($config['nickname'] as $key => $val) {
  1074. if(empty($val))continue;
  1075. $val = array_merge($textDefault,$val);
  1076. list($R,$G,$B) = explode(',', $val['fontColor']);
  1077. $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
  1078. $val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']):$val['left'];
  1079. $val['top'] = $val['top']<0?$backgroundHeight- abs($val['top']):$val['top'];
  1080. imagettftext($imageRes,$val['fontSize'],$val['angle'],$val['left'],$val['top'],$fontColor,$val['fontPath'],$val['text']);
  1081. }
  1082. }
  1083. imagesavealpha($imageRes , true);
  1084. //生成图片
  1085. if(!empty($filename)){
  1086. $res = imagepng ($imageRes,$filename); //保存到本地
  1087. imagedestroy($imageRes);
  1088. if(!$res) return false;
  1089. return $filename;
  1090. }else{
  1091. imagepng ($imageRes); //在浏览器上显示
  1092. imagedestroy($imageRes);
  1093. }
  1094. }
  1095. /*
  1096. * 转换为圆形
  1097. */
  1098. function toCircleImg($imgpath)
  1099. {
  1100. $wh = getimagesize($imgpath);//pathinfo()不准
  1101. $src_img = null;
  1102. switch ($wh[2]) {
  1103. case 1:
  1104. //gif
  1105. $src_img = imagecreatefromgif($imgpath);
  1106. break;
  1107. case 2:
  1108. //jpg
  1109. $src_img = imagecreatefromjpeg($imgpath);
  1110. break;
  1111. case 3:
  1112. //png
  1113. $src_img = imagecreatefrompng($imgpath);
  1114. break;
  1115. }
  1116. $w = $wh[0];
  1117. $h = $wh[1];
  1118. $w = min($w, $h);
  1119. $h = $w;
  1120. $img = imagecreatetruecolor($w, $h);
  1121. imageantialias($img, true);
  1122. //这一句一定要有
  1123. imagesavealpha($img, true);
  1124. //拾取一个完全透明的颜色,最后一个参数127为全透明
  1125. $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
  1126. imagefill($img, 0, 0, $bg);
  1127. //拾取一个完全透明的颜色,最后一个参数127为全透明
  1128. /*$bg=imagecolorallocate($img,255,255,255);
  1129. imagecolortransparent($img,$bg);
  1130. imagefill($img, 0, 0, $bg);*/
  1131. $r = $w / 2; //圆半径
  1132. $y_x = $r; //圆心X坐标
  1133. $y_y = $r; //圆心Y坐标
  1134. for ($x = 0; $x < $w; $x++) {
  1135. for ($y = 0; $y < $h; $y++) {
  1136. $rgbColor = imagecolorat($src_img, $x, $y);
  1137. if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
  1138. imagesetpixel($img, $x, $y, $rgbColor);
  1139. }
  1140. }
  1141. }
  1142. return [$img,$w,$h];
  1143. }
  1144. /*
  1145. * 根据指定尺寸裁剪目标图片,这里统一转成132*132的
  1146. * 注意第一个参数,为了简便,直接传递的是图片资源,如果是绝对地址图片路径,可以加以改造
  1147. */
  1148. function getNewImgSize($imgpath,$new_width,$new_height,$w)
  1149. {
  1150. $image_p = imagecreatetruecolor($new_width, $new_height);//新画布
  1151. $bg = imagecolorallocatealpha($image_p, 255, 255, 255, 127);
  1152. imagefill($image_p, 0, 0, $bg);
  1153. imagecopyresampled($image_p, $imgpath, 0, 0, 0, 0, $new_width, $new_height, $w, $w);
  1154. return $image_p;
  1155. }
  1156. //生成分享单
  1157. function createShareNo($table_name,$data_id,$agent_id)
  1158. {
  1159. $shareLog = \app\modules\ucenter\models\ShareLog::find()->where("table_name='".$table_name."' and data_id=$data_id and agent_id=$agent_id")->one();
  1160. if(empty($shareLog))
  1161. {
  1162. $shareLog = new \app\modules\ucenter\models\ShareLog();
  1163. $shareLog->table_name = $table_name;
  1164. $shareLog->data_id = $data_id;
  1165. $shareLog->agent_id = $agent_id;
  1166. $shareLog->share_no = getShareNo();
  1167. $shareLog->create_time = TIMESTAMP;
  1168. $shareLog->status = 1;
  1169. $shareLog->save();
  1170. }
  1171. return $shareLog;
  1172. }
  1173. //获取搜索敏感词
  1174. function getSearchBadWords()
  1175. {
  1176. $badwords = [];
  1177. $resultList = \app\models\BadWord::find()->where("type=4")->all();
  1178. if(is_array($resultList))foreach($resultList as $result)
  1179. {
  1180. $badwords[] = $result->bad_word;
  1181. }
  1182. if(!\app\common\helpers\Identify::hasLogined())
  1183. {
  1184. $resultList = \app\models\BadWord::find()->where("type=5")->all();
  1185. if(is_array($resultList))foreach($resultList as $result)
  1186. {
  1187. $badwords[] = $result->bad_word;
  1188. }
  1189. }
  1190. if(!\app\common\helpers\Identify::hasLogined()||(\app\common\helpers\Identify::hasLogined()&&!\app\common\helpers\Identify::getUserInfo(NULL,'vip_info')))
  1191. {
  1192. $resultList = \app\models\BadWord::find()->where("type=6")->all();
  1193. if(is_array($resultList))foreach($resultList as $result)
  1194. {
  1195. $badwords[] = $result->bad_word;
  1196. }
  1197. }
  1198. $badwords = array_unique($badwords);
  1199. return $badwords;
  1200. }
  1201. //获取详情页显示敏感词
  1202. function getRecordBadWords()
  1203. {
  1204. $badwords = [];
  1205. $resultList = \app\models\BadWord::find()->where("type=1")->all();
  1206. if(is_array($resultList))foreach($resultList as $result)
  1207. {
  1208. $badwords[] = $result->bad_word;
  1209. }
  1210. if(!\app\common\helpers\Identify::hasLogined())
  1211. {
  1212. $resultList = \app\models\BadWord::find()->where("type=2")->all();
  1213. if(is_array($resultList))foreach($resultList as $result)
  1214. {
  1215. $badwords[] = $result->bad_word;
  1216. }
  1217. }
  1218. if(!\app\common\helpers\Identify::hasLogined()||(\app\common\helpers\Identify::hasLogined()&&!\app\common\helpers\Identify::getUserInfo(NULL,'vip_info')))
  1219. {
  1220. $resultList = \app\models\BadWord::find()->where("type=3")->all();
  1221. if(is_array($resultList))foreach($resultList as $result)
  1222. {
  1223. $badwords[] = $result->bad_word;
  1224. }
  1225. }
  1226. $badwords = array_unique($badwords);
  1227. return $badwords;
  1228. }
  1229. //获取敏感词 type:类型
  1230. function getBadWords($type)
  1231. {
  1232. $badwords = [];
  1233. $resultList = \app\models\BadWord::find()->where("type=$type")->all();
  1234. if(is_array($resultList))foreach($resultList as $result)
  1235. {
  1236. $badwords[] = $result->bad_word;
  1237. }
  1238. $badwords = array_unique($badwords);
  1239. return $badwords;
  1240. }
  1241. //给文本添加锚链接(TAG集合)
  1242. function anchorLinks($str)
  1243. {
  1244. $arr = array();
  1245. $keyLinks = \app\models\Tag::find()->all();
  1246. foreach($keyLinks as $keylink)
  1247. {
  1248. $arr[] = array(
  1249. 'id' => $keylink->key_link_id,
  1250. 'name' => $keylink->word,
  1251. 'url' => $keylink->url,
  1252. );
  1253. }
  1254. $newArr = array();
  1255. foreach ($arr as $k=>$v) {
  1256. $arr[$k]['length'] = abslength($v['name']);
  1257. }
  1258. $newArr = array_sort($arr);
  1259. $array = array($str);
  1260. foreach ($newArr as $vo) {
  1261. $res = preg_replace('/' . $vo['name'] . '/', '|||' . $vo['id'] . '|||', $array[0], 1);
  1262. if($res !== null){
  1263. $array[0] = $res;
  1264. }
  1265. }
  1266. foreach ($newArr as $vo) {
  1267. $array[0] = str_replace('|||' . $vo['id'] . '|||', '<a href="'.$vo['url'].'" target="_blank" title="'.$vo['name'].'">' . $vo['name'] . '</a>', $array[0]);
  1268. }
  1269. return $array[0];
  1270. }
  1271. //上传文件到远程服务器
  1272. function remoteUpload($url,$file,$name='file')
  1273. {
  1274. //$file = UPLOAD_PATH.str_replace("/",DIRECTORY_SEPARATOR,$fileRealPath))
  1275. $ch = curl_init();
  1276. //post数据,使用@符号,curl就会认为是有文件上传
  1277. $curlPost = array($name=> new \CURLFile($file));
  1278. curl_setopt($ch, CURLOPT_URL, $url);
  1279. curl_setopt($ch, CURLOPT_HEADER, 0);
  1280. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  1281. curl_setopt($ch, CURLOPT_POST, 1); //POST提交
  1282. curl_setopt($ch, CURLOPT_POSTFIELDS,$curlPost);
  1283. $data =curl_exec($ch);
  1284. curl_close($ch);
  1285. return json_decode($data,true);
  1286. }
  1287. //发送短信通知
  1288. function sendMobileMsg($action,$array)
  1289. {
  1290. extract($array);
  1291. require_once BASE_PATH.'vendor/alimsg/AliMsg.php';
  1292. $tplResult = \app\modules\admin\models\MobileMsgTpl::find()->asArray()->all();
  1293. if(is_array($tplResult))foreach($tplResult as $tpl)
  1294. {
  1295. $tplList[$tpl['key']] = $tpl;
  1296. }
  1297. $configInfo = \app\models\Config::find()->where("name='msg'")->limit(1)->one();
  1298. $config = string2array($configInfo->value);
  1299. $config['tpl'] = $tplList[$action]['code'];
  1300. $argsInfos = explode('\r\n',$tplList[$action]['args']);
  1301. $args = [];
  1302. if(is_array($argsInfos))foreach($argsInfos as $argInfo)
  1303. {
  1304. if(empty($argInfo))continue;
  1305. $tmp = explode("|",$argInfo);
  1306. $args[$tmp[0]] = eval("return $tmp[1];");
  1307. }
  1308. $msgtitle = $tplList[$action]['name'];
  1309. $msgcontent = $tplList[$action]['tpl'];
  1310. foreach($args as $k=>$v)
  1311. {
  1312. $msgcontent = str_replace('${'.$k.'}',$v,$msgcontent);
  1313. }
  1314. Yii::$app->db->createCommand("insert into {{%mobile_msg}} set mobile='".$mobile."',title='".$msgtitle."',content='".$msgcontent."',remarks='".$msgcontent."',sent_time=".TIMESTAMP.",real_sent_time=".TIMESTAMP."")->query();
  1315. $alimsg = \AliMsg::getInitCls($config);
  1316. if($mobile)
  1317. {
  1318. return $alimsg::sendMsg($mobile,$action,$args);
  1319. }
  1320. }
  1321. //剩余时间
  1322. function left_time_str($sale_time,$full=0){
  1323. $left_time = $sale_time - TIMESTAMP;
  1324. if($left_time <= 0){
  1325. return '';
  1326. }
  1327. $str = '';
  1328. $day = floor($left_time/86400);
  1329. $hour = floor(($left_time - $day * 86400)/3600);
  1330. $min = floor((($left_time - $day * 86400) - $hour * 3600)/60);
  1331. if($full)
  1332. {
  1333. if ($day > 0) $str .= $day . '天';
  1334. if ($hour > 0) $str .= $hour . '小时';
  1335. if ($min > 0) $str .= $min . '分钟';
  1336. return $str;
  1337. }
  1338. else
  1339. {
  1340. if ($day > 0) return $day . '天';
  1341. if ($hour > 0) return $hour . '小时';
  1342. if ($min > 0) return $min . '分钟';
  1343. }
  1344. }
  1345. //文档类型封面
  1346. function docTypeThumb($assetsUrl,$ext)
  1347. {
  1348. $agent = check_mobile()?'wap':'web';
  1349. if(!empty(Yii::$app->params['style']))
  1350. {
  1351. $sourcePath = Yii::$app->params['themePath'].Yii::$app->params['theme'].'/'.$agent.'/assets/'.Yii::$app->params['style'];
  1352. }
  1353. else
  1354. {
  1355. $sourcePath = Yii::$app->params['themePath'].Yii::$app->params['theme'].'/'.$agent.'/assets';
  1356. }
  1357. if(file_exists($sourcePath.'/images/icon/'.$ext.'.png'))
  1358. {
  1359. return $assetsUrl.'/images/icon/'.$ext.'.png';
  1360. }
  1361. else
  1362. {
  1363. return $assetsUrl.'/images/icon/null.png';
  1364. }
  1365. }
  1366. ?>