SubjectController.class.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. <?php
  2. /**
  3. * 小梦科技资源nanodreamtech.com
  4. *
  5. * ==========================================================================
  6. * @link https://www.nanodreamtech.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license https://www.nanodreamtech.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. namespace Home\Controller;
  15. class SubjectController extends CommonController {
  16. /**
  17. kangjia
  18. **/
  19. public function assist_bargain_coupon()
  20. {
  21. $free_ad_list = M('plugins_slider')->where( array('type' => 'wepro_bargain_ad') )->order('slider_id desc')->find();
  22. $goods_model = D('Home/goods');
  23. $per_page = 10;
  24. $page = I('post.page',1);
  25. $offset = ($page - 1) * $per_page;
  26. $now_time = time();
  27. $where = " g.status =1 and g.type != 'normal' and g.quantity >0 ";
  28. $where .= " and pg.begin_time < {$now_time} and pg.end_time >{$now_time} ";
  29. /**
  30. $sql = 'select g.goods_id,g.name,g.quantity,g.pinprice,g.price,g.danprice,g.pin_count,g.image,g.fan_image,g.store_id,g.seller_count,g.virtual_count from '.C('DB_PREFIX')."goods as g
  31. where g.status =1 and g.quantity >0 and g.type = 'bargain' order by g.sort_order desc limit {$offset},{$per_page}";
  32. **/
  33. $sortby = ' pg.id desc ';
  34. $list = D('Home/Pingoods')->get_bargaingoods_list('*', $where,$sortby,$offset,$per_page);
  35. /**
  36. $sql = 'select z.id,z.need_person,z.total_money as pinprice,z.deal_money,z.end_time,z.state,g.goods_id,g.name,g.quantity,g.price,g.danprice,g.image,g.fan_image,g.store_id,g.seller_count,g.virtual_count
  37. from '.C('DB_PREFIX').'goods as g ,'.C('DB_PREFIX').'bargain_order as z
  38. where z.member_id = '.$member_id.' and z.goods_id = g.goods_id order by z.end_time desc limit '.$offset.','.$per_page;
  39. $list = M()->query($sql);
  40. **/
  41. foreach($list as $key => $v){
  42. $list[$key]['seller_count'] += $v['virtual_count'];
  43. if(!empty($v['fan_image'])){
  44. $list[$key]['image']= $v['fan_image'];
  45. }
  46. }
  47. $this->list = $list;
  48. if($page > 1) {
  49. $result = array('code' => 0);
  50. if(!empty($list)) {
  51. $result['code'] = 1;
  52. $result['html'] = $this->fetch('Subject:assist_bargain_fetch');
  53. }
  54. echo json_encode($result);
  55. die();
  56. }
  57. $success_order_list = M('bargain_order')->where( array( 'state' => 1 ) )->order('id desc')->limit(10)->select();
  58. if( !empty($success_order_list) )
  59. {
  60. foreach($success_order_list as $key => $val)
  61. {
  62. $mem_info = M('member')->field('uname,avatar')->where( array('member_id' => $val['member_id']) )->find();
  63. $gd_info = M('goods')->field('name')->where( array('goods_id' => $val['goods_id']) )->find();
  64. $val['uname'] = $mem_info['uname'];
  65. $val['avatar'] = $mem_info['avatar'];
  66. $val['goods_name'] = $gd_info['name'];
  67. $success_order_list[$key] = $val;
  68. }
  69. }
  70. $this->success_order_list = $success_order_list;
  71. $zan_notice = C('zan_notice');
  72. $qian=array("\r\n");
  73. $hou=array("@F@");
  74. $zan_notice_str = str_replace($qian,$hou,$zan_notice);
  75. $zan_notice_arr = explode('@F@',$zan_notice_str);
  76. $this->zan_notice_arr = $zan_notice_arr;
  77. $this->free_ad_list = $free_ad_list;
  78. $this->display();
  79. }
  80. /**
  81. 砍价详情
  82. **/
  83. public function assist_bargain_coupon_detail()
  84. {
  85. //判断是否自己的,
  86. //判断是否砍过
  87. $is_me = false;
  88. $has_kan = false;
  89. $member_id = session('user_auth.uid');
  90. $id = I('get.id');
  91. $bargain_info = M('bargain_order')->where( array('id' => $id) )->find();
  92. if($bargain_info['member_id'] == $member_id)
  93. {
  94. $is_me = true;
  95. }
  96. $has_kan_count = M('bargain_order_detail')->where( array('member_id' => $member_id,'bargain_order_id' => $id) )->count();
  97. if($has_kan_count >0)
  98. {
  99. $has_kan = true;
  100. }
  101. //end_time
  102. $now_time = time();
  103. $goods_info = M('goods')->field('name,price,seller_count,virtual_count,image,fan_image')->where( array('goods_id' => $bargain_info['goods_id']) )->find();
  104. $goods_info['image'] = resize($goods_info['image'],300,300);
  105. //fan_image
  106. if(!empty($goods_info['fan_image']))
  107. {
  108. $goods_info['image'] = resize($goods_info['fan_image'],300,300);
  109. }
  110. $goods_info['seller_count'] += $goods_info['virtual_count'];
  111. $kan_order_list = M('bargain_order_detail')->where( array('bargain_order_id' =>$id) )->order('addtime desc')->limit(15)->select();
  112. $this->kan_order_list = $kan_order_list;
  113. $this->bargain_info = $bargain_info;
  114. $this->goods_info = $goods_info;
  115. $this->is_me = $is_me;
  116. $this->has_kan = $has_kan;
  117. //member_id
  118. $member_info = M('member')->where( array('member_id' => $bargain_info['member_id']) )->find();
  119. $this->member_info = $member_info;
  120. $kan_description = C('kan_description');
  121. $qian=array("\r\n");
  122. $hou=array("@F@");
  123. $kan_description_str = str_replace($qian,$hou,$kan_description);
  124. $kan_description_str = explode('@F@',$kan_description_str);
  125. $this->kan_description_str = $kan_description_str;
  126. $kan_rules = C('kan_rules');
  127. $qian=array("\r\n");
  128. $hou=array("@F@");
  129. $kan_rules_str = str_replace($qian,$hou,$kan_rules);
  130. $kan_rules_str = explode('@F@',$kan_rules_str);
  131. $this->kan_rules_str = $kan_rules_str;
  132. $kan_person_count = C('kan_person_count');
  133. $this->kan_person_count = $kan_person_count;
  134. //随机商品 8 个
  135. $sql='SELECT goods_id,image,fan_image,seller_count,virtual_count,danprice,name FROM '.C('DB_PREFIX').'goods WHERE status=1 and type != "integral" and type != "bargain" and quantity>0 ORDER BY rand() LIMIT 0,12';
  136. $list=M()->query($sql);
  137. $goods_model = D('Home/Goods');
  138. $hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  139. foreach ($list as $k => $v) {
  140. //$list[$k]['image']=resize($v['image'], C('goods_related_thumb_width'), C('goods_related_thumb_height'));
  141. $price_arr = $goods_model->get_goods_price($v['goods_id']);
  142. $list[$k]['price'] = $price_arr['price'];
  143. $list[$k]['seller_count'] += $list[$k]['virtual_count'];
  144. //$val['danprice'] = $price_arr['price'];
  145. if(!empty($v['fan_image'])){
  146. $list[$k]['image']=resize($v['fan_image'], 480,480);
  147. //$list[$k]['image']='/Uploads/image/'.$v['fan_image'];
  148. }else {
  149. $list[$k]['image']=resize($v['image'], 480,480);
  150. //$list[$k]['image']='/Uploads/image/'.$v['image'];
  151. }
  152. }
  153. $this->related_goods=$list;
  154. $hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  155. $member_id = is_login();
  156. $hash_member_id = $hashids->encode($member_id);
  157. $bargain_order = M('bargain_order')->where( array('id' => $id) )->find();
  158. $goods_id = $bargain_order['goods_id'];
  159. $goods_info = M('goods')->where( array('goods_id' => $goods_id) )->find();
  160. //"我正在参加拼多多官方砍价,砍到0元就可以免费拿啦,帮我砍一下吧"
  161. $desc = "我正在参加".C('SITE_NAME')."砍价,砍到0元就可以免费拿啦,帮我砍一下吧";
  162. $this->share_logo = C('SITE_URL').resize($goods_info['image'], C('common_image_thumb_width'), C('common_image_thumb_height'));
  163. if(!empty($goods_info['fan_image'])){
  164. $this->share_logo = C('SITE_URL').resize($goods_info['fan_image'], C('common_image_thumb_width'), C('common_image_thumb_height'));
  165. }else {
  166. $this->share_logo = C('SITE_URL').resize($goods_info['image'], C('common_image_thumb_width'), C('common_image_thumb_height'));
  167. }
  168. if(!empty($goods_description['water_image']))
  169. {
  170. $this->share_logo = C('SITE_URL').'/Uploads/image/'.$goods_description['water_image'];
  171. }
  172. //Subject/assist_bargain_coupon_detail/id/10.html
  173. $this->indexsharetitle = '['.C('SITE_NAME')."]我在砍价免费拿".$goods_info['name'].",帮我砍价,0元拿回家!";
  174. $this->url = C('SITE_URL')."/index.php?s=/Subject/assist_bargain_coupon_detail/id/{$id}/rmid/{$hash_member_id}";
  175. $this->indexsharesummary = $desc;
  176. $appid_info = M('config')->where( array('name' => 'APPID') )->find();
  177. $appsecret_info = M('config')->where( array('name' => 'APPSECRET') )->find();
  178. $mchid_info = M('config')->where( array('name' => 'MCHID') )->find();
  179. $weixin_config = array();
  180. $weixin_config['appid'] = $appid_info['value'];
  181. $weixin_config['appscert'] = $appsecret_info['value'];
  182. $jssdk = new \Lib\Weixin\Jssdk( $weixin_config['appid'], $weixin_config['appscert']);
  183. $signPackage = $jssdk->GetSignPackage();
  184. $this->signPackage = $signPackage;
  185. //goods_id
  186. $this->assign('signPackage',$this->signPackage);
  187. $this->display();
  188. }
  189. /**
  190. 我的砍价
  191. **/
  192. public function assist_bargain_coupon_me()
  193. {
  194. $free_ad_list = M('plugins_slider')->where( array('type' => 'free_ad') )->order('slider_id desc')->find();
  195. $member_id = session('user_auth.uid');
  196. $per_page = 10;
  197. $page = I('post.page',1);
  198. $offset = ($page - 1) * $per_page;
  199. $sql = 'select z.id,z.need_person,z.total_money as pinprice,z.deal_money,z.end_time,z.state,g.goods_id,g.name,g.quantity,g.price,g.danprice,g.image,g.fan_image,g.store_id,g.seller_count,g.virtual_count
  200. from '.C('DB_PREFIX').'goods as g ,'.C('DB_PREFIX').'bargain_order as z
  201. where z.member_id = '.$member_id.' and z.goods_id = g.goods_id order by z.end_time desc limit '.$offset.','.$per_page;
  202. $list = M()->query($sql);
  203. foreach($list as $key => $v){
  204. //$has_zan_count = M('zan_order_detail')->where( array('zan_order_id' => $v['id']) )->count();
  205. //$list[$key]['has_zan_count'] = $has_zan_count;
  206. $list[$key]['seller_count'] += $v['virtual_count'];
  207. if(!empty($v['fan_image'])){
  208. $list[$key]['image']= $v['fan_image'];
  209. }
  210. }
  211. $this->list = $list;
  212. if($page > 1) {
  213. $result = array('code' => 0);
  214. if(!empty($list)) {
  215. $result['code'] = 1;
  216. $result['html'] = $this->fetch('Subject:assist_bargain_me_fetch');
  217. }
  218. echo json_encode($result);
  219. die();
  220. }
  221. $this->free_ad_list = $free_ad_list;
  222. $this->display();
  223. }
  224. public function get_mult_sku()
  225. {
  226. $goods_id = I('get.goods_id');
  227. $sku_arr_str = I('get.sku_arr_str');
  228. //goods_id
  229. $goods_option_mult_value_info = M('goods_option_mult_value')->where( array('rela_goodsoption_valueid' => $sku_arr_str,'goods_id' => $goods_id) )->find();
  230. $goods_option_mult_value_info['image'] = resize($goods_option_mult_value_info['image'],200,200);
  231. echo json_encode( array('code' => 0 , 'data' => $goods_option_mult_value_info) );
  232. die();
  233. }
  234. public function get_user_addresslist()
  235. {
  236. $model= D('Home/order');
  237. $list=$model->get_all_address(session('user_auth.uid'));
  238. if(!empty($list))
  239. {
  240. foreach($list as &$address)
  241. {
  242. $province_info = M('area')->where( array('area_id' => $address['province_id']) )->find();
  243. $city_info = M('area')->where( array('area_id' => $address['city_id']) )->find();
  244. $country_info = M('area')->where( array('area_id' => $address['country_id']) )->find();
  245. $address['province_name'] = $province_info['area_name'];
  246. $address['city_name'] = $city_info['area_name'];
  247. $address['country'] = $country_info['area_name'];
  248. }
  249. }
  250. //var_dump($list);die();
  251. $this->address_list = $list;
  252. $html = $this->fetch('Subject:assist_free_addresslist');
  253. echo json_encode( array('code' => 1,'html' => $html) );
  254. die();
  255. }
  256. public function get_option_sku()
  257. {
  258. $goods_id = I('get.goods_id');
  259. //判断当前商品是否有进行中的二维码,如果有,那么直接返回出来
  260. $member_id = session('user_auth.uid');
  261. $now_time = time();
  262. $old_zan_order = M('bargain_order')->where("member_id={$member_id} and goods_id={$goods_id} and state =0 and begin_time < {$now_time} and end_time > {$now_time}")->find();
  263. if( !empty($old_zan_order) )
  264. {
  265. //一个会员当前一个商品 只能发出去一个二维码
  266. echo json_encode( array('code' => 3, 'need_person' => $old_zan_order['need_person']) );
  267. die();
  268. }
  269. $goods_contr = A('Home/Goods');
  270. $this->options=$goods_contr->get_goods_options($goods_id);
  271. $goods_info = M('goods')->field('price,image,fan_image')->where( array('goods_id' => $goods_id) )->find();
  272. $goods_info['image'] = resize($goods_info['image'],200,200);
  273. if( !empty($goods_info['fan_image']) )
  274. {
  275. $goods_info['image'] = resize($goods_info['fan_image'],200,200);
  276. }
  277. $this->goods_info = $goods_info;
  278. $goods_option_mult_value = M('goods_option_mult_value')->order('pin_price asc')->where( array('goods_id' =>$goods_id) )->select();
  279. $html = '';
  280. if( !empty($goods_option_mult_value) )
  281. {
  282. foreach($goods_option_mult_value as $key => $val)
  283. {
  284. $val['image'] = resize($val['image'],200,200);
  285. //
  286. if($val['pin_price'] < 0.01)
  287. {
  288. $val['pin_price'] = $goods[0]['pinprice'];
  289. }
  290. if($val['dan_price'] < 0.01)
  291. {
  292. $val['dan_price'] = $goods[0]['danprice'];
  293. }
  294. $goods_option_mult_value[$key] = $val;
  295. }
  296. $this->goods_option_mult_value = $goods_option_mult_value;
  297. $html = $this->fetch('Subject:assist_free_option');
  298. echo json_encode( array('code' => 1,'html' => $html) );
  299. die();
  300. }else{
  301. echo json_encode( array('code' => 0) );
  302. die();
  303. }
  304. }
  305. private function _curl_get_avatar($url)
  306. {
  307. $ch = curl_init();
  308. curl_setopt($ch, CURLOPT_URL, $url);
  309. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  310. curl_setopt($ch, CURLOPT_HEADER, 0);
  311. $output = curl_exec($ch);
  312. curl_close($ch);
  313. return $output;
  314. }
  315. /**
  316. 开始砍价表_bang_kan_order($bargain_order_id,$member_id,$is_self = false, $just_update = false )
  317. **/
  318. private function _bang_kan_order($bargain_order_id,$is_self = false, $just_update = false )
  319. {
  320. $member_id = session('user_auth.uid');
  321. //检测是否砍过了
  322. $has_order = M('bargain_order_detail')->where( array('member_id'=>$member_id, 'bargain_order_id' =>$bargain_order_id) )->find();
  323. if(!empty($has_order) && !$just_update)
  324. {
  325. return 1;//已经帮忙砍过
  326. }
  327. //活动是否正常
  328. $bargain_order_info = M('bargain_order')->where( array('id' => $bargain_order_id) )->find();
  329. if( $bargain_order_info['state'] != 0 )
  330. {
  331. return 2; //活动已经成功,或者已经结束
  332. }
  333. $now_time = strtotime( date('Y-m-d').' 00:00:00' );
  334. $end_time = $now_time + 86400;
  335. $kan_person_count = C('kan_person_count');
  336. if(!$is_self)
  337. {
  338. //检测今天帮别人砍价的次数
  339. //addtime
  340. //bargain_order_detail
  341. $kan_count = M('bargain_order_detail')->where( array('addtime' =>array('between', array($now_time,$end_time) ), 'member_id'=>$member_id,'is_sleft' =>0 ) )->count();
  342. //$condition['id'] = array(between,array('1','8'));
  343. if($kan_person_count >0 && $kan_count >= $kan_person_count)
  344. {
  345. return 3;//今天已经不能再帮别人砍价
  346. }
  347. }
  348. //开始砍价流程
  349. if($bargain_order_info['need_person'] > 0)
  350. {
  351. $per_money = $bargain_order_info['deal_money'] / $bargain_order_info['need_person'];
  352. if($bargain_order_info['need_person'] >2)
  353. {
  354. $per_money = $per_money * 2;
  355. }
  356. }else{
  357. $per_money = round($bargain_order_info['deal_money'] ,2);
  358. }
  359. $min = 1;
  360. $max = $per_money * 100;
  361. $ran_money = round( mt_rand($min,$max) / 100 , 2) ;
  362. if($bargain_order_info['need_person'] < 0)
  363. {
  364. $win_rd = mt_rand(1,9);
  365. if($win_rd >=5)
  366. {
  367. $ran_money = $bargain_order_info['deal_money'];
  368. }
  369. }
  370. $member_info = M('member')->field('uname,avatar')->where( array('member_id' => $member_id) )->find();
  371. //TODO 插入砍价详细订单
  372. if($just_update)
  373. {
  374. $res = M('bargain_order_detail')->where( array('bargain_order_id' => $bargain_order_id,'member_id' =>$member_id ) )->setInc('kan_money',$ran_money);
  375. //setInc
  376. M('bargain_order')->where( array('id' => $bargain_order_id) )->save( array('is_share' => 1) );
  377. } else{
  378. $ins_data = array();
  379. $ins_data['member_id'] = $member_id;
  380. $ins_data['kan_money'] = $ran_money;
  381. $ins_data['bargain_order_id'] = $bargain_order_id;
  382. $ins_data['addtime'] = time();
  383. $ins_data['is_sleft'] = $is_self ? 1: 0;
  384. $ins_data['avatar'] = $member_info['avatar'];
  385. $ins_data['nickname'] = $member_info['uname'];
  386. $res = M('bargain_order_detail')->add($ins_data);
  387. }
  388. if($res)
  389. {
  390. if(!$just_update)
  391. {
  392. M('bargain_order')->where( array('id' => $bargain_order_id) )->setDec('need_person',1);
  393. }
  394. M('bargain_order')->where( array('id' => $bargain_order_id) )->setDec('deal_money',$ran_money);
  395. $bargain_order_info = M('bargain_order')->where( array('id' => $bargain_order_id) )->find();
  396. if($bargain_order_info['deal_money'] <=0 )
  397. {
  398. //TODO send msg
  399. $model = D('Home/Goods');
  400. $order_id = $model->_zan_bargain_order($bargain_order_id);
  401. //M('zan_order')->where( array('id' => $zan_order_id) )->save( array('order_id' => $order_id,'state' => 1) );
  402. M('bargain_order')->where( array('id' => $bargain_order_id) )->save( array('order_id' => $order_id,'state' => 1) );
  403. //add order
  404. $or_member_info = M('member')->field('openid')->where( array('member_id' => $bargain_order_info['member_id']) )->find();
  405. $or_open_id = $or_member_info['openid'];
  406. //$or_open_id = 'o0n_HwcGIfwf5b8PN8-gmNfsJBLA';
  407. $goods_info = M('goods')->field('name')->where( array('goods_id' => $bargain_order_info['goods_id']) )->find();
  408. $first_title = "您参与:".$goods_info['name'];
  409. $keyword1 = '您的活动已砍价成功';
  410. $template_data = array();
  411. $template_data['first'] = array('value' => $first_title, 'color' => '#173177');
  412. $template_data['keyword1'] = array('value' => $keyword1, 'color' => '#173177');
  413. $template_data['keyword2'] = array('value' => '点击查看订单详情', 'color' => '#173177');
  414. $template_data['remark'] = array('value' => '当前活动已成功,等待发货', 'color' => '#173177');
  415. $quan_msg_info = M('config')->where( array('name' => 'zanSuccessNotice') )->find();
  416. $template_id = $quan_msg_info['value'];
  417. //$url = C('SITE_URL')."/index.php?s=/user/myvoucherlist.html";
  418. $url = C('SITE_URL')."/index.php?s=/Order/info/id/{$order_id}.html";
  419. send_template_msg($template_data,$url,$or_open_id,$template_id);
  420. }
  421. return 0;
  422. }else{
  423. return 4;//砍价失败
  424. }
  425. }
  426. /**
  427. 分享成功后,砍
  428. **/
  429. public function share_success_kan()
  430. {
  431. $id = I('get.id');
  432. $member_id = session('user_auth.uid');
  433. $bargain_order = M('bargain_order')->where( array('id' => $id) )->find();
  434. //is_share
  435. if($bargain_order['is_share'] == 0 && $member_id == $bargain_order['member_id'] )
  436. {
  437. $order_detail = M('bargain_order_detail')->where( array('member_id' => $member_id, 'bargain_order_id' => $id) )->find();
  438. $old = $order_detail['kan_money'];
  439. $state = $this->_bang_kan_order($id,true,true );
  440. $bargain_order = M('bargain_order')->field('total_money,deal_money')->where( array('id' => $id) )->find();
  441. $bargain_order['kan_money'] = round($bargain_order['total_money'] - $bargain_order['deal_money'],2);
  442. $bargain_order['deal_money'] = round($bargain_order['deal_money'],2);
  443. $order_detail = M('bargain_order_detail')->where( array('member_id' => $member_id, 'bargain_order_id' => $id) )->find();
  444. $order_detail['kan_money'] = round($order_detail['kan_money'] - $old, 2);
  445. $li_html = "";
  446. $li_html.= '<li class="user-item">';
  447. $li_html.= ' <img src="'.$order_detail['avatar'].'" />';
  448. $li_html.= ' <div class="text">';
  449. $li_html.= ' <p class="nickname">'.$order_detail['nickname'].'</p>';
  450. $li_html.= ' <p class="hint">看我青龙偃月刀</p>';
  451. $li_html.= ' </div>';
  452. $li_html.= ' <div class="amount">';
  453. $li_html.= ' <div class="knives knives-1">';
  454. $li_html.= ' <div class="knife"></div>';
  455. $li_html.= ' </div>';
  456. $li_html.= ' 砍掉';
  457. $li_html.= ' <i class="detail ">'.$order_detail['kan_money'].'</i>';
  458. $li_html.= ' 元';
  459. $li_html.= ' </div>';
  460. $li_html.= '</li>';
  461. echo json_encode( array('code' => 0,'li_html' => $li_html, 'order_detail' => $order_detail, 'bargain_order' => $bargain_order) );
  462. die();
  463. }else{
  464. echo json_encode( array('code' => 1) );
  465. die();
  466. }
  467. }
  468. /**
  469. 帮忙别人砍价
  470. **/
  471. public function kan_others_bargain()
  472. {
  473. $id = I('get.id',0);
  474. $member_id = session('user_auth.uid');
  475. $state = $this->_bang_kan_order($id,false);
  476. //1;//已经帮忙砍过
  477. //2; //活动已经成功,或者已经结束
  478. //3;//今天已经不能再帮别人砍价
  479. //4 砍价失败
  480. //0成功
  481. $appid_info = M('config')->where( array('name' => 'APPID') )->find();
  482. $appsecret_info = M('config')->where( array('name' => 'APPSECRET') )->find();
  483. $weixin_config = array();
  484. $weixin_config['appid'] = $appid_info['value'];
  485. $weixin_config['appscert'] = $appsecret_info['value'];
  486. $member_info = M('member')->where( array('member_id' => $member_id) )->find();
  487. $jssdk = new \Lib\Weixin\Jssdk( $weixin_config['appid'], $weixin_config['appscert']);
  488. $is_sub = $jssdk->cgigetinfo($member_info['openid']);
  489. //echo json_encode();
  490. //die();
  491. if(!$is_sub)
  492. {
  493. echo json_encode( array('code' => 5) );
  494. die();
  495. }
  496. $this->is_sub = $is_sub;
  497. if($state > 0)
  498. {
  499. $msg = '';
  500. switch($state)
  501. {
  502. case 1 :
  503. $msg = '已经帮忙砍过了';
  504. break;
  505. case 2:
  506. $msg = '活动已经结束';
  507. break;
  508. case 3:
  509. $msg = '今天已经不能再帮别人砍价了';
  510. break;
  511. case 4:
  512. $msg = '砍价失败';
  513. break;
  514. }
  515. echo json_encode( array('code' => $state, 'msg' => $msg) );
  516. die();
  517. }else{
  518. $bargain_order = M('bargain_order')->field('total_money,deal_money')->where( array('id' => $id) )->find();
  519. $bargain_order['kan_money'] = round($bargain_order['total_money'] - $bargain_order['deal_money'],2);
  520. $bargain_order['deal_money'] = round($bargain_order['deal_money'],2);
  521. $order_detail = M('bargain_order_detail')->where( array('member_id' => $member_id, 'bargain_order_id' => $id) )->find();
  522. $order_detail['kan_money'] = round($order_detail['kan_money'], 2);
  523. $li_html = "";
  524. $li_html.= '<li class="user-item">';
  525. $li_html.= ' <img src="'.$order_detail['avatar'].'" />';
  526. $li_html.= ' <div class="text">';
  527. $li_html.= ' <p class="nickname">'.$order_detail['nickname'].'</p>';
  528. $li_html.= ' <p class="hint">看我青龙偃月刀</p>';
  529. $li_html.= ' </div>';
  530. $li_html.= ' <div class="amount">';
  531. $li_html.= ' <div class="knives knives-1">';
  532. $li_html.= ' <div class="knife"></div>';
  533. $li_html.= ' </div>';
  534. $li_html.= ' 砍掉';
  535. $li_html.= ' <i class="detail ">'.$order_detail['kan_money'].'</i>';
  536. $li_html.= ' 元';
  537. $li_html.= ' </div>';
  538. $li_html.= '</li>';
  539. echo json_encode( array('code' => 0,'li_html' => $li_html, 'order_detail' => $order_detail, 'bargain_order' => $bargain_order) );
  540. die();
  541. }
  542. }
  543. /**
  544. 自己开启一个砍价
  545. **/
  546. public function get_user_goods_bargain()
  547. {
  548. $member_id = session('user_auth.uid');
  549. $address_id = I('post.address_id');
  550. $goods_id = I('post.goods_id');
  551. $need_optionc = I('post.need_optionc');
  552. //寻找现在是否有未完成的订单,如果有的话,那么就不要继续生成了
  553. //$goods_info = M('goods')->field('pin_count,bargain_count,pin_hour,image,price,name')->where( array('goods_id' => $goods_id) )->find();
  554. $goods_model = D('Home/Goods');
  555. //寻找现在是否有未完成的订单,如果有的话,那么就不要继续生成了
  556. $goods_info = M('goods')->field('image,name')->where( array('goods_id' => $goods_id) )->find();
  557. $bargain_goods_info = M('bargain_goods')->where( array('goods_id' => $goods_id) )->find();
  558. //pin_count,pin_hour,
  559. //pin_price
  560. $now_time = time();
  561. $old_zan_order = M('bargain_order')->where("member_id={$member_id} and goods_id={$goods_id} and state =0 and begin_time < {$now_time} and end_time > {$now_time}")->find();
  562. if( !empty($old_zan_order) )
  563. {
  564. //一个会员当前一个商品 只能发出去一个二维码
  565. echo json_encode( array('code' => 0, 'id' => $old_zan_order['id']) );
  566. die();
  567. }
  568. //zan_order
  569. $zan_order_data = array();
  570. $zan_order_data['member_id'] = $member_id;
  571. $zan_order_data['goods_id'] = $goods_id;
  572. $zan_order_data['sku_str'] = $need_optionc;
  573. $zan_order_data['state'] = 0;
  574. $price = $bargain_goods_info['bargain_price'];
  575. //goods_option_mult_value
  576. if( !empty($need_optionc) )
  577. {
  578. $goods_option_mult_value = M('goods_option_mult_value')->where( array('rela_goodsoption_valueid' => $need_optionc,'goods_id' =>$goods_id) )->find();
  579. if(!empty($goods_option_mult_value))
  580. {
  581. $price = $goods_option_mult_value['pin_price'];
  582. }
  583. }
  584. $zan_order_data['need_person'] = empty($bargain_goods_info['bargain_count']) ? 1: $bargain_goods_info['bargain_count'];
  585. $zan_order_data['total_person'] = empty($bargain_goods_info['bargain_count']) ? 1: $bargain_goods_info['bargain_count'];
  586. $zan_order_data['deal_money'] = $price;
  587. $zan_order_data['total_money'] = $price;
  588. $zan_order_data['begin_time'] = $now_time;
  589. $zan_order_data['end_time'] = $now_time + 3600 * $bargain_goods_info['hour'];
  590. $zan_order_data['add_time'] = time();
  591. $zan_order_data['order_id'] = 0;
  592. $zan_order_data['address_id'] = $address_id;
  593. $zan_order_id = M('bargain_order')->add($zan_order_data);
  594. //bargain_order_detail
  595. //member_id kan_money bargain_order_id addtime avatar nickname
  596. $state = $this->_bang_kan_order($zan_order_id,true);
  597. //subject/assist_bargain_coupon_detail/id/5.html
  598. $redir_url = U('subject/assist_bargain_coupon_detail' , array('id' => $zan_order_id));
  599. echo json_encode( array('code' =>0, 'state' => $state,'gourl' => $redir_url,'bargain_order_id' => $zan_order_id) );
  600. die();
  601. }
  602. //进行中
  603. public function index(){
  604. $per_page = 10;
  605. $page = I('post.page',1);
  606. $offset = ($page - 1) * $per_page;
  607. $subject_id = I('get.id',0);
  608. $this->subject_id = $subject_id;
  609. $this->fromindex = I('get.fromindex',0);
  610. $subject = M('subject')->where( array('id' => $subject_id) )->find();
  611. $this->subject = $subject;
  612. if($subject['type'] == 'niyuan' && $this->fromindex == 1)
  613. {
  614. $niyuansubjects = M('subject')->where( 'id != '.$subject_id.' and type="niyuan" and begin_time<'.time().' and end_time > '.time() )->order('id asc')->limit(4)->select();
  615. $this->niyuansubjects = $niyuansubjects;
  616. }
  617. if($subject){
  618. $sql = 'select sg.state,g.goods_id,g.name,g.quantity,g.price,g.danprice,g.image,g.fan_image,g.store_id,g.seller_count from '.C('DB_PREFIX')."subject_goods as sg , ".C('DB_PREFIX')."goods as g
  619. where subject_id = ".$subject['id']." and sg.state =1 and sg.goods_id = g.goods_id and g.status =1 and g.quantity >0 order by sg.id asc limit {$offset},{$per_page}";
  620. $list = M()->query($sql);
  621. foreach($list as $key => $v){
  622. if(!empty($v['fan_image'])){
  623. $list[$key]['image']= $v['fan_image'];
  624. }
  625. }
  626. $this->list = $list;
  627. } else {
  628. $this->list = array();
  629. }
  630. $type_template = array();
  631. $type_template['normal'] = array('html' => 'normal_index',
  632. 'fetch_html' => 'Subject:normal_ajax_fetch');
  633. $type_template['zeyuan'] = array(
  634. 'html' => 'zeyuan_index',
  635. 'fetch_html' => 'Subject:zeyuan_ajax_fetch'
  636. );
  637. $type_template['niyuan'] = array(
  638. 'html' => 'niyuan_index',
  639. 'fetch_html' => 'Subject:niyuan_ajax_fetch'
  640. );
  641. $type_template['oneyuan'] = array(
  642. 'html' => 'normal_index',
  643. 'fetch_html' => 'Subject:normal_ajax_fetch'
  644. );
  645. if($page > 1) {
  646. $result = array('code' => 0);
  647. if(!empty($list)) {
  648. $result['code'] = 1;
  649. $result['html'] = $this->fetch($type_template[$subject['type']]['fetch_html']);
  650. }
  651. echo json_encode($result);
  652. die();
  653. }
  654. $this->display($type_template[$subject['type']]['html']);
  655. }
  656. //未开始
  657. public function wait()
  658. {
  659. $per_page = 10;
  660. $page = I('post.page',1);
  661. $offset = ($page - 1) * $per_page;
  662. $sql = 'select sg.state,sg.begin_time,sg.end_time,g.goods_id,g.name,g.quantity,g.pinprice,g.price,g.image from '.C('DB_PREFIX')."spike_goods as sg , ".C('DB_PREFIX')."goods as g
  663. where sg.state =1 and sg.goods_id = g.goods_id and g.status =1 and g.quantity >0 and sg.begin_time > ".time()." order by sg.begin_time asc limit {$offset},{$per_page}";
  664. $list = M()->query($sql);
  665. foreach ($list as $k => $v) {
  666. $list[$k]['image']=resize($v['image'], C('spike_thumb_width'), C('spike_thumb_height'));
  667. }
  668. $this->list = $list;
  669. $result = array('code' => 0);
  670. if(!empty($list)) {
  671. $result['code'] = 1;
  672. $result['html'] = $this->fetch('Widget:spike_ajax_wait_fetch');
  673. }
  674. echo json_encode($result);
  675. die();
  676. }
  677. public function over()
  678. {
  679. $per_page = 10;
  680. $page = I('post.page',1);
  681. $offset = ($page - 1) * $per_page;
  682. $sql = 'select sg.state,sg.begin_time,sg.end_time,g.goods_id,g.name,g.quantity,g.pinprice,g.price,g.image from '.C('DB_PREFIX')."spike_goods as sg , ".C('DB_PREFIX')."goods as g
  683. where sg.state =1 and sg.goods_id = g.goods_id and g.quantity =0 order by sg.begin_time asc limit {$offset},{$per_page}";
  684. $list = M()->query($sql);
  685. foreach ($list as $k => $v) {
  686. $list[$k]['image']=resize($v['image'], C('spike_thumb_width'), C('spike_thumb_height'));
  687. }
  688. $this->list = $list;
  689. $result = array('code' => 0);
  690. if(!empty($list)) {
  691. $result['code'] = 1;
  692. $result['html'] = $this->fetch('Widget:spike_ajax_over_fetch');
  693. }
  694. echo json_encode($result);
  695. die();
  696. }
  697. }