ScanCodePaySettingForm.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\scanCodePay\models\form;
  8. use app\plugins\scanCodePay\models\ScanCodePaySetting;
  9. use yii\base\Model;
  10. class ScanCodePaySettingForm extends Model
  11. {
  12. public $store_id;
  13. public $is_scan_code_pay;
  14. public $payment_type;
  15. public $is_share;
  16. public $is_sms;
  17. public $is_mail;
  18. public $is_print;
  19. public $poster;
  20. public $price_type;
  21. public $first;
  22. public $second;
  23. public $third;
  24. public $mch;
  25. public function rules()
  26. {
  27. return [
  28. [['store_id', 'is_scan_code_pay', 'payment_type', 'is_share', 'is_sms', 'is_mail', 'is_print'], 'required', 'on' => ['save']],
  29. [['poster', 'payment_type', 'price_type', 'first', 'second', 'third', 'mch'], 'safe'],
  30. ];
  31. }
  32. public function scenarios()
  33. {
  34. return [
  35. 'search' => ['store_id','mch'],
  36. 'save' => ['store_id', 'is_scan_code_pay', 'is_share', 'is_sms', 'is_mail', 'is_print', 'payment_type', 'price_type', 'first', 'second', 'third', 'mch'],
  37. ];
  38. }
  39. public function save()
  40. {
  41. if (!$this->validate()) {
  42. return [
  43. 'code' => 1,
  44. 'msg' => $this->getErrorSummary(false)[0],
  45. ];
  46. }
  47. try {
  48. $model = ScanCodePaySetting::findOne(['store_id' => $this->store_id, 'mch' => $this->mch, 'is_delete' => 0]);
  49. if (!$model) {
  50. $model = new ScanCodePaySetting();
  51. $model->store_id = $this->store_id;
  52. }
  53. $model->is_scan_code_pay = $this->is_scan_code_pay;
  54. $model->payment_type = json_encode($this->payment_type ? $this->payment_type : []);
  55. $model->is_share = $this->is_share;
  56. $model->is_sms = $this->is_sms;
  57. $model->is_mail = $this->is_mail;
  58. $model->is_print = $this->is_print;
  59. $model->poster = json_encode($this->poster);
  60. $model->price_type = $this->price_type;
  61. $model->first = $this->first;
  62. $model->second = $this->second;
  63. $model->third = $this->third;
  64. $model->mch = $this->mch;
  65. $res = $model->save();
  66. if (!$res) {
  67. throw new \Exception((new Model())->getErrorResponse($model)->msg);
  68. }
  69. return [
  70. 'code' => 0,
  71. 'msg' => "保存成功"
  72. ];
  73. } catch (\Exception $e) {
  74. return [
  75. 'code' => 1,
  76. 'msg' => $e->getMessage()
  77. ];
  78. }
  79. }
  80. public function search()
  81. {
  82. $setting = ScanCodePaySetting::find()->where(['store_id' => $this->store_id, 'mch' => $this->mch, 'is_delete' => 0])->asArray()->one();
  83. $setting['payment_type'] = json_decode($setting['payment_type'], true);
  84. return $setting;
  85. }
  86. }