ConfigController.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Http\Controllers\AdminApi;
  3. use App\Models\SysModels\Config;
  4. use App\Http\Controllers\AdminController;
  5. use App\Servers\Common\RedisDataServer;
  6. class ConfigController extends AdminController
  7. {
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. }
  12. /**
  13. * 获取&更新基础配置信息
  14. * @return \Illuminate\Http\JsonResponse
  15. */
  16. function setConfig(){
  17. if(request()->isMethod('post')){
  18. $data = request()->all();
  19. //更新config数据
  20. $this->update($data);
  21. //清除redis数据
  22. RedisDataServer::creatServer()->delData('gw_configs');
  23. return $this->apiResponseSuccess('更新成功');
  24. }else{
  25. $configs = Config::whereIn('key',['logo','cn_title','en_title','tel','email','cn_address','en_address','wx_image','icp','put_on_record','lon','lat'])->pluck('value','key');
  26. return $this->apiResponseSuccess('获取成功',$configs);
  27. }
  28. }
  29. /**
  30. * 获取&更新留言短信通知手机号
  31. * @return \Illuminate\Http\JsonResponse
  32. */
  33. function setSmsPhone(){
  34. if(request()->isMethod('post')){
  35. $sms_phone = request()->input(['sms_phone']);
  36. if(!is_array($sms_phone)) return $this->apiResponseError('数据格式错误');
  37. $data['sms_phone'] = implode(',',$sms_phone);
  38. //更新config数据
  39. $this->update($data);
  40. return $this->apiResponseSuccess('更新成功');
  41. }else{
  42. $sms_phone = Config::where('key','sms_phone')->value('value');
  43. $data['sms_phone'] = array_filter(explode(',',$sms_phone));
  44. return $this->apiResponseSuccess('获取成功',$data);
  45. }
  46. }
  47. /**
  48. * 更新config数据
  49. * @param $data
  50. * @return bool
  51. */
  52. function update($data){
  53. foreach ($data as $key => $value ){
  54. // 查找当前数据是否存在
  55. if( Config::where('key', $key)->where('value', $value)->count() ){ continue; }
  56. Config::where('key', $key)->update([ 'value' => $value ]);
  57. }
  58. return true;
  59. }
  60. }