MengbaoController.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. namespace Admin\Controller;
  3. /**
  4. * 素材管理
  5. * @author brent
  6. * @version 0.0.1
  7. */
  8. class MengbaoController extends CommonController {
  9. protected $table = ''; //表名
  10. /**
  11. * [_initialize 前置操作-继承公共前置方法]
  12. * @author Devil
  13. * @blog http://gong.gg/
  14. * @version 0.0.1
  15. * @datetime 2016-12-03T12:39:08+0800
  16. */
  17. public function _initialize() {
  18. // 调用父类前置方法
  19. parent::_initialize();
  20. // 登录校验
  21. $this->Is_Login();
  22. // 权限校验
  23. $this->Is_Power();
  24. //要执行的表
  25. $this->table = M('activity_mengbao');
  26. }
  27. /**
  28. * [Index 列表]
  29. * @author Devil
  30. * @blog http://gong.gg/
  31. * @version 0.0.1
  32. * @datetime 2016-12-06T21:31:53+0800
  33. */
  34. public function Index() {
  35. $where = array();
  36. $listRows = I('listRows') ? I('listRows') : 100;
  37. $keyword = I("keyword", "", "trim");
  38. $keyword && $where['name'] = array('like', '%' . $keyword . '%');
  39. if (isset($keyword) && !empty($keyword)) {
  40. $this->assign('keyword', $keyword);
  41. }
  42. // var_dump(I('post.'));die;
  43. $page = I('p') ? I('p') : 1;
  44. $this->assign('p', $page);
  45. $list = D('Admin/Mengbao')->get_mengbao_list($where, $page, $listRows, $count);
  46. // echo "<pre>";
  47. // var_dump($list); die;
  48. //分页
  49. $Page = new \Think\Page($count, $listRows);
  50. $Page->parameter .= "&p=[PAGE]";
  51. $Page->parameter .= "&listRows=" . $listRows;
  52. $Page->parameter .= "&keyword=" .$keyword;
  53. $Page = $this->page_config($Page);
  54. $show = $Page->show();
  55. $this->assign('page', $show);
  56. $this->assign('List', $list);
  57. $this->display('Index');
  58. }
  59. /**
  60. * [ExcelExport excel文件导出]
  61. * @author Devil
  62. * @blog http://gong.gg/
  63. * @version 0.0.1
  64. * @datetime 2017-01-10T15:46:00+0800
  65. */
  66. public function ExcelExport()
  67. {
  68. $where = array();
  69. $data = M('activity_mengbao')->where($where)->order('score desc , id asc')->select();
  70. $title = array(
  71. 'id' => array('col' => 'A', 'name' => 'ID'),
  72. 'name' => array('col' => 'B', 'name' => '名字'),
  73. 'uid' => array('col' => 'C', 'name' => 'uid'),
  74. 'phone' => array('col' => 'D', 'name' => '联系人手机号'),
  75. 'score' => array('col' => 'E', 'name' => '得票数'),
  76. 'is_show' => array('col' => 'F', 'name' => '在线状态(1在线,0下线)')
  77. );
  78. // Excel驱动导出数据
  79. $excel = new \My\Excel(array('filename' => '萌宝排行榜', 'title' => $title, 'data' => $data, 'msg' => L('common_not_data_tips')));
  80. $excel->Export();
  81. }
  82. /**
  83. * [SaveInfo 文章添加/编辑页面]
  84. * @author Devil
  85. * @blog http://gong.gg/
  86. * @version 0.0.1
  87. * @datetime 2016-12-14T21:37:02+0800
  88. */
  89. public function SaveInfo() {
  90. // 文章信息
  91. if (empty($_REQUEST['id'])) {
  92. $data = array();
  93. } else {
  94. $data = $this->table->find(I('id'));
  95. if (empty($data)) {
  96. $data = array('id' => I('id'));
  97. }
  98. }
  99. if ($_REQUEST['map']) {
  100. $map = $_REQUEST['map'];
  101. }
  102. //素材列表
  103. if (I('id')) {
  104. $sourceList = M('activity_mengbao')->where(['id' => array('neq', I('id'))])->field('id as source_id,name as source_name')->select();
  105. }
  106. $this->assign('sourceList', $sourceList);
  107. $this->assign('data', $data);
  108. $this->display('SaveInfo');
  109. }
  110. /**
  111. * [Save 文章添加/编辑]
  112. * @author Devil
  113. * @blog http://gong.gg/
  114. * @version 0.0.1
  115. * @datetime 2016-12-14T21:37:02+0800
  116. */
  117. public function Save() {
  118. // 是否ajax请求
  119. if (!IS_AJAX) {
  120. $this->error(L('common_unauthorized_access'));
  121. }
  122. // 添加
  123. if (empty($_POST['id'])) {
  124. $this->Add();
  125. // 编辑
  126. } else {
  127. $this->Edit();
  128. }
  129. }
  130. /**
  131. * [Add 文章添加]
  132. * @author Devil
  133. * @blog http://gong.gg/
  134. * @version 0.0.1
  135. * @datetime 2016-12-18T16:20:59+0800
  136. */
  137. private function Add() {
  138. $m = $this->table;
  139. $data = array();
  140. // 额外数据处理
  141. // $data['created_at'] = date('Y-m-d H:i:s', time());
  142. // $data['name'] = I('name');
  143. // if ($data['name']) {
  144. // $data['first_string'] = $this->first_string($data['name']);
  145. // }
  146. $data['uid'] = I('uid');
  147. $data['name'] = I('name');
  148. $data['age'] = I('age');
  149. $data['image'] = I('image');
  150. $data['desc'] = I('desc');
  151. $data['phone'] = I('phone');
  152. $data['media_id'] = I('media_id');
  153. $data['media_num'] = I('media_num');
  154. $data['score'] = I('score');
  155. $data['video'] = I('video');
  156. $data['is_show'] = I('is_show');
  157. // 数据添加
  158. if ($m->add($data)) {
  159. $this->ajaxReturn(L('common_operation_add_success'));
  160. } else {
  161. $this->ajaxReturn(L('common_operation_add_error'), -100);
  162. }
  163. }
  164. /**
  165. * [Edit 文章编辑]
  166. * @author Devil
  167. * @blog http://gong.gg/
  168. * @version 0.0.1
  169. * @datetime 2016-12-17T22:13:40+0800
  170. */
  171. private function Edit() {
  172. $m = $this->table;
  173. $source_id = I('id');
  174. $data = array();
  175. // 额外数据处理
  176. // $data['name'] = I('name');
  177. // if ($data['name']) {
  178. // $data['first_string'] = $this->first_string($data['name']);
  179. // }
  180. $data['uid'] = I('uid');
  181. $data['name'] = I('name');
  182. $data['age'] = I('age');
  183. $data['image'] = I('image');
  184. $data['desc'] = I('desc');
  185. $data['phone'] = I('phone');
  186. $data['media_id'] = I('media_id');
  187. $data['media_num'] = I('media_num');
  188. $data['score'] = I('score');
  189. $data['video'] = I('video');
  190. $data['is_show'] = I('is_show');
  191. // 数据更新
  192. if (false !== $m->where(array('id' => I('id')))->save($data)) {
  193. $this->ajaxReturn(L('common_operation_edit_success'));
  194. } else {
  195. // echo M()->getLastsql();die;
  196. $this->ajaxReturn(L('common_operation_edit_error'), -100);
  197. }
  198. }
  199. /**
  200. * [Delete 删除]
  201. * @author Devil
  202. * @blog http://gong.gg/
  203. * @version 0.0.1
  204. * @datetime 2016-12-15T11:03:30+0800
  205. */
  206. public function Delete() {
  207. // 是否ajax请求
  208. if (!IS_AJAX) {
  209. $this->error(L('common_unauthorized_access'));
  210. }
  211. // 删除数据
  212. if (!empty($_POST['id'])) {
  213. // 更新
  214. $id = I('id');
  215. $update = ['id'=>$id,'is_show'=>0];
  216. if ($this->table->save($update)) {
  217. $this->ajaxReturn(L('common_operation_delete_success'));
  218. } else {
  219. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  220. }
  221. } else {
  222. $this->ajaxReturn(L('common_param_error'), -1);
  223. }
  224. }
  225. public function importExcel()
  226. {
  227. $param['title'] = array(
  228. "id" => array('name' => 'id'),
  229. "uid" => array('name' => 'uid'),
  230. "phone" => array('name' => 'phone'),
  231. "name" => array('name' => 'name'),
  232. "age" => array('name' => 'age'),
  233. "desc" => array('name' => 'desc'),
  234. 'score' => array('name' => 'score'),
  235. "image" => array('name' => 'image'),
  236. "video" => array('name' => 'video'),
  237. "media_id" => array('name' => 'media_id'),
  238. "media_num" => array('name' => 'media_num'),
  239. "is_show" => array('name' => 'is_show')
  240. );
  241. $excel = new \My\Excel($param);
  242. $excel_data = $excel->Import($_FILES['excel']['tmp_name']);
  243. $data = array_filter($excel_data);
  244. if(!$data){
  245. $msg = "检查标题是否对应正确";
  246. goto end;
  247. }
  248. $update_data = $insert_data = array();
  249. foreach ($data as $index=>$row) {
  250. $row_num = $index + 2;
  251. if(!$row = array_filter($row)){
  252. $msg = '检查数据行: '.$row_num;
  253. goto end;
  254. }
  255. //存在ID即代表更新
  256. if($row['id']){
  257. if(count($row) > 1){
  258. //查看该素材是否存在,如果存在则更新,不存在则插入
  259. if(M('activity_mengbao')->where(['id'=>$row['id']])->find()){
  260. if(M('activity_mengbao')->save($row) === false){
  261. Writelog( M()->getLastSql(), 'errorsql', 'inserttag');
  262. $msg = '数据行更新失败: ' . $row_num . ' ,请告知技术进行处理';
  263. goto end;
  264. }
  265. }
  266. }else{
  267. $msg = '检查数据行: '.$row_num;
  268. goto end;
  269. }
  270. }else{
  271. //新插入时检查注入所必须字段
  272. $must_fields = array('name','image','phone','uid');
  273. foreach ($must_fields as $field) {
  274. if(!isset($row[$field])){
  275. $msg = '检查数据行: '.$row_num;
  276. goto end;
  277. }
  278. }
  279. // $row['first_string'] = $this->first_string($row['name']);//首字母
  280. $row['created_at'] = date('Y-m-d H:i:s');
  281. if(!M('activity_mengbao')->add($row)){
  282. Writelog( M()->getLastSql(), 'errorsql', 'inserttag');
  283. $msg = '数据行插入失败: ' . $insertID . ' ,请告知技术进行处理';
  284. goto end;
  285. }
  286. $insert_data[] = $row;
  287. }
  288. }
  289. $msg = '导入完成';
  290. goto end;
  291. end:
  292. $responsedata = array("msg" => $msg, "res" => 1, 'data' => []);
  293. echo json_encode($responsedata,JSON_UNESCAPED_UNICODE);
  294. exit;
  295. }
  296. /**
  297. * 批量更新
  298. * @param string $table_name [description]
  299. * @param array $data [description]
  300. * @param string $field [description]
  301. * @return [type] [description]
  302. */
  303. public function batch_update($table_name = '', $data = array(), $field = '') {
  304. if (!$table_name || !$data || !$field) {
  305. return false;
  306. } else {
  307. $sql = 'UPDATE ' . C('DB_PREFIX') . $table_name;
  308. }
  309. $con = array();
  310. $con_sql = array();
  311. $fields = array();
  312. foreach ($data as $key => $value) {
  313. $x = 0;
  314. foreach ($value as $k => $v) {
  315. if ($k != $field && !$con[$x] && $x == 0) {
  316. $con[$x] = " set {$k} = (CASE {$field} ";
  317. } elseif ($k != $field && !$con[$x] && $x > 0) {
  318. $con[$x] = " {$k} = (CASE {$field} ";
  319. }
  320. if ($k != $field) {
  321. $temp = $value[$field];
  322. $con_sql[$x] .= " WHEN '{$temp}' THEN '{$v}' ";
  323. $x++;
  324. }
  325. }
  326. $temp = $value[$field];
  327. if (!in_array($temp, $fields)) {
  328. $fields[] = $temp;
  329. }
  330. }
  331. $num = count($con) - 1;
  332. foreach ($con as $key => $value) {
  333. foreach ($con_sql as $k => $v) {
  334. if ($k == $key && $key < $num) {
  335. $sql .= $value . $v . ' end),';
  336. } elseif ($k == $key && $key == $num) {
  337. $sql .= $value . $v . ' end)';
  338. }
  339. }
  340. }
  341. $str = implode(',', $fields);
  342. $sql .= " where {$field} in({$str})";
  343. $res = M($table_name)->execute($sql);
  344. return $res;
  345. }
  346. /**
  347. * 上传图片文件
  348. * @return [type] [description]
  349. */
  350. public function uploadPics() {
  351. $temp_config['pathFormat'] = C('uploaderSourceImg');
  352. $temp_config['maxSize'] = C('ueditor_config.imageMaxSize');
  353. $temp_config['allowFiles'] = C('ueditor_config.imageAllowFiles');
  354. $up = new \My\Uploader('pics', $temp_config);
  355. $info = $up->getFileInfo();
  356. if ($info['state'] == 'SUCCESS') {
  357. $this->ajaxReturn($info['state'], 200);
  358. } else {
  359. $this->ajaxReturn($info['state'], 400);
  360. }
  361. }
  362. }