Sellergroupbuy.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. use think\facade\Db;
  6. /**
  7. *
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * 控制器
  12. */
  13. class Sellergroupbuy extends BaseSeller
  14. {
  15. public function initialize()
  16. {
  17. parent::initialize(); // TODO: Change the autogenerated stub
  18. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/sellergroupbuy.lang.php');
  19. //检查抢购功能是否开启
  20. if (intval(config('ds_config.groupbuy_allow')) !== 1) {
  21. $this->error(lang('groupbuy_unavailable'), 'seller/index');
  22. }
  23. }
  24. /**
  25. * 默认显示抢购列表
  26. * */
  27. public function index()
  28. {
  29. $this->groupbuy_list();
  30. return View::fetch($this->template_dir . 'index');
  31. }
  32. /**
  33. * 抢购套餐购买
  34. * */
  35. public function groupbuy_quota_add()
  36. {
  37. //输出导航
  38. $this->setSellerCurMenu('Sellergroupbuy');
  39. $this->setSellerCurItem('groupbuy_quota_add');
  40. return View::fetch($this->template_dir . 'groupbuy_quota_add');
  41. }
  42. /**
  43. * 抢购套餐购买保存
  44. * */
  45. public function groupbuy_quota_add_save()
  46. {
  47. if (intval(config('ds_config.groupbuy_price')) == 0) {
  48. ds_json_encode(10001, lang('param_error'));
  49. }
  50. $groupbuy_quota_quantity = intval(input('post.groupbuy_quota_quantity'));
  51. if ($groupbuy_quota_quantity <= 0) {
  52. ds_json_encode(10001, lang('purchase_quantity_cannot_empty'));
  53. }
  54. $groupbuyquota_model = model('groupbuyquota');
  55. //获取当前价格
  56. $current_price = intval(config('ds_config.groupbuy_price'));
  57. //获取该用户已有套餐
  58. $current_groupbuy_quota = $groupbuyquota_model->getGroupbuyquotaCurrent(session('store_id'));
  59. $add_time = 86400 * 30 * $groupbuy_quota_quantity;
  60. if (empty($current_groupbuy_quota)) {
  61. //生成套餐
  62. $param = array();
  63. $param['member_id'] = session('member_id');
  64. $param['member_name'] = session('member_name');
  65. $param['store_id'] = session('store_id');
  66. $param['store_name'] = session('store_name');
  67. $param['groupbuyquota_starttime'] = TIMESTAMP;
  68. $param['groupbuyquota_endtime'] = TIMESTAMP + $add_time;
  69. $groupbuyquota_model->addGroupbuyquota($param);
  70. } else {
  71. $param = array();
  72. $param['groupbuyquota_endtime'] = Db::raw('groupbuyquota_endtime+' . $add_time);
  73. $groupbuyquota_model->editGroupbuyquota($param, array('groupbuyquota_id' => $current_groupbuy_quota['groupbuyquota_id']));
  74. }
  75. //记录店铺费用
  76. $this->recordStorecost($current_price * $groupbuy_quota_quantity, lang('buy_to_snap_up'));
  77. $this->recordSellerlog(lang('buy') . $groupbuy_quota_quantity . lang('snap_up_package') . $current_price . lang('ds_yuan'));
  78. ds_json_encode(10000, lang('groupbuy_quota_add_success'));
  79. }
  80. /**
  81. * 抢购列表
  82. * */
  83. public function groupbuy_list()
  84. {
  85. $groupbuy_model = model('groupbuy');
  86. $groupbuyquota_model = model('groupbuyquota');
  87. if (check_platform_store()) {
  88. View::assign('isPlatformStore', true);
  89. } else {
  90. $current_groupbuy_quota = $groupbuyquota_model->getGroupbuyquotaCurrent(session('store_id'));
  91. View::assign('current_groupbuy_quota', $current_groupbuy_quota);
  92. }
  93. $condition = array();
  94. $condition[] = array('store_id', '=', session('store_id'));
  95. if ((input('param.groupbuy_state'))) {
  96. $condition[] = array('groupbuy_state', '=', input('param.groupbuy_state'));
  97. }
  98. $condition[] = array('groupbuy_name', 'like', '%' . input('param.groupbuy_name') . '%');
  99. if (strlen($groupbuy_vr = trim(input('param.groupbuy_vr')))) {
  100. $condition[] = array('groupbuy_is_vr', '=', $groupbuy_vr ? 1 : 0);
  101. View::assign('groupbuy_vr', $groupbuy_vr);
  102. }
  103. $groupbuy_list = $groupbuy_model->getGroupbuyExtendList($condition, 10);
  104. //halt($groupbuy_list);
  105. View::assign('group', $groupbuy_list);
  106. View::assign('show_page', $groupbuy_model->page_info->render());
  107. View::assign('groupbuy_state_array', $groupbuy_model->getGroupbuyStateArray());
  108. $this->setSellerCurMenu('Sellergroupbuy');
  109. $this->setSellerCurItem('groupbuy_list');
  110. }
  111. /**
  112. * 添加抢购页面
  113. * */
  114. public function groupbuy_add()
  115. {
  116. $groupbuyquota_model = model('groupbuyquota');
  117. if (check_platform_store()) {
  118. View::assign('isPlatformStore', true);
  119. } else {
  120. $current_groupbuy_quota = $groupbuyquota_model->getGroupbuyquotaCurrent(session('store_id'));
  121. // halt($current_groupbuy_quota);
  122. if (empty($current_groupbuy_quota)) {
  123. if (intval(config('ds_config.groupbuy_price')) != 0) {
  124. $this->error(lang('please_buy_package_first'), (string) url('Sellergroupbuy/groupbuy_quota_add'));
  125. } else {
  126. $current_groupbuy_quota = array('groupbuyquota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  127. }
  128. }
  129. View::assign('current_groupbuy_quota', $current_groupbuy_quota);
  130. }
  131. // 根据后台设置的审核期重新设置抢购开始时间
  132. View::assign('groupbuy_starttime', TIMESTAMP + intval(config('ds_config.groupbuy_review_day')) * 86400);
  133. View::assign('groupbuy_classes', model('groupbuy')->getGroupbuyClasses());
  134. $this->setSellerCurMenu('Sellergroupbuy');
  135. $this->setSellerCurItem('groupbuy_add');
  136. return View::fetch($this->template_dir . 'groupbuy_add');
  137. }
  138. /**
  139. * 抢购保存
  140. * */
  141. public function groupbuy_save()
  142. {
  143. //获取提交的数据
  144. $goods_id = intval(input('post.groupbuy_goods_id'));
  145. if (empty($goods_id)) {
  146. $this->error(lang('param_error'));
  147. }
  148. $groupbuy_model = model('groupbuy');
  149. $goods_model = model('goods');
  150. $groupbuyquota_model = model('groupbuyquota');
  151. if (!check_platform_store()) {
  152. // 检查套餐
  153. $current_groupbuy_quota = $groupbuyquota_model->getGroupbuyquotaCurrent(session('store_id'));
  154. if (empty($current_groupbuy_quota) && intval(config('ds_config.groupbuy_price')) != 0) {
  155. $this->error(lang('please_buy_package_first'), (string) url('Store_groupbuy/groupbuy_quota_add'));
  156. }
  157. }
  158. $goods_info = $goods_model->getGoodsInfoByID($goods_id);
  159. if (empty($goods_info) || $goods_info['store_id'] != session('store_id')) {
  160. $this->error(lang('param_error'));
  161. }
  162. $param = array();
  163. $param['groupbuy_name'] = input('post.groupbuy_name');
  164. $param['groupbuy_remark'] = input('post.remark');
  165. $param['groupbuy_starttime'] = strtotime(input('post.start_time'));
  166. $param['groupbuy_endtime'] = strtotime(input('post.end_time'));
  167. $param['groupbuy_price'] = floatval(input('post.groupbuy_price'));
  168. $param['groupbuy_rebate'] = ds_price_format(floatval(input('post.groupbuy_price')) / floatval($goods_info['goods_price']) * 10);
  169. $param['groupbuy_image'] = input('post.groupbuy_image');
  170. $param['groupbuy_image1'] = input('post.groupbuy_image1');
  171. $param['virtual_quantity'] = intval(input('post.virtual_quantity'));
  172. $param['groupbuy_upper_limit'] = intval(input('post.upper_limit'));
  173. $param['groupbuy_intro'] = input('post.groupbuy_intro');
  174. $param['gclass_id'] = input('post.gclass_id', 0);
  175. $param['goods_id'] = $goods_info['goods_id'];
  176. $param['goods_commonid'] = $goods_info['goods_commonid'];
  177. $param['goods_name'] = $goods_info['goods_name'];
  178. $param['goods_price'] = $goods_info['goods_price'];
  179. $param['store_id'] = session('store_id');
  180. $param['store_name'] = session('store_name');
  181. // 虚拟抢购
  182. if (input('param.vr')) {
  183. if ($param['groupbuy_upper_limit'] > 0 && $goods_info['virtual_limit'] > 0 && $param['groupbuy_upper_limit'] > $goods_info['virtual_limit']) {
  184. $this->error(sprintf(lang('virtual_panic_buying'), $param['groupbuy_upper_limit'], $goods_info['virtual_limit']), (string) url('Sellergroupbuy/index'));
  185. }
  186. $param += array(
  187. 'groupbuy_is_vr' => 1,
  188. 'vr_class_id' => (int) input('post.class'),
  189. 'vr_s_class_id' => (int) input('post.s_class'),
  190. );
  191. }
  192. //保存
  193. $result = $groupbuy_model->addGroupbuy($param);
  194. if ($result) {
  195. // 自动发布动态
  196. // group_id,group_name,goods_id,goods_price,groupbuy_price,group_pic,rebate,start_time,end_time
  197. $data_array = array();
  198. $data_array['group_id'] = $result;
  199. $data_array['group_name'] = $param['groupbuy_name'];
  200. $data_array['store_id'] = session('store_id');
  201. $data_array['goods_id'] = $param['goods_id'];
  202. $data_array['goods_price'] = $param['goods_price'];
  203. $data_array['groupbuy_price'] = $param['groupbuy_price'];
  204. $data_array['group_pic'] = $param['groupbuy_image1'];
  205. $data_array['rebate'] = $param['groupbuy_rebate'];
  206. $data_array['start_time'] = $param['groupbuy_starttime'];
  207. $data_array['end_time'] = $param['groupbuy_endtime'];
  208. $this->storeAutoShare($data_array, 'groupbuy');
  209. $this->recordSellerlog(lang('release_snap_up') . $param['groupbuy_name'] . ',' . lang('groupbuy_index_goods_name') . ':' . $param['goods_name']);
  210. $this->success(lang('groupbuy_add_success'), (string) url('Sellergroupbuy/index'));
  211. } else {
  212. $this->error(lang('groupbuy_add_fail'), (string) url('Sellergroupbuy/index'));
  213. }
  214. }
  215. public function groupbuy_goods_info()
  216. {
  217. $goods_commonid = intval(input('param.goods_commonid'));
  218. $data = array();
  219. $data['result'] = true;
  220. $goods_model = model('goods');
  221. $condition = array();
  222. $condition[] = array('goods_commonid', '=', $goods_commonid);
  223. $goods_list = $goods_model->getGoodsOnlineList($condition);
  224. if (empty($goods_list)) {
  225. $data['result'] = false;
  226. $data['message'] = lang('param_error');
  227. echo json_encode($data);
  228. die;
  229. }
  230. $goods_info = $goods_list[0];
  231. $data['goods_id'] = $goods_info['goods_id'];
  232. $data['goods_name'] = $goods_info['goods_name'];
  233. $data['goods_price'] = $goods_info['goods_price'];
  234. $data['goods_image'] = goods_thumb($goods_info, 240);
  235. $data['goods_href'] = (string) url('Goods/index', array('goods_id' => $goods_info['goods_id']));
  236. if ($goods_info['is_virtual']) {
  237. $data['is_virtual'] = 1;
  238. $data['virtual_indate'] = $goods_info['virtual_indate'];
  239. $data['virtual_indate_str'] = date('Y-m-d H:i', $goods_info['virtual_indate']);
  240. $data['virtual_limit'] = $goods_info['virtual_limit'];
  241. }
  242. echo json_encode($data);
  243. die;
  244. }
  245. public function check_groupbuy_goods()
  246. {
  247. $start_time = strtotime(input('param.start_time'));
  248. $goods_id = input('param.goods_id');
  249. $groupbuy_model = model('groupbuy');
  250. $data = array();
  251. $data['result'] = true;
  252. //检查商品是否已经参加同时段活动
  253. $condition = array();
  254. $condition[] = array('groupbuy_endtime', '>', $start_time);
  255. $condition[] = array('goods_id', '=', $goods_id);
  256. $groupbuy_list = $groupbuy_model->getGroupbuyAvailableList($condition);
  257. if (!empty($groupbuy_list)) {
  258. $data['result'] = false;
  259. echo json_encode($data);
  260. die;
  261. }
  262. echo json_encode($data);
  263. die;
  264. }
  265. /**
  266. * 上传图片
  267. * */
  268. public function image_upload()
  269. {
  270. $old_groupbuy_image = input('post.old_groupbuy_image');
  271. if (!empty($old_groupbuy_image)) {
  272. $this->_image_del($old_groupbuy_image);
  273. }
  274. $this->_image_upload('groupbuy_image');
  275. }
  276. private function _image_upload($file)
  277. {
  278. $data = array();
  279. $data['result'] = true;
  280. if (!empty($_FILES[$file]['name'])) {
  281. $upload_path = BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_GROUPBUY . DIRECTORY_SEPARATOR . session('store_id') . DIRECTORY_SEPARATOR;
  282. $file_name = session('store_id') . '_' . date('YmdHis') . rand(10000, 99999) . '.png';
  283. $res = ds_upload_pic(ATTACH_GROUPBUY . DIRECTORY_SEPARATOR . session('store_id'), $file, $file_name);
  284. if ($res['code']) {
  285. $file_name = $res['data']['file_name'];
  286. $pic = $file_name;
  287. $data['file_name'] = $pic;
  288. $data['origin_file_name'] = $_FILES[$file]['name'];
  289. $data['file_url'] = ds_get_pic(ATTACH_GROUPBUY . DIRECTORY_SEPARATOR . session('store_id'), $pic);
  290. ds_create_thumb($upload_path, $file_name, '120,420', '120,420', '_small,_normal');
  291. } else {
  292. $data['result'] = false;
  293. $data['message'] = $res['msg'];
  294. }
  295. } else {
  296. $data['result'] = false;
  297. }
  298. echo json_encode($data);
  299. die;
  300. }
  301. /**
  302. * 图片删除
  303. */
  304. private function _image_del($image_name)
  305. {
  306. $image_array = explode('_', $image_name);
  307. $store_id = $image_array[0];
  308. //判断只能删除当前用户下的文件
  309. if ($store_id != session('store_id')) {
  310. return;
  311. }
  312. //防止构造恶意 ../ 进行任意删除文件
  313. if (strpos($image_name, '..') !== false) {
  314. return;
  315. }
  316. $upload_path = BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_GROUPBUY . DIRECTORY_SEPARATOR . $store_id . DIRECTORY_SEPARATOR;
  317. ds_unlink($upload_path, $image_name);
  318. }
  319. /**
  320. * 选择活动商品
  321. * */
  322. public function search_goods()
  323. {
  324. $goods_model = model('goods');
  325. $condition = array();
  326. $condition[] = array('store_id', '=', session('store_id'));
  327. $condition[] = array('goods_name', 'like', '%' . input('param.goods_name') . '%');
  328. $goods_list = $goods_model->getGoodsCommonListForPromotion($condition, '*', 8, 'groupbuy');
  329. View::assign('goods_list', $goods_list);
  330. View::assign('show_page', $goods_model->page_info->render());
  331. echo View::fetch($this->template_dir . 'search_goods');
  332. exit;
  333. }
  334. /**
  335. * 添加虚拟抢购页面
  336. */
  337. public function groupbuy_add_vr()
  338. {
  339. $groupbuyquota_model = model('groupbuyquota');
  340. if (check_platform_store()) {
  341. View::assign('isPlatformStore', true);
  342. } else {
  343. $current_groupbuy_quota = $groupbuyquota_model->getGroupbuyquotaCurrent(session('store_id'));
  344. if (empty($current_groupbuy_quota)) {
  345. if (intval(config('ds_config.groupbuy_price')) != 0) {
  346. $this->error(lang('please_buy_package_first'), (string) url('Sellergroupbuy/groupbuy_quota_add'));
  347. } else {
  348. $current_groupbuy_quota = array('groupbuyquota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  349. }
  350. }
  351. View::assign('current_groupbuy_quota', $current_groupbuy_quota);
  352. }
  353. // 根据后台设置的审核期重新设置抢购开始时间
  354. View::assign('groupbuy_starttime', TIMESTAMP + intval(config('ds_config.groupbuy_review_day')) * 86400);
  355. // 虚拟抢购分类
  356. View::assign('groupbuy_vr_classes', model('groupbuy')->getGroupbuyVrClasses());
  357. $vrgroupbuyclass_model = model('vrgroupbuyclass');
  358. $classlist = $vrgroupbuyclass_model->getVrgroupbuyclassList(array('vrgclass_parent_id' => 0));
  359. View::assign('classlist', $classlist);
  360. $this->setSellerCurMenu('Sellergroupbuy');
  361. $this->setSellerCurItem('groupbuy_add_vr');
  362. return View::fetch($this->template_dir . 'groupbuy_add_vr');
  363. }
  364. public function ajax_vr_class()
  365. {
  366. $vrgclass_id = intval(input('param.vrgclass_id'));
  367. if (empty($vrgclass_id)) {
  368. exit('false');
  369. }
  370. $condition = array();
  371. $condition[] = array('vrgclass_parent_id', '=', $vrgclass_id);
  372. $vrgroupbuyclass_model = model('vrgroupbuyclass');
  373. $class_list = $vrgroupbuyclass_model->getVrgroupbuyclassList($condition);
  374. if (!empty($class_list)) {
  375. echo json_encode($class_list);
  376. } else {
  377. echo 'false';
  378. }
  379. exit;
  380. }
  381. /**
  382. * 选择活动虚拟商品
  383. */
  384. public function search_vr_goods()
  385. {
  386. $goods_model = model('goods');
  387. $condition = array();
  388. $condition[] = array('store_id', '=', session('store_id'));
  389. $condition[] = array('goods_name', 'like', '%' . input('param.goods_name') . '%');
  390. $goods_list = $goods_model->getGoodsCommonListForVrPromotion($condition, '*', 8);
  391. View::assign('goods_list', $goods_list);
  392. View::assign('show_page', $goods_model->page_info->render());
  393. echo View::fetch($this->template_dir . 'search_goods');
  394. }
  395. /**
  396. * 用户中心右边,小导航
  397. *
  398. * @param string $name 当前导航的name
  399. * @param array $array 附加菜单
  400. * @return
  401. */
  402. protected function getSellerItemList()
  403. {
  404. $menu_array = array(
  405. array(
  406. 'name' => 'groupbuy_list', 'text' => lang('ds_member_path_group_list'),
  407. 'url' => (string) url('Sellergroupbuy/index')
  408. )
  409. );
  410. switch (request()->action()) {
  411. case 'groupbuy_add':
  412. $menu_array[] = array(
  413. 'name' => 'groupbuy_add', 'text' => lang('ds_member_path_new_group'),
  414. 'url' => (string) url('Sellergroupbuy/groupbuy_add')
  415. );
  416. break;
  417. case 'groupbuy_add_vr':
  418. $menu_array[] = array(
  419. 'name' => 'groupbuy_add_vr', 'text' => lang('new_virtual_panic_buying'), 'url' => (string) url('Sellergroupbuy/groupbuy_add_vr')
  420. );
  421. break;
  422. case 'groupbuy_quota_add':
  423. $menu_array[] = array(
  424. 'name' => 'groupbuy_quota_add', 'text' => lang('purchase_plan'),
  425. 'url' => (string) url('Sellergroupbuy/groupbuy_quota_add')
  426. );
  427. break;
  428. case 'groupbuy_edit':
  429. $menu_array[] = array(
  430. 'name' => 'groupbuy_edit', 'text' => lang('ds_member_path_edit_group'),
  431. 'url' => (string) url('Sellergroupbuy/index')
  432. );
  433. break;
  434. case 'cancel':
  435. $menu_array[] = array('name' => 'groupbuy_cancel', 'text' => lang('ds_member_path_cancel_group'));
  436. break;
  437. }
  438. return $menu_array;
  439. }
  440. }