GoodsSettingService.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace addons\WechatVideoShop\common\service;
  3. use addons\WechatVideoShop\common\models\WechatVideoShopGoodsSetting;
  4. use common\traits\ErrorTrait;
  5. use yii\helpers\Json;
  6. class GoodsSettingService
  7. {
  8. use ErrorTrait;
  9. /**
  10. * 商品模式全局调用
  11. * @param array $params
  12. * @return mixed|null
  13. */
  14. public static function globalGoodsModel(array $params)
  15. {
  16. $data = WechatVideoShopGoodsSetting::getOne($params['where'], true);
  17. if (!empty($data['config'])) {
  18. $config = Json::decode($data['config']);
  19. ksort($config);
  20. $data['config'] = Json::encode($config);
  21. }
  22. return $data;
  23. }
  24. /**
  25. * 商品模式更新数据
  26. * @param array $params
  27. * @param array $data
  28. * @return bool
  29. */
  30. public static function setData(array $params, array $data)
  31. {
  32. try {
  33. $model = WechatVideoShopGoodsSetting::getOne([
  34. ['mall_id' => $params['mall_id']],
  35. ['item_id' => $params['item_id']]
  36. ]);
  37. if (empty($model)) {
  38. $model = new WechatVideoShopGoodsSetting();
  39. $model->mall_id = $params['mall_id'];
  40. $model->item_id = $params['item_id'];
  41. }
  42. $config = $data['config'];
  43. ksort($config);
  44. $model->is_enable = $data['is_enable'];
  45. $model->config = $config;
  46. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  47. return true;
  48. } catch (\Exception $e) {
  49. self::$static_error = $e->getMessage();
  50. return false;
  51. }
  52. }
  53. /**
  54. * @return string
  55. */
  56. public static function getErrorMessage()
  57. {
  58. return self::$static_error;
  59. }
  60. }