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