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