国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
7.0KB

  1. <?php
  2. define("DE_ItemEcode",'Shop_De_');//识别购物车Cookie前缀,非开发人员请不要随意更改!
  3. /**
  4. * 购物车类
  5. *
  6. * @version $Id: shopcar.class.php 2 20:58 2010年7月7日Z tianya $
  7. * @package DedeCMS.Libraries
  8. * @copyright Copyright (c) 2007 - 2018, DesDev, Inc.
  9. * @copyright Copyright (c) 2020, DedeBIZ.COM
  10. * @license https://www.dedebiz.com/license/v6
  11. * @link https://www.dedebiz.com
  12. */
  13. // ------------------------------------------------------------------------
  14. /**
  15. * 会员购物车类
  16. *
  17. * @package MemberShops
  18. * @subpackage DedeCMS.Libraries
  19. * @link http://www.dedecms.com
  20. */
  21. class MemberShops
  22. {
  23. var $OrdersId;
  24. var $productsId;
  25. function __construct()
  26. {
  27. $this->OrdersId = $this->getCookie("OrdersId");
  28. if(empty($this->OrdersId))
  29. {
  30. $this->OrdersId = $this->MakeOrders();
  31. }
  32. }
  33. function MemberShops()
  34. {
  35. $this->__construct();
  36. }
  37. /**
  38. * 创建一个专有订单编号
  39. *
  40. * @return string
  41. */
  42. function MakeOrders()
  43. {
  44. $this->OrdersId = 'S-P'.time().'RN'.mt_rand(100,999);
  45. $this->deCrypt($this->saveCookie("OrdersId",$this->OrdersId));
  46. return $this->OrdersId;
  47. }
  48. /**
  49. * 添加一个商品编号及信息
  50. *
  51. * @param string $id 购物车ID
  52. * @param string $value 值
  53. * @return void
  54. */
  55. function addItem($id, $value)
  56. {
  57. $this->productsId = DE_ItemEcode.$id;
  58. $this->saveCookie($this->productsId,$value);
  59. }
  60. /**
  61. * 删去一个带编号的商品
  62. *
  63. * @param string $id 购物车ID
  64. * @return void
  65. */
  66. function delItem($id)
  67. {
  68. $this->productsId = DE_ItemEcode.$id;
  69. setcookie($this->productsId, "", time()-3600000,"/");
  70. }
  71. /**
  72. * 清空购物车商品
  73. *
  74. * @return string
  75. */
  76. function clearItem()
  77. {
  78. foreach($_COOKIE as $key => $vals)
  79. {
  80. if(preg_match('/'.DE_ItemEcode.'/', $key))
  81. {
  82. setcookie($key, "", time()-3600000,"/");
  83. }
  84. }
  85. return 1;
  86. }
  87. /**
  88. * 得到订单记录
  89. *
  90. * @return array
  91. */
  92. function getItems()
  93. {
  94. $Products = array();
  95. foreach($_COOKIE as $key => $vals)
  96. {
  97. if(preg_match("#".DE_ItemEcode."#", $key) && preg_match("#[^_0-9a-z]#", $key))
  98. {
  99. parse_str($this->deCrypt($vals), $arrays);
  100. $values = @array_values($arrays);
  101. if(!empty($values))
  102. {
  103. $arrays['price'] = sprintf("%01.2f", $arrays['price']);
  104. if($arrays['buynum'] < 1)
  105. {
  106. $arrays['buynum'] = 0;
  107. }
  108. $Products[$key] = $arrays;
  109. }
  110. }
  111. }
  112. unset($key,$vals,$values,$arrays);
  113. return $Products;
  114. }
  115. /**
  116. * 得到指定商品信息
  117. *
  118. * @param string $id 购物车ID
  119. * @return array
  120. */
  121. function getOneItem($id)
  122. {
  123. $key = DE_ItemEcode.$id;
  124. if(!isset($_COOKIE[$key]) && empty($_COOKIE[$key]))
  125. {
  126. return '';
  127. }
  128. $itemValue = $_COOKIE[$key];
  129. parse_str($this->deCrypt($itemValue), $Products);
  130. unset($key,$itemValue);
  131. return $Products;
  132. }
  133. /**
  134. * 获得购物车中的商品数
  135. *
  136. * @return int
  137. */
  138. function cartCount()
  139. {
  140. $Products = $this->getItems();
  141. $itemsCount = count($Products);
  142. $i = 0;
  143. if($itemsCount > 0)
  144. {
  145. foreach($Products as $val)
  146. {
  147. $i = $i+$val['buynum'];
  148. }
  149. }
  150. unset($Products,$val,$itemsCount);
  151. return $i;
  152. }
  153. /**
  154. * 获得购物车中的总金额
  155. *
  156. * @return string
  157. */
  158. function priceCount()
  159. {
  160. $price = 0.00;
  161. foreach($_COOKIE as $key => $vals)
  162. {
  163. if(preg_match("/".DE_ItemEcode."/", $key))
  164. {
  165. $Products = $this->getOneItem(str_replace(DE_ItemEcode,"",$key));
  166. if($Products['buynum'] > 0 && $Products['price'] > 0)
  167. {
  168. $price = $price + ($Products['price']*$Products['buynum']);
  169. }
  170. }
  171. }
  172. unset($key,$vals,$Products);
  173. return sprintf("%01.2f", $price);
  174. }
  175. //加密接口字符
  176. function enCrypt($txt)
  177. {
  178. return $this->mchStrCode($txt);
  179. }
  180. //解密接口字符串
  181. function deCrypt($txt)
  182. {
  183. return $this->mchStrCode($txt,'DECODE');
  184. }
  185. function mchStrCode($string, $operation = 'ENCODE')
  186. {
  187. $key_length = 4;
  188. $expiry = 0;
  189. $key = md5($GLOBALS['cfg_cookie_encode']);
  190. $fixedkey = md5($key);
  191. $egiskeys = md5(substr($fixedkey, 16, 16));
  192. $runtokey = $key_length ? ($operation == 'ENCODE' ? substr(md5(microtime(true)), -$key_length) : substr($string, 0, $key_length)) : '';
  193. $keys = md5(substr($runtokey, 0, 16) . substr($fixedkey, 0, 16) . substr($runtokey, 16) . substr($fixedkey, 16));
  194. $string = $operation == 'ENCODE' ? sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$egiskeys), 0, 16) . $string : base64_decode(substr($string, $key_length));
  195. $i = 0; $result = '';
  196. $string_length = strlen($string);
  197. for ($i = 0; $i < $string_length; $i++){
  198. $result .= chr(ord($string[$i]) ^ ord($keys[$i % 32]));
  199. }
  200. if($operation == 'ENCODE') {
  201. return $runtokey . str_replace('=', '', base64_encode($result));
  202. } else {
  203. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$egiskeys), 0, 16)) {
  204. return substr($result, 26);
  205. } else {
  206. return '';
  207. }
  208. }
  209. }
  210. //串行化数组
  211. function enCode($array)
  212. {
  213. $arrayenc = array();
  214. foreach($array as $key => $val)
  215. {
  216. $arrayenc[] = $key.'='.urlencode($val);
  217. }
  218. return implode('&', $arrayenc);
  219. }
  220. //创建加密的_cookie
  221. function saveCookie($key,$value)
  222. {
  223. if(is_array($value))
  224. {
  225. $value = $this->enCrypt($this->enCode($value));
  226. }
  227. else
  228. {
  229. $value = $this->enCrypt($value);
  230. }
  231. setcookie($key,$value,time()+36000,'/');
  232. }
  233. //获得解密的_cookie
  234. function getCookie($key)
  235. {
  236. if(isset($_COOKIE[$key]) && !empty($_COOKIE[$key]))
  237. {
  238. return $this->deCrypt($_COOKIE[$key]);
  239. }
  240. }
  241. }