Goodimglibrary.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 = "https://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. $title = trim($title);
  39. $this->db->like('g.title', $title);
  40. }
  41. if($zh){
  42. $zh = trim($zh);
  43. $this->db->like('g.zh', $zh);
  44. }
  45. $this->db->order_by('g.id', 'asc');
  46. // 分页 (limit, offset)
  47. $this->db->limit($perpage, $start);
  48. $query = $this->db->get();
  49. $list = $query->result_array();
  50. $info_list = [];
  51. foreach($list as $k => $v){
  52. $info_list[$k]['id'] = $v['id'];
  53. $info_list[$k]['sku'] = $v['sku'];
  54. $info_list[$k]['title'] = $v['title'];
  55. $info_list[$k]['zh'] = $v['zh'];
  56. //$info_list[$k]['jm'] = $v['jm'];
  57. $info_list[$k]['imgs'] = $this->transimg($v['source_cont']);
  58. }
  59. $this->db->from('crowd_goods');
  60. if($title){
  61. $title = trim($title);
  62. $this->db->like('title', $title);
  63. }
  64. if($zh){
  65. $zh = trim($zh);
  66. $this->db->like('zh', $zh);
  67. }
  68. $total = $this->db->count_all_results();
  69. $pagenum = ceil($total/$perpage);
  70. $over = $total-($start+$perpage);
  71. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  72. echo json_encode($rows);exit;
  73. }else{
  74. $this->_Template('goodimglibrary',$this->data);
  75. }
  76. }
  77. private function transimg($source_cont){
  78. $str = ' ';
  79. if(!empty($source_cont)){
  80. $imgs = json_decode($source_cont,true);
  81. foreach($imgs as $k => $v){
  82. $type = $this->_checkVideo($v);
  83. if($type == "video"){
  84. $str.= '<video controls="true" width="100" src="'.$this->show_url.$v.'" alt="video image" style="margin-right:3px;"></video>';
  85. }else{
  86. $str .= '<img width="100px" src="'.$this->show_url.$v.'" alt="image" style="margin-right:3px;">';
  87. }
  88. }
  89. }
  90. return$str;
  91. }
  92. private function _editData($arg_array){
  93. if($this->input->method() == 'post'){
  94. $id = $this->input->post('id',true);
  95. $img = $this->input->post('img',true);
  96. if(empty($img)){
  97. $img = [];
  98. }
  99. foreach($img as $k => $v){
  100. $img[$k] = str_replace($this->show_url, '', $v);
  101. }
  102. $info = $this->db->get_where('crowd_goods',array('id'=>$id))->row_array();
  103. $images = $this->goodimgs->find("goods_id = ".$id);
  104. if(empty($images)){
  105. if($this->goodimgs->insert(array('goods_id'=>$id,'features'=>$info['features'],'source_cont'=>json_encode($img)))){
  106. echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
  107. }else{
  108. echo json_encode(array('msg'=>'修改失败,请重试1','success'=>false));exit;
  109. }
  110. }else{
  111. if($this->goodimgs->save(array('features'=>$info['features'],'source_cont'=>json_encode($img)),$images['id'])){
  112. echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
  113. }else{
  114. echo json_encode(array('msg'=>'修改失败,请重试2','success'=>false));exit;
  115. }
  116. }
  117. }
  118. if($this->input->method() == 'get'){
  119. $id = $arg_array[0];
  120. $goods = $this->db->get_where('crowd_goods',array('id'=>$id))->row_array();
  121. $this->db->where('goods_id',$id);
  122. $images = $this->db->get('crowd_goodimgs')->result_array();
  123. if(empty($images)){
  124. $goods['images'] = [];
  125. }else{
  126. $imgs = json_decode($images[0]['source_cont'],true);
  127. $final_imgs = [];
  128. foreach($imgs as $k => $v){
  129. $final_imgs[$k]['url'] = $this->show_url.$v;
  130. $final_imgs[$k]['type'] = $this->_checkVideo($v);
  131. }
  132. $goods['images'] = $final_imgs;
  133. }
  134. $this->data['goods'] = $goods;
  135. $this->_Template('goodimglibrary_edit',$this->data);
  136. }
  137. }
  138. //根据后缀判断是否是视频
  139. private function _checkVideo($str){
  140. $hz_arr = explode(".",$str);
  141. $hz = end($hz_arr);
  142. if(in_array($hz,['mp4','flv','rmvb','avi','wmv','mov','rm','mkv'])){
  143. return "video";
  144. }
  145. return "img";
  146. }
  147. }