SetBasics.class.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. <?php
  2. namespace Cas\Controller\Admin\Activity;
  3. use Cas\Module\Lottery;
  4. use Cas\Dao\PlatformBulletin;
  5. use KIF\Core\Request;
  6. use Cas\Dao\LotteryEvents;
  7. use Cas\Controller\Admin\Controller;
  8. use Cas\Dao\Platform;
  9. use KIF\Core\Config;
  10. use Cas\Dao\BackUser as DBackUser;
  11. /**
  12. * ["活动相关" - "基础设置" ]
  13. * 页面: 创建活动基础设置 doPageCreate
  14. * 事件:操作: 创建活动基础设置 doReqCreate
  15. * 页面: 修改&查看 活动基础设置 doPageShow
  16. * 事件: 修改活动基础设置 doReqUp
  17. *
  18. * @author lihuanchun 
  19. *  
  20. */
  21. class SetBasics extends Controller {
  22. private $objLottery; // 活动后端
  23. private $operatorData;
  24. /**
  25. *  初始化
  26. */
  27. public function __construct() {
  28. header ( "Content-Type: text/html; charset=utf-8" );
  29. $this->objLottery = new Lottery ();
  30. $this->operatorData = $this->getUser ();
  31. }
  32. public function doDefault() {
  33. }
  34. /**
  35. * 页面: 创建活动 基础设置
  36. * 地址:http://cas.lishuy.com/?c=Admin_Activity_SetBasics&a=PageCreate&type=nnn&bigType=ssss
  37. * 参数:
  38. * type = 活动类型ID [可空]
  39. * bigType = 活动大类字符串
  40. */
  41. public function doPageCreate() {
  42. $thisType = Request::g ( 'type' ); // 获取类型
  43. $thisBigType = Request::g ( 'bigType' ); // 获取大类型
  44. // 获取所有活动类型
  45. $typeData = LotteryEvents::getType ();
  46. // 获取活动大类配置
  47. $bigTypeConfig = LotteryEvents::getBigTypeConfig ();
  48. // 获取所有发布列表(平台列表)
  49. $platformList = array ();
  50. $objDPlatform = new Platform ();
  51. $platformList = $objDPlatform->getsAll ();
  52. if (!self::isSuperadmin()) {
  53. $objDBackUser = new DBackUser();
  54. $user = $objDBackUser->get(self::getUid());
  55. $platfrom = $user['platfrom'];
  56. $tmpPlatformList = array();
  57. foreach ($platfrom as $tmpPlatformId) {
  58. $tmpPlatformList[] = $platformList[$tmpPlatformId];
  59. }
  60. $platformList = $tmpPlatformList;
  61. }
  62. // 获取是否授权配置参数配置
  63. $authorize = LotteryEvents::getAuthorize ();
  64. // 获取是是否在列表中显示配置
  65. $listDisplay = LotteryEvents::getListDisplay ();
  66. // 音乐图标位置
  67. $getMusicPosition = LotteryEvents::getMusicPosition ();
  68. // 导航
  69. $navConfig = $this->getCreatePageNav($thisType,$thisBigType);
  70. // 微信相关模块 不包含类型
  71. $weixinFormNotType = array(
  72. LotteryEvents::TYPE_EVENTS_OTHER,
  73. );
  74. $this->setOutput ( 'weixinFormNotType', $weixinFormNotType );
  75. // 音乐相关 不包含类型
  76. $musicFormNotType = array(
  77. LotteryEvents::TYPE_EVENTS_CUT_SCREEN,
  78. );
  79. $this->setOutput ( 'musicFormNotType', $musicFormNotType );
  80. // 切屏方向 不包含的类型
  81. $cutDirectionFormNotType = array(
  82. LotteryEvents::TYPE_EVENTS_CUT_SCREEN,
  83. );
  84. $this->setOutput ( 'cutDirectionFormNotType', $cutDirectionFormNotType );
  85. $this->tpl = 'admin/activity/page_create';
  86. $title = '基础设置';
  87. $this->setOutput('title', $title);
  88. $this->setOutput('menu_active', array('name' => 'quickpublish', 'item' => '')); //激活菜单
  89. $this->addNavMenu('活动列表');
  90. $this->addNavMenu($title);
  91. $this->setOutput ( 'navConfig', $navConfig );// 导航 显示 URL 配置
  92. $this->setOutput ( 'getMusicPosition', $getMusicPosition );
  93. $this->setOutput ( 'platformList', $platformList );
  94. $this->setOutput ( 'authorize', $authorize );
  95. $this->setOutput ( 'listDisplay', $listDisplay );
  96. $this->setOutput ( 'getMusicPosition', $getMusicPosition );
  97. $this->setOutput ( 'thisBigType', $thisBigType ); // 当前选择大类
  98. $this->setOutput ( 'thisType', $thisType ); // 当前选择类型
  99. $this->setOutput ( 'typeData', $typeData ); // 所有活动类型
  100. $this->setOutput ( 'bigTypeConfig', $bigTypeConfig ); // 所有活动类型
  101. $this->setOutput('typeDesc', $typeData[$thisType]['desc']);
  102. $this->setOutput('pagePublicData', $this->getPagePublicData()); // 后台管理相关数据
  103. }
  104. /**
  105. * 事件: 创建活动 基础设置
  106. * 请求地址:http://cas.lishuy.com/?c=Admin_Activity_SetBasics&a=ReqCreate
  107. * 参数:见方法内 $info
  108. */
  109. public function doReqCreate() {
  110. $info ['type'] = Request::p ( 'type' ); // 获取所选类型
  111. $info ['img_url'] = Request::p('img_url'); // 活动头图 img_url 表单名称 img_url
  112. $info ['events_name'] = Request::p ( 'events_name' ); // 活动标题 events_name 时间戳
  113. if ($info['type'] == LotteryEvents::TYPE_EVENTS_ARTICLE) {
  114. $info ['begin_time'] = date('Y-m-d H:i');
  115. } else {
  116. $info ['begin_time'] = Request::p ( 'begin_time' ) ? Request::p ( 'begin_time' ) : '2025-01.01 00:00:00'; // 活动开始时间 begin_time 时间戳
  117. }
  118. $info ['begin_time'] = strtotime($info ['begin_time']);
  119. $info ['end_time'] = Request::p ( 'end_time' ) ? Request::p ( 'end_time' ) : '2025-01.01 00:00:00'; // 活动结束时间 end_time
  120. $info ['end_time'] = strtotime($info ['end_time']);
  121. $tmp_platform_ids = $_POST['platform_ids']; // 所属"列表"IDs platform_ids_str
  122. foreach ( $tmp_platform_ids as $platform_id ) {
  123. $info ['platform_ids'] += $platform_id;
  124. }
  125. $info ['weixinShare'] ['imgUrl'] = Request::p('wx_img_url'); // 分享图片 weixinShare[imgUrl] 表单名称 wx_img_url
  126. $info ['weixinShare'] ['title'] = Request::p ( 'weixinShare_title' ); // 分享标题 weixinShare[title]
  127. $info ['weixinShare'] ['content'] = Request::p ( 'weixinShare_content' ); // 分享内容 weixinShare[content]
  128. $info ['weixinShare'] ['jump_url'] = Request::p ( 'weixinShare_jump_url' ); // 分享后跳转地址 weixinShare[jump_url]
  129. $info ['authorize'] = Request::p ( 'authorize' ); // 是否需要登录 authorize
  130. $info ['list_display'] = Request::p ( 'list_display' ); // 是否在列表中出现
  131. $info ['participation_user_num'] = Request::p ( 'participation_user_num' ); // 每天用户参与次数 participation_user_num
  132. $info ['participation_ip_num'] = Request::p ( 'participation_ip_num' ); // 允许每天IP参与次数 participation_ip_num
  133. $info ['participation_user_all_num'] = Request::p ( 'participation_user_all_num' ); // 允许用户参与次数 participation_user_all_num
  134. $info ['winning_user_num'] = Request::p ( 'winning_user_num' ); // 允许用户获奖次数 winning_user_num
  135. $info['cut_fx'] = Request::p('cut_fx'); // 切屏方向
  136. $info ['music_url'] = Request::p('music_url'); // 获取背景音乐 music_url 表单名称:music_url
  137. $info ['operator_uid'] = $this->operatorData['uid'];// 创建人UID
  138. $info ['other_url'] = Request::p ( 'other_url' ); // 跳转地址
  139. $info ['music_position'] = Request::p ( 'music_position' ); // 音乐图标位置 music_position
  140. $info['loading_bg_color'] = Request::p('loading_bg_color');
  141. $info['loading_text_color'] = Request::p('loading_text_color');
  142. $info ['setPageType'] = 'Basics';
  143. // 执行添加方法
  144. $events_id = $this->objLottery->addLotteryEvents ( $info );
  145. if (! $events_id) {
  146. $this->ajax_fail_exit('基础设置失败');
  147. }
  148. $objPlatform = new Platform();
  149. $formData = $objPlatform -> getsAll();
  150. $eventData = $this->objLottery->getOneLotteryEvents($events_id);
  151. foreach ( $tmp_platform_ids as $platform_id ) {
  152. $url = $this->objLottery ->getEventsUrl($info ['type'], $eventData['create_time'],$formData[$platform_id]['url']);
  153. $this->createImgCodeUrl($url);
  154. }
  155. // 更具不同活动 大类型 跳转 到不同的地址
  156. $bigTypeConfig = LotteryEvents::getBigTypeConfig ();
  157. if(in_array($info['type'], $bigTypeConfig['Event'])){
  158. $redirect_url = Request::schemeDomain () . "/?c=Admin_Activity_SetInteractive&a=PageShow&events_id={$events_id}";
  159. }else{
  160. $redirect_url = Request::schemeDomain () . "/?c=Admin_Activity_SetPage&a=PageShow&events_id={$events_id}";
  161. }
  162. if($info['type'] == LotteryEvents::TYPE_EVENTS_OTHER){
  163. $redirect_url = Request::schemeDomain () . "/?c=Admin_Activity_DeliveryChannels&a=PageList&events_id={$events_id}";
  164. }
  165. $this->ajax_success_exit($redirect_url);
  166. }
  167. /**
  168. * 页面: 修改&查看 活动基础设置 doPageShow
  169. * 地址:http://cas.lishuy.com/?c=Admin_Activity_SetBasics&a=PageShow&events_id=nnn
  170. * 参数:events_id 活动ID
  171. */
  172. public function doPageShow() {
  173. $events_id = Request::g('events_id');
  174. $eventData =$this->objLottery->getOneLotteryEvents($events_id);
  175. $typeData = LotteryEvents::getType ();
  176. $bigTypeConfig = LotteryEvents::getBigTypeConfig ();
  177. $platformList = array ();
  178. $objDPlatform = new Platform ();
  179. $platformList = $objDPlatform->getsAll ();
  180. if (!self::isSuperadmin()) {
  181. $objDBackUser = new DBackUser();
  182. $user = $objDBackUser->get(self::getUid());
  183. $platfrom = $user['platfrom'];
  184. $tmpPlatformList = array();
  185. foreach ($platfrom as $tmpPlatformId) {
  186. $tmpPlatformList[] = $platformList[$tmpPlatformId];
  187. }
  188. $platformList = $tmpPlatformList;
  189. }
  190. $authorize = LotteryEvents::getAuthorize ();
  191. // 获取是是否在列表中显示配置
  192. $listDisplay = LotteryEvents::getListDisplay ();
  193. $getMusicPosition = LotteryEvents::getMusicPosition ();
  194. $navConfig = $this->getUpPageNav($events_id, 'Basics');
  195. // 微信相关模块 不包含类型
  196. $weixinFormNotType = array(
  197. LotteryEvents::TYPE_EVENTS_OTHER,
  198. );
  199. $this->setOutput ( 'weixinFormNotType', $weixinFormNotType );
  200. // 音乐相关 不包含类型
  201. $musicFormNotType = array(
  202. LotteryEvents::TYPE_EVENTS_CUT_SCREEN,
  203. );
  204. $this->setOutput ( 'musicFormNotType', $musicFormNotType );
  205. // 切屏方向 不包含的类型
  206. $cutDirectionFormNotType = array(
  207. LotteryEvents::TYPE_EVENTS_CUT_SCREEN,
  208. );
  209. $this->setOutput ( 'cutDirectionFormNotType', $cutDirectionFormNotType );
  210. // 发货状态 不包含的类型
  211. $deliveryFormNotType = array(
  212. LotteryEvents::TYPE_EVENTS_CUT_SCREEN,
  213. LotteryEvents::TYPE_EVENTS_ARTICLE,
  214. LotteryEvents::TYPE_EVENTS_OTHER,
  215. LotteryEvents::TYPE_EVENTS_CODE,
  216. LotteryEvents::TYPE_EVENTS_INVITATION,
  217. LotteryEvents::TYPE_EVENTS_VOTE,
  218. );
  219. $this->setOutput ( 'deliveryFormNotType', $deliveryFormNotType );
  220. $deliveryDesc = LotteryEvents::getDeliveryType (); // 发货状态
  221. if(!$eventData['delivery']){
  222. $eventData['delivery'] = LotteryEvents::DELIVERY_FALSE;
  223. }
  224. $this->setOutput ( 'deliveryDesc', $deliveryDesc );
  225. $this->tpl = 'admin/activity/page_show';
  226. $title = '基础设置';
  227. $this->setOutput('title', $title);
  228. $this->setOutput('menu_active', array('name' => 'mypublish', 'item' => '')); //激活菜单
  229. $this->addNavMenu('活动列表');
  230. $this->addNavMenu($title);
  231. $objDPlatform = new Platform();
  232. $platFormData = $objDPlatform -> getsAll();
  233. # 获取当前活动都添加哪些 列表
  234. # 返回列表IDs
  235. $platFormIds = array();
  236. foreach($platFormData as $key => $data){
  237. if($eventData['platform_ids']&$key){
  238. $platFormIds[$key] = $key;
  239. }
  240. }
  241. $this->setOutput('displayDesc', LotteryEvents::getDisplay());
  242. $this->setOutput ( 'navConfig', $navConfig ); // 导航显示 URL 配置
  243. $this->setOutput ( 'getMusicPosition', $getMusicPosition );// 音乐图标位置
  244. $this->setOutput ( 'platformList', $platformList );// 获取所有发布列表(平台列表)
  245. $this->setOutput ( 'listDisplay', $listDisplay ); // 获取是否在列表中显示
  246. $this->setOutput ( 'authorize', $authorize );// 获取是否登录配置参数配置
  247. $this->setOutput ( 'typeData', $typeData ); // 所有活动类型
  248. $this->setOutput ( 'thisType', $eventData['type'] ); // 所有活动类型
  249. $this->setOutput ( 'bigTypeConfig', $bigTypeConfig ); // 所有活动类型
  250. $this->setOutput ( 'eventData', $eventData ); // 当前活动详情
  251. $this->setOutput('platFormIds', $platFormIds);
  252. $this->setOutput('pagePublicData', $this->getPagePublicData($events_id)); // 后台管理相关数据
  253. }
  254. /**
  255. * 事件: 修改活动基础设置 doReqUp
  256. * 地址:http://cas.lishuy.com/?c=Admin_Activity_SetBasics&a=PageShow&events_id=nnn
  257. * 参数:见方法内
  258. */
  259. public function doReqUp() {
  260. $events_id = Request::p('events_id');
  261. $info ['img_url'] = Request::p('img_url'); // 活动头图 img_url 表单名称 img_url
  262. // $tmpImgData = $this->loadImg ( 'img_url' );
  263. // if (is_array ( $tmpImgData )) {
  264. // $info ['img_url'] = $tmpImgData ['url'] [0];
  265. // }
  266. $info ['events_name'] = Request::p ( 'events_name' ); // 活动标题 events_name 时间戳
  267. $info ['begin_time'] = Request::p ( 'begin_time' ) ? Request::p ( 'begin_time' ) : '2025-01.01 00:00:00'; // 活动开始时间 begin_time 时间戳
  268. $info ['begin_time'] = strtotime($info ['begin_time']);
  269. $info ['end_time'] = Request::p ( 'end_time' ) ? Request::p ( 'end_time' ) : '2025-01.01 00:00:00'; // 活动结束时间 end_time
  270. $info ['end_time'] = strtotime($info ['end_time']);
  271. /*
  272. $tmp_platform_ids = $_POST['platform_ids'];
  273. foreach ( $tmp_platform_ids as $platform_id ) {
  274. $info ['platform_ids'] += $platform_id;
  275. }
  276. */
  277. $info ['weixinShare'] ['imgUrl'] = Request::p('wx_img_url'); // 分享图片 weixinShare[imgUrl] 表单名称 wx_img_url
  278. // $tmpImgData = $this->loadImg ( 'wx_img_url' );
  279. // if (is_array ( $tmpImgData )) {
  280. // $info ['weixinShare'] ['imgUrl'] = $tmpImgData ['url'] [0];
  281. // }
  282. $info ['weixinShare'] ['title'] = Request::p ( 'weixinShare_title' ); // 分享标题 weixinShare[title]
  283. $info ['weixinShare'] ['content'] = Request::p ( 'weixinShare_content' ); // 分享内容 weixinShare[content]
  284. $info ['weixinShare'] ['jump_url'] = Request::p ( 'weixinShare_jump_url' ); // 分享后跳转地址 weixinShare[jump_url]
  285. $info ['authorize'] = Request::p ( 'authorize' ); // 是否需要登录 authorize
  286. $info ['list_display'] = Request::p ( 'list_display' ); // 是否在列表中出现
  287. $info ['delivery'] = Request::p ( 'delivery' ); // 是否发货
  288. $info ['participation_user_num'] = Request::p ( 'participation_user_num' ); // 每天用户参与次数 participation_user_num
  289. $info ['participation_ip_num'] = Request::p ( 'participation_ip_num' ); // 允许每天IP参与次数 participation_ip_num
  290. $info ['participation_user_all_num'] = Request::p ( 'participation_user_all_num' ); // 允许用户参与次数 participation_user_all_num
  291. $info ['winning_user_num'] = Request::p ( 'winning_user_num' ); // 允许用户获奖次数 winning_user_num
  292. $info['cut_fx'] = Request::p('cut_fx'); // 切屏方向
  293. $info ['music_url'] = Request::p('music_url'); // 获取背景音乐 music_url 表单名称:music_url
  294. $info['loading_bg_color'] = Request::p('loading_bg_color');
  295. $info['loading_text_color'] = Request::p('loading_text_color');
  296. // $tmpMusicData = $this->loadMusic ( 'music' );
  297. // if (is_array ( $tmpMusicData )) {
  298. // $info ['music_url'] = $tmpMusicData ['url'] [0];
  299. // }
  300. $info ['music_position'] = Request::p ( 'music_position' ); // 音乐图标位置 music_position
  301. $display = Request::p ( 'display' );
  302. if($display!=null){
  303. $info ['display'] = $display;
  304. }
  305. # 修改操作
  306. $tmpData = $this->objLottery->upLotteryEvents($info, array('id' => $events_id));
  307. if(!$tmpData){
  308. $this->ajax_fail_exit('修改失败');
  309. }
  310. $this->setNowUpPage($events_id, 'Basics');
  311. $this->ajax_success_exit(Request::schemeDomain () . "/?c=Admin_Activity_SetBasics&a=PageShow&events_id={$events_id}");
  312. }
  313. /**
  314. * 上传音乐
  315. *
  316. * @param string $files_name
  317. */
  318. public function loadMusic($files_name = 'music') {
  319. $uploadConfig = Config::getInstance ()->get ( 'upload' );
  320. // 定义允许上传的文件扩展名
  321. $ext_arr = array (
  322. 'music' => array (
  323. 'mp3'
  324. )
  325. );
  326. if (! is_array ( $_FILES [$image_name] ['name'] )) {
  327. foreach ( $_FILES [$image_name] as $key => $data ) {
  328. unset ( $_FILES [$image_name] [$key] );
  329. $_FILES [$image_name] [$key] [] = $data;
  330. }
  331. }
  332. $file_url = array ();
  333. foreach ( $_FILES [$image_name] ['name'] as $key => $oneData ) {
  334. // 文件保存目录路径
  335. $save_path = $uploadConfig ['path'] . '/';
  336. $save_path = realpath ( $save_path ) . '/';
  337. // PHP上传失败
  338. if ($_FILES [$image_name] ['error'] [$key] != 0) {
  339. switch ($_FILES [$image_name] ['error'] [$key]) {
  340. case '1' :
  341. $error = '超过php.ini允许的大小。';
  342. break;
  343. case '2' :
  344. $error = '超过表单允许的大小。';
  345. break;
  346. case '3' :
  347. $error = '音乐只有部分被上传。';
  348. break;
  349. case '4' :
  350. $error = '请选择音乐文件。';
  351. break;
  352. case '6' :
  353. $error = '找不到临时目录。';
  354. break;
  355. case '7' :
  356. $error = '写文件到硬盘出错。';
  357. break;
  358. case '8' :
  359. $error = 'File upload stopped by extension。';
  360. break;
  361. case '999' :
  362. default :
  363. $error = '未知错误。';
  364. }
  365. return $error;
  366. }
  367. // 有上传文件时
  368. if (empty ( $_FILES ) === false) {
  369. // 原文件名
  370. $file_name = $_FILES [$image_name] ['name'] [$key];
  371. // 服务器上临时文件名
  372. $tmp_name = $_FILES [$image_name] ['tmp_name'] [$key];
  373. // 文件大小
  374. $file_size = $_FILES [$image_name] ['size'] [$key];
  375. // 检查文件名
  376. if (! $file_name) {
  377. return "请选择文件。";
  378. }
  379. // 检查目录
  380. if (@is_dir ( $save_path ) === false) {
  381. return "上传目录不存在。";
  382. }
  383. // 检查目录写权限
  384. if (@is_writable ( $save_path ) === false) {
  385. return "上传目录没有写权限。";
  386. }
  387. // 检查是否已上传
  388. if (@is_uploaded_file ( $tmp_name ) === false) {
  389. return "上传失败。";
  390. }
  391. // 检查目录名
  392. $dir_name = 'music';
  393. if (empty ( $ext_arr [$dir_name] )) {
  394. return "目录名不正确。";
  395. }
  396. // 获得文件扩展名
  397. $temp_arr = explode ( ".", $file_name );
  398. $file_ext = array_pop ( $temp_arr );
  399. $file_ext = trim ( $file_ext );
  400. $file_ext = strtolower ( $file_ext );
  401. // 检查扩展名
  402. if (in_array ( $file_ext, $ext_arr [$dir_name] ) === false) {
  403. return "上传文件扩展名是不允许的扩展名。\n只允许" . implode ( ",", $ext_arr [$dir_name] ) . "格式。";
  404. }
  405. // 创建文件夹
  406. $ymd = date ( "Ymd" );
  407. $ymd = date ( "Y" ) . '/' . date ( 'md' );
  408. // 文件保存目录URL
  409. $save_url = $uploadConfig ['url'] . '/';
  410. $new_file_name = '';
  411. $save_path .= $dir_name . "/" . $ymd . "/";
  412. $save_url .= $dir_name . "/" . $ymd . "/";
  413. if (! file_exists ( $save_path )) {
  414. if (! mkdir ( $save_path, 0775, true )) {
  415. return "创建目录失败:{$save_path}";
  416. }
  417. }
  418. // 新文件名
  419. $new_file_name = date ( "His" ) . '_' . rand ( 10000, 99999 ) . '.' . $file_ext;
  420. // 移动文件
  421. $file_path = $save_path . $new_file_name;
  422. // 原图上传
  423. if (move_uploaded_file ( $tmp_name, $file_path ) === false) {
  424. return "上传文件失败。";
  425. }
  426. @chmod ( $file_path, 0777 );
  427. $file_url [$key] = $file_path;
  428. }
  429. }
  430. return array (
  431. 'error' => 0,
  432. 'url' => $file_url
  433. );
  434. }
  435. /**
  436. * 方法:图片上传
  437. */
  438. public function loadImg($image_name = 'image') {
  439. $uploadConfig = Config::getInstance ()->get ( 'upload' );
  440. // 定义允许上传的文件扩展名
  441. $ext_arr = array (
  442. 'image' => array (
  443. 'gif',
  444. 'jpg',
  445. 'jpeg',
  446. 'png',
  447. 'bmp'
  448. )
  449. );
  450. if (! is_array ( $_FILES [$image_name] ['name'] )) {
  451. foreach ( $_FILES [$image_name] as $key => $data ) {
  452. unset ( $_FILES [$image_name] [$key] );
  453. $_FILES [$image_name] [$key] [] = $data;
  454. }
  455. }
  456. $file_url = array ();
  457. foreach ( $_FILES [$image_name] ['name'] as $key => $oneData ) {
  458. // 文件保存目录路径
  459. $save_path = $uploadConfig ['path'] . '/';
  460. $save_path = realpath ( $save_path ) . '/';
  461. // PHP上传失败
  462. if ($_FILES [$image_name] ['error'] [$key] != 0) {
  463. switch ($_FILES [$image_name] ['error'] [$key]) {
  464. case '1' :
  465. $error = '超过php.ini允许的大小。';
  466. break;
  467. case '2' :
  468. $error = '超过表单允许的大小。';
  469. break;
  470. case '3' :
  471. $error = '图片只有部分被上传。';
  472. break;
  473. case '4' :
  474. $error = '请选择图片。';
  475. break;
  476. case '6' :
  477. $error = '找不到临时目录。';
  478. break;
  479. case '7' :
  480. $error = '写文件到硬盘出错。';
  481. break;
  482. case '8' :
  483. $error = 'File upload stopped by extension。';
  484. break;
  485. case '999' :
  486. default :
  487. $error = '未知错误。';
  488. }
  489. return $error;
  490. }
  491. // 有上传文件时
  492. if (empty ( $_FILES ) === false) {
  493. // 原文件名
  494. $file_name = $_FILES [$image_name] ['name'] [$key];
  495. // 服务器上临时文件名
  496. $tmp_name = $_FILES [$image_name] ['tmp_name'] [$key];
  497. // 文件大小
  498. $file_size = $_FILES [$image_name] ['size'] [$key];
  499. // 检查文件名
  500. if (! $file_name) {
  501. return "请选择文件。";
  502. }
  503. // 检查目录
  504. if (@is_dir ( $save_path ) === false) {
  505. return "上传目录不存在。";
  506. }
  507. // 检查目录写权限
  508. if (@is_writable ( $save_path ) === false) {
  509. return "上传目录没有写权限。";
  510. }
  511. // 检查是否已上传
  512. if (@is_uploaded_file ( $tmp_name ) === false) {
  513. return "上传失败。";
  514. }
  515. // 检查目录名
  516. $dir_name = 'image';
  517. if (empty ( $ext_arr [$dir_name] )) {
  518. return "目录名不正确。";
  519. }
  520. // 获得文件扩展名
  521. $temp_arr = explode ( ".", $file_name );
  522. $file_ext = array_pop ( $temp_arr );
  523. $file_ext = trim ( $file_ext );
  524. $file_ext = strtolower ( $file_ext );
  525. // 检查扩展名
  526. if (in_array ( $file_ext, $ext_arr [$dir_name] ) === false) {
  527. return "上传文件扩展名是不允许的扩展名。\n只允许" . implode ( ",", $ext_arr [$dir_name] ) . "格式。";
  528. }
  529. // 创建文件夹
  530. $ymd = date ( "Ymd" );
  531. $ymd = date ( "Y" ) . '/' . date ( 'md' );
  532. // 文件保存目录URL
  533. $save_url = $uploadConfig ['url'] . '/';
  534. $new_file_name = '';
  535. $save_path .= $dir_name . "/" . $ymd . "/";
  536. $save_url .= $dir_name . "/" . $ymd . "/";
  537. if (! file_exists ( $save_path )) {
  538. if (! mkdir ( $save_path, 0775, true )) {
  539. return "创建目录失败:{$save_path}";
  540. }
  541. }
  542. // 新文件名
  543. $new_file_name = date ( "His" ) . '_' . rand ( 10000, 99999 ) . '.' . $file_ext;
  544. // 移动文件
  545. $file_path = $save_path . $new_file_name;
  546. // 原图上传
  547. if (move_uploaded_file ( $tmp_name, $file_path ) === false) {
  548. return "上传文件失败。";
  549. }
  550. error_reporting ( E_ALL );
  551. // 压缩65质量原图
  552. $this_basename = basename ( $file_path );
  553. $this_dirname = dirname ( $file_path );
  554. $z_file_path = $this_dirname . '/' . 'z.' . $this_basename;
  555. Image::transformFormat ( $file_path, $z_file_path, 'JPG' );
  556. @chmod ( $file_path, 0777 );
  557. $file_url [$key] = $save_url . 'z.' . $new_file_name;
  558. }
  559. }
  560. return array (
  561. 'error' => 0,
  562. 'url' => $file_url
  563. );
  564. }
  565. public function display() {
  566. return $this->render ();
  567. }
  568. }