DefaultController.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\controllers;
  8. use app\constants\OptionSetting;
  9. use app\librarys\KuaiDi100\KuaiDi100;
  10. use app\models\AboutArticle;
  11. use app\models\AppNavbar;
  12. use app\models\BrowseLog;
  13. use app\models\Coupon;
  14. use app\models\District;
  15. use app\models\Goods;
  16. use app\models\GoodsBrand;
  17. use app\models\Option;
  18. use app\models\Shop;
  19. use app\models\Store;
  20. use app\utils\Address\Address;
  21. use yii\helpers\Json;
  22. use app\utils\Tools;
  23. use app\models\WechatConfig;
  24. use app\constants\StoreImageFrom;
  25. use app\modules\alliance\models\ConfigForm;
  26. use app\modules\admin\models\StoreForm;
  27. use app\modules\alliance\models\ShareQrcodeForm;
  28. //use app\modules\client\models\v1\TopicForm;
  29. use app\modules\alliance\models\TopicListForm;
  30. use app\modules\alliance\models\TopicTypeForm;
  31. use app\modules\alliance\models\GoodsAttrInfoForm;
  32. use app\modules\alliance\models\CommentListForm;
  33. use app\modules\alliance\models\GoodsListForm;
  34. use app\modules\alliance\models\TopicForm;
  35. use app\modules\client\models\v1\GoodsBrandForm;
  36. use app\modules\alliance\models\CatListForm;
  37. use app\models\Admin;
  38. class DefaultController extends BaseController
  39. {
  40. /**
  41. * 领券中心
  42. * @return \yii\web\Response
  43. */
  44. public function actionSaasCouponList()
  45. {
  46. $page = get_params('page',1);
  47. $pageSize = get_params('pageSize',20);
  48. return $this->asJson(['code' => 0, 'msg' => 'success', 'data' => Coupon::getAllianceList($page,$pageSize, [], get_store_id()) ]);
  49. }
  50. /**
  51. * 获取商盟设置
  52. */
  53. public function actionAlliance()
  54. {
  55. if (cache()->get('AllianceConfig') && false) {
  56. $config = cache()->get('AllianceConfig');
  57. } else {
  58. $config = ConfigForm::getConfig();
  59. cache()->set('AllianceConfig', $config, 60 * 60);
  60. }
  61. return $this->asJson([
  62. 'code' => 0,
  63. 'msg' => 'success',
  64. 'data' => $config
  65. ]);
  66. }
  67. /**
  68. * 获取当前具体位置
  69. * @return \yii\web\Response
  70. */
  71. public function actionLocationContent()
  72. {
  73. $longitude = (float)$_REQUEST['longitude']; //用户当前定位的经度
  74. $latitude = (float)$_REQUEST['latitude']; //用户当前定位的纬度
  75. $tencent_map_key = Option::get('tencent_map_key', 0, 'saas', '')['value'];
  76. if (get_store_id() > 0) {
  77. $tencent_map_key = Option::get(OptionSetting::TENCENT_MAP_KEY, get_store_id(), 'pay', Option::get(OptionSetting::TENCENT_MAP_KEY, get_store_id(), 'store', '')['value'] ?: $tencent_map_key)['value'];
  78. }
  79. $place_url = 'https://apis.map.qq.com/ws/geocoder/v1/?location=' . $latitude . ',' . $longitude . '&key=' . $tencent_map_key;
  80. $json_place = file_get_contents($place_url);
  81. $place_arr = json_decode($json_place, true);
  82. $address = $place_arr['result']['address_component'];
  83. return $this->asJson([
  84. 'code' => 0,
  85. 'msg' => 'success',
  86. 'data' => $address
  87. ]);
  88. }
  89. /**
  90. * 获取规格详情
  91. * @return mixed
  92. */
  93. public function actionGoodsAttrInfo()
  94. {
  95. $form = new GoodsAttrInfoForm();
  96. $form->attributes = get_params();
  97. return $this->asJson($form->search());
  98. }
  99. /**
  100. * 获取所有地区
  101. * @return \yii\web\Response
  102. */
  103. public function actionDistrict()
  104. {
  105. $list = District::getAll();
  106. return $this->asJson([
  107. 'code' => 0,
  108. 'data' => $list
  109. ]);
  110. }
  111. /**
  112. * 根据位置解析地址
  113. */
  114. public function actionAddressSmart()
  115. {
  116. $store_id = get_store_id();
  117. $address = get_params('address');
  118. $tencent_map_key = Option::get(OptionSetting::TENCENT_MAP_KEY, $store_id, 'pay', Option::get(OptionSetting::TENCENT_MAP_KEY, $store_id, 'store', '')['value'])['value'];
  119. //通过地址信息获取经纬度
  120. if (empty($tencent_map_key)) {
  121. $tencent_map_key = Option::get('tencent_map_key', 0, 'saas', '')['value'];
  122. }
  123. $url = 'https://apis.map.qq.com/ws/geocoder/v1/?';
  124. $params = [
  125. 'key' => $tencent_map_key,
  126. 'address' => $address,
  127. ];
  128. $url .= http_build_query($params);
  129. $json = file_get_contents($url);
  130. $tencent_result = json_decode($json, true);
  131. if ($tencent_result['status'] === 0) {
  132. $address_area = $tencent_result['result']['address_components']['province'] .
  133. $tencent_result['result']['address_components']['city'] .
  134. $tencent_result['result']['address_components']['district'] .
  135. $tencent_result['result']['title'];
  136. } else {
  137. $address_area = $address;
  138. }
  139. $res = $this->addressSmart(['address' => $address_area]);
  140. return $this->asJson([
  141. 'code' => 0,
  142. 'data' => $res
  143. ]);
  144. }
  145. public function addressSmart($params = []) {
  146. $res = Address::smart($params['address'], false);
  147. $province_id = District::find()->where(['and', ['like', 'name', $res['province']], ['level' => 'province']])->one()->id;
  148. $city_id = District::findOne(['name' => $res['city'], 'level' => 'city'])->id;
  149. $region_id = District::findOne(['name' => $res['region'], 'level' => 'district'])->id;
  150. if (!$province_id || !$city_id || !$region_id) {
  151. $province_id = District::findOne(['name' => '其他', 'level' => 'province'])->id;
  152. $city_id = District::findOne(['name' => '其他', 'level' => 'city'])->id;
  153. $region_id = District::findOne(['name' => '其他', 'level' => 'district'])->id;
  154. $res['province'] = '其他';
  155. $res['city'] = '其他';
  156. $res['region'] = '其他';
  157. }
  158. $res['province_id'] = $province_id;
  159. $res['city_id'] = $city_id;
  160. $res['region_id'] = $region_id;
  161. return $res;
  162. }
  163. /**
  164. * 获取商城设置
  165. */
  166. public function actionStoreInfo()
  167. {
  168. $longitude = get_params('longitude', 0); //用户当前定位的经度
  169. $latitude = get_params('latitude', 0); //用户当前定位的纬度
  170. $store_id = get_store_id();
  171. $storeInfo = Store::find()->where(['id'=>$store_id,'is_delete'=>0])->select('address,recommend_keyword,coordinate,name,logo,sales,rank,per_spend,tags,user_name,contact_tel,open_status,s_time,e_time,self_rebate_switch,self_rebate_value,transfer_profit')->asArray()->one();
  172. if(!$storeInfo){
  173. return $this->asJson([
  174. 'code' => 1,
  175. 'msg' => '不存在店铺信息',
  176. ]);
  177. }
  178. if($storeInfo['coordinate']){
  179. $coordinateInfo = explode(",",$storeInfo['coordinate']);
  180. if(isset($coordinateInfo[0]) && isset($coordinateInfo[1])) {
  181. $storeInfo['distance'] = Tools::getDistance($latitude, $longitude, $coordinateInfo[0], $coordinateInfo[1]);
  182. }else{
  183. $storeInfo['distance'] = 0;
  184. }
  185. }
  186. if($storeInfo['self_rebate_switch']){
  187. if($storeInfo['transfer_profit']>0){
  188. $storeInfo['rebate'] = sprintf("%.2f", 100 * ($storeInfo['self_rebate_value'] / 100 ) * ($storeInfo['transfer_profit'] / 100 ) );
  189. }else{
  190. $storeInfo['rebate'] = 0;
  191. }
  192. }else{
  193. $storeInfo['rebate'] = 0;
  194. }
  195. $storeInfo['goods_send_profit'] = Option::get('goods_send_profit', $store_id, 'store', 0)['value'];
  196. $storeInfo['goods_take_price'] = Option::get('goods_take_price', $store_id, 'store', 0)['value'];
  197. return $this->asJson([
  198. 'code' => 0,
  199. 'msg' => 'success',
  200. 'data' => $storeInfo
  201. ]);
  202. }
  203. /**
  204. * 获取商城设置
  205. */
  206. public function actionStore()
  207. {
  208. // 服务器图片
  209. $form = new StoreImageFrom();
  210. $wxappImg = $form->search();
  211. $store = null;
  212. if (get_store_id() > 0) {
  213. $store = Store::find()->where(['id' => get_store_id()])->select('id, name, logo, kefu_id')->asArray()->one();
  214. }
  215. $saasUser = get_saas_user();
  216. if ($saasUser) {
  217. $store_admin = Admin::find()->where(['mobile' => $saasUser->mobile, 'is_delete' => 0, 'type' => 'store'])->orderBy('id DESC')->one();
  218. if (!$store_admin) {
  219. $store_admin = Admin::find()->where(['saas_user_id' => $saasUser->id, 'is_delete' => 0, 'type' => 'store'])->orderBy('id DESC')->one();
  220. }
  221. //此处兼容服务端系统将客服以及云仓功能放入店铺里
  222. try {
  223. $payload = \Yii::$app->jwt->getPayload();
  224. $store_admin_id = $payload['store_admin_id'];
  225. if ($store_admin_id > 0) {
  226. $StoreMiniAdmin = \app\models\StoreAdmin::findOne(['id' => $store_admin_id, 'status' => 1, 'is_delete' => 0]);
  227. $store_id = $StoreMiniAdmin->store_id;
  228. } else {
  229. $admin = \app\models\Admin::findOne(['id' => $payload['admin_id']]);
  230. $store_id = $admin->type_id;
  231. }
  232. } catch (\Exception $e) {
  233. $store_id = $store_admin->type_id;
  234. }
  235. if ($store_id) {
  236. $store = Store::find()->where(['id' => $store_id, 'is_delete' => 0])
  237. ->select('id, name, logo, kefu_id')->asArray()->one();
  238. }
  239. }
  240. $alliance_service_article = AboutArticle::find()->where(['type' => AboutArticle::TYPE_SERVICE, 'is_delete' => 0])
  241. ->select(['id'])->asArray()->one();
  242. $alliance_privacy_article = AboutArticle::find()->where(['type' => AboutArticle::TYPE_PRIVACY, 'is_delete' => 0])
  243. ->select(['id'])->asArray()->one();
  244. $store['client_service_id'] = $alliance_service_article['id'] ?? 0;
  245. $store['client_privacy_id'] = $alliance_privacy_article['id'] ?? 0;
  246. $store['is_dandianpu'] = \Yii::$app->prod_is_dandianpu();
  247. $store['is_duli'] = \Yii::$app->prod_is_duli();
  248. $store['is_saas'] = \Yii::$app->prod_is_saas();
  249. $store['is_shangmeng'] = \Yii::$app->prod_is_shangmeng();
  250. $store['platform_gh_id'] = Option::get('platform_gh_id', 0, 'saas')['value'];
  251. $config =[
  252. 'images'=>$wxappImg,
  253. 'store' => $store,
  254. ];
  255. return $this->asJson([
  256. 'code' => 0,
  257. 'msg' => 'success',
  258. 'data' => $config
  259. ]);
  260. }
  261. /**
  262. * 添加浏览记录
  263. */
  264. public function actionBrowse()
  265. {
  266. if (get_saas_user_id() > 0) {
  267. $start = strtotime(date('Y-m-d',time()));
  268. $form = BrowseLog::find()->where([
  269. 'log_id' => get_params('id'),
  270. 'type' => get_params('type'),
  271. 'store_id' => get_store_id(),
  272. 'user_id' => 0,
  273. 'saas_id' => get_saas_user_id(),
  274. 'is_delete' => 0
  275. ])->andWhere([
  276. ">",
  277. 'created_at',
  278. $start
  279. ])->one();
  280. $form = $form ?: new BrowseLog();
  281. $form->log_id = get_params('id');
  282. $form->type = get_params('type');
  283. $form->store_id = get_store_id();
  284. $form->user_id = 0;
  285. $form->saas_id = get_saas_user_id();
  286. $form->created_at = time();
  287. $form->save();
  288. }
  289. return $this->asJson([
  290. 'code' => 0,
  291. 'msg' => 'success'
  292. ]);
  293. }
  294. /**
  295. * 浏览记录列表
  296. */
  297. public function actionBrowseList()
  298. {
  299. $choose_day = get_params('day');
  300. if (!empty($choose_day)) {
  301. $day = strtotime($choose_day);
  302. } else {
  303. $day = strtotime(date('Y-m-d', time()));
  304. }
  305. $browse_list = BrowseLog::find()->where([
  306. 'type' => 0,
  307. //'store_id' => get_store_id(),
  308. 'user_id' => 0,
  309. 'saas_id' => get_saas_user_id(),
  310. 'is_delete' => 0
  311. ])->andWhere(['>', 'created_at', $day])->andWhere(['<=', 'created_at', $day + 3600 * 24])->all();
  312. $re_arr = [];
  313. foreach ($browse_list as $k => $v) {
  314. $goods_info = Goods::find()->alias('g')
  315. //->leftJoin(["s"=>Store::tableName()],"g.store_id=s.id")
  316. ->select('g.id,g.name,g.cover_pic,g.price,g.store_id')->where([
  317. 'g.id' => $v['log_id'],
  318. //'store_id' => get_store_id()
  319. ])->asArray()->one();
  320. if($goods_info['store_id'] > 0){
  321. $storeInfo = Store::find()->alias('s')->leftJoin(['w'=>WechatConfig::tableName()],'s.id = w.store_id')->where(['s.id'=>$goods_info['store_id'],'s.is_delete'=>0,'w.type'=>1,'w.is_delete'=>0])->select('w.app_id wechat_app_id,s.id,s.name,s.logo,s.address,s.category_id,s.sales,s.rank,s.per_spend,s.coordinate,s.business_model')->asArray()->one();
  322. if (!$storeInfo) {
  323. $goods_info['storeInfo'] = [];
  324. }else{
  325. $goods_info['storeInfo'] = $storeInfo;
  326. }
  327. }
  328. $goods_info['log_id'] = $v['id'];
  329. $re_arr[] = $goods_info;
  330. }
  331. $browse_list_shop = BrowseLog::find()->where([
  332. 'type' => 1,
  333. 'store_id' => get_store_id(),
  334. 'user_id' => 0,
  335. 'saas_id' => get_saas_user_id(),
  336. 'is_delete' => 0
  337. ])->andWhere(['>', 'created_at', $day])->andWhere(['<=', 'created_at', $day + 3600 * 24])->all();
  338. $re_shop_arr = [];
  339. // 门店浏览记录
  340. foreach ($browse_list_shop as $k => $v) {
  341. $shops_info = Shop::find()->select(['id', 'name', 'cover_url as logo', 'mobile as tel', 'score'])->where([
  342. 'id' => $v['log_id'],
  343. 'store_id' => get_store_id()
  344. ])->asArray()->one();
  345. $shops_info['log_id'] = $v['id'];
  346. $re_shop_arr[] = $shops_info;
  347. }
  348. $data = [
  349. 're_arr' => $re_arr,
  350. 're_shop_arr' => $re_shop_arr
  351. ];
  352. return $this->asJson([
  353. 'code' => 0,
  354. 'msg' => 'success',
  355. 'data' => $data
  356. ]);
  357. }
  358. /**
  359. * 删除商品浏览记录
  360. */
  361. public function actionDelGoodsLog()
  362. {
  363. $del_id = get_params('id');
  364. $del_list = get_params('list');
  365. if (!empty($del_id)) {
  366. $log_info = BrowseLog::find()->where([
  367. 'id' => $del_id,
  368. 'type' => 0,
  369. //'store_id' => get_store_id(),
  370. 'user_id' => 0,
  371. 'saas_id' => get_saas_user_id(),
  372. 'is_delete' => 0
  373. ])->one();
  374. $log_info->is_delete = 1;
  375. if ($log_info->save()) {
  376. return $this->asJson(['code' => 0, 'msg' => '删除成功']);
  377. } else {
  378. return $this->asJson(['code' => 1, 'msg' => '删除失败']);
  379. }
  380. } else if(!empty($del_list)) {
  381. $del_list_arr = explode(',', $del_list);
  382. foreach ($del_list_arr as $v) {
  383. $log_info = BrowseLog::find()->where([
  384. 'id' => $v,
  385. 'type' => 0,
  386. //'store_id' => get_store_id(),
  387. 'user_id' => 0,
  388. 'saas_id' => get_saas_user_id(),
  389. 'is_delete' => 0
  390. ])->one();
  391. $log_info->is_delete = 1;
  392. $log_info->save();
  393. }
  394. return $this->asJson(['code' => 0, 'msg' => '删除成功']);
  395. }
  396. }
  397. /**
  398. * 文章详情
  399. * @return \yii\web\Response
  400. */
  401. public function actionArticleDetail()
  402. {
  403. $id = get_params('id');
  404. if ($id == 'about_us') {
  405. $model = AboutArticle::findOne(['type' => AboutArticle::TYPE_ABOUT_US, 'is_delete' => 0]);
  406. if (!$model) {
  407. $model = new AboutArticle();
  408. $model->name = '关于我们';
  409. $model->store_id = -1;
  410. $model->type = AboutArticle::TYPE_ABOUT_US;
  411. $model->desc = '关于我们';
  412. $model->save();
  413. }
  414. $data = [
  415. 'id' => $model->id,
  416. 'title' => $model->name,
  417. 'content' => $model->desc,
  418. ];
  419. return $this->asJson([
  420. 'code' => 0,
  421. 'msg' => 'success',
  422. 'data' => $data
  423. ]);
  424. } else {
  425. $model = AboutArticle::find()->where([
  426. 'is_delete' => 0,
  427. 'id' => $id,
  428. ])->select('id,name as title,desc as content')->asArray()->one();
  429. if (empty($model)) {
  430. return $this->asJson([
  431. 'code' => 1,
  432. 'msg' => '内容不存在'
  433. ]);
  434. }
  435. return $this->asJson([
  436. 'code' => 0,
  437. 'msg' => 'success',
  438. 'data' => $model
  439. ]);
  440. }
  441. }
  442. public function actionGoodsQrcode()
  443. {
  444. $form = new ShareQrcodeForm();
  445. $form->attributes = \Yii::$app->request->get();
  446. $form->store_id = get_store_id();
  447. $form->type = 0;
  448. $form->user = get_user();
  449. $form->user_id = 0;
  450. $form->saas_id = get_saas_user_id();
  451. return $this->asJson($form->search());
  452. }
  453. /**
  454. * 商品评价列表
  455. * @return \yii\web\Response
  456. */
  457. public function actionCommentList()
  458. {
  459. $form = new CommentListForm();
  460. $form->attributes = get_params();
  461. return $this->asJson($form->search());
  462. }
  463. /**
  464. * 默认商品列表
  465. */
  466. public function actionGoodsList()
  467. {
  468. $form = new GoodsListForm();
  469. $form->attributes = get_params();
  470. $form->store_id = get_store_id();
  471. return $this->asJson($form->search());
  472. }
  473. /**
  474. * 获取品牌列表
  475. */
  476. public function actionGetBrandList()
  477. {
  478. $list = GoodsBrand::getList(get_store_id());
  479. return $this->asJson([
  480. 'code' => 0,
  481. 'data' => [
  482. 'list' => $list
  483. ]
  484. ]);
  485. }
  486. /**
  487. * 专题列表
  488. * @return \yii\web\Response
  489. */
  490. public function actionTopicList()
  491. {
  492. $form = new TopicListForm();
  493. $form->attributes = get_params();
  494. $form->store_id = get_store_id();
  495. return $this->asJson($form->search());
  496. }
  497. /**
  498. * 专题详情
  499. * @return \yii\web\Response
  500. */
  501. public function actionTopic()
  502. {
  503. $form = new TopicForm();
  504. $form->attributes = get_params();
  505. $form->store_id = get_store_id();
  506. if (get_user_id() > 0) {
  507. $form->user_id = get_user_id();
  508. }
  509. return $this->asJson($form->search());
  510. }
  511. /**
  512. * 专题分类
  513. * @return \yii\web\Response
  514. */
  515. public function actionTopicType()
  516. {
  517. $form = new TopicTypeForm();
  518. $form->store_id = get_store_id();
  519. return $this->asJson($form->search());
  520. }
  521. /**
  522. * 专题海报
  523. * @return \yii\web\Response
  524. */
  525. public function actionTopicQrcode()
  526. {
  527. $form = new ShareQrcodeForm();
  528. $form->attributes = get_params();
  529. $form->store_id = get_store_id();
  530. $form->type = 6;
  531. if (get_user_id() > 0) {
  532. $form->user = get_user();
  533. $form->user_id = get_user_id();
  534. }
  535. return $this->asJson($form->search());
  536. }
  537. /**
  538. * 分类列表
  539. */
  540. public function actionCatList()
  541. {
  542. $form = new CatListForm();
  543. $form->attributes = get_params();
  544. $form->store_id = get_store_id();
  545. return $this->asJson($form->search());
  546. }
  547. /**
  548. * 地址识别
  549. */
  550. public function actionAddressRecognition() {
  551. try {
  552. $store_id = get_store_id();
  553. $content = post_params('content');
  554. if (empty(trim($content))) {
  555. throw new \Exception('缺少必填参数');
  556. }
  557. $kuaiDi100UseChange = Option::get(OptionSetting::KUAIDI100_USE_CHANGE, $store_id, 'kuaidi100')['value'];
  558. if($kuaiDi100UseChange == 1){
  559. //获取配置信息
  560. $key = Option::get(OptionSetting::KUAIDI100_SECRET_KEY_NEW, $store_id, 'kuaidi100')['value'];
  561. $secret = Option::get(OptionSetting::KUAIDI100_SECRET_SECRET_NEW, $store_id, 'kuaidi100')['value'];
  562. $kuaiDi100Api = new KuaiDi100(['key' => $key, 'secret' => $secret]);
  563. $result = $kuaiDi100Api->addressResolution($content);
  564. if($result['code'] != 200){
  565. throw new \Exception($result['message']);
  566. }
  567. $address = [
  568. 'name' => $result['data']['result'][0]['name'],
  569. 'mobile' => $result['data']['result'][0]['mobile'][0]
  570. ];
  571. $address_area = $result['data']['result'][0]['xzq']['fullName'] . '' . $result['data']['result'][0]['xzq']['subArea'];
  572. }else{
  573. $secret_key = Option::get(OptionSetting::KUAIDI100_SECRET_KEY, 0, 'saas')['value'];
  574. $secret_secret = Option::get(OptionSetting::KUAIDI100_SECRET_SECRET, 0, 'saas')['value'];
  575. $secret_code = Option::get(OptionSetting::KUAIDI100_SECRET_CODE, 0, 'saas')['value'];
  576. $secret_sign = Option::get(OptionSetting::KUAIDI100_SECRET_SIGN, 0, 'saas')['value'];
  577. $secret_sign_ = strtoupper(md5($secret_key . $secret_secret));
  578. if (!empty($secret_sign)) {
  579. //验证信息 (感觉没必要)
  580. if ($secret_sign !== $secret_sign_) {
  581. throw new \Exception('验证签名失败');
  582. }
  583. }
  584. $params = 'secret_key=' . $secret_key . '&';
  585. $params .= 'secret_code=' . $secret_code .'&';
  586. $params .= 'secret_sign=' . $secret_sign_ .'&';
  587. $params .= 'content=' . $content;
  588. $ch = curl_init();
  589. curl_setopt($ch,CURLOPT_POST,1);
  590. curl_setopt($ch,CURLOPT_HEADER,0);
  591. curl_setopt($ch,CURLOPT_URL,'http://cloud.kuaidi100.com/api');
  592. curl_setopt($ch,CURLOPT_POSTFIELDS,$params);
  593. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  594. $result = curl_exec($ch);
  595. $result = json_decode($result, true);
  596. if ($result['code'] !== 200) {
  597. throw new \Exception($result['message']);
  598. }
  599. // $result = [
  600. // 'code' => 200,
  601. // "message" => "success",
  602. // "data"=> [
  603. // [
  604. // 'address' => "建业智慧港1308",
  605. // 'mobile' => ['15538531881'],
  606. // 'name' => '张三',
  607. // 'content' => "张三,15538531881,河南省洛阳市洛龙区建业智慧港1308",
  608. // 'xzq' => [
  609. // 'code' => 410311,
  610. // 'province' => "河南",
  611. // 'city' => '洛阳市',
  612. // 'level' => 3,
  613. // 'district' => '洛龙区',
  614. // 'pCode' => '410300',
  615. // 'fullName' => '河南,洛阳市,洛龙区',
  616. // 'subArea' => '建业智慧港1308'
  617. // ]
  618. // ]
  619. // ]
  620. // ];
  621. $address = [
  622. 'name' => $result['data'][0]['name'],
  623. 'mobile' => $result['data'][0]['mobile'][0]
  624. ];
  625. $address_area = $result['data'][0]['xzq']['fullName'] . '' . $result['data'][0]['xzq']['subArea'];
  626. }
  627. //通过地址信息获取经纬度
  628. $tencent_map_key = Option::get('tencent_map_key', 0, 'saas', '')['value'];
  629. $url = 'https://apis.map.qq.com/ws/geocoder/v1/?';
  630. $params = [
  631. 'key' => $tencent_map_key,
  632. 'address' => $address_area,
  633. ];
  634. $url .= http_build_query($params);
  635. $json = file_get_contents($url);
  636. $tencent_result = json_decode($json, true);
  637. if ($tencent_result['status'] !== 0) {
  638. throw new \Exception($tencent_result['message']);
  639. }
  640. $address['latitude'] = $tencent_result['result']['location']['lat'];
  641. $address['longitude'] = $tencent_result['result']['location']['lng'];
  642. $address_area = $tencent_result['result']['address_components']['province'] .
  643. $tencent_result['result']['address_components']['city'] .
  644. $tencent_result['result']['address_components']['district'] .
  645. $tencent_result['result']['title'];
  646. $smart = $this->addressSmart(['address' => $address_area]);
  647. $address['addr'] = $result['data']['result'][0]['xzq']['subArea'];
  648. $address = array_merge($address, $smart);
  649. return $this->asJson([
  650. 'code' => 0,
  651. 'msg' => '识别成功',
  652. 'data' => $address
  653. ]);
  654. } catch (\Exception $e) {
  655. return $this->asJson([
  656. 'code' => 1,
  657. 'msg' => $e->getMessage()
  658. ]);
  659. }
  660. }
  661. }