ownshop.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <?php
  2. /**
  3. * 自营店铺
  4. ***/
  5. defined('InShopNC') or exit('Access Invalid!');
  6. class ownshopControl extends SystemControl
  7. {
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. }
  12. public function indexOp()
  13. {
  14. $this->listOp();
  15. }
  16. public function listOp()
  17. {
  18. $model = model('store');
  19. $condition = array(
  20. 'is_own_shop' => 1,
  21. );
  22. $storeName = trim($_GET['store_name']);
  23. if (strlen($storeName) > 0) {
  24. $condition['store_name'] = array('like', "%$storeName%");
  25. Tpl::output('store_name', $storeName);
  26. }
  27. $storeList = $model->where($condition)->page(10)->select();
  28. $storeIds = array();
  29. foreach ($storeList as $s)
  30. $storeIds[$s['store_id']] = null;
  31. $storeIds = array_keys($storeIds);
  32. $storesWithGoods = model('goods')->where(array(
  33. 'store_id' => array('in', $storeIds),
  34. ))->field('distinct store_id')->key('store_id')->select();
  35. Tpl::output('store_list', $storeList);
  36. Tpl::output('page', $model->showpage());
  37. Tpl::output('storesWithGoods', $storesWithGoods);
  38. Tpl::showpage('ownshop.list');
  39. }
  40. public function addOp()
  41. {
  42. if (chksubmit())
  43. {
  44. $memberName = $_POST['member_name'];
  45. $memberPasswd = (string) $_POST['member_passwd'];
  46. if (strlen($memberName) < 3 || strlen($memberName) > 20
  47. || strlen($_POST['seller_name']) < 3 || strlen($_POST['seller_name']) > 20)
  48. showMessage('账号名称必须是3~20位', '', 'html', 'error');
  49. if (strlen($memberPasswd) < 6)
  50. showMessage('登录密码不能短于6位', '', 'html', 'error');
  51. if (!$this->checkMemberName($memberName))
  52. showMessage('店主账号已被占用', '', 'html', 'error');
  53. if (!$this->checkSellerName($_POST['seller_name']))
  54. showMessage('店主卖家账号名称已被其它店铺占用', '', 'html', 'error');
  55. try
  56. {
  57. $memberId = model('member')->addMember(array(
  58. 'member_name' => $memberName,
  59. 'member_passwd' => $memberPasswd,
  60. 'member_email' => '',
  61. ));
  62. }
  63. catch (Exception $ex)
  64. {
  65. showMessage('店主账号新增失败', '', 'html', 'error');
  66. }
  67. $storeModel = model('store');
  68. $saveArray = array();
  69. $saveArray['store_name'] = $_POST['store_name'];
  70. $saveArray['member_id'] = $memberId;
  71. $saveArray['member_name'] = $memberName;
  72. $saveArray['seller_name'] = $_POST['seller_name'];
  73. $saveArray['bind_all_gc'] = 1;
  74. $saveArray['store_state'] = 1;
  75. $saveArray['store_time'] = time();
  76. $saveArray['is_own_shop'] = 1;
  77. $storeId = $storeModel->addStore($saveArray);
  78. model('seller')->addSeller(array(
  79. 'seller_name' => $_POST['seller_name'],
  80. 'member_id' => $memberId,
  81. 'store_id' => $storeId,
  82. 'seller_group_id' => 0,
  83. 'is_admin' => 1,
  84. ));
  85. // 添加相册默认
  86. $album_model = Model('album');
  87. $album_arr = array();
  88. $album_arr['aclass_name'] = '默认相册';
  89. $album_arr['store_id'] = $storeId;
  90. $album_arr['aclass_des'] = '';
  91. $album_arr['aclass_sort'] = '255';
  92. $album_arr['aclass_cover'] = '';
  93. $album_arr['upload_time'] = time();
  94. $album_arr['is_default'] = '1';
  95. $album_model->addClass($album_arr);
  96. //插入店铺扩展表
  97. $model = Model();
  98. $model->table('store_extend')->insert(array('store_id'=>$storeId));
  99. // 删除自营店id缓存
  100. Model('store')->dropCachedOwnShopIds();
  101. $this->log("新增自营店铺: {$saveArray['store_name']}");
  102. showMessage('操作成功', urlAdmin('ownshop', 'list'));
  103. return;
  104. }
  105. Tpl::showpage('ownshop.add');
  106. }
  107. public function delOp()
  108. {
  109. $storeId = (int) $_GET['id'];
  110. $storeModel = model('store');
  111. $storeArray = $storeModel->field('is_own_shop,store_name')->find($storeId);
  112. if (empty($storeArray)) {
  113. showMessage('自营店铺不存在', '', 'html', 'error');
  114. }
  115. if (!$storeArray['is_own_shop']) {
  116. showMessage('不能在此删除非自营店铺', '', 'html', 'error');
  117. }
  118. $condition = array(
  119. 'store_id' => $storeId,
  120. );
  121. if ((int) model('goods')->getGoodsCount($condition) > 0)
  122. showMessage('已经发布商品的自营店铺不能被删除', '', 'html', 'error');
  123. // 完全删除店铺
  124. $storeModel->delStoreEntirely($condition);
  125. // 删除自营店id缓存
  126. Model('store')->dropCachedOwnShopIds();
  127. $this->log("删除自营店铺: {$storeArray['store_name']}");
  128. showMessage('操作成功', getReferer());
  129. }
  130. public function editOp()
  131. {
  132. $storeModel = model('store');
  133. $storeArray = $storeModel->find($_GET['id']);
  134. if (!$storeArray['is_own_shop']) {
  135. showMessage('不能在此管理非自营店铺', '', 'html', 'error');
  136. }
  137. if (chksubmit()) {
  138. if (!$this->checkSellerName($_POST['seller_name'], $_GET['id']))
  139. showMessage('店主卖家账号名称已被其它店铺占用', '', 'html', 'error');
  140. $saveArray = array();
  141. $saveArray['store_name'] = $_POST['store_name'];
  142. $saveArray['seller_name'] = $_POST['seller_name'];
  143. $saveArray['bind_all_gc'] = $_POST['bind_all_gc'] ? 1 : 0;
  144. $saveArray['store_state'] = $_POST['store_state'] ? 1 : 0;
  145. $saveArray['store_close_info'] = $_POST['store_close_info'];
  146. $storeModel->editStore($saveArray, array(
  147. 'store_id' => $_GET['id'],
  148. ));
  149. if ($saveArray['seller_name'] != $storeArray['seller_name']) {
  150. model('seller')->editSeller(array(
  151. 'seller_name' => $_POST['seller_name'],
  152. ), array(
  153. 'store_id' => $_GET['id'],
  154. 'seller_group_id' => 0,
  155. 'is_admin' => 1,
  156. ));
  157. }
  158. /* 自营店不下架商品
  159. if ($storeArray['bind_all_gc'] == '1' && $saveArray['bind_all_gc'] == '0' && $_POST['offshelf'] == '1') {
  160. // 全部商品下架
  161. Model('goods')->editProducesLockUp(array(
  162. 'goods_stateremark' => '管理员编辑经营类目',
  163. ), array(
  164. 'store_id' => $_GET['id'],
  165. ));
  166. }
  167. */
  168. // 删除自营店id缓存
  169. Model('store')->dropCachedOwnShopIds();
  170. $this->log("编辑自营店铺: {$saveArray['store_name']}");
  171. showMessage('操作成功', urlAdmin('ownshop', 'list'));
  172. }
  173. if (empty($storeArray))
  174. showMessage('店铺不存在', '', 'html', 'error');
  175. Tpl::output('store_array', $storeArray);
  176. Tpl::showpage('ownshop.edit');
  177. }
  178. public function check_seller_nameOp()
  179. {
  180. echo json_encode($this->checkSellerName($_GET['seller_name'], $_GET['id']));
  181. exit;
  182. }
  183. private function checkSellerName($sellerName, $storeId = 0)
  184. {
  185. // 判断store_joinin是否存在记录
  186. $count = (int) Model('store_joinin')->getStoreJoininCount(array(
  187. 'seller_name' => $sellerName,
  188. ));
  189. if ($count > 0)
  190. return false;
  191. $seller = Model('seller')->getSellerInfo(array(
  192. 'seller_name' => $sellerName,
  193. ));
  194. if (empty($seller))
  195. return true;
  196. if (!$storeId)
  197. return false;
  198. if ($storeId == $seller['store_id'] && $seller['seller_group_id'] == 0 && $seller['is_admin'] == 1)
  199. return true;
  200. return false;
  201. }
  202. public function check_member_nameOp()
  203. {
  204. $ret = json_encode($this->checkMemberName($_GET['member_name']));
  205. echo $ret;
  206. exit;
  207. }
  208. private function checkMemberName($memberName)
  209. {
  210. // 判断store_joinin是否存在记录
  211. $count = (int) Model('store_joinin')->getStoreJoininCount(array(
  212. 'member_name' => $memberName,
  213. ));
  214. if ($count > 0)
  215. return false;
  216. return ! Model('member')->getMemberCount(array(
  217. 'member_name' => $memberName,
  218. ));
  219. }
  220. public function bind_classOp()
  221. {
  222. //批量删除分类
  223. if (chksubmit()) {
  224. if (!empty($_POST['bid'])) {
  225. foreach ($_POST['bid'] as $bid) {
  226. $this->_bind_class_del($bid);
  227. }
  228. }
  229. }
  230. $store_id = intval($_GET['id']);
  231. $model_store = Model('store');
  232. $model_store_bind_class = Model('store_bind_class');
  233. $model_goods_class = Model('goods_class');
  234. $gc_list = $model_goods_class->getGoodsClassListByParentId(0);
  235. Tpl::output('gc_list',$gc_list);
  236. $store_info = $model_store->getStoreInfoByID($store_id);
  237. if(empty($store_info)) {
  238. showMessage(L('param_error'),'','','error');
  239. }
  240. Tpl::output('store_info', $store_info);
  241. $store_bind_class_list = $model_store_bind_class->getStoreBindClassList(array('store_id'=>$store_id), 30);
  242. $goods_class = Model('goods_class')->getGoodsClassIndexedListAll();
  243. for($i = 0, $j = count($store_bind_class_list); $i < $j; $i++) {
  244. $store_bind_class_list[$i]['class_1_name'] = $goods_class[$store_bind_class_list[$i]['class_1']]['gc_name'];
  245. $store_bind_class_list[$i]['class_2_name'] = $goods_class[$store_bind_class_list[$i]['class_2']]['gc_name'];
  246. $store_bind_class_list[$i]['class_3_name'] = $goods_class[$store_bind_class_list[$i]['class_3']]['gc_name'];
  247. }
  248. Tpl::output('store_bind_class_list', $store_bind_class_list);
  249. Tpl::output('showpage',$model_store_bind_class->showpage());
  250. Tpl::showpage('ownshop.bind_class');
  251. }
  252. /**
  253. * 添加经营类目
  254. */
  255. public function bind_class_addOp()
  256. {
  257. $store_id = intval($_POST['store_id']);
  258. $commis_rate = intval($_POST['commis_rate']);
  259. if($commis_rate < 0 || $commis_rate > 100) {
  260. showMessage(L('param_error'), '');
  261. }
  262. list($class_1, $class_2, $class_3) = explode(',', $_POST['goods_class']);
  263. $model_store_bind_class = Model('store_bind_class');
  264. $model_goods_class = Model('goods_class');
  265. $param = array();
  266. $param['store_id'] = $store_id;
  267. $param['class_1'] = $class_1;
  268. $param['state'] = 2;
  269. $param['commis_rate'] = $commis_rate;
  270. if (empty($class_2)) {
  271. //如果没选 二级
  272. $class_2_list = $model_goods_class->getGoodsClassList(array('gc_parent_id'=>$class_1));
  273. if (!empty($class_2_list)) {
  274. foreach ($class_2_list as $class_2_info) {
  275. $class_3_list = $model_goods_class->getGoodsClassList(array('gc_parent_id'=>$class_2_info['gc_id']));
  276. if (!empty($class_3_list)) {
  277. $param['class_2'] = $class_2_info['gc_id'];
  278. foreach ($class_3_list as $class_3_info) {
  279. $param['class_3'] = $class_3_info['gc_id'];
  280. $result = $this->_add_bind_class($param);
  281. }
  282. }
  283. }
  284. } else {
  285. //只有一级分类
  286. $param['class_2'] = $param['class_3'] = 0;
  287. $result = $this->_add_bind_class($param);
  288. }
  289. } else if (empty($class_3)) {
  290. //如果没选二没选三级
  291. $param['class_2'] = $class_2;
  292. $class_3_list = $model_goods_class->getGoodsClassList(array('gc_parent_id'=>$class_2));
  293. if (!empty($class_3_list)) {
  294. foreach ($class_3_list as $class_3_info) {
  295. $param['class_3'] = $class_3_info['gc_id'];
  296. // 检查类目是否已经存在
  297. $store_bind_class_info = $model_store_bind_class->getStoreBindClassInfo($param);
  298. if(empty($store_bind_class_info)) {
  299. $result = $this->_add_bind_class($param);
  300. }
  301. }
  302. } else {
  303. //二级就是最后一级
  304. $param['class_3'] = 0;
  305. $result = $this->_add_bind_class($param);
  306. }
  307. } else {
  308. $param['class_2'] = $class_2;
  309. $param['class_3'] = $class_3;
  310. $result = $this->_add_bind_class($param);
  311. }
  312. if($result) {
  313. // 删除自营店id缓存
  314. Model('store')->dropCachedOwnShopIds();
  315. $this->log('增加自营店铺经营类目,类目编号:'.$result.',店铺编号:'.$store_id);
  316. showMessage(L('nc_common_save_succ'), '');
  317. } else {
  318. showMessage(L('nc_common_save_fail'), '');
  319. }
  320. }
  321. private function _add_bind_class($param) {
  322. $model_store_bind_class = Model('store_bind_class');
  323. // 检查类目是否已经存在
  324. $store_bind_class_info = $model_store_bind_class->getStoreBindClassInfo($param);
  325. if(!empty($store_bind_class_info)) return true;
  326. return $model_store_bind_class->addStoreBindClass($param);
  327. }
  328. /**
  329. * 删除经营类目
  330. */
  331. public function bind_class_delOp()
  332. {
  333. $bid = intval($_POST['bid']);
  334. $result = $this->_bind_class_del($bid);
  335. echo json_encode($result);
  336. }
  337. private function _bind_class_del($bid)
  338. {
  339. $data = array();
  340. $data['result'] = true;
  341. $model_store_bind_class = Model('store_bind_class');
  342. $model_goods = Model('goods');
  343. $store_bind_class_info = $model_store_bind_class->getStoreBindClassInfo(array('bid' => $bid));
  344. if(empty($store_bind_class_info)) {
  345. $data['result'] = false;
  346. $data['message'] = '经营类目删除失败';
  347. return $data;
  348. }
  349. /* 自营店不下架商品
  350. // 商品下架
  351. $condition = array();
  352. $condition['store_id'] = $store_bind_class_info['store_id'];
  353. $gc_id = $store_bind_class_info['class_1'].','.$store_bind_class_info['class_2'].','.$store_bind_class_info['class_3'];
  354. $update = array();
  355. $update['goods_stateremark'] = '管理员删除经营类目';
  356. $condition['gc_id'] = array('in', rtrim($gc_id, ','));
  357. $model_goods->editProducesLockUp($update, $condition);
  358. */
  359. $result = $model_store_bind_class->delStoreBindClass(array('bid'=>$bid));
  360. if(!$result) {
  361. $data['result'] = false;
  362. $data['message'] = '经营类目删除失败';
  363. }
  364. // 删除自营店id缓存
  365. Model('store')->dropCachedOwnShopIds();
  366. $this->log('删除自营店铺经营类目,类目编号:'.$bid.',店铺编号:'.$store_bind_class_info['store_id']);
  367. return $data;
  368. }
  369. public function bind_class_updateOp()
  370. {
  371. $bid = intval($_GET['id']);
  372. if($bid <= 0) {
  373. echo json_encode(array('result'=>FALSE,'message'=>Language::get('param_error')));
  374. die;
  375. }
  376. $new_commis_rate = intval($_GET['value']);
  377. if ($new_commis_rate < 0 || $new_commis_rate >= 100) {
  378. echo json_encode(array('result'=>FALSE,'message'=>Language::get('param_error')));
  379. die;
  380. } else {
  381. $update = array('commis_rate' => $new_commis_rate);
  382. $condition = array('bid' => $bid);
  383. $model_store_bind_class = Model('store_bind_class');
  384. $result = $model_store_bind_class->editStoreBindClass($update, $condition);
  385. if($result) {
  386. // 删除自营店id缓存
  387. Model('store')->dropCachedOwnShopIds();
  388. $this->log('更新自营店铺经营类目,类目编号:'.$bid);
  389. echo json_encode(array('result'=>TRUE));
  390. die;
  391. } else {
  392. echo json_encode(array('result'=>FALSE,'message'=>L('nc_common_op_fail')));
  393. die;
  394. }
  395. }
  396. }
  397. /**
  398. * 验证店铺名称是否存在
  399. */
  400. public function ckeck_store_nameOp() {
  401. /**
  402. * 实例化卖家模型
  403. */
  404. $where = array();
  405. $where['store_name'] = $_GET['store_name'];
  406. if (isset($_GET['store_id'])) {
  407. $where['store_id'] = array('neq', $_GET['store_id']);
  408. }
  409. $store_info = Model('store')->getStoreInfo($where);
  410. if(!empty($store_info['store_name'])) {
  411. echo 'false';
  412. } else {
  413. echo 'true';
  414. }
  415. }
  416. }