CheckoutController.class.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. <?php
  2. /**
  3. * lionfish 商城系统
  4. *
  5. * ==========================================================================
  6. * @link http://www.liofis.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license http://www.liofis.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. namespace Home\Controller;
  15. class CheckoutController extends CommonController {
  16. //步骤1:结算选项
  17. function login(){
  18. $this->display();
  19. }
  20. //验证登录
  21. function validate_login(){
  22. //是否已经登录
  23. $json=array();
  24. if (is_login()) {
  25. $json['redirect'] = U('/checkout');
  26. }
  27. if (!$json) {
  28. $d=I('post.');
  29. $user=M('Member')->getByUname($d['uname']);
  30. if(!$user){
  31. $user=M('Member')->getByEmail($d['uname']);
  32. }
  33. //用户存在且可用
  34. if(!($user&&$user['status']==1)){
  35. $json['error']['warning']='用户不存在或被禁用!!';
  36. }
  37. if(think_ucenter_encrypt($d['password'],C('PWD_KEY'))!=$user['pwd']){
  38. $json['error']['warning']='密码错误!!';
  39. }
  40. }
  41. if (!$json) {
  42. $auth = array(
  43. 'uid' => $user['member_id'],
  44. 'username' => $user['uname'],
  45. );
  46. session('user_auth', $auth);
  47. session('user_auth_sign', data_auth_sign($auth));
  48. $data = array();
  49. $data['member_id'] = $user['member_id'];
  50. $data['last_login_time'] = time();
  51. $data['login_count'] = array('exp','login_count+1');
  52. $data['last_login_ip'] = get_client_ip();
  53. M('Member')->save($data);
  54. storage_user_action($user['member_id'],$user['uname'],C('FRONTEND_USER'),'登录了网站');
  55. if($user['address_id']!=0){
  56. session('shipping_address_id',$user['address_id']);
  57. }
  58. //是否有货
  59. $cart=new \Lib\Cart();
  60. if ((!$cart->has_goods()) ) {
  61. $json['redirect'] = U('/cart');
  62. $this->ajaxReturn($json);
  63. die;
  64. }
  65. $json['redirect'] = U('/checkout');
  66. }
  67. $this->ajaxReturn($json);
  68. die();
  69. }
  70. //选择是注册购买还是不注册购买
  71. function user(){
  72. if(I('u')=='register'){
  73. $this->province=M('area')->where('area_parent_id=0')->select();
  74. $this->display('register');
  75. }
  76. if(I('u')=='guest'){
  77. }
  78. }
  79. //用户注册验证写入
  80. function register(){
  81. if(IS_POST){
  82. //未登录
  83. $json=array();
  84. if(!is_login()){
  85. //验证是否有货
  86. $cart=new \Lib\Cart();
  87. if ((!$cart->has_goods())) {
  88. $json['redirect'] = U('/cart');
  89. }
  90. //验证最小商品数量
  91. $products = $cart->get_all_goods();
  92. foreach ($products as $product) {
  93. $product_total = 0;
  94. foreach ($products as $product_2) {
  95. if ($product_2['goods_id'] == $product['goods_id']) {
  96. $product_total += $product_2['quantity'];
  97. }
  98. }
  99. if ($product['minimum'] > $product_total) {
  100. $json['redirect'] =U('/cart');
  101. break;
  102. }
  103. }
  104. if (!$json) {
  105. $d=I('post.');
  106. if ((utf8_strlen($d['uname']) <= 1) || (utf8_strlen($d['uname']) > 20)) {
  107. $json['error']['uname'] = '用户名长度必须大于1,小于20位!!';
  108. }
  109. if ((utf8_strlen($d['name']) <= 1) || (utf8_strlen($d['name']) > 20)) {
  110. $json['error']['name'] = '性名长度必须大于1,小于20位!!';
  111. }
  112. if (M('Member')->getByUname($d['uname'])) {
  113. $json['error']['uname'] = '用户名已经存在!!';
  114. }
  115. if(empty($d['email'])){
  116. $json['error']['email'] = 'email必填!!';
  117. }
  118. if(!empty($d['email'])){
  119. if ((utf8_strlen($d['email']) > 96) || !preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $d['email'])) {
  120. $json['error']['email'] = 'email格式错误!!';
  121. }
  122. if (M('Member')->getByEmail($d['email'])) {
  123. $json['error']['email'] = 'email已经存在!!';
  124. }
  125. }
  126. if ((utf8_strlen($d['telephone']) < 3) || (utf8_strlen($d['telephone']) > 32)) {
  127. $json['error']['telephone'] = '电话长度错误!!';
  128. }
  129. if ((utf8_strlen($d['address']) < 3) || (utf8_strlen($d['address']) > 128)) {
  130. $json['error']['address'] = '地址长度错误!!';
  131. }
  132. if ((utf8_strlen($d['password']) < 4) || (utf8_strlen($d['password']) > 20)) {
  133. $json['error']['password'] = '密码长度错误!!';
  134. }
  135. if ($d['confirm'] != $d['password']) {
  136. $json['error']['confirm'] = '两次密码输入不一致!!';
  137. }
  138. if($d['province_id']==-1){
  139. $json['error']['area'] = '请选择省份!!';
  140. }
  141. if($d['city_id']==-1){
  142. $json['error']['area'] = '请选择城市!!';
  143. }
  144. }
  145. if (!$json) {
  146. $uid=D('Member')->add_member();
  147. $auth = array(
  148. 'uid' => $uid,
  149. 'username' => $d['uname'],
  150. );
  151. storage_user_action($uid,$d['uname'],C('FRONTEND_USER'),'注册成为会员');
  152. $email_content='您好,感谢您注册成为'.C('SITE_NAME').'会员<br />'.
  153. '您的账号是 '.$d['uname'].'<br />'.
  154. '邮箱是 '.$d['email'].'<br />'.
  155. '密码是 '.$d['password'].'<br />'.
  156. '您可以使用账号或者邮箱来进行网站的登录<a href="'.C('SITE_URL').U('/login').'">点此进行登录</a>';
  157. //发送邮件
  158. think_send_mail($d['email'],$d['uname'],C('SITE_NAME').'会员注册成功',$email_content);
  159. session('user_auth', $auth);
  160. session('user_auth_sign', data_auth_sign($auth));
  161. session('shipping_address_id', D('Member')->getAddress($uid));
  162. }
  163. }else{
  164. $json['redirect'] = U('/checkout');
  165. }
  166. $this->ajaxReturn($json);
  167. die();
  168. }
  169. $this->display();
  170. }
  171. //收货地址
  172. function shipping_address(){
  173. $s=session('shipping_address_id');
  174. if (isset($s)) {
  175. $this->address_id=$s;
  176. } else {
  177. $this->address_id=D('Member')->get_address_id(session('user_auth.uid'));
  178. }
  179. $this->province=M('area')->where('area_parent_id=0')->select();
  180. $this->addresses=D('Member')->getAddress(session('user_auth.uid'));
  181. $this->display();
  182. }
  183. function validate($cart,$json){
  184. if (!is_login()) {
  185. $json['redirect'] = U('/checkout');
  186. }
  187. //验证是否需要运送
  188. if (!$cart->has_shipping()) {
  189. $json['redirect'] = U('/checkout');
  190. }
  191. // 验证是否有货
  192. if ((!$cart->has_goods() ) ) {
  193. $json['redirect'] = U('/cart');
  194. }
  195. // 验证商品数量
  196. $products = $cart->get_all_goods();
  197. foreach ($products as $product) {
  198. $product_total = 0;
  199. foreach ($products as $product_2) {
  200. if ($product_2['goods_id'] == $product['goods_id']) {
  201. $product_total += $product_2['quantity'];
  202. }
  203. }
  204. if ($product['minimum'] > $product_total) {
  205. $json['redirect'] = U('/cart');
  206. break;
  207. }
  208. }
  209. }
  210. //验证收货地址
  211. function validate_shipping_address(){
  212. $cart=new \Lib\Cart();
  213. $json=array();
  214. $this->validate($cart,$json);
  215. $w=new \Lib\Weight();
  216. $weight=$w->format($cart->getWeight(), C('WEIGHT_ID'));
  217. session('weight',$weight['num']);
  218. if (!$json) {
  219. $d=I('post.');
  220. if (isset($d['shipping_address']) && $d['shipping_address'] == 'existing') {
  221. if (empty($d['address_id'])) {
  222. $json['error']['warning'] ='请选择送货地址!!';
  223. } elseif (!in_array($d['address_id'], array_keys(D('Member')->getAddress(session('user_auth.uid'))))) {
  224. $json['error']['warning'] = '无效地址!!';
  225. }
  226. if (!$json) {
  227. session('shipping_address_id',$d['address_id']);
  228. $address_info = M('Address')->where('address_id='.$d['address_id'])->find();
  229. if ($address_info) {
  230. session('shipping_city_id',$address_info['city_id']);
  231. //session('postcode',$address_info['postcode']);
  232. session('shipping_name',$address_info['name']);
  233. } else {
  234. session('shipping_city_id',null);
  235. //session('postcode',null);
  236. }
  237. session('shipping_method',null);
  238. }
  239. }
  240. if ($d['shipping_address'] == 'new') {
  241. if ((utf8_strlen($d['name']) < 1) || (utf8_strlen($d['name']) > 32)) {
  242. $json['error']['name'] = '姓名必须大于1位,小于32位!!';
  243. }
  244. if ((utf8_strlen($d['address']) < 3) || (utf8_strlen($d['address']) > 128)) {
  245. $json['error']['address'] = '地址必须大于3位小于128位!!';
  246. }
  247. if ((utf8_strlen($d['telephone']) < 3) || (utf8_strlen($d['telephone']) > 32)) {
  248. $json['error']['telephone'] = '电话长度错误!!';
  249. }
  250. if($d['province_id']==-1){
  251. $json['error']['area'] = '请选择省份!!';
  252. }
  253. if($d['city_id']==-1){
  254. $json['error']['area'] = '请选择城市!!';
  255. }
  256. if (!$json) {
  257. session('shipping_address_id',D('Member')->add_address());
  258. storage_user_action(session('user_auth.uid'),session('user_auth.username'),C('FRONTEND_USER'),'新增了收货地址');
  259. session('shipping_city_id',$d['city_id']);
  260. session('shipping_method',null);
  261. }
  262. }
  263. }
  264. $this->ajaxReturn($json);
  265. die();
  266. }
  267. function shipping_method_ajax()
  268. {
  269. }
  270. //货运方式
  271. function shipping_method(){
  272. $list=M('Transport')->select();
  273. if(isset($list)&&is_array($list)){
  274. foreach ($list as $k => $v) {
  275. $sm[$k]['id']=$v['id'];
  276. $sm[$k]['name']=$v['title'];
  277. $sm[$k]['info']=D('Transport')->calc_transport($v['id'], session('weight'), session('shipping_city_id') );
  278. }
  279. }
  280. $this->sm=$sm;
  281. $this->display();
  282. }
  283. //验证货运方式
  284. function validate_shipping_method(){
  285. $cart=new \Lib\Cart();
  286. $json=array();
  287. $this->validate($cart,$json);
  288. if (!$json) {
  289. $d=I('post.');
  290. if (!isset($d['shipping_method'])) {
  291. $json['error']['warning'] = '请选择货运方式!!';
  292. } else {
  293. if ($d['shipping_method']!=$d['shipping_method']) {
  294. $json['error']['warning'] ='非法操作!!';
  295. }
  296. }
  297. if (!$json) {
  298. session('shipping_method',$d['shipping_method']);
  299. session('comment',strip_tags($d['comment']));
  300. }
  301. }
  302. $this->ajaxReturn($json);
  303. die();
  304. }
  305. //支付方式
  306. function payment_method(){
  307. $this->list=M('payment')->where(array('payment_state'=>'1'))->select();
  308. $this->display();
  309. }
  310. function validate_payment_method(){
  311. $cart=new \Lib\Cart();
  312. $json=array();
  313. $this->validate($cart,$json);
  314. if (!$json) {
  315. $d=I('post.');
  316. if (!isset($d['payment_method'])) {
  317. $json['error']['warning'] = '请选择支付方式!!';
  318. } elseif (!M('Payment')->where(array('payment_code'=>$d['payment_method']))->find()) {
  319. //支付方式不存在
  320. $json['error']['warning'] = '非法操作!!';
  321. }
  322. if (!$json) {
  323. session('payment_method',$d['payment_method']);
  324. }
  325. }
  326. $this->ajaxReturn($json);
  327. die();
  328. }
  329. function confirm_done()
  330. {
  331. $data = I('post.');
  332. $cart=new \Lib\Cart();
  333. $token=pay_token('token');
  334. $goods=$cart->get_all_goods();
  335. $goods_data = array_pop($goods);
  336. $hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  337. $goods_id=$hashids->decode($goods_data['goods_id']);
  338. $result = array('code' => 0);
  339. if (!is_login()) {
  340. $result['msg'] = '登录失效';
  341. echo json_encode($result);
  342. die();
  343. }
  344. //需要送货
  345. if ($cart->has_shipping()) {
  346. $address_id=session('shipping_address_id');
  347. $member_id = session('user_auth.uid');
  348. $shipping_address = M('Address')->where(array('member_id'=>$member_id,'address_id' => $data['address_id']))->find();
  349. if (empty($shipping_address)) {
  350. $result['msg'] = '请填写收货地址';
  351. echo json_encode($result);
  352. die();
  353. } else {
  354. $address_id = $shipping_address['address_id'];
  355. session('shipping_address_id',$address_id);
  356. }
  357. //是否选定了配送方式
  358. session('shipping_method',$data['transport_id']);
  359. session('payment_method',$data['payment_method']);
  360. //payment_method shipping_method
  361. $shipping_method=session('shipping_method');
  362. if (!isset($shipping_method)) {
  363. $result['msg'] = '请选择配送方式';
  364. echo json_encode($result);
  365. die();
  366. }
  367. }else{
  368. session('shipping_method',null);
  369. }
  370. session('remark',$data['remark']);
  371. session('shipping_method',$data['delivery']);
  372. if($data['delivery'] == 'pickup')
  373. {
  374. session('express_id',0);
  375. }else {
  376. session('express_id',$data['express_id']);
  377. }
  378. session('pick_up_id',$data['pick_up_id']);
  379. if($data['delivery'] == 'pickup' && $data['pick_up_id'] == 0)
  380. {
  381. $result['msg'] = '请选择自提地点';
  382. echo json_encode($result);
  383. die();
  384. }
  385. session('shipping_address_id',$data['address_id']);
  386. // 验证是否有货
  387. if ((!$cart->has_goods() ) ) {
  388. $result['msg'] = '购物车是空的';
  389. echo json_encode($result);
  390. die();
  391. }
  392. //商品规格库存状态
  393. $goods_option_data = R('Goods/get_goods_options',$goods_id);
  394. $goods_info = M('goods')->where( array('goods_id' => $goods_id) )->find();
  395. $max_quantity = $goods_info['quantity'];
  396. if(!empty($goods_data['option'])) {
  397. $opt_arr = array();
  398. foreach($goods_data['option'] as $val){
  399. $opt_arr[] = $val['option_value_id'].'_'.$val['goods_option_value_id'];
  400. }
  401. //判断规格库存是否比商品库存还小
  402. foreach($goods_option_data['list'] as $vv)
  403. {
  404. foreach($vv['option_value'] as $option_value)
  405. {
  406. $tp_opt_str = $option_value['option_value_id'].'_'.$val['goods_option_value_id'];
  407. if(in_array($tp_opt_str,$opt_arr)){
  408. if($max_quantity > $option_value['quantity']){
  409. $max_quantity = $option_value['quantity'];
  410. }
  411. }
  412. }
  413. }
  414. $mul_opt_arr = array();
  415. foreach($goods_data['option'] as $val){
  416. $mul_opt_arr[] = $val['option_value_id'];
  417. }
  418. if(!empty($mul_opt_arr))
  419. {
  420. $rela_goodsoption_valueid = implode('_', $mul_opt_arr);
  421. $goods_option_mult_value = M('goods_option_mult_value')->where( array('rela_goodsoption_valueid' => $rela_goodsoption_valueid,'goods_id'=>$goods_id) )->find();
  422. if( !empty($goods_option_mult_value) )
  423. {
  424. if($goods_option_mult_value['quantity']<$data['num']){
  425. $result['msg'] = '商品数量不足,剩余'.$goods_option_mult_value['quantity'].'个!';
  426. echo json_encode($result);
  427. die();
  428. }
  429. }
  430. }
  431. }
  432. if($data['num'] > $max_quantity)
  433. {
  434. $result['msg'] = '库存不足';
  435. echo json_encode($result);
  436. die();
  437. }
  438. $goods_model = D('Home/Goods');
  439. //检测商品限购
  440. $can_buy_count = $goods_model->check_goods_user_canbuy_count(is_login(), $goods_id[0]);
  441. if($can_buy_count == -1)
  442. {
  443. $result['msg'] = '该商品限购,您已经不能再买了。';
  444. echo json_encode($result);
  445. die();
  446. }else if($can_buy_count >0 && $data['num'] >$can_buy_count)
  447. {
  448. $result['msg'] = '该商品限购,您还能购买'.$can_buy_count.'份';
  449. echo json_encode($result);
  450. die();
  451. }
  452. session('quantity',$data['num']);
  453. session('payment_voucher_id', $data['voucher_id']);
  454. //更新购物车数量
  455. $cart->update($goods_data['key'], $data['num']);
  456. $result['code'] = 1;
  457. $pay_url = U('Payment/pay',array('token'=>$token));
  458. $result['url'] = $pay_url;
  459. echo json_encode($result);
  460. die();
  461. }
  462. function confirm(){
  463. $cart=new \Lib\Cart();
  464. $this->token=pay_token('token');
  465. //需要送货
  466. if ($cart->has_shipping()) {
  467. $address_id=session('shipping_address_id');
  468. if (is_login()&& isset($address_id)) {
  469. $shipping_address = M('Address')->find($address_id);
  470. }
  471. if (empty($shipping_address)) {
  472. $redirect =U('/checkout');
  473. }
  474. //是否选定了配送
  475. $shipping_method=session('shipping_method');
  476. if (!isset($shipping_method)) {
  477. $redirect =U('/checkout');
  478. }
  479. }else{
  480. session('shipping_method',null);
  481. }
  482. //是否有选择支付方法
  483. $payment_method=session('payment_method');
  484. if (!isset($payment_method)) {
  485. $redirect =U('/checkout');
  486. }
  487. // 验证是否有货
  488. if ((!$cart->has_goods() ) ) {
  489. $redirect = U('/cart');
  490. }
  491. // 验证商品数量
  492. $products = $cart->get_all_goods();
  493. foreach ($products as $product) {
  494. $product_total = 0;
  495. foreach ($products as $product_2) {
  496. if ($product_2['goods_id'] == $product['goods_id']) {
  497. $product_total += $product_2['quantity'];
  498. }
  499. }
  500. if ($product['minimum'] > $product_total) {
  501. $redirect = U('/cart');
  502. break;
  503. }
  504. }
  505. if (!isset($redirect)) {
  506. if($products){
  507. //运费
  508. $sm=D('Transport')->calc_transport(session('shipping_method'),
  509. session('weight'),
  510. session('shipping_city_id'));
  511. $this->sm=$sm;
  512. foreach ($products as $product) {
  513. $p[] = array(
  514. 'key' => $product['key'],
  515. 'image' => $product['image'],
  516. 'name' => $product['name'],
  517. 'model' => $product['model'],
  518. 'quantity' => $product['quantity'],
  519. 'price' => $product['price'],
  520. 'total' => $product['total'],
  521. 'goods_id' =>$product['goods_id'],
  522. 'total_price' =>$product['total'],
  523. 'option' =>$product['option']
  524. );
  525. }
  526. $this->products=$p;
  527. }
  528. }
  529. $this->display();
  530. }
  531. //获取地区
  532. function get_area(){
  533. $goods_id = I('post.goods_id', 0);
  534. $where['area_parent_id']=$_REQUEST['areaId'];
  535. $hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  536. if(!empty($goods_id)) {
  537. $goods_id=$hashids->decode($goods_id );
  538. $goods_id = $goods_id[0];
  539. }
  540. if($where['area_parent_id'] == 0 && $goods_id > 0)
  541. {
  542. $goods_area_limit = M('goods_area')->where( array('goods_id' => $goods_id) )->find();
  543. if(!empty($goods_area_limit)) {
  544. $area_limit_ids = unserialize($goods_area_limit['area_ids_text']);
  545. if(empty($area_limit_ids)) {
  546. $par_list = M('area')->where()->select();
  547. } else {
  548. $par_list = M('area')->where(array('area_id' => array('in', implode(',',$area_limit_ids) ) ))->select();
  549. }
  550. $par_ids = array();
  551. foreach($par_list as $val)
  552. {
  553. if(empty($par_ids) || !in_array($val['area_parent_id'], $par_ids))
  554. {
  555. $par_ids[] = $val['area_parent_id'];
  556. }
  557. }
  558. if(!empty($par_ids))
  559. {
  560. $where['area_id'] = array('in', implode(',',$par_ids));
  561. }
  562. }
  563. } else if($where['area_parent_id'] > 0 && $goods_id > 0) {
  564. $goods_area_limit = M('goods_area')->where( array('goods_id' => $goods_id) )->find();
  565. if(!empty($goods_area_limit)) {
  566. $area_limit_ids = unserialize($goods_area_limit['area_ids_text']);
  567. if(!empty($area_limit_ids))
  568. {
  569. $where['area_id'] = array('in', implode(',',$area_limit_ids));
  570. }
  571. }
  572. }
  573. $area=M('area')->where($where)->select();
  574. $this->ajaxReturn($area);
  575. }
  576. }