Sellergroupbuy.php 20 KB

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