ConfigController.php 2.0 KB

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