MengbaoController.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. );
  77. // Excel驱动导出数据
  78. $excel = new \My\Excel(array('filename' => '萌宝排行榜', 'title' => $title, 'data' => $data, 'msg' => L('common_not_data_tips')));
  79. $excel->Export();
  80. }
  81. /**
  82. * [SaveInfo 文章添加/编辑页面]
  83. * @author Devil
  84. * @blog http://gong.gg/
  85. * @version 0.0.1
  86. * @datetime 2016-12-14T21:37:02+0800
  87. */
  88. public function SaveInfo() {
  89. // 文章信息
  90. if (empty($_REQUEST['id'])) {
  91. $data = array();
  92. } else {
  93. $data = $this->table->find(I('id'));
  94. if (empty($data)) {
  95. $data = array('id' => I('id'));
  96. }
  97. }
  98. if ($_REQUEST['map']) {
  99. $map = $_REQUEST['map'];
  100. }
  101. //素材列表
  102. if (I('id')) {
  103. $sourceList = M('activity_mengbao')->where(['id' => array('neq', I('id'))])->field('id as source_id,name as source_name')->select();
  104. }
  105. $this->assign('sourceList', $sourceList);
  106. $this->assign('data', $data);
  107. $this->display('SaveInfo');
  108. }
  109. /**
  110. * [Save 文章添加/编辑]
  111. * @author Devil
  112. * @blog http://gong.gg/
  113. * @version 0.0.1
  114. * @datetime 2016-12-14T21:37:02+0800
  115. */
  116. public function Save() {
  117. // 是否ajax请求
  118. if (!IS_AJAX) {
  119. $this->error(L('common_unauthorized_access'));
  120. }
  121. // 添加
  122. if (empty($_POST['id'])) {
  123. $this->Add();
  124. // 编辑
  125. } else {
  126. $this->Edit();
  127. }
  128. }
  129. /**
  130. * [Add 文章添加]
  131. * @author Devil
  132. * @blog http://gong.gg/
  133. * @version 0.0.1
  134. * @datetime 2016-12-18T16:20:59+0800
  135. */
  136. private function Add() {
  137. $m = $this->table;
  138. $data = array();
  139. // 额外数据处理
  140. // $data['created_at'] = date('Y-m-d H:i:s', time());
  141. // $data['name'] = I('name');
  142. // if ($data['name']) {
  143. // $data['first_string'] = $this->first_string($data['name']);
  144. // }
  145. $data['uid'] = I('uid');
  146. $data['name'] = I('name');
  147. $data['age'] = I('age');
  148. $data['image'] = I('image');
  149. $data['desc'] = I('desc');
  150. $data['phone'] = I('phone');
  151. $data['media_id'] = I('media_id');
  152. $data['media_num'] = I('media_num');
  153. $data['score'] = I('score');
  154. $data['video'] = I('video');
  155. // 数据添加
  156. if ($m->add($data)) {
  157. $this->ajaxReturn(L('common_operation_add_success'));
  158. } else {
  159. $this->ajaxReturn(L('common_operation_add_error'), -100);
  160. }
  161. }
  162. /**
  163. * [Edit 文章编辑]
  164. * @author Devil
  165. * @blog http://gong.gg/
  166. * @version 0.0.1
  167. * @datetime 2016-12-17T22:13:40+0800
  168. */
  169. private function Edit() {
  170. $m = $this->table;
  171. $source_id = I('id');
  172. $data = array();
  173. // 额外数据处理
  174. // $data['name'] = I('name');
  175. // if ($data['name']) {
  176. // $data['first_string'] = $this->first_string($data['name']);
  177. // }
  178. $data['uid'] = I('uid');
  179. $data['name'] = I('name');
  180. $data['age'] = I('age');
  181. $data['image'] = I('image');
  182. $data['desc'] = I('desc');
  183. $data['phone'] = I('phone');
  184. $data['media_id'] = I('media_id');
  185. $data['media_num'] = I('media_num');
  186. $data['score'] = I('score');
  187. $data['video'] = I('video');
  188. // 数据更新
  189. if (false !== $m->where(array('id' => I('id')))->save($data)) {
  190. $this->ajaxReturn(L('common_operation_edit_success'));
  191. } else {
  192. // echo M()->getLastsql();die;
  193. $this->ajaxReturn(L('common_operation_edit_error'), -100);
  194. }
  195. }
  196. /**
  197. * [Delete 删除]
  198. * @author Devil
  199. * @blog http://gong.gg/
  200. * @version 0.0.1
  201. * @datetime 2016-12-15T11:03:30+0800
  202. */
  203. public function Delete() {
  204. // 是否ajax请求
  205. if (!IS_AJAX) {
  206. $this->error(L('common_unauthorized_access'));
  207. }
  208. // 删除数据
  209. if (!empty($_POST['id'])) {
  210. // 更新
  211. if ($this->table->delete(I('id'))) {
  212. $this->ajaxReturn(L('common_operation_delete_success'));
  213. } else {
  214. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  215. }
  216. } else {
  217. $this->ajaxReturn(L('common_param_error'), -1);
  218. }
  219. }
  220. public function importExcel()
  221. {
  222. $param['title'] = array(
  223. "id" => array('name' => 'id'),
  224. "uid" => array('name' => 'uid'),
  225. "phone" => array('name' => 'phone'),
  226. "name" => array('name' => 'name'),
  227. "age" => array('name' => 'age'),
  228. "desc" => array('name' => 'desc'),
  229. 'score' => array('namr' => 'score'),
  230. "image" => array('name' => 'image'),
  231. "video" => array('name' => 'video'),
  232. "media_id" => array('name' => 'media_id'),
  233. "media_num" => array('name' => 'media_num'),
  234. );
  235. $excel = new \My\Excel($param);
  236. $excel_data = $excel->Import($_FILES['excel']['tmp_name']);
  237. $data = array_filter($excel_data);
  238. if(!$data){
  239. $msg = "检查标题是否对应正确";
  240. goto end;
  241. }
  242. $update_data = $insert_data = array();
  243. foreach ($data as $index=>$row) {
  244. $row_num = $index + 2;
  245. if(!$row = array_filter($row)){
  246. $msg = '检查数据行: '.$row_num;
  247. goto end;
  248. }
  249. //存在ID即代表更新
  250. if($row['id']){
  251. if(count($row) > 1){
  252. //查看该素材是否存在,如果存在则更新,不存在则插入
  253. if(M('activity_mengbao')->where(['id'=>$row['id']])->find()){
  254. if(M('activity_mengbao')->save($row) === false){
  255. Writelog( M()->getLastSql(), 'errorsql', 'inserttag');
  256. $msg = '数据行更新失败: ' . $row_num . ' ,请告知技术进行处理';
  257. goto end;
  258. }
  259. }
  260. }else{
  261. $msg = '检查数据行: '.$row_num;
  262. goto end;
  263. }
  264. }else{
  265. //新插入时检查注入所必须字段
  266. $must_fields = array('name','image','phone','uid');
  267. foreach ($must_fields as $field) {
  268. if(!isset($row[$field])){
  269. $msg = '检查数据行: '.$row_num;
  270. goto end;
  271. }
  272. }
  273. // $row['first_string'] = $this->first_string($row['name']);//首字母
  274. $row['created_at'] = date('Y-m-d H:i:s');
  275. if(!M('activity_mengbao')->add($row)){
  276. Writelog( M()->getLastSql(), 'errorsql', 'inserttag');
  277. $msg = '数据行插入失败: ' . $insertID . ' ,请告知技术进行处理';
  278. goto end;
  279. }
  280. $insert_data[] = $row;
  281. }
  282. }
  283. $msg = '导入完成';
  284. goto end;
  285. end:
  286. $responsedata = array("msg" => $msg, "res" => 1, 'data' => []);
  287. echo json_encode($responsedata,JSON_UNESCAPED_UNICODE);
  288. exit;
  289. }
  290. /**
  291. * 批量更新
  292. * @param string $table_name [description]
  293. * @param array $data [description]
  294. * @param string $field [description]
  295. * @return [type] [description]
  296. */
  297. public function batch_update($table_name = '', $data = array(), $field = '') {
  298. if (!$table_name || !$data || !$field) {
  299. return false;
  300. } else {
  301. $sql = 'UPDATE ' . C('DB_PREFIX') . $table_name;
  302. }
  303. $con = array();
  304. $con_sql = array();
  305. $fields = array();
  306. foreach ($data as $key => $value) {
  307. $x = 0;
  308. foreach ($value as $k => $v) {
  309. if ($k != $field && !$con[$x] && $x == 0) {
  310. $con[$x] = " set {$k} = (CASE {$field} ";
  311. } elseif ($k != $field && !$con[$x] && $x > 0) {
  312. $con[$x] = " {$k} = (CASE {$field} ";
  313. }
  314. if ($k != $field) {
  315. $temp = $value[$field];
  316. $con_sql[$x] .= " WHEN '{$temp}' THEN '{$v}' ";
  317. $x++;
  318. }
  319. }
  320. $temp = $value[$field];
  321. if (!in_array($temp, $fields)) {
  322. $fields[] = $temp;
  323. }
  324. }
  325. $num = count($con) - 1;
  326. foreach ($con as $key => $value) {
  327. foreach ($con_sql as $k => $v) {
  328. if ($k == $key && $key < $num) {
  329. $sql .= $value . $v . ' end),';
  330. } elseif ($k == $key && $key == $num) {
  331. $sql .= $value . $v . ' end)';
  332. }
  333. }
  334. }
  335. $str = implode(',', $fields);
  336. $sql .= " where {$field} in({$str})";
  337. $res = M($table_name)->execute($sql);
  338. return $res;
  339. }
  340. /**
  341. * 上传图片文件
  342. * @return [type] [description]
  343. */
  344. public function uploadPics() {
  345. $temp_config['pathFormat'] = C('uploaderSourceImg');
  346. $temp_config['maxSize'] = C('ueditor_config.imageMaxSize');
  347. $temp_config['allowFiles'] = C('ueditor_config.imageAllowFiles');
  348. $up = new \My\Uploader('pics', $temp_config);
  349. $info = $up->getFileInfo();
  350. if ($info['state'] == 'SUCCESS') {
  351. $this->ajaxReturn($info['state'], 200);
  352. } else {
  353. $this->ajaxReturn($info['state'], 400);
  354. }
  355. }
  356. }