Sellerspec.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /*
  3. * 店铺规格值
  4. * 每个店铺都有对应分类下保存的规格值
  5. */
  6. namespace app\home\controller;
  7. use think\facade\View;
  8. use think\facade\Lang;
  9. /**
  10. * ============================================================================
  11. *
  12. * ============================================================================
  13. *
  14. * ----------------------------------------------------------------------------
  15. *
  16. * ============================================================================
  17. * 控制器
  18. */
  19. class Sellerspec extends BaseSeller
  20. {
  21. var $_store_id;
  22. public function initialize()
  23. {
  24. parent::initialize();
  25. Lang::load(base_path() . 'home/lang/'.config('lang.default_lang').'/sellerspec.lang.php');
  26. $this->_store_id = session('store_id');
  27. }
  28. /**
  29. * 选择分类
  30. */
  31. public function index()
  32. {
  33. // 获取商品分类
  34. $goodsclass_model = model('goodsclass');
  35. $gc_list = $goodsclass_model->getGoodsclass($this->_store_id);
  36. View::assign('gc_list', $gc_list);
  37. $this->setSellerCurItem('spec');
  38. $this->setSellerCurMenu('sellerspec');
  39. return View::fetch($this->template_dir.'index');
  40. }
  41. /**
  42. * 添加规格值
  43. */
  44. public function add_spec()
  45. {
  46. $sp_id = intval(input('param.spid'));
  47. $gc_id = intval(input('param.gcid'));
  48. // 验证参数
  49. if ($sp_id <= 0) {
  50. $this->error(lang('param_error'));
  51. }
  52. // 分类信息
  53. $gc_info = model('goodsclass')->getGoodsclassInfoById($gc_id);
  54. View::assign('gc_info', $gc_info);
  55. // 规格信息
  56. $spec_model = model('spec');
  57. $sp_info = $spec_model->getSpecInfo($sp_id, 'sp_id,sp_name');
  58. //halt($sp_info);
  59. View::assign('sp_info', $sp_info);
  60. // 规格值信息
  61. $sp_value_list = $spec_model->getSpecvalueList(array(
  62. 'store_id' => $this->_store_id, 'sp_id' => $sp_id,
  63. 'gc_id' => $gc_id
  64. ));
  65. View::assign('sp_value_list', $sp_value_list);
  66. return View::fetch($this->template_dir.'spec_add');
  67. }
  68. /**
  69. * 保存规格值
  70. */
  71. public function save_spec()
  72. {
  73. $sp_id = intval(input('post.sp_id'));
  74. $gc_id = intval(input('post.gc_id'));
  75. if ($sp_id <= 0 || $gc_id <= 0 ) {
  76. ds_json_encode(10001,lang('param_error'));
  77. }
  78. $spec_model = model('spec');
  79. // 更新原规格值
  80. $sv_array = input('post.sv/a');
  81. if (!empty($sv_array['old']) && is_array($sv_array['old'])) {
  82. foreach ($sv_array['old'] as $key => $value) {
  83. if (empty($value['name'])) {
  84. continue;
  85. }
  86. $where = array('spvalue_id' => $key);
  87. $update = array(
  88. 'spvalue_name' => $value['name'], 'sp_id' => $sp_id, 'gc_id' => $gc_id,
  89. 'store_id' => $this->_store_id,
  90. 'spvalue_color' => isset($value['color'])?$value['color']:'',
  91. 'spvalue_sort' => intval($value['sort'])
  92. );
  93. $spec_model->editSpecvalue($update, $where);
  94. }
  95. }
  96. // 添加新规格值
  97. if (!empty($sv_array['new']) && is_array($sv_array['new'])) {
  98. $insert_array = array();
  99. foreach ($sv_array['new'] as $value) {
  100. if (empty($value['name'])) {
  101. continue;
  102. }
  103. $tmp_insert = array(
  104. 'spvalue_name' => $value['name'], 'sp_id' => $sp_id, 'gc_id' => $gc_id,
  105. 'store_id' => $this->_store_id,
  106. 'spvalue_color' => isset($value['color'])?$value['color']:'',
  107. 'spvalue_sort' => intval($value['sort'])
  108. );
  109. $insert_array[] = $tmp_insert;
  110. }
  111. $spec_model->addSpecvalueALL($insert_array);
  112. }
  113. ds_json_encode(10000,lang('ds_common_op_succ'));
  114. }
  115. /**
  116. * ajax删除规格值
  117. */
  118. public function ajax_delspec()
  119. {
  120. $spvalue_id = intval(input('param.id'));
  121. if ($spvalue_id <= 0) {
  122. echo 'false';
  123. exit();
  124. }
  125. $rs = model('spec')->delSpecvalue(array('spvalue_id' => $spvalue_id, 'store_id' => $this->_store_id));
  126. if ($rs) {
  127. echo 'true';
  128. exit();
  129. }
  130. else {
  131. echo 'false';
  132. exit();
  133. }
  134. }
  135. /**
  136. * AJAX获取商品分类
  137. */
  138. public function ajax_class()
  139. {
  140. $id = intval(input('param.id'));
  141. $deep = intval(input('param.deep'));
  142. if ($id <= 0 || $deep <= 0 || $deep >= 4) {
  143. echo 'false';
  144. exit();
  145. }
  146. $deep += 1;
  147. $goodsclass_model = model('goodsclass');
  148. // 验证分类是否存在
  149. $gc_info = $goodsclass_model->getGoodsclassInfoById($id);
  150. if (empty($gc_info)) {
  151. echo 'false';
  152. exit();
  153. }
  154. // 读取商品分类
  155. if ($deep != 4) {
  156. $gc_list = $goodsclass_model->getGoodsclass($this->_store_id, $id, $deep);
  157. }
  158. // 分类不为空输出分类信息
  159. if (!empty($gc_list)) {
  160. $data = array('type' => 'class', 'data' => $gc_list, 'deep' => $deep);
  161. }
  162. else {
  163. // 查询类型
  164. $type_model = model('type');
  165. $spec_list = $type_model->getSpecByType(array('type_id' => $gc_info['type_id']), 't.type_id, s.*');
  166. $data = array('type' => 'spec', 'data' => $spec_list, 'gcid' => $id, 'deep' => $deep);
  167. }
  168. echo json_encode($data);
  169. exit();
  170. }
  171. /**
  172. * 用户中心右边,小导航
  173. *
  174. * @param string $menu_type 导航类型
  175. * @param string $menu_key 当前导航的menu_key
  176. * @return
  177. */
  178. protected function getSellerItemList()
  179. {
  180. $menu_array = array(
  181. array('name' => 'spec', 'text' => lang('edit_product_specifications'), 'url' => 'index')
  182. );
  183. return $menu_array;
  184. }
  185. }