Goodimglibrary.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class Goodimglibrary extends Start_Controller {
  3. public function __construct(){
  4. parent::__construct();
  5. $this->load->library('session');
  6. $this->load->_model('Model_goodimgs','goodimgs');
  7. }
  8. private $show_url = "http://lyerposs.wepolicy.cn";
  9. public function _remap($arg,$arg_array)
  10. {
  11. if($arg=="detail"){
  12. //$this->_detial($arg_array);
  13. }elseif($arg=="edit"){
  14. $this->_editData($arg_array);
  15. }else{
  16. $this->_index();
  17. }
  18. }
  19. private function _index(){
  20. if($this->input->method() == 'post'){
  21. $page = $this->input->post('page',true);
  22. $perpage = $this->input->post('perpage',true);
  23. $title = $this->input->post('title',true);
  24. $zh= $this->input->post('zh',true);
  25. if(empty($page))
  26. {
  27. $start = 0;
  28. $perpage = 1;
  29. }
  30. else
  31. {
  32. $start = ($page - 1)*$perpage;
  33. }
  34. $this->db->select('g.id, g.features, g.title, g.sku, g.zh, g.jm, gi.id as img_id, gi.source_cont');
  35. $this->db->from('crowd_goods as g');
  36. $this->db->join('crowd_goodimgs as gi', 'g.id = gi.goods_id', 'left');
  37. if($title){
  38. $this->db->like('g.title', $title);
  39. }
  40. if($zh){
  41. $this->db->like('g.zh', $zh);
  42. }
  43. $this->db->order_by('g.id', 'asc');
  44. // 分页 (limit, offset)
  45. $this->db->limit($perpage, $start);
  46. $query = $this->db->get();
  47. $list = $query->result_array();
  48. $info_list = [];
  49. foreach($list as $k => $v){
  50. $info_list[$k]['id'] = $v['id'];
  51. $info_list[$k]['sku'] = $v['sku'];
  52. $info_list[$k]['title'] = $v['title'];
  53. $info_list[$k]['zh'] = $v['zh'];
  54. //$info_list[$k]['jm'] = $v['jm'];
  55. $info_list[$k]['imgs'] = $this->transimg($v['source_cont']);
  56. }
  57. $this->db->from('crowd_goods');
  58. if($title){
  59. $this->db->like('title', $title);
  60. }
  61. if($zh){
  62. $this->db->like('zh', $zh);
  63. }
  64. $total = $this->db->count_all_results();
  65. $pagenum = ceil($total/$perpage);
  66. $over = $total-($start+$perpage);
  67. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  68. echo json_encode($rows);exit;
  69. }else{
  70. $this->_Template('goodimglibrary',$this->data);
  71. }
  72. }
  73. private function transimg($source_cont){
  74. $str = ' ';
  75. if(!empty($source_cont)){
  76. $imgs = json_decode($source_cont,true);
  77. foreach($imgs as $k => $v){
  78. $type = $this->_checkVideo($v);
  79. if($type == "video"){
  80. $str.= '<video controls="true" width="100" src="'.$this->show_url.$v.'" alt="video image" style="margin-right:3px;"></video>';
  81. }else{
  82. $str .= '<img width="100px" src="'.$this->show_url.$v.'" alt="image" style="margin-right:3px;">';
  83. }
  84. }
  85. }
  86. return$str;
  87. }
  88. private function _editData($arg_array){
  89. if($this->input->method() == 'post'){
  90. $id = $this->input->post('id',true);
  91. $img = $this->input->post('img',true);
  92. if(empty($img)){
  93. $img = [];
  94. }
  95. foreach($img as $k => $v){
  96. $img[$k] = str_replace('http://lyerposs.wepolicy.cn', '', $v);
  97. }
  98. $info = $this->db->get_where('crowd_goods',array('id'=>$id))->row_array();
  99. $images = $this->goodimgs->find("goods_id = ".$id);
  100. if(empty($images)){
  101. if($this->goodimgs->insert(array('goods_id'=>$id,'features'=>$info['features'],'source_cont'=>json_encode($img)))){
  102. echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
  103. }else{
  104. echo json_encode(array('msg'=>'修改失败,请重试1','success'=>false));exit;
  105. }
  106. }else{
  107. if($this->goodimgs->save(array('features'=>$info['features'],'source_cont'=>json_encode($img)),$images['id'])){
  108. echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
  109. }else{
  110. echo json_encode(array('msg'=>'修改失败,请重试2','success'=>false));exit;
  111. }
  112. }
  113. }
  114. if($this->input->method() == 'get'){
  115. $id = $arg_array[0];
  116. $goods = $this->db->get_where('crowd_goods',array('id'=>$id))->row_array();
  117. $this->db->where('goods_id',$id);
  118. $images = $this->db->get('crowd_goodimgs')->result_array();
  119. if(empty($images)){
  120. $goods['images'] = [];
  121. }else{
  122. $imgs = json_decode($images[0]['source_cont'],true);
  123. $final_imgs = [];
  124. foreach($imgs as $k => $v){
  125. $final_imgs[$k]['url'] = $this->show_url.$v;
  126. $final_imgs[$k]['type'] = $this->_checkVideo($v);
  127. }
  128. $goods['images'] = $final_imgs;
  129. }
  130. $this->data['goods'] = $goods;
  131. $this->_Template('goodimglibrary_edit',$this->data);
  132. }
  133. }
  134. //根据后缀判断是否是视频
  135. private function _checkVideo($str){
  136. $hz_arr = explode(".",$str);
  137. $hz = end($hz_arr);
  138. if(in_array($hz,['mp4','flv','rmvb','avi','wmv','mov','rm','mkv'])){
  139. return "video";
  140. }
  141. return "img";
  142. }
  143. }