SettingController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /****************************************************************
  3. * Author: king -- LiBaoJia
  4. * Date: 2020/2/24 10:16 AM
  5. * Email: livsyitian@163.com
  6. * QQ: 995265288
  7. * IDE: PhpStorm
  8. * User: 芸众商城 www.yunzshop.com
  9. ****************************************************************/
  10. namespace app\backend\modules\map\controllers;
  11. use app\common\components\BaseController;
  12. use app\common\facades\Setting;
  13. use Illuminate\Support\Facades\DB;
  14. class SettingController extends BaseController
  15. {
  16. public function index()
  17. {
  18. if ($this->postData()) {
  19. return $this->store();
  20. }
  21. return view('map.setting', $this->viewData());
  22. }
  23. /**
  24. * 数据存储
  25. */
  26. private function store()
  27. {
  28. DB::beginTransaction();
  29. Setting::set("map.a_map", $this->postData());
  30. Setting::set('map.a_map.synchronize_store', 1);
  31. if (!$this->webConfig()) {
  32. DB::rollBack();
  33. return $this->errorJson('地图设置失败');
  34. }
  35. DB::commit();
  36. return $this->successJson('地图设置成功');
  37. }
  38. /**
  39. * 提交数据
  40. *
  41. * @return array
  42. */
  43. private function postData()
  44. {
  45. $request = request()->input('a_map', []);
  46. return $request;
  47. }
  48. /**
  49. * view 数据
  50. *
  51. * @return array
  52. */
  53. private function viewData()
  54. {
  55. $map = Setting::get('map.a_map');
  56. //兼容同步门店配送规则
  57. if (!$map['web'] && !$map['synchronize_store']) {
  58. Setting::set('map.a_map.synchronize_store', 1);
  59. $map['synchronize_store'] = 1;
  60. }
  61. return ['map' => $map];
  62. }
  63. public function synchronize()
  64. {
  65. if (!app('plugins')->isEnabled('store-delivery')) {
  66. return $this->errorJson('请先开启门店配送插件');
  67. }
  68. if (Setting::get('map.a_map.synchronize_store') == 1) {
  69. return $this->errorJson('同步失败,已经同步过数据或者修改过地图KEY');
  70. }
  71. $mapKey = Setting::get('map.a_map.web');
  72. if (!$mapKey) {
  73. return $this->errorJson('同步失败,地图KEY没有设置');
  74. }
  75. Setting::set('map.a_map.synchronize_store', 1);
  76. //更新门店地图key
  77. $uniacid = \Yunshop::app()->uniacid;
  78. $list = DB::table('yz_store_geo_fence')
  79. ->select('yz_store_geo_fence.*')
  80. ->leftJoin('yz_store', 'yz_store_geo_fence.store_id', '=', 'yz_store.id')
  81. ->where('yz_store.uniacid', $uniacid)
  82. ->get()->toArray();
  83. foreach ($list as $key => $value) {
  84. $webKey = Setting::get("store_cashier_{$value['store_id']}.a_map.web_key") ?? '';
  85. if (!$webKey) {
  86. Setting::set("store_cashier_{$value['store_id']}.a_map", ['web_key' => $mapKey]);
  87. }
  88. }
  89. return $this->successJson('地图KEY同步成功');
  90. }
  91. // 在前端生成shopConfig.js 文件
  92. private function webConfig()
  93. {
  94. $aMapConfig = \Illuminate\Support\Facades\DB::table('yz_setting')
  95. ->where('group', 'map')
  96. ->get()
  97. ->toArray();
  98. $data = [];
  99. $data[0] = [
  100. "key" => "1186e26be922287789d6218dbce587e5", // 高德地图的key
  101. "securityJsCode" => '67200859e522bc2fcac1a2d2b11a2f77' // 高德地图秘钥
  102. ];
  103. foreach ($aMapConfig as $item) {
  104. $item['value'] = unserialize($item['value']);
  105. if (!$item['value']['web_js_key'] || !$item['value']['web_js_secret_key']) {
  106. continue;
  107. }
  108. $data[$item['uniacid']] = [
  109. 'key' => $item['value']['web_js_key'],
  110. 'securityJsCode' => $item['value']['web_js_secret_key'],
  111. ];
  112. }
  113. $url = $this->getUrl();
  114. $shopConfig = fopen($url, 'w');
  115. if ($shopConfig) {
  116. fwrite($shopConfig, 'var $AMapConfig = ' . json_encode($data));
  117. fclose($shopConfig);
  118. return true;
  119. } else {
  120. fclose($shopConfig);
  121. return false;
  122. }
  123. }
  124. private function getUrl() {
  125. $re = "/yun_shop$/";
  126. if (preg_match($re, base_path())) {
  127. $url = base_path() . DIRECTORY_SEPARATOR . "shopConfig.js";
  128. } else {
  129. $url = base_path() . DIRECTORY_SEPARATOR . "addons" . DIRECTORY_SEPARATOR . "yun_shop" . DIRECTORY_SEPARATOR . "shopConfig.js";
  130. }
  131. return $url;
  132. }
  133. }