api.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. !defined('BASEPATH') && exit('No direct script access allowed');
  3. class Api extends MY_Main_Controller
  4. {
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. $this->load->model('product_model','product');
  9. $this->load->model('type_model','type');
  10. }
  11. // public function hehe(){
  12. // $sql = "CREATE TABLE `product` (
  13. // `id` int(11) NOT NULL AUTO_INCREMENT,
  14. // `type` varchar(255) NOT NULL DEFAULT '',
  15. // `title` varchar(255) NOT NULL DEFAULT '',
  16. // `pid` varchar(255) NOT NULL DEFAULT '',
  17. // `level` tinyint(4) NOT NULL DEFAULT '0',
  18. // `img` text NOT NULL,
  19. // `desc` text NOT NULL,
  20. // `sence` text NOT NULL,
  21. // `addtime` int(10) NOT NULL DEFAULT '0',
  22. // `publishtime` int(10) NOT NULL DEFAULT '0',
  23. // `ord` int(11) NOT NULL DEFAULT '0',
  24. // `status` int(4) NOT NULL DEFAULT '0',
  25. // PRIMARY KEY (`id`)
  26. // ) ENGINE=InnoDB DEFAULT CHARSET=utf8";
  27. // $this->db->query($sql);
  28. // echo $this->db->last_query();
  29. // }
  30. public function getProducts()
  31. {
  32. $types = $this->type->all();
  33. $products = $this->product->all();
  34. foreach($products as &$product) {
  35. $t = $product['type'];
  36. $t_arr = explode(',', $t);
  37. $product['type'] = implode(', ', $t_arr);
  38. $product['img'] = json_decode($product['img'], true);
  39. }
  40. $item['types'] = $types;
  41. $item['products'] = $products;
  42. $data = $item;
  43. $this->echoJson($data);
  44. }
  45. public function getProductDetail()
  46. {
  47. $id = $this->input->get('id', true);
  48. $data = $this->product->get($id);
  49. $type = $this->type->get($data['type']);
  50. $data['type_name'] = $type['name'];
  51. $data['img'] = json_decode($data['img'], true);
  52. $this->echoJson($data);
  53. }
  54. private function fetchWebSetting($name){
  55. $item = $this->web_setting->getValueByName($name);
  56. $data = json_decode($item[0]['value'] , true);
  57. return $data;
  58. }
  59. private function fetchWebSetting2($name){
  60. $item = $this->web_setting->getValueByName($name);
  61. $data = $item[0]['value'];
  62. return $data;
  63. }
  64. private function echoJson($data){
  65. if($data){
  66. $code = 200;
  67. $msg = 'success';
  68. }
  69. else{
  70. $code = 500;
  71. $msg = 'fails';
  72. }
  73. $res['code'] = $code;
  74. $res['msg'] = $msg;
  75. $res['data'] = $data;
  76. header('Content-type:text/json');
  77. echo json_encode($res);
  78. }
  79. }
  80. ?>