Local.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace app\model\express;
  11. use app\model\BaseModel;
  12. use app\model\shop\Shop;
  13. use app\model\store\Store;
  14. /**
  15. * 外卖配送
  16. */
  17. class Local extends BaseModel
  18. {
  19. /**
  20. * 添加站点外卖配送配置
  21. * @param $data
  22. * @return array
  23. */
  24. public function addLocal($data)
  25. {
  26. $id = model('local')->add($data);
  27. return $this->success($id);
  28. }
  29. /**
  30. * 修改站点外卖配送配置
  31. * @param $data
  32. * @param $condition
  33. * @return array
  34. */
  35. public function editLocal($data, $condition)
  36. {
  37. $res = model('local')->update($data, $condition);
  38. return $this->success($res);
  39. }
  40. /**
  41. * 删除站点外卖配送 (通常删除站点会用到)
  42. * @param $condition
  43. * @return array
  44. */
  45. public function deleteLocal($condition)
  46. {
  47. $res = model('local')->delete($condition);
  48. return $this->success($res);
  49. }
  50. /**
  51. * 获取站点外卖配送信息
  52. * @param array $condition
  53. * @param string $field
  54. */
  55. public function getLocalInfo($condition, $field = '*')
  56. {
  57. $check_condition = array_column($condition, 2, 0);
  58. $site_id = isset($check_condition[ 'site_id' ]) ? $check_condition[ 'site_id' ] : '';
  59. $store_id = isset($check_condition[ 'store_id' ]) ? $check_condition[ 'store_id' ] : 0;
  60. $info = model('local')->getInfo($condition, $field);
  61. if (empty($info)) {
  62. $local_data = array (
  63. 'site_id' => $site_id,
  64. 'store_id' => $store_id,
  65. 'update_time' => time()
  66. );
  67. $this->addLocal($local_data);
  68. $info = model('local')->getInfo($condition, $field);
  69. }
  70. if (!empty($info)) {
  71. $local_area_array = [];
  72. if (!empty($info[ 'local_area_json' ])) {
  73. $local_area_array = json_decode($info[ 'local_area_json' ], true);
  74. }
  75. $info[ 'local_area_array' ] = $local_area_array;
  76. $time_week = [];
  77. if (!empty($info[ 'time_week' ])) {
  78. $time_week = explode(',', $info[ 'time_week' ]);
  79. }
  80. $info[ 'time_week' ] = $time_week;
  81. $area_array = [];
  82. if (!empty($info[ 'area_array' ])) {
  83. $area_array = explode(',', $info[ 'area_array' ]);
  84. }
  85. $info[ 'area_array' ] = $area_array;
  86. if (empty($info[ 'delivery_time' ])) {
  87. $info[ 'delivery_time' ] = [
  88. [ 'start_time' => $info[ 'start_time' ], 'end_time' => $info[ 'end_time' ] ]
  89. ];
  90. } else {
  91. $info[ 'delivery_time' ] = json_decode($info[ 'delivery_time' ], true);
  92. }
  93. }
  94. return $this->success($info);
  95. }
  96. /**
  97. * 获取站点外卖配送列表
  98. * @param array $condition
  99. * @param string $field
  100. * @param string $order
  101. * @param null $limit
  102. * @return array
  103. */
  104. public function getLocalList($condition = [], $field = '*', $order = '', $limit = null)
  105. {
  106. $list = model('local')->getList($condition, $field, $order, '', '', '', $limit);
  107. return $this->success($list);
  108. }
  109. /**
  110. * 获取站点外卖配送分页列表
  111. * @param array $condition
  112. * @param int $page
  113. * @param int $page_size
  114. * @param string $order
  115. * @param string $field
  116. * @return array
  117. */
  118. public function getLocalPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
  119. {
  120. $list = model('local')->pageList($condition, $field, $order, $page, $page_size);
  121. return $this->success($list);
  122. }
  123. /**
  124. * 计算费用
  125. * @param $shop_goods
  126. * @param $data
  127. * @return array
  128. */
  129. public function calculate($shop_goods, $data)
  130. {
  131. $site_id = $data[ 'site_id' ];
  132. $local_condition = array (
  133. [ 'site_id', '=', $site_id ]
  134. );
  135. $store_id = $data[ 'store_id' ] ?? 0;
  136. //todo 应该判断一下是否有门店运营插件
  137. if (!addon_is_exit('store')) {
  138. $store_id = 0;
  139. }
  140. if ($store_id > 0) $local_condition[] = [ 'store_id', '=', $store_id ];
  141. $local_info_result = $this->getLocalInfo($local_condition);
  142. $local_info = $local_info_result[ 'data' ];
  143. if (empty($local_info)) return $this->error('', '没有可以配送的门店');
  144. $start_price_error = 0;//起送价错误
  145. $distance_error = 0;//配送距离错误
  146. $time_error = 0;
  147. $error_code = 12;
  148. $error = '所选地址无法配送';
  149. //判断时间 是否在时间段内
  150. if ($local_info[ 'time_is_open' ] == 1) {
  151. $week = date('w');
  152. if (in_array($week, $local_info[ 'time_week' ])) {
  153. $time = $shop_goods[ 'buyer_ask_delivery_time' ];
  154. if ($time == 0) {
  155. $time_error++;
  156. $error = '请选择配送时间';
  157. } else {
  158. $time_error++;
  159. $error = '配送时间不在营业时间内';
  160. }
  161. }
  162. }
  163. if ($store_id == 0) {
  164. $shop_model = new Shop();
  165. $shop_info = $shop_model->getShopInfo([ [ 'site_id', '=', $site_id ] ])[ 'data' ] ?? [];
  166. } else {
  167. $store_model = new Store();
  168. $shop_info = $store_model->getStoreInfo([ [ 'site_id', '=', $site_id ], [ 'store_id', '=', $store_id ] ])[ 'data' ] ?? [];
  169. }
  170. $is_delivery = false;
  171. $start_money_array = [];
  172. if ($local_info[ 'area_type' ] == 1 || $local_info[ 'area_type' ] == 2) {
  173. if ($data[ 'member_address' ][ 'longitude' ] == 0 && $data[ 'member_address' ][ 'latitude' ] == 0) {
  174. return $this->error([ 'code' => $error_code ], '当前配送地址没有配置定位坐标');
  175. }
  176. $shop_longitude = $shop_info[ 'longitude' ];
  177. $shop_latitude = $shop_info[ 'latitude' ];
  178. $longitude = $data[ 'member_address' ][ 'longitude' ];
  179. $latitude = $data[ 'member_address' ][ 'latitude' ];
  180. if ($local_info[ 'area_type' ] == 1) {
  181. $distance = $this->getDistance($latitude, $longitude, $shop_latitude, $shop_longitude);
  182. if ($distance <= $local_info[ 'start_distance' ]) {//是否在起送距离以内
  183. $delivery_money = $local_info[ 'start_delivery_money' ];
  184. } else {
  185. $diff_distance = $distance - $local_info[ 'start_distance' ];//减去起送距离 求得差
  186. if ($local_info[ 'continued_distance' ] == 0) return $this->error([ 'code' => $error_code ], '当前配送地址不支持配送');
  187. $delivery_money = $local_info[ 'start_delivery_money' ] + ceil($diff_distance / $local_info[ 'continued_distance' ]) * $local_info[ 'continued_delivery_money' ];
  188. }
  189. } else {
  190. $delivery_money_array = [];
  191. }
  192. $local_area_array = $local_info[ 'local_area_array' ];
  193. if (!empty($local_area_array)) {
  194. foreach ($local_area_array as $k => $v) {
  195. //起送价是否满足
  196. if ($shop_goods[ 'goods_money' ] >= $v[ 'start_price' ]) {
  197. $path = $v[ 'path' ];
  198. if ($v[ 'rule_type' ] == 'circle') {//半径
  199. $item_longitude = $path[ 'center' ][ 'longitude' ];
  200. $item_latitude = $path[ 'center' ][ 'latitude' ];
  201. $radius = $path[ 'radius' ];
  202. $item_distance = $this->getDistance($latitude, $longitude, $item_latitude, $item_longitude);
  203. //判断有无超出范围
  204. if ($item_distance <= $radius / 1000) {
  205. $is_delivery = true;
  206. if ($local_info[ 'area_type' ] == 2) {
  207. //非半径 配送费是取设置的
  208. $delivery_money_array[] = $v[ 'delivery_money' ];
  209. }
  210. } else {
  211. $distance_error++;
  212. }
  213. } else if ($v[ 'rule_type' ] == 'polygon') {//多边形
  214. $point = array (
  215. 'latitude' => $latitude,
  216. 'longitude' => $longitude,
  217. );
  218. //判断坐标是否在多边形内
  219. if (is_point_in_polygon($point, $path)) {
  220. $is_delivery = true;
  221. if ($local_info[ 'area_type' ] == 2) {
  222. //非半径 配送费是取设置的
  223. $delivery_money_array[] = $v[ 'delivery_money' ];
  224. }
  225. } else {
  226. $distance_error++;
  227. }
  228. }
  229. } else {
  230. $start_money_array[] = $v[ 'start_price' ];
  231. $start_price_error++;
  232. }
  233. }
  234. //区域配送 存在相交的区域,以配送费低的区域价格来计算
  235. if ($local_info[ 'area_type' ] == 2) {
  236. if (!empty($delivery_money_array)) {
  237. $delivery_money = min($delivery_money_array);
  238. }
  239. }
  240. }
  241. } else {
  242. //行政区域配送
  243. if ($shop_goods[ 'goods_money' ] >= $local_info[ 'start_money' ]) {
  244. $district_id = $data[ 'member_address' ][ 'district_id' ];//区县地域id
  245. $area_array = $local_info[ 'area_array' ];
  246. if (in_array($district_id, $area_array)) {
  247. //启用阶梯价 的话 也是必须要具体坐标的
  248. if ($local_info[ 'is_open_step' ] == 1) {
  249. if ($data[ 'member_address' ][ 'longitude' ] == 0 && $data[ 'member_address' ][ 'latitude' ] == 0) {
  250. return $this->error([ 'code' => $error_code ], '当前配送地址没有配置定位坐标');
  251. }
  252. $is_delivery = true;
  253. $shop_longitude = $shop_info[ 'longitude' ];
  254. $shop_latitude = $shop_info[ 'latitude' ];
  255. $longitude = $data[ 'member_address' ][ 'longitude' ];
  256. $latitude = $data[ 'member_address' ][ 'latitude' ];
  257. $distance = $this->getDistance($latitude, $longitude, $shop_latitude, $shop_longitude);
  258. if ($distance <= $local_info[ 'start_distance' ]) {//是否在起送距离以内
  259. $delivery_money = $local_info[ 'start_delivery_money' ];
  260. } else {
  261. $diff_distance = $distance - $local_info[ 'start_distance' ];//减去起送距离 求得差
  262. $delivery_money = $local_info[ 'start_delivery_money' ] + ceil($diff_distance / $local_info[ 'continued_distance' ]) * $local_info[ 'continued_delivery_money' ];
  263. }
  264. } else {
  265. $is_delivery = true;
  266. $delivery_money = $local_info[ 'delivery_money' ];
  267. }
  268. } else {
  269. $distance_error++;//配送距离错误
  270. }
  271. } else {
  272. $start_price_error++;//起送价错误
  273. $start_money_array[] = $local_info[ 'start_money' ];
  274. }
  275. }
  276. if ($is_delivery) {
  277. if ($delivery_money > 0) {
  278. $man_type = $local_info[ 'man_type' ];
  279. $man_money = $local_info[ 'man_money' ];
  280. switch ( $man_type ) {
  281. case 'free':
  282. if ($shop_goods[ 'goods_money' ] >= $man_money) {
  283. $delivery_money = 0;
  284. }
  285. break;
  286. case 'discount':
  287. if ($shop_goods[ 'goods_money' ] >= $man_money) {
  288. $man_discount = $local_info[ 'man_discount' ];
  289. $delivery_money -= $man_discount;
  290. $delivery_money = $delivery_money < 0 ? 0 : $delivery_money;
  291. }
  292. break;
  293. }
  294. }
  295. $return_result = array (
  296. 'delivery_money' => $delivery_money,
  297. );
  298. if ($time_error > 0) {
  299. $return_result[ 'code' ] = 1;
  300. $return_result[ 'error' ] = $error;
  301. }
  302. return $this->success($return_result);
  303. } else {
  304. if ($distance_error > 0) {
  305. $error = '当前地址不在该门店配送区域,请重新选择可配送该区域的门店';
  306. $error_code = 10;
  307. } else if ($start_price_error > 0 && !isset($data[ 'unlimited_start_money' ])) {
  308. $error = '当前商品金额尚不满足商家配送的最低起送价格';
  309. $error_code = 11;
  310. }
  311. return $this->error([ 'code' => $error_code, 'start_money_array' => $start_money_array ], $error);
  312. }
  313. }
  314. /**
  315. * 区域是否支持配送
  316. * @param $data
  317. * @return array
  318. */
  319. public function isSupportDelivery($data)
  320. {
  321. $local_condition = array (
  322. [ 'site_id', '=', $data[ 'site_id' ] ],
  323. );
  324. if (isset($data[ 'store_id' ]) && !empty($data[ 'store_id' ])) $local_condition[] = [ 'store_id', '=', $data[ 'store_id' ] ];
  325. $local_info_result = $this->getLocalInfo($local_condition);
  326. $local_info = $local_info_result[ 'data' ];
  327. $distance_error = 0;//配送距离错误
  328. $error_code = 12;
  329. $is_delivery = false;
  330. if ($local_info[ 'area_type' ] == 1 || $local_info[ 'area_type' ] == 2) {
  331. if ($data[ 'longitude' ] == 0 && $data[ 'latitude' ] == 0) {
  332. return $this->error([ 'code' => $error_code ], '超出配送范围');
  333. }
  334. $longitude = $data[ 'longitude' ];
  335. $latitude = $data[ 'latitude' ];
  336. $local_area_array = $local_info[ 'local_area_array' ];
  337. if (!empty($local_area_array)) {
  338. foreach ($local_area_array as $k => $v) {
  339. $path = $v[ 'path' ];
  340. if ($v[ 'rule_type' ] == 'circle') {//半径
  341. $item_longitude = $path[ 'center' ][ 'longitude' ];
  342. $item_latitude = $path[ 'center' ][ 'latitude' ];
  343. $radius = $path[ 'radius' ];
  344. $item_distance = $this->getDistance($latitude, $longitude, $item_latitude, $item_longitude);
  345. //判断有无超出范围
  346. if ($item_distance <= $radius / 1000) {
  347. $is_delivery = true;
  348. } else {
  349. $distance_error++;
  350. }
  351. } else if ($v[ 'rule_type' ] == 'polygon') {//多边形
  352. $point = array (
  353. 'latitude' => $latitude,
  354. 'longitude' => $longitude,
  355. );
  356. //判断坐标是否在多边形内
  357. if (is_point_in_polygon($point, $path)) {
  358. $is_delivery = true;
  359. } else {
  360. $distance_error++;
  361. }
  362. }
  363. }
  364. }
  365. } else {
  366. //行政区域配送
  367. $district_id = $data[ 'district_id' ];//区县地域id
  368. $area_array = $local_info[ 'area_array' ];
  369. if (in_array($district_id, $area_array)) {
  370. //启用阶梯价 的话 也是必须要具体坐标的
  371. if ($local_info[ 'is_open_step' ] == 1) {
  372. if ($data[ 'longitude' ] == 0 && $data[ 'latitude' ] == 0) {
  373. return $this->error([ 'code' => $error_code ], '超出配送范围');
  374. }
  375. $is_delivery = true;
  376. } else {
  377. $is_delivery = true;
  378. }
  379. } else {
  380. $distance_error++;//配送距离错误
  381. }
  382. }
  383. if (!$is_delivery && $distance_error) {
  384. $error = '超出配送范围';
  385. return $this->error([ 'code' => $error_code ], $error);
  386. }
  387. return $this->success(1);
  388. }
  389. /**
  390. * 判断可用的区域
  391. * @param $type
  392. * @param $latlng
  393. * @param $range
  394. */
  395. public function getWithAreaList($type, $latlng, $range)
  396. {
  397. }
  398. /**
  399. * 求两个已知经纬度之间的距离,单位为km
  400. * @param lng1,lng2 经度
  401. * @param lat1,lat2 纬度
  402. * @return float 距离,单位为km
  403. **/
  404. public function getDistance($lat1, $lng1, $lat2, $lng2)
  405. {
  406. //将角度转为狐度
  407. $radLat1 = deg2rad($lat1);//deg2rad()函数将角度转换为弧度
  408. $radLat2 = deg2rad($lat2);
  409. $radLng1 = deg2rad($lng1);
  410. $radLng2 = deg2rad($lng2);
  411. $a = $radLat1 - $radLat2;
  412. $b = $radLng1 - $radLng2;
  413. $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6371;
  414. return round($s, 1);
  415. }
  416. /**
  417. * 积分兑换计算费用
  418. * @param $goods_info
  419. * @param $data
  420. * @return array
  421. */
  422. public function pointExchangeCalculate($goods_info, $data)
  423. {
  424. $local_condition = array (
  425. [ 'site_id', '=', $data[ 'site_id' ] ]
  426. );
  427. if (isset($data[ 'delivery' ][ 'store_id' ])) $local_condition[] = [ 'store_id', '=', $data[ 'delivery' ][ 'store_id' ] ];
  428. $local_info_result = $this->getLocalInfo($local_condition);
  429. $local_info = $local_info_result[ 'data' ];
  430. if (empty($local_info)) return $this->error('', '没有可以配送的门店');
  431. $start_price_error = 0;//起送价错误
  432. $distance_error = 0;//配送距离错误
  433. $time_error = 0;
  434. $error_code = 12;
  435. $error = '';
  436. //判断时间 是否在时间段内
  437. if ($local_info[ 'time_is_open' ] == 1) {
  438. $week = date('w');
  439. if (in_array($week, $local_info[ 'time_week' ])) {
  440. $time = $goods_info[ 'buyer_ask_delivery_time' ];
  441. if ($time == 0) {
  442. $time_error++;
  443. $error = '请选择配送时间';
  444. } else {
  445. $time_error++;
  446. $error = '配送时间不在营业时间内';
  447. }
  448. }
  449. }
  450. $shop_model = new Shop();
  451. $shop_info_result = $shop_model->getShopInfo([ [ 'site_id', '=', $data[ 'site_id' ] ] ]);
  452. $shop_info = $shop_info_result[ 'data' ];
  453. $is_delivery = false;
  454. $start_money_array = [];
  455. if ($local_info[ 'area_type' ] == 1 || $local_info[ 'area_type' ] == 2) {
  456. if (empty($data[ 'member_address' ])) {
  457. return $this->error([ 'code' => $error_code ], '请设置配送地址');
  458. }
  459. if ($data[ 'member_address' ][ 'longitude' ] == 0 && $data[ 'member_address' ][ 'latitude' ] == 0) {
  460. return $this->error([ 'code' => $error_code ], '当前配送地址没有配置定位坐标');
  461. }
  462. $shop_longitude = $shop_info[ 'longitude' ];
  463. $shop_latitude = $shop_info[ 'latitude' ];
  464. $longitude = $data[ 'member_address' ][ 'longitude' ];
  465. $latitude = $data[ 'member_address' ][ 'latitude' ];
  466. if ($local_info[ 'area_type' ] == 1) {
  467. $distance = $this->getDistance($latitude, $longitude, $shop_latitude, $shop_longitude);
  468. if ($distance <= $local_info[ 'start_distance' ]) {//是否在起送距离以内
  469. $delivery_money = $local_info[ 'start_delivery_money' ];
  470. } else {
  471. $diff_distance = $distance - $local_info[ 'start_distance' ];//减去起送距离 求得差
  472. $delivery_money = $local_info[ 'start_delivery_money' ] + ceil($diff_distance / $local_info[ 'continued_distance' ]) * $local_info[ 'continued_delivery_money' ];
  473. }
  474. } else {
  475. $delivery_money_array = [];
  476. }
  477. $local_area_array = $local_info[ 'local_area_array' ];
  478. if (!empty($local_area_array)) {
  479. foreach ($local_area_array as $k => $v) {
  480. //起送价是否满足
  481. if ($data[ 'exchange_info' ][ 'price' ] >= $v[ 'start_price' ]) {
  482. $path = $v[ 'path' ];
  483. if ($v[ 'rule_type' ] == 'circle') {//半径
  484. $item_longitude = $path[ 'center' ][ 'longitude' ];
  485. $item_latitude = $path[ 'center' ][ 'latitude' ];
  486. $radius = $path[ 'radius' ];
  487. $item_distance = $this->getDistance($latitude, $longitude, $item_latitude, $item_longitude);
  488. //判断有无超出范围
  489. if ($item_distance <= $radius / 1000) {
  490. $is_delivery = true;
  491. if ($local_info[ 'area_type' ] == 2) {
  492. // //非半径 配送费是取设置的
  493. $delivery_money_array[] = $v[ 'delivery_money' ];
  494. }
  495. } else {
  496. $distance_error++;
  497. }
  498. } else if ($v[ 'rule_type' ] == 'polygon') {//多边形
  499. $point = array (
  500. 'latitude' => $latitude,
  501. 'longitude' => $longitude,
  502. );
  503. //判断坐标是否在多边形内
  504. if (is_point_in_polygon($point, $path)) {
  505. $is_delivery = true;
  506. if ($local_info[ 'area_type' ] == 2) {
  507. //非半径 配送费是取设置的
  508. $delivery_money_array[] = $v[ 'delivery_money' ];
  509. }
  510. } else {
  511. $distance_error++;
  512. }
  513. }
  514. } else {
  515. $start_money_array[] = $v[ 'start_price' ];
  516. $start_price_error++;
  517. }
  518. }
  519. //区域配送 存在相交的区域,以配送费低的区域价格来计算
  520. if ($local_info[ 'area_type' ] == 2) {
  521. if (!empty($delivery_money_array)) {
  522. $delivery_money = min($delivery_money_array);
  523. }
  524. }
  525. }
  526. } else {
  527. //行政区域配送
  528. if ($data[ 'exchange_info' ][ 'price' ] >= $local_info[ 'start_money' ]) {
  529. $district_id = $data[ 'member_address' ][ 'district_id' ];//区县地域id
  530. $area_array = $local_info[ 'area_array' ];
  531. if (in_array($district_id, $area_array)) {
  532. //启用阶梯价 的话 也是必须要具体坐标的
  533. if ($local_info[ 'is_open_step' ] == 1) {
  534. if ($data[ 'member_address' ][ 'longitude' ] == 0 && $data[ 'member_address' ][ 'latitude' ] == 0) {
  535. return $this->error([ 'code' => $error_code ], '当前配送地址没有配置定位坐标');
  536. }
  537. $is_delivery = true;
  538. $shop_longitude = $shop_info[ 'longitude' ];
  539. $shop_latitude = $shop_info[ 'latitude' ];
  540. $longitude = $data[ 'member_address' ][ 'longitude' ];
  541. $latitude = $data[ 'member_address' ][ 'latitude' ];
  542. $distance = $this->getDistance($latitude, $longitude, $shop_latitude, $shop_longitude);
  543. if ($distance <= $local_info[ 'start_distance' ]) {//是否在起送距离以内
  544. $delivery_money = $local_info[ 'start_delivery_money' ];
  545. } else {
  546. $diff_distance = $distance - $local_info[ 'start_distance' ];//减去起送距离 求得差
  547. $delivery_money = $local_info[ 'start_delivery_money' ] + ceil($diff_distance / $local_info[ 'continued_distance' ]) * $local_info[ 'continued_delivery_money' ];
  548. }
  549. } else {
  550. $is_delivery = true;
  551. $delivery_money = $local_info[ 'delivery_money' ];
  552. }
  553. } else {
  554. $distance_error++;//配送距离错误
  555. }
  556. } else {
  557. $start_price_error++;//起送价错误
  558. $start_money_array[] = $local_info[ 'start_money' ];
  559. }
  560. }
  561. if ($is_delivery) {
  562. $return_result = array (
  563. 'delivery_money' => $delivery_money,
  564. 'start_money_array' => $start_money_array,
  565. );
  566. if ($time_error > 0) {
  567. $return_result[ 'code' ] = 1;
  568. $return_result[ 'error' ] = $error;
  569. }
  570. return $this->success($return_result);
  571. } else {
  572. if ($distance_error > 0) {
  573. $error = '当前地址不在该门店配送区域,请重新选择可配送该区域的门店';
  574. $error_code = 10;
  575. } else if ($start_price_error > 0) {
  576. $error = '当前商品金额尚不满足商家配送的最低起送价格';
  577. $error_code = 11;
  578. }
  579. return $this->error([ 'code' => $error_code ], $error);
  580. }
  581. }
  582. /**
  583. * 核验是否可以开启本地配送
  584. * @param $site_id
  585. * @return array
  586. */
  587. public function checkIsCanTradeLocal($site_id)
  588. {
  589. $store = new Store();
  590. $default_store = $store->getStoreInfo([ [ 'site_id', '=', $site_id ], [ 'is_default', '=', 1 ] ], 'store_id')[ 'data' ] ?? [];
  591. $store_id = $default_store[ 'store_id' ] ?? 0;
  592. $local_info = $this->getLocalInfo([ [ 'site_id', '=', $site_id ], [ 'store_id', '=', $store_id ] ])[ 'data' ] ?? [];
  593. if (empty($local_info))
  594. return $this->error([], '您未完成起送金额、配送费 、配送区域等同城送配置项设置,需设置并提交保存后,才能开启同城配送开关。');
  595. //判断配置有没有配置完善
  596. $local_area_array = $local_info[ 'local_area_array' ];
  597. $area_array = $local_info[ 'area_array' ];
  598. if (empty($local_area_array) && empty($area_array))
  599. return $this->error([], '您未完成起送金额、配送费 、配送区域等同城送配置项设置,需设置并提交保存后,才能开启同城配送开关。');
  600. return $this->success();
  601. }
  602. }