Sellerspec.php 6.0 KB

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