Sellerpromotionbooth.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. * ============================================================================
  7. *
  8. * ============================================================================
  9. *
  10. * ----------------------------------------------------------------------------
  11. *
  12. * ============================================================================
  13. * 控制器
  14. */
  15. class Sellerpromotionbooth extends BaseSeller
  16. {
  17. public function initialize()
  18. {
  19. parent::initialize(); // TODO: Change the autogenerated stub
  20. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/sellerpromotionbooth.lang.php');
  21. if (intval(config('ds_config.promotion_allow')) !== 1) {
  22. $this->error(lang('promotion_unavailable'), (string) url('Seller/index'));
  23. }
  24. }
  25. /**
  26. * 套餐商品列表
  27. */
  28. public function index()
  29. {
  30. $pbooth_model = model('pbooth');
  31. // 更新套餐状态
  32. $where = array();
  33. $where[] = array('store_id', '=', session('store_id'));
  34. $where[] = array('boothquota_endtime', '<', TIMESTAMP);
  35. $pbooth_model->editBoothClose($where);
  36. $isPlatformStore = check_platform_store() ? true : false;
  37. View::assign('isPlatformStore', $isPlatformStore);
  38. $hasList = $isPlatformStore;
  39. if (!$isPlatformStore) {
  40. // 检查是否已购买套餐
  41. $where = array();
  42. $where[] = array('store_id', '=', session('store_id'));
  43. $booth_quota = $pbooth_model->getBoothquotaInfo($where);
  44. View::assign('booth_quota', $booth_quota);
  45. if (!empty($booth_quota)) {
  46. $hasList = true;
  47. } elseif (intval(config('ds_config.promotion_booth_price')) == 0) {
  48. $hasList = true;
  49. } else {
  50. $update = array('boothgoods_state' => $pbooth_model::STATE0);
  51. $pbooth_model->editBooth($update, $where);
  52. }
  53. }
  54. if ($hasList) {
  55. // 查询已选择商品
  56. $boothgoods_list = $pbooth_model->getBoothgoodsList(array(array('store_id', '=', session('store_id'))), 'goods_id');
  57. if (!empty($boothgoods_list)) {
  58. $goodsid_array = array();
  59. foreach ($boothgoods_list as $val) {
  60. $goodsid_array[] = $val['goods_id'];
  61. }
  62. $goods_list = model('goods')->getGoodsList(array(array('goods_id', 'in', $goodsid_array)), 'goods_id,goods_name,goods_image,goods_price,store_id,gc_id');
  63. if (!empty($goods_list)) {
  64. $gcid_array = array(); // 商品分类id
  65. foreach ($goods_list as $key => $val) {
  66. $gcid_array[] = $val['gc_id'];
  67. $goods_list[$key]['goods_image'] = goods_thumb($val);
  68. $goods_list[$key]['url'] = (string) url('Goods/index', array('goods_id' => $val['goods_id']));
  69. }
  70. $goodsclass_list = model('goodsclass')->getGoodsclassListByIds($gcid_array);
  71. $goodsclass_list = array_under_reset($goodsclass_list, 'gc_id');
  72. View::assign('goods_list', $goods_list);
  73. View::assign('goodsclass_list', $goodsclass_list);
  74. }
  75. }
  76. }
  77. $this->setSellerCurMenu('Sellerpromotionbooth');
  78. $this->setSellerCurItem('index');
  79. return View::fetch($this->template_dir . 'index');
  80. }
  81. /**
  82. * 选择商品
  83. */
  84. public function booth_select_goods()
  85. {
  86. $goods_model = model('goods');
  87. $condition = array();
  88. $condition[] = array('store_id', '=', session('store_id'));
  89. $goods_name = input('post.goods_name');
  90. if ($goods_name != '') {
  91. $condition[] = array('goods_name', 'like', '%' . $goods_name . '%');
  92. }
  93. $goods_list = $goods_model->getGoodsOnlineList($condition, '*', 10);
  94. View::assign('goods_list', $goods_list);
  95. View::assign('show_page', $goods_model->page_info->render());
  96. echo View::fetch($this->template_dir . 'select_goods');
  97. }
  98. /**
  99. * 购买套餐
  100. */
  101. public function booth_quota_add()
  102. {
  103. if (request()->isPost()) {
  104. if (intval(config('ds_config.promotion_booth_price')) == 0) {
  105. ds_json_encode(10001, lang('param_error'));
  106. }
  107. $quantity = intval(input('post.booth_quota_quantity')); // 购买的数量(月)
  108. $price_quantity = $quantity * intval(config('ds_config.promotion_booth_price')); // 扣款数
  109. if ($quantity <= 0 || $quantity > 12) {
  110. ds_json_encode(10001, lang('param_error'));
  111. }
  112. // 实例化模型
  113. $pbooth_model = model('pbooth');
  114. $data = array();
  115. $data['store_id'] = session('store_id');
  116. $data['store_name'] = session('store_name');
  117. $data['boothquota_starttime'] = TIMESTAMP;
  118. $data['boothquota_endtime'] = TIMESTAMP + 60 * 60 * 24 * 30 * $quantity;
  119. $data['boothquota_state'] = 1;
  120. $return = $pbooth_model->addBoothquota($data);
  121. if ($return) {
  122. // 添加店铺费用记录
  123. $this->recordStorecost($price_quantity, '购买推荐展位');
  124. // 添加任务队列
  125. $end_time = TIMESTAMP + 60 * 60 * 24 * 30 * $quantity;
  126. $this->addcron(array('cron_exetime' => $end_time, 'cron_value' => serialize(intval(session('store_id'))), 'cron_type' => 'editBoothClose'), true);
  127. $this->recordSellerlog('购买' . $quantity . '套推荐展位,单位元');
  128. ds_json_encode(10000, lang('ds_common_op_succ'));
  129. } else {
  130. ds_json_encode(10001, lang('ds_common_op_fail'));
  131. }
  132. }
  133. // 输出导航
  134. $this->setSellerCurMenu('Sellerpromotionbooth');
  135. $this->setSellerCurItem('booth_quota_add');
  136. return View::fetch($this->template_dir . 'quota_add');
  137. }
  138. /**
  139. * 套餐续费
  140. */
  141. public function booth_renew()
  142. {
  143. if (request()->isPost()) {
  144. if (intval(config('ds_config.promotion_booth_price')) == 0) {
  145. ds_json_encode(10001, lang('param_error'));
  146. }
  147. $pbooth_model = model('pbooth');
  148. $quantity = intval(input('post.booth_quota_quantity')); // 购买数量(月)
  149. $price_quantity = $quantity * intval(config('ds_config.promotion_booth_price')); // 扣款数
  150. if ($quantity <= 0 || $quantity > 12) {
  151. ds_json_encode(10001, lang('param_error'));
  152. }
  153. $condition = array();
  154. $condition[] = array('store_id', '=', session('store_id'));
  155. $booth_quota = $pbooth_model->getBoothquotaInfo($condition);
  156. if ($booth_quota['boothquota_endtime'] > TIMESTAMP) {
  157. // 套餐未超时(结束时间+购买时间)
  158. $update['boothquota_endtime'] = intval($booth_quota['boothquota_endtime']) + 60 * 60 * 24 * 30 * $quantity;
  159. } else {
  160. // 套餐已超时(当前时间+购买时间)
  161. $update['boothquota_endtime'] = TIMESTAMP + 60 * 60 * 24 * 30 * $quantity;
  162. }
  163. $return = $pbooth_model->editBoothquotaOpen($update, $condition);
  164. if ($return) {
  165. // 添加店铺费用记录
  166. $this->recordStorecost($price_quantity, '购买推荐展位');
  167. // 添加任务队列
  168. $end_time = TIMESTAMP + 60 * 60 * 24 * 30 * $quantity;
  169. $this->addcron(array('cron_exetime' => $end_time, 'cron_value' => serialize(intval(session('store_id'))), 'cron_type' => 'editBoothClose'), true);
  170. $this->recordSellerlog('续费' . $quantity . '套推荐展位,单位元');
  171. ds_json_encode(10000, lang('ds_common_op_succ'));
  172. } else {
  173. ds_json_encode(10001, lang('ds_common_op_fail'));
  174. }
  175. }
  176. $this->setSellerCurMenu('Sellerpromotionbooth');
  177. $this->setSellerCurItem('booth_renew');
  178. return View::fetch($this->template_dir . 'quota_add');
  179. }
  180. /**
  181. * 选择商品
  182. */
  183. public function choosed_goods()
  184. {
  185. $data = array();
  186. $data['result'] = 'true';
  187. $gid = input('param.gid');
  188. if ($gid <= 0) {
  189. $data = array('result' => 'false', 'msg' => lang('param_error'));
  190. $this->_echoJson($data);
  191. }
  192. // 验证商品是否存在
  193. $goods_info = model('goods')->getGoodsInfoByID($gid);
  194. if (empty($goods_info) || $goods_info['store_id'] != session('store_id')) {
  195. $data = array('result' => 'false', 'msg' => lang('param_error'));
  196. $this->_echoJson($data);
  197. }
  198. $pbooth_model = model('pbooth');
  199. if (!check_platform_store()) {
  200. // 验证套餐时候过期
  201. $booth_info = $pbooth_model->getBoothquotaInfo(array(
  202. array('store_id', '=', session('store_id')),
  203. array('boothquota_endtime', '>', TIMESTAMP)
  204. ), 'boothquota_id');
  205. if (empty($booth_info)) {
  206. if (intval(config('ds_config.promotion_booth_price')) != 0) {
  207. $data = array('result' => 'false', 'msg' => lang('boothquota_expire'));
  208. $this->_echoJson($data);
  209. }
  210. }
  211. }
  212. // 验证已添加商品数量,及选择商品是否已经被添加过
  213. $bootgoods_info = $pbooth_model->getBoothgoodsList(array(array('store_id', '=', session('store_id'))), 'goods_id');
  214. // 已添加商品总数
  215. if (count($bootgoods_info) >= config('ds_config.promotion_booth_goods_sum')) {
  216. $data = array('result' => 'false', 'msg' => sprintf(lang('promotion_booth_goods_sum_error'), config('ds_config.promotion_booth_goods_sum')));
  217. $this->_echoJson($data);
  218. }
  219. // 商品是否已经被添加
  220. $bootgoods_info = array_under_reset($bootgoods_info, 'goods_id');
  221. if (isset($bootgoods_info[$gid])) {
  222. $data = array('result' => 'false', 'msg' => lang('goods_already_add'));
  223. $this->_echoJson($data);
  224. }
  225. // 保存到推荐展位商品表
  226. $insert = array();
  227. $insert['store_id'] = session('store_id');
  228. $insert['goods_id'] = $goods_info['goods_id'];
  229. $insert['gc_id'] = $goods_info['gc_id'];
  230. $pbooth_model->addBoothgoods($insert);
  231. $this->recordSellerlog('添加推荐展位商品,商品id:' . $goods_info['goods_id']);
  232. // 输出商品信息
  233. $goods_info['goods_image'] = goods_thumb($goods_info);
  234. $goods_info['url'] = (string) url('Goods/index', array('goods_id' => $goods_info['goods_id']));
  235. $goods_class = model('goodsclass')->getGoodsclassInfoById($goods_info['gc_id']);
  236. $goods_info['gc_name'] = $goods_class['gc_name'];
  237. $goods_info['result'] = 'true';
  238. $data['msg'] = lang('add_success');
  239. $data['goods_info'] = $goods_info;
  240. $this->_echoJson($data);
  241. }
  242. /**
  243. * 删除选择商品
  244. */
  245. public function del_choosed_goods()
  246. {
  247. $gid = input('param.gid');
  248. if ($gid <= 0) {
  249. $data = array('result' => 'false', 'msg' => lang('param_error'));
  250. $this->_echoJson($data);
  251. }
  252. $result = model('pbooth')->delBoothgoods(array('goods_id' => $gid, 'store_id' => session('store_id')));
  253. if ($result) {
  254. $this->recordSellerlog('删除推荐展位商品,商品id:' . $gid);
  255. $data = array('result' => 'true', 'msg' => lang('ds_common_del_succ'));
  256. } else {
  257. $data = array('result' => 'false', 'msg' => lang('ds_common_del_fail'));
  258. }
  259. $this->_echoJson($data);
  260. }
  261. /**
  262. * 输出JSON
  263. * @param array $data
  264. */
  265. private function _echoJson($data)
  266. {
  267. echo json_encode($data);
  268. exit();
  269. }
  270. /**
  271. * 用户中心右边,小导航
  272. *
  273. * @param string $menu_type 导航类型
  274. * @param string $name 当前导航的name
  275. * @return
  276. */
  277. protected function getSellerItemList()
  278. {
  279. $menu_array = array();
  280. switch (request()->action()) {
  281. case 'index':
  282. $menu_array = array(
  283. array(
  284. 'name' => 'index', 'text' => lang('recommended_stand'),
  285. 'url' => (string) url('Sellerpromotionbooth/index')
  286. )
  287. );
  288. break;
  289. case 'booth_quota_add':
  290. $menu_array = array(
  291. array(
  292. 'name' => 'index', 'text' => lang('recommended_stand'),
  293. 'url' => (string) url('Sellerpromotionbooth/index')
  294. ), array(
  295. 'name' => 'booth_quota_add', 'text' => lang('purchased_packages'),
  296. 'url' => (string) url('Sellerpromotionbooth/booth_quota_add')
  297. )
  298. );
  299. break;
  300. case 'booth_renew':
  301. $menu_array = array(
  302. array(
  303. 'name' => 'index', 'text' => lang('recommended_stand'),
  304. 'url' => (string) url('Sellerpromotionbooth/index')
  305. ), array(
  306. 'name' => 'booth_renew', 'text' => lang('subscription_fee'), 'url' => (string) url('Sellerpromotionbooth/booth_renew')
  307. )
  308. );
  309. break;
  310. }
  311. return $menu_array;
  312. }
  313. }