Sellertransport.php 16 KB

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