PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/mall/api/goods.php

https://bitbucket.org/huanteng/touyou
PHP | 191 lines | 145 code | 13 blank | 33 comment | 17 complexity | e2e5547d6887add67e68ed2ebbf8f37c MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * ECSHOP 获取商品信息
  4. * ============================================================================
  5. * * 版权所有 2005-2012 上海商派网络科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.ecshop.com;
  7. * ----------------------------------------------------------------------------
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
  9. * 使用;不允许对程序代码以任何形式任何目的的再发布。
  10. * ============================================================================
  11. * $Author: liubo $
  12. * $Id: goods.php 17217 2011-01-19 06:29:08Z liubo $
  13. */
  14. define('IN_ECS', true);
  15. require('./init.php');
  16. require_once(ROOT_PATH . 'includes/cls_json.php');
  17. $json = new JSON;
  18. $hash_code = $db->getOne("SELECT `value` FROM " . $ecs->table('shop_config') . " WHERE `code`='hash_code'", true);
  19. $action = isset($_REQUEST['action'])? $_REQUEST['action']:'';
  20. if (empty($_REQUEST['verify']) || empty($_REQUEST['auth']) || empty($_REQUEST['action']))
  21. {
  22. $results = array('result'=>'false', 'data'=>'缺少必要的参数');
  23. exit($json->encode($results));
  24. }
  25. if ($_REQUEST['verify'] != md5($hash_code.$_REQUEST['action'].$_REQUEST['auth']))
  26. {
  27. $results = array('result'=>'false', 'data'=>'数据来源不合法,请返回');
  28. exit($json->encode($results));
  29. }
  30. parse_str(passport_decrypt($_REQUEST['auth'], $hash_code), $data);
  31. switch ($action)
  32. {
  33. case 'get_goods_info':
  34. {
  35. $shop_id = isset($data['shop_id'])? intval($data['shop_id']):0;
  36. $record_number = isset($data['record_number'])? intval($data['record_number']):20;
  37. $page_number = isset($data['page_number'])? intval($data['page_number']):0;
  38. $limit = ' LIMIT ' . ($record_number * $page_number) . ', ' . ($record_number+1);
  39. $sql = "SELECT `goods_id`, `goods_name`, `goods_number`, `shop_price`, `keywords`, `goods_brief`, `goods_thumb`, `goods_img`, `last_update` FROM " . $ecs->table('goods') . " WHERE `is_delete`='0' ORDER BY `goods_id` ASC $limit ";
  40. $results = array('result' => 'false', 'next' => 'false', 'data' => array());
  41. $query = $db->query($sql);
  42. $record_count = 0;
  43. while ($goods = $db->fetch_array($query))
  44. {
  45. $goods['goods_thumb'] = (!empty($goods['goods_thumb']))? 'http://' . $_SERVER['SERVER_NAME'] . '/' . $goods['goods_thumb']:'';
  46. $goods['goods_img'] = (!empty($goods['goods_img']))? 'http://' . $_SERVER['SERVER_NAME'] . '/' . $goods['goods_img']:'';
  47. $results['data'][] = $goods;
  48. $record_count++;
  49. }
  50. if ($record_count > 0)
  51. {
  52. $results['result'] = 'true';
  53. }
  54. if ($record_count > $record_number)
  55. {
  56. array_pop($results['data']);
  57. $results['next'] = 'true';
  58. }
  59. exit($json->encode($results));
  60. break;
  61. }
  62. case 'get_shop_info':
  63. {
  64. $results = array('result' => 'true', 'data' => array());
  65. $sql = "SELECT `value` FROM " . $ecs->table('shop_config') . " WHERE code='shop_name'";
  66. $shop_name = $db->getOne($sql);
  67. $sql = "SELECT `value` FROM " . $ecs->table('shop_config') . " WHERE code='currency_format'";
  68. $currency_format = $db->getOne($sql);
  69. $sql = "SELECT r.region_name, sc.value FROM " . $ecs->table('region') . " AS r INNER JOIN " . $ecs->table('shop_config') . " AS sc ON r.`region_id`=sc.`value` WHERE sc.`code`='shop_country' OR sc.`code`='shop_province' OR sc.`code`='shop_city' ORDER BY sc.`id` ASC";
  70. $shop_region = $db->getAll($sql);
  71. $results['data'] = array
  72. (
  73. 'shop_name' => $shop_name,
  74. 'domain' => 'http://' . $_SERVER['SERVER_NAME'] . '/',
  75. 'shop_region' => $shop_region[0]['region_name'] . ' ' . $shop_region[1]['region_name'] . ' ' . $shop_region[2]['region_name'],
  76. 'currency_format' => $currency_format
  77. );
  78. exit($json->encode($results));
  79. break;
  80. }
  81. case 'get_shipping':
  82. {
  83. $results = array('result' => 'false', 'data' => array());
  84. $sql = "SELECT `shipping_id`, `shipping_name`, `insure` FROM " . $ecs->table('shipping');
  85. $result = $db->getAll($sql);
  86. if (!empty($result))
  87. {
  88. $results['result'] = 'true';
  89. $results['data'] = $result;
  90. }
  91. exit($json->encode($results));
  92. break;
  93. }
  94. case 'get_goods_attribute':
  95. {
  96. $results = array('result' => 'false', 'data' => array());
  97. $goods_id = isset($data['goods_id'])? intval($data['goods_id']):0;
  98. if (!empty($goods_id))
  99. {
  100. $sql = "SELECT t2.attr_name, t1.attr_value FROM " . $ecs->table('goods_attr') . " AS t1 LEFT JOIN " . $ecs->table('attribute') . " AS t2 ON t1.attr_id=t2.attr_id WHERE t1.goods_id='$goods_id'";
  101. $result = $db->getAll($sql);
  102. if (!empty($result))
  103. {
  104. $results['result'] = 'true';
  105. $results['data'] = $result;
  106. }
  107. }
  108. else
  109. {
  110. $results = array('result'=>'false', 'data'=>'缺少商品ID,无法获取其属性');
  111. }
  112. exit($json->encode($results));
  113. break;
  114. }
  115. default:
  116. {
  117. $results = array('result'=>'false', 'data'=>'缺少动作');
  118. exit(json_encode($results));
  119. break;
  120. }
  121. }
  122. /**
  123. * 解密函数
  124. *
  125. * @param string $txt
  126. * @param string $key
  127. * @return string
  128. */
  129. function passport_decrypt($txt, $key)
  130. {
  131. $txt = passport_key(base64_decode($txt), $key);
  132. $tmp = '';
  133. for ($i = 0;$i < strlen($txt); $i++) {
  134. $md5 = $txt[$i];
  135. $tmp .= $txt[++$i] ^ $md5;
  136. }
  137. return $tmp;
  138. }
  139. /**
  140. * 加密函数
  141. *
  142. * @param string $txt
  143. * @param string $key
  144. * @return string
  145. */
  146. function passport_encrypt($txt, $key)
  147. {
  148. srand((double)microtime() * 1000000);
  149. $encrypt_key = md5(rand(0, 32000));
  150. $ctr = 0;
  151. $tmp = '';
  152. for($i = 0; $i < strlen($txt); $i++ )
  153. {
  154. $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
  155. $tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
  156. }
  157. return base64_encode(passport_key($tmp, $key));
  158. }
  159. /**
  160. * 编码函数
  161. *
  162. * @param string $txt
  163. * @param string $key
  164. * @return string
  165. */
  166. function passport_key($txt, $encrypt_key)
  167. {
  168. $encrypt_key = md5($encrypt_key);
  169. $ctr = 0;
  170. $tmp = '';
  171. for($i = 0; $i < strlen($txt); $i++)
  172. {
  173. $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
  174. $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
  175. }
  176. return $tmp;
  177. }
  178. ?>