TransportController.class.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * 小梦科技资源nanodreamtech.com
  4. *
  5. * ==========================================================================
  6. * @link https://www.nanodreamtech.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license https://www.nanodreamtech.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. namespace Admin\Controller;
  15. use Seller\Model\TransportModel;
  16. class TransportController extends CommonController{
  17. protected function _initialize(){
  18. parent::_initialize();
  19. $this->breadcrumb1='商品管理';
  20. $this->breadcrumb2='运费模板';
  21. }
  22. function index(){
  23. $model = new TransportModel();
  24. $where = array('seller_id' => SELLERUID);
  25. $transportList = $model->getTransportList($where);
  26. $list=$transportList['list'];
  27. if (!empty($list) && is_array($list)){
  28. $transport = array();
  29. foreach ($list as $v) {
  30. if (!array_key_exists($v['id'],$transport)){
  31. $transport[$v['id']] = $v['title'];
  32. }
  33. }
  34. $extend = $model->getExtendList(array('transport_id'=>array('in',array_keys($transport))));
  35. // 整理
  36. if (!empty($extend)) {
  37. $tmp_extend = array();
  38. foreach ($extend as $val) {
  39. $tmp_extend[$val['transport_id']]['data'][] = $val;
  40. if ($val['is_default'] == 1) {
  41. $tmp_extend[$val['transport_id']]['price'] = $val['sprice'];
  42. }
  43. }
  44. $extend = $tmp_extend;
  45. }
  46. }
  47. $data['list']=$list;
  48. if(isset($extend))
  49. $data['extend']=$extend;
  50. $this->output=$data;
  51. $this->assign('page',$transportList['page']);// 赋值分页输出
  52. $this->display();
  53. }
  54. function clone_data(){
  55. $id = intval($_GET['id']);
  56. $model = new TransportModel();
  57. $transport = $model->getTransportInfo(array('id'=>$id));
  58. unset($transport['id']);
  59. $transport['title'] .= '的副本';
  60. $transport['update_time'] = time();
  61. try {
  62. $model->startTrans();
  63. $insert = $model->addTransport($transport);
  64. if ($insert) {
  65. $extend = $model->getExtendList(array('transport_id'=>$id));
  66. foreach ($extend as $k=>$v) {
  67. foreach ($v as $key=>$value) {
  68. $extend[$k]['transport_id'] = $insert;
  69. }
  70. unset($extend[$k]['id']);
  71. }
  72. $insert = $model->addExtend($extend);
  73. }
  74. if (!$insert) throw new Exception('操作失败');
  75. $model->commit();
  76. $this->redirect('Transport/index');
  77. die;
  78. }catch (Exception $e){
  79. $model->rollback();
  80. $this->error('复制失败');
  81. }
  82. }
  83. function add(){
  84. $this->crumbs='新增';
  85. $this->display('edit');
  86. }
  87. function edit(){
  88. $id = intval($_GET['id']);
  89. $model = new TransportModel();
  90. $transport = $model->getTransportInfo(array('id'=>$id));
  91. $extend = $model->getExtendInfo(array('transport_id'=>$id));
  92. $data['transport']=$transport;
  93. $data['extend']=$extend;
  94. $this->output=$data;
  95. $this->crumbs='修改';
  96. $this->display();
  97. }
  98. function save(){
  99. if(IS_POST){
  100. $trans_info = array();
  101. $trans_info['title'] = $_POST['title'];
  102. $trans_info['seller_id'] = SELLERUID;
  103. $trans_info['update_time'] = time();
  104. $model = new TransportModel();
  105. if (is_numeric($_POST['transport_id'])){
  106. //编辑时,删除所有附加表信息
  107. $trans_info['id'] = intval($_POST['transport_id']);
  108. $transport_id = intval($_POST['transport_id']);
  109. $model->transUpdate($trans_info);
  110. $model->delExtend($transport_id);
  111. }else{
  112. //新增
  113. $transport_id = $model->addTransport($trans_info);
  114. }
  115. //保存默认运费
  116. if (is_array($_POST['default']['kd'])){
  117. $a = $_POST['default']['kd'];
  118. $trans_list[0]['area_id'] = '';
  119. $trans_list[0]['area_name'] = '全国';
  120. $trans_list[0]['snum'] = $a['start'];
  121. $trans_list[0]['sprice'] = $a['postage'];
  122. $trans_list[0]['xnum'] = $a['plus'];
  123. $trans_list[0]['xprice'] = $a['postageplus'];
  124. $trans_list[0]['is_default'] = 1;
  125. $trans_list[0]['transport_id'] = $transport_id;
  126. $trans_list[0]['transport_title'] = $_POST['title'];
  127. $trans_list[0]['top_area_id'] = '';
  128. }
  129. //保存自定义地区的运费设置
  130. $areas = $_POST['areas']['kd'];
  131. $special = $_POST['special']['kd'];
  132. if (is_array($areas) && is_array($special)){
  133. //$key需要加1,因为快递默认运费占了第一个下标
  134. foreach ($special as $key=>$value) {
  135. if (empty($areas[$key])) continue;
  136. $areas[$key] = explode('|||',$areas[$key]);
  137. $trans_list[$key+1]['area_id'] = ','.$areas[$key][0].',';
  138. $trans_list[$key+1]['area_name'] = $areas[$key][1];
  139. $trans_list[$key+1]['snum'] = $value['start'];
  140. $trans_list[$key+1]['sprice'] = $value['postage'];
  141. $trans_list[$key+1]['xnum'] = $value['plus'];
  142. $trans_list[$key+1]['xprice'] = $value['postageplus'];
  143. $trans_list[$key+1]['is_default'] = 2;
  144. $trans_list[$key+1]['transport_id'] = $transport_id;
  145. $trans_list[$key+1]['transport_title'] = $_POST['title'];
  146. //计算省份ID
  147. $province = array();
  148. $tmp = explode(',',$areas[$key][0]);
  149. if (!empty($tmp) && is_array($tmp)){
  150. $city = $this->getCity();
  151. foreach ($tmp as $t) {
  152. if(isset($city[$t])){
  153. $pid = $city[$t];
  154. if (!in_array($pid,$province) && !empty($pid))
  155. $province[] = $pid;
  156. }
  157. }
  158. }
  159. if (count($province)>0){
  160. $trans_list[$key+1]['top_area_id'] = ','.implode(',',$province).',';
  161. }else{
  162. $trans_list[$key+1]['top_area_id'] = '';
  163. }
  164. //$i++;
  165. }
  166. }
  167. $result = $model->addExtend($trans_list);
  168. if($result){
  169. $this->success('保存成功',U('Transport/index'));
  170. }else{
  171. $this->error('保存失败');
  172. }
  173. }
  174. }
  175. /**
  176. * 获取商家的运费模板列表
  177. */
  178. public function getTransportList()
  179. {
  180. $trans_model = D('Seller/Transport');
  181. $seller_id = I('get.store_id');
  182. $list = $trans_model->getStoreTransportList($seller_id);
  183. $result = array();
  184. $result['code'] = empty($list) ? 0 : 1;
  185. $result['list'] = $list;
  186. echo json_encode($result);
  187. die();
  188. }
  189. function del(){
  190. $id = intval($_GET['id']);
  191. $model = new TransportModel();
  192. if($model->delTansport(array('id'=>$id))){
  193. $this->redirect('Transport/index');
  194. die;
  195. }else{
  196. $this->error('操作失败');
  197. }
  198. }
  199. /**
  200. * 返回 市ID => 省ID 对应关系数组
  201. *
  202. * @return array
  203. */
  204. private function getCity(){
  205. return array (36=>1,39=>9,40=>2,62=>22,73=>3,74=>3,75=>3,76=>3,77=>3,78=>3,79=>3,80=>3,81=>3,82=>3,83=>3,84=>4,85=>4,86=>4,87=>4,88=>4,89=>4,90=>4,91=>4,92=>4,93=>4,94=>4,95=>5,96=>5,97=>5,98=>5,99=>5,100=>5,101=>5,102=>5,103=>5,104=>5,105=>5,106=>5,107=>6,108=>6,109=>6,110=>6,111=>6,112=>6,113=>6,114=>6,115=>6,116=>6,117=>6,118=>6,119=>6,120=>6,121=>7,122=>7,123=>7,124=>7,125=>7,126=>7,127=>7,128=>7,129=>7,130=>8,131=>8,132=>8,133=>8,134=>8,135=>8,136=>8,137=>8,138=>8,139=>8,140=>8,141=>8,142=>8,162=>10,163=>10,164=>10,165=>10,166=>10,167=>10,168=>10,169=>10,170=>10,171=>10,172=>10,173=>10,174=>10,175=>11,176=>11,177=>11,178=>11,179=>11,180=>11,181=>11,182=>11,183=>11,184=>11,185=>11,186=>12,187=>12,188=>12,189=>12,190=>12,191=>12,192=>12,193=>12,194=>12,195=>12,196=>12,197=>12,198=>12,199=>12,200=>12,201=>12,202=>12,203=>13,204=>13,205=>13,206=>13,207=>13,208=>13,209=>13,210=>13,211=>13,212=>14,213=>14,214=>14,215=>14,216=>14,217=>14,218=>14,219=>14,220=>14,221=>14,222=>14,223=>15,224=>15,225=>15,226=>15,227=>15,228=>15,229=>15,230=>15,231=>15,232=>15,233=>15,234=>15,235=>15,236=>15,237=>15,238=>15,239=>15,240=>16,241=>16,242=>16,243=>16,244=>16,245=>16,246=>16,247=>16,248=>16,249=>16,250=>16,251=>16,252=>16,253=>16,254=>16,255=>16,256=>16,257=>16,258=>17,259=>17,260=>17,261=>17,262=>17,263=>17,264=>17,265=>17,266=>17,267=>17,268=>17,269=>17,270=>17,271=>17,272=>17,273=>17,274=>17,275=>18,276=>18,277=>18,278=>18,279=>18,280=>18,281=>18,282=>18,283=>18,284=>18,285=>18,286=>18,287=>18,288=>18,289=>19,290=>19,291=>19,292=>19,293=>19,294=>19,295=>19,296=>19,297=>19,298=>19,299=>19,300=>19,301=>19,302=>19,303=>19,304=>19,305=>19,306=>19,307=>19,308=>19,309=>19,310=>20,311=>20,312=>20,313=>20,314=>20,315=>20,316=>20,317=>20,318=>20,319=>20,320=>20,321=>20,322=>20,323=>20,324=>21,325=>21,326=>21,327=>21,328=>21,329=>21,330=>21,331=>21,332=>21,333=>21,334=>21,335=>21,336=>21,337=>21,338=>21,339=>21,340=>21,341=>21,342=>21,343=>21,344=>21,385=>23,386=>23,387=>23,388=>23,389=>23,390=>23,391=>23,392=>23,393=>23,394=>23,395=>23,396=>23,397=>23,398=>23,399=>23,400=>23,401=>23,402=>23,403=>23,404=>23,405=>23,406=>24,407=>24,408=>24,409=>24,410=>24,411=>24,412=>24,413=>24,414=>24,415=>25,416=>25,417=>25,418=>25,419=>25,420=>25,421=>25,422=>25,423=>25,424=>25,425=>25,426=>25,427=>25,428=>25,429=>25,430=>25,431=>26,432=>26,433=>26,434=>26,435=>26,436=>26,437=>26,438=>27,439=>27,440=>27,441=>27,442=>27,443=>27,444=>27,445=>27,446=>27,447=>27,448=>28,449=>28,450=>28,451=>28,452=>28,453=>28,454=>28,455=>28,456=>28,457=>28,458=>28,459=>28,460=>28,461=>28,462=>29,463=>29,464=>29,465=>29,466=>29,467=>29,468=>29,469=>29,470=>30,471=>30,472=>30,473=>30,474=>30,475=>31,476=>31,477=>31,478=>31,479=>31,480=>31,481=>31,482=>31,483=>31,484=>31,485=>31,486=>31,487=>31,488=>31,489=>31,490=>31,491=>31,492=>31,493=>32,494=>32,495=>32,496=>32,497=>32,498=>32,499=>32,500=>32,501=>32,502=>32,503=>32,504=>32,505=>32,506=>32,507=>32,508=>32,509=>32,510=>32,511=>32,512=>32,513=>32,514=>32,515=>32,516=>33,517=>33,518=>33,519=>33,520=>33,521=>33,522=>33,523=>33,524=>33,525=>33,526=>33,527=>33,528=>33,529=>33,530=>33,531=>33,532=>33,533=>33,534=>34,45055=>35);
  206. }
  207. }
  208. ?>