Goodimglibrary.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. $this->load->_model('Model_goods','goods');
  8. }
  9. private $show_url = "https://lyerposs.wepolicy.cn";
  10. public function _remap($arg,$arg_array)
  11. {
  12. if($arg=="detail"){
  13. //$this->_detial($arg_array);
  14. }elseif($arg=="edit"){
  15. $this->_editData($arg_array);
  16. }else{
  17. $this->_index();
  18. }
  19. }
  20. private function _index(){
  21. if($this->input->method() == 'post'){
  22. $page = $this->input->post('page',true);
  23. $perpage = $this->input->post('perpage',true);
  24. $title = $this->input->post('title',true);
  25. $zh= $this->input->post('zh',true);
  26. $sku = $this->input->post('sku',true);
  27. $category = $this->input->post('category',true);
  28. $category = trim($category);
  29. $grade = $this->input->post('grade',true);
  30. $grade = trim($grade);
  31. $color = $this->input->post('color',true);
  32. $color = trim($color);
  33. $lowe = $this->input->post('lowe',true);
  34. $lowe = trim($lowe);
  35. $lacetype = $this->input->post('lacetype',true);
  36. $lacetype = trim($lacetype);
  37. $lacecolor = $this->input->post('lacecolor',true);
  38. $lacecolor = trim($lacecolor);
  39. $size = $this->input->post('size',true);
  40. $size = trim($size);
  41. $density = $this->input->post('density',true);
  42. $density = trim($density);
  43. $haircap = $this->input->post('haircap',true);
  44. $haircap = trim($haircap);
  45. $hairnumber = $this->input->post('hairnumber',true);
  46. $hairnumber = trim($hairnumber);
  47. if(empty($page))
  48. {
  49. $start = 0;
  50. $perpage = 1;
  51. }
  52. else
  53. {
  54. $start = ($page - 1)*$perpage;
  55. }
  56. $this->db->select('g.id, g.features, g.title, g.sku, g.zh, g.jm,g.has_source,g.update_time, gi.id as img_id, gi.source_cont');
  57. $this->db->from('crowd_goods as g');
  58. $this->db->join('crowd_goodimgs as gi', 'g.id = gi.goods_id', 'left');
  59. if($title){
  60. $title = trim($title);
  61. $this->db->like('g.title', $title);
  62. }
  63. if($zh){
  64. $zh = trim($zh);
  65. $this->db->like('g.zh', $zh);
  66. }
  67. if($sku){
  68. $sku = trim($sku);
  69. $this->db->like('g.sku', $sku);
  70. }
  71. if($category){
  72. $category = trim($category);
  73. $this->db->like('g.features', $category);
  74. }
  75. if($grade){
  76. $grade = trim($grade);
  77. $this->db->like('g.features', $grade);
  78. }
  79. if($color){
  80. $color = trim($color);
  81. $this->db->like('g.features', $color);
  82. }
  83. if($lowe){
  84. $lowe = trim($lowe);
  85. $this->db->like('g.features', $lowe);
  86. }
  87. if($lacetype){
  88. $lacetype = trim($lacetype);
  89. $this->db->like('g.features', $lacetype);
  90. }
  91. if($lacecolor){
  92. $lacecolor = trim($lacecolor);
  93. $this->db->like('g.features', $lacecolor);
  94. }
  95. if($size){
  96. $size = trim($size);
  97. $this->db->like('g.features', $size);
  98. }
  99. if($density){
  100. $density = trim($density);
  101. $this->db->like('g.features', $density);
  102. }
  103. if($haircap){
  104. $haircap = trim($haircap);
  105. $this->db->like('g.features', $haircap);
  106. }
  107. if($hairnumber){
  108. $hairnumber = trim($hairnumber);
  109. $this->db->like('g.features', $hairnumber);
  110. }
  111. $this->db->order_by('g.id', 'asc');
  112. $this->db->order_by('g.has_source', 'desc');
  113. // 分页 (limit, offset)
  114. $this->db->limit($perpage, $start);
  115. $query = $this->db->get();
  116. $list = $query->result_array();
  117. $info_list = [];
  118. foreach($list as $k => $v){
  119. $info_list[$k]['id'] = $v['id'];
  120. $info_list[$k]['sku'] = $v['sku'];
  121. $info_list[$k]['title'] = $v['title'];
  122. $info_list[$k]['zh'] = $v['zh'];
  123. $info_list[$k]['update_time'] = empty($v['update_time'])? '' : date('Y-m-d H:i:s',$v['update_time']);
  124. $info_list[$k]['imgs'] = $this->transimg($v['source_cont']);
  125. }
  126. $this->db->from('crowd_goods');
  127. if($title){
  128. $title = trim($title);
  129. $this->db->like('title', $title);
  130. }
  131. if($zh){
  132. $zh = trim($zh);
  133. $this->db->like('zh', $zh);
  134. }
  135. if($sku){
  136. $sku = trim($sku);
  137. $this->db->like('sku', $sku);
  138. }
  139. if($category){
  140. $category = trim($category);
  141. $this->db->like('features', $category);
  142. }
  143. if($grade){
  144. $grade = trim($grade);
  145. $this->db->like('features', $grade);
  146. }
  147. if($color){
  148. $color = trim($color);
  149. $this->db->like('features', $color);
  150. }
  151. if($lowe){
  152. $lowe = trim($lowe);
  153. $this->db->like('features', $lowe);
  154. }
  155. if($lacetype){
  156. $lacetype = trim($lacetype);
  157. $this->db->like('features', $lacetype);
  158. }
  159. if($lacecolor){
  160. $lacecolor = trim($lacecolor);
  161. $this->db->like('features', $lacecolor);
  162. }
  163. if($size){
  164. $size = trim($size);
  165. $this->db->like('features', $size);
  166. }
  167. if($density){
  168. $density = trim($density);
  169. $this->db->like('features', $density);
  170. }
  171. if($haircap){
  172. $haircap = trim($haircap);
  173. $this->db->like('features', $haircap);
  174. }
  175. if($hairnumber){
  176. $hairnumber = trim($hairnumber);
  177. $this->db->like('features', $hairnumber);
  178. }
  179. $total = $this->db->count_all_results();
  180. $pagenum = ceil($total/$perpage);
  181. $over = $total-($start+$perpage);
  182. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  183. echo json_encode($rows);exit;
  184. }else{
  185. $this->_Template('goodimglibrary',$this->data);
  186. }
  187. }
  188. private function transimg($source_cont){
  189. $str = ' ';
  190. if(!empty($source_cont)){
  191. $imgs = json_decode($source_cont,true);
  192. foreach($imgs as $k => $v){
  193. $type = $this->_checkVideo($v);
  194. $url = $this->_transingurl($v);
  195. if($type == "video"){
  196. $str.= '<video controls="true" width="100" src="'.$url.'" alt="video image" style="margin-right:3px;"></video>';
  197. }else{
  198. $str .= '<img width="100px" src="'.$url.'" alt="image" style="margin-right:3px;">';
  199. }
  200. }
  201. }
  202. return$str;
  203. }
  204. private function _editData($arg_array){
  205. if($this->input->method() == 'post'){
  206. $id = $this->input->post('id',true);
  207. $img = $this->input->post('img',true);
  208. if(empty($img)){
  209. $img = [];
  210. }
  211. foreach($img as $k => $v){
  212. $img[$k] = str_replace($this->show_url, '', $v);
  213. }
  214. $info = $this->db->get_where('crowd_goods',array('id'=>$id))->row_array();
  215. $images = $this->goodimgs->find("goods_id = ".$id);
  216. $has_source = empty($img)?0:5;
  217. $update_time = time();
  218. if(empty($images)){
  219. if($this->goodimgs->insert(array('goods_id'=>$id,'features'=>$info['features'],'source_cont'=>json_encode($img,JSON_UNESCAPED_SLASHES)))){
  220. $this->goods->save(array('has_source'=>$has_source,'update_time'=>$update_time),$id);
  221. echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
  222. }else{
  223. echo json_encode(array('msg'=>'修改失败,请重试1','success'=>false));exit;
  224. }
  225. }else{
  226. if($this->goodimgs->save(array('features'=>$info['features'],'source_cont'=>json_encode($img,JSON_UNESCAPED_SLASHES)),$images['id'])){
  227. $this->goods->save(array('has_source'=>$has_source,'update_time'=>$update_time),$id);
  228. echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
  229. }else{
  230. echo json_encode(array('msg'=>'修改失败,请重试2','success'=>false));exit;
  231. }
  232. }
  233. }
  234. if($this->input->method() == 'get'){
  235. $id = $arg_array[0];
  236. $goods = $this->db->get_where('crowd_goods',array('id'=>$id))->row_array();
  237. $this->db->where('goods_id',$id);
  238. $images = $this->db->get('crowd_goodimgs')->result_array();
  239. if(empty($images)){
  240. $goods['images'] = [];
  241. }else{
  242. $imgs = json_decode($images[0]['source_cont'],true);
  243. $final_imgs = [];
  244. foreach($imgs as $k => $v){
  245. $final_imgs[$k]['url'] = $this->_transingurl($v);
  246. $final_imgs[$k]['type'] = $this->_checkVideo($v);
  247. }
  248. $goods['images'] = $final_imgs;
  249. }
  250. $this->data['goods'] = $goods;
  251. $this->_Template('goodimglibrary_edit',$this->data);
  252. }
  253. }
  254. //根据后缀判断是否是视频
  255. private function _checkVideo($str){
  256. $hz_arr = explode(".",$str);
  257. $hz = end($hz_arr);
  258. if(in_array($hz,['mp4','flv','rmvb','avi','wmv','mov','rm','mkv'])){
  259. return "video";
  260. }
  261. return "img";
  262. }
  263. private function _transingurl($url){
  264. $url_arr = explode('/', $url);
  265. $filename = array_pop($url_arr);
  266. $filename = rawurlencode($filename);
  267. $url_arr[] = $filename;
  268. $final_url = implode('/', $url_arr);
  269. $final_url = $this->show_url.$final_url;
  270. return $final_url;
  271. }
  272. }