Sellertransport.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\Db;
  4. use think\facade\View;
  5. use think\facade\Lang;
  6. /**
  7. *
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * 控制器
  12. */
  13. class Sellertransport extends BaseSeller
  14. {
  15. public function initialize()
  16. {
  17. parent::initialize(); // TODO: Change the autogenerated stub
  18. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/sellertransport.lang.php');
  19. $type = input('param.type');
  20. if ($type != '' && $type != 'select') {
  21. $type = 'select';
  22. }
  23. }
  24. /**
  25. * 售卖区域列表
  26. *
  27. */
  28. public function index()
  29. {
  30. $transport_model = model('transport');
  31. $transport_list = $transport_model->getTransportList(array('store_id' => session('store_id')), 4);
  32. $extend = '';
  33. if (!empty($transport_list) && is_array($transport_list)) {
  34. $transport = array();
  35. foreach ($transport_list as $v) {
  36. if (!array_key_exists($v['transport_id'], $transport)) {
  37. $transport[$v['transport_id']] = $v['transport_title'];
  38. }
  39. }
  40. $extend = $transport_model->getTransportextendList(array(array('transport_id', 'in', array_keys($transport))));
  41. //halt($extend);
  42. // 整理
  43. if (!empty($extend)) {
  44. $tmp_extend = array();
  45. foreach ($extend as $val) {
  46. $tmp_extend[$val['transport_id']]['data'][] = $val;
  47. $tmp_extend[$val['transport_id']]['price'] = $val['transportext_sprice'];
  48. }
  49. $extend = $tmp_extend;
  50. }
  51. }
  52. //dump($transport_list);
  53. //dump($extend);exit;
  54. /**
  55. * 页面输出
  56. */
  57. View::assign('transport_list', $transport_list);
  58. View::assign('extend', $extend);
  59. View::assign('show_page', $transport_model->page_info->render());
  60. $this->setSellerCurMenu('sellertransport');
  61. $this->setSellerCurItem('index');
  62. return View::fetch($this->template_dir . 'index');
  63. }
  64. /**
  65. * 新增售卖区域
  66. *
  67. */
  68. public function add()
  69. {
  70. $areas = model('area')->getAreas();
  71. View::assign('areas', $areas);
  72. $this->setSellerCurMenu('sellertransport');
  73. $this->setSellerCurItem('index');
  74. return View::fetch($this->template_dir . 'add');
  75. }
  76. public function edit()
  77. {
  78. $id = intval(input('get.id'));
  79. $transport_model = model('transport');
  80. $transport = $transport_model->getTransportInfo(array('transport_id' => $id));
  81. $extend = $transport_model->getExtendInfo(array('transport_id' => $id));
  82. View::assign('transport', $transport);
  83. View::assign('extend', $extend);
  84. $areas = model('area')->getAreas();
  85. View::assign('areas', $areas);
  86. $this->setSellerCurItem('index');
  87. $this->setSellerCurMenu('sellertransport');
  88. return View::fetch($this->template_dir . 'add');
  89. }
  90. public function delete()
  91. {
  92. $transport_id = intval(input('param.id'));
  93. $transport_model = model('transport');
  94. $transport = $transport_model->getTransportInfo(array('transport_id' => $transport_id));
  95. if ($transport['store_id'] != session('store_id')) {
  96. $this->error(lang('transport_op_fail'));
  97. }
  98. //查看是否正在被使用
  99. if ($transport_model->isTransportUsing($transport_id)) {
  100. $this->error(lang('transport_op_using'));
  101. }
  102. if ($transport_model->delTansport($transport_id)) {
  103. header('location: ' . $_SERVER['HTTP_REFERER']);
  104. exit;
  105. } else {
  106. $this->error(lang('transport_op_fail'));
  107. }
  108. }
  109. public function cloned()
  110. {
  111. $id = intval(input('get.id'));
  112. $transport_model = model('transport');
  113. $transport = $transport_model->getTransportInfo(array('transport_id' => $id));
  114. unset($transport['transport_id']);
  115. $transport['transport_title'] .= lang('transport_clone_name');
  116. $transport['transport_updatetime'] = TIMESTAMP;
  117. try {
  118. Db::startTrans();
  119. $insert = $transport_model->addTransport($transport);
  120. if ($insert) {
  121. $extend = $transport_model->getTransportextendList(array('transport_id' => $id));
  122. foreach ($extend as $k => $v) {
  123. foreach ($v as $key => $value) {
  124. $extend[$k]['transport_id'] = $insert;
  125. }
  126. unset($extend[$k]['transportext_id']);
  127. }
  128. $insert = $transport_model->addExtend($extend);
  129. }
  130. if (!$insert) {
  131. throw new \think\Exception(lang('transport_op_fail'), 10006);
  132. }
  133. Db::commit();
  134. header('location: ' . $_SERVER['HTTP_REFERER']);
  135. exit;
  136. } catch (Exception $e) {
  137. Db::rollback();
  138. $this->error($e->getMessage(), $_SERVER['HTTP_REFERER']);
  139. }
  140. }
  141. public function save()
  142. {
  143. if (!request()->isPost()) {
  144. return false;
  145. }
  146. $trans_info = array();
  147. $trans_info['transport_title'] = input('post.title');
  148. $trans_info['send_tpl_id'] = 1;
  149. $trans_info['store_id'] = session('store_id');
  150. $trans_info['transport_updatetime'] = TIMESTAMP;
  151. $trans_info['transport_is_limited'] = input('post.transport_is_limited');
  152. $trans_info['transport_type'] = input('post.transport_type');
  153. $transport_model = model('transport');
  154. $transport_id = input('post.transport_id');
  155. if (is_numeric($transport_id)) {
  156. //编辑时,删除所有附加表信息
  157. $transport_id = intval($transport_id);
  158. $transport_model->editTransport($trans_info, array('transport_id' => $transport_id));
  159. $transport_model->delTransportextend($transport_id);
  160. } else {
  161. //新增
  162. $transport_id = $transport_model->addTransport($trans_info);
  163. }
  164. $post = input('post.'); #获取POST 数据
  165. $trans_list = array();
  166. $areas = !empty($post['areas']['kd']) ? $post['areas']['kd'] : '';
  167. $special = !empty($post['special']['kd']) ? $post['special']['kd'] : '';
  168. //默认运费
  169. $default = $post['default']['kd'];
  170. $trans_list[] = array(
  171. 'transportext_area_id' => '',
  172. 'transportext_area_name' => lang('transport_note_1'),
  173. 'transportext_sprice' => $default['postage'],
  174. 'transport_id' => $transport_id,
  175. 'transport_title' => input('post.title'),
  176. 'transportext_snum' => $default['start'],
  177. 'transportext_xnum' => $default['plus'],
  178. 'transportext_xprice' => $default['postageplus'],
  179. 'transportext_is_default' => '1',
  180. 'transportext_top_area_id' => '',
  181. );
  182. if (is_array($special)) {
  183. foreach ($special as $key => $value) {
  184. $tmp = array();
  185. if (empty($areas[$key])) {
  186. continue;
  187. }
  188. $areas[$key] = explode('|||', $areas[$key]);
  189. $tmp['transportext_area_id'] = ',' . $areas[$key][0] . ',';
  190. $tmp['transportext_area_name'] = $areas[$key][1];
  191. $tmp['transportext_sprice'] = $value['postage'];
  192. $tmp['transport_id'] = $transport_id;
  193. $tmp['transport_title'] = input('post.title');
  194. $tmp['transportext_snum'] = $value['start'];
  195. $tmp['transportext_xnum'] = $value['plus'];
  196. $tmp['transportext_xprice'] = $value['postageplus'];
  197. $tmp['transportext_is_default'] = '0';
  198. //计算省份ID
  199. $province = array();
  200. $tmp1 = explode(',', $areas[$key][0]);
  201. if (!empty($tmp1) && is_array($tmp1)) {
  202. $city = model('area')->getCityProvince();
  203. foreach ($tmp1 as $t) {
  204. $pid = isset($city[$t]) ? $city[$t] : array();
  205. if (!in_array($pid, $province) && !empty($pid)) {
  206. $province[] = $pid;
  207. }
  208. }
  209. }
  210. if (count($province) > 0) {
  211. $tmp['transportext_top_area_id'] = ',' . implode(',', $province) . ',';
  212. } else {
  213. $tmp['transportext_top_area_id'] = '';
  214. }
  215. $trans_list[] = $tmp;
  216. }
  217. }
  218. $result = $transport_model->addExtend($trans_list);
  219. $type = input('param.type');
  220. if ($result) {
  221. $this->redirect('sellertransport/index', ['type' => $type]);
  222. } else {
  223. $this->error(lang('transport_op_fail'));
  224. }
  225. }
  226. /**
  227. * 货到付款地区设置
  228. *
  229. */
  230. public function offpay_area()
  231. {
  232. $offpayarea_model = model('offpayarea');
  233. $area_model = model('area');
  234. $store_id = session('store_id');
  235. if (request()->isPost()) {
  236. if (!check_platform_store()) {
  237. ds_json_encode(10001, lang('only_for_ownshop'));
  238. }
  239. $county_array = input('post.county'); #获取字符串
  240. if (!preg_match('/^[\d,]+$/', $county_array)) {
  241. $county_array = '';
  242. }
  243. //内置自营店ID
  244. $area_info = $offpayarea_model->getOffpayareaInfo(array('store_id' => $store_id));
  245. $data = array();
  246. $county = trim($county_array, ',');
  247. $county_array = explode(',', $county);
  248. $all_array = array();
  249. $province_array = input('post.province/a'); #获取数组
  250. if (!empty($province_array) && is_array($province_array)) {
  251. foreach ($province_array as $v) {
  252. $all_array[$v] = $v;
  253. }
  254. }
  255. $city_array = input('post.city/a'); #获取数组
  256. if (!empty($city_array) && is_array($city_array)) {
  257. foreach ($city_array as $v) {
  258. $all_array[$v] = $v;
  259. }
  260. }
  261. foreach ($county_array as $pid) {
  262. $all_array[$pid] = $pid;
  263. $temp = $area_model->getChildsByPid($pid);
  264. if (!empty($temp) && is_array($temp)) {
  265. foreach ($temp as $v) {
  266. $all_array[$v] = $v;
  267. }
  268. }
  269. }
  270. $all_array = array_values($all_array);
  271. $data['area_id'] = serialize($all_array);
  272. if (!$area_info) {
  273. $data['store_id'] = $store_id;
  274. $result = $offpayarea_model->addOffpayarea($data);
  275. } else {
  276. $result = $offpayarea_model->editOffpayarea(array('store_id' => $store_id), $data);
  277. }
  278. if ($result) {
  279. ds_json_encode(10000, lang('ds_common_save_succ'));
  280. } else {
  281. ds_json_encode(10001, lang('ds_common_save_fail'));
  282. }
  283. } else {
  284. if (!check_platform_store()) {
  285. $this->error(lang('only_for_ownshop'));
  286. }
  287. //取出支持货到付款的县ID及上级市ID
  288. $parea_info = $offpayarea_model->getOffpayareaInfo(array('store_id' => $store_id));
  289. if (!empty($parea_info['area_id'])) {
  290. $parea_ids = @unserialize($parea_info['area_id']);
  291. }
  292. if (empty($parea_ids)) {
  293. $parea_ids = array();
  294. }
  295. View::assign('areaIds', $parea_ids);
  296. $area_model = model('area');
  297. $areas = $area_model->getAreas();
  298. View::assign('areas', $areas);
  299. //取出支持货到付款县ID的上级市ID
  300. $city_checked_child_array = array();
  301. $county_array = $area_model->getAreaList(array('area_deep' => 3), 'area_id,area_parent_id');
  302. foreach ($county_array as $v) {
  303. if (in_array($v['area_id'], $parea_ids)) {
  304. $city_checked_child_array[$v['area_parent_id']][] = $v['area_id'];
  305. }
  306. }
  307. View::assign('city_checked_child_array', $city_checked_child_array);
  308. //市级下面的县是不是全部支持货到付款,如果全部支持,默认选中
  309. //如果其中部分县支持货到付款,默认不选中但显示一个支付到付县的数量
  310. //格式 city_id => 下面支持到付的县ID数量
  311. $city_count_array = array();
  312. //格式 city_id => 是否选中true/false
  313. $city_checked_array = array();
  314. foreach ($city_checked_child_array as $city_id => $c) {
  315. $city_count_array[$city_id] = count($areas['children'][$city_id]);
  316. $c = count($c);
  317. if ($c > 0 && $c == $city_count_array[$city_id]) {
  318. $city_checked_array[$city_id] = true;
  319. }
  320. }
  321. View::assign('city_count_array', $city_count_array);
  322. View::assign('city_checked_array', $city_checked_array);
  323. //计算哪些省需要默认选中(即该省下面的所有县都支持到付,即所有市都是选中状态)
  324. $province_checked_array = array();
  325. foreach ($areas['children'][0] as $province_id) {
  326. $b = true;
  327. if (isset($areas['children'][$province_id]) && is_array($areas['children'][$province_id])) {
  328. foreach ($areas['children'][$province_id] as $city_id) {
  329. if (empty($city_checked_array[$city_id])) {
  330. $b = false;
  331. break;
  332. }
  333. }
  334. }
  335. if ($b) {
  336. $province_checked_array[$province_id] = true;
  337. }
  338. }
  339. View::assign('province_checked_array', $province_checked_array);
  340. $area_array_json = json_encode($area_model->getAreaArrayForJson());
  341. View::assign('area_array_json', $area_array_json);
  342. $this->setSellerCurMenu('sellertransport');
  343. $this->setSellerCurItem('offpay_area');
  344. return View::fetch($this->template_dir . 'offpay_area');
  345. }
  346. }
  347. /**
  348. * 用户中心右边,小导航
  349. *
  350. * @param string $menu_type 导航类型
  351. * @param string $name 当前导航的name
  352. *
  353. * @return
  354. */
  355. protected function getSellerItemList()
  356. {
  357. $menu_array = array(
  358. array(
  359. 'name' => 'transport', 'text' => lang('ds_member_path_postage'), 'url' => (string) url('Sellertransport/index')
  360. ),
  361. );
  362. if (check_platform_store()) {
  363. $menu_array[] = array(
  364. 'name' => 'offpay_area', 'text' => lang('offpay_area'), 'url' => (string) url('Sellertransport/offpay_area')
  365. );
  366. }
  367. return $menu_array;
  368. }
  369. }