SettingService.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace addons\RandFee\common\service;
  3. use common\enums\AddonsEnum;
  4. use Yii;
  5. use yii\base\Model;
  6. use yii\helpers\Json;
  7. class SettingService extends Model
  8. {
  9. /**
  10. * 商户ID
  11. *
  12. * @var integer
  13. */
  14. public $mall_id;
  15. /**
  16. * 保存设置
  17. *
  18. * @param array $data
  19. * @return boolean
  20. */
  21. public function saveSetting($data)
  22. {
  23. unset($data['_csrf']);
  24. $data['condition'] = self::priceSort($data['condition']);
  25. empty($data['order']) && $data['order'] = [];
  26. $conf['basic'] = $data;
  27. return Yii::$app->services->setting->addons->set($this->mall_id, AddonsEnum::ADDONS_NAME_RAND_FEE, $conf);
  28. }
  29. /**
  30. * 获取设置
  31. *
  32. * @return array
  33. */
  34. public function getSetting($key = null)
  35. {
  36. $conf = Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_RAND_FEE, 'basic');
  37. if (!empty($conf)) {
  38. foreach ($conf as $k => $v) {
  39. $conf[$k] = Json::decode($v);
  40. }
  41. }
  42. $conf && empty($conf['order']) && $conf['order'] = [];
  43. return $key && isset($conf[$key]) ? $conf[$key] : $conf;
  44. }
  45. /**
  46. * 金额排序
  47. *
  48. * @param array $condition
  49. * @return array
  50. */
  51. protected static function priceSort($condition)
  52. {
  53. if (empty($condition)) {
  54. return [];
  55. }
  56. foreach ($condition as $val) {
  57. $key_arrays[] = $val['total_price'];
  58. }
  59. array_multisort($key_arrays, SORT_ASC, SORT_NUMERIC, $condition);
  60. return $condition;
  61. }
  62. }