Sellertransport.php 16 KB

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