| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\constants\OptionSetting;
- use app\models\Option;
- use app\models\Store;
- use yii\base\Model;
- class RechargeSettingForm extends Model
- {
- public $recharge_wallet_status;
- public $recharge_custom_status;
- public $balance_to_commission;
- public $combine_pay;
- public $recharge_pic_url;
- public $recharge_ad_pic_url;
- public $recharge_page_url;
- public $recharge_p_pic_url;
- public $recharge_help;
- public $commission_to_balance;
- public $give_integral_profit;
- public $give_balance_profit;
- public $store_id;
- public $lg_cash;
- public $balance_to_cash;
- public $balance_to_cash_setting;
- //搜索
- const OPTION_KEY = 'recharge';
- const SCENARIO_ADD = 'add';
- const SCENARIO_EDIT = 'edit';
- const SCENARIO_DEL = 'del';
- const SCENARIO_LIST = 'list';
- const OPTION_SETTING = [
- 'recharge' => [
- 'name' => '充值设置',
- 'list' => [
- [
- 'name' => OptionSetting::RECHARGE_WALLET_STATUS,
- 'text' => '开启余额',
- 'type' => 'radio',
- 'required' => true,
- 'default' => '0',
- 'unit' => '',
- 'muted' => '开启余额功能'
- ],
- [
- 'name' => OptionSetting::RECHARGE_CUSTOM_STATUS,
- 'text' => '自定义金额',
- 'type' => 'radio',
- 'required' => true,
- 'default' => '0',
- 'unit' => '',
- 'muted' => '是否开放自定义金额'
- ],
- [
- 'name' => OptionSetting::STORE_COMBINE_PAY,
- 'text' => '是否开启余额抵扣',
- 'type' => 'radio',
- 'required' => true,
- 'default' => '1',
- 'unit' => '',
- 'muted' => '是否开启余额抵扣'
- ],
- [
- 'name' => OptionSetting::BALANCE_TO_COMMISSION,
- 'text' => '是否开启余额转佣金',
- 'type' => 'radio',
- 'required' => true,
- 'default' => '0',
- 'unit' => '',
- 'muted' => '是否开启余额转佣金'
- ],
- [
- 'name' => 'give_integral_profit',
- 'text' => '积分转赠手续费比例',
- 'type' => 'number',
- 'required' => true,
- 'default' => '0.00',
- 'unit' => '%',
- 'muted' => '佣金转余额比例'
- ],
- [
- 'name' => 'give_balance_profit',
- 'text' => '余额转赠手续费比例',
- 'type' => 'number',
- 'required' => true,
- 'default' => '0.00',
- 'unit' => '%',
- 'muted' => '佣金转余额比例'
- ],
- [
- 'name' => OptionSetting::COMMISSION_TO_BALANCE,
- 'text' => '佣金转余额比例',
- 'type' => 'number',
- 'required' => true,
- 'default' => '0.00',
- 'unit' => '%',
- 'muted' => '佣金转余额比例'
- ],
- [
- 'name' => OptionSetting::BALANCE_TO_CASH,
- 'text' => '是否开启余额提现',
- 'type' => 'radio',
- 'required' => true,
- 'default' => '0',
- 'unit' => '',
- 'muted' => '是否开启余额提现'
- ],
- [
- 'name' => OptionSetting::BALANCE_TO_CASH_SETTING,
- 'text' => '余额提现设置',
- 'type' => 'text',
- 'required' => true,
- 'default' => '',
- 'unit' => '',
- 'muted' => '是否开启余额提现'
- ],
- [
- 'name' => 'cash_service_charge',
- 'text' => '提现手续费',
- 'type' => 'text',
- 'required' => true,
- 'default' => '0.00',
- 'unit' => '',
- 'muted' => ''
- ],
- // [
- // 'name' => OptionSetting::RECHARGE_PIC_URL,
- // 'text' => '背景图片',
- // 'type' => 'image',
- // 'size' => '200*50',
- // 'required' => false,
- // 'default' => '',
- // 'unit' => ''
- // ],
- // [
- // 'name' => OptionSetting::RECHARGE_AD_PIC_URL,
- // 'text' => '广告图片',
- // 'type' => 'image',
- // 'size' => '200*50',
- // 'required' => false,
- // 'default' => '',
- // 'unit' => ''
- // ],
- // [
- // 'name' => OptionSetting::RECHARGE_PAGE_URL,
- // 'text' => '跳转链接',
- // 'type' => 'text',
- // 'required' => true,
- // 'default' => 0,
- // 'unit' => '',
- // 'muted' => '广告图片跳转链接'
- // ],
- // [
- // 'name' => OptionSetting::RECHARGE_P_PIC_URL,
- // 'text' => '说明图标',
- // 'type' => 'image',
- // 'size' => '200*50',
- // 'required' => false,
- // 'default' => '',
- // 'unit' => '充值说明图标'
- // ],
- // [
- // 'name' => OptionSetting::RECHARGE_HELP,
- // 'text' => '充值说明',
- // 'type' => 'text',
- // 'required' => true,
- // 'default' => '',
- // 'unit' => '',
- // 'muted' => ''
- // ],
- ]
- ]
- ];
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- // [['recharge_pic_url', 'recharge_ad_pic_url', 'recharge_page_url', 'recharge_p_pic_url', 'recharge_help'], 'string', 'max' => 255],
- [['recharge_wallet_status', 'recharge_custom_status', 'balance_to_commission', 'combine_pay'], 'in', 'range' => [0, 1]],
- // [['recharge_wallet_status', 'recharge_custom_status', 'balance_to_commission', 'combine_pay', 'commission_to_balance', 'give_integral_profit', 'give_balance_profit'],'required', 'on'=> [self::SCENARIO_ADD, self::SCENARIO_EDIT]],
- [['commission_to_balance', 'give_integral_profit', 'give_balance_profit','lg_cash'], 'number'],
- [['balance_to_cash', 'balance_to_cash_setting'], 'safe'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'recharge_wallet_status' => '开启余额功能',
- 'recharge_custom_status' => '是否开放自定义金额',
- 'balance_to_commission' => '是否开启余额转佣金',
- 'combine_pay' => '是否开启余额抵扣',
- 'recharge_pic_url' => '背景图片',
- 'recharge_ad_pic_url' => '广告图片',
- // 'recharge_page_url' => '广告图片跳转链接',
- 'recharge_p_pic_url' => '充值说明图标',
- 'recharge_help' => '充值说明',
- "commission_to_balance" => '佣金转余额比例',
- "give_integral_profit" => '赠送积分比例',
- "give_balance_profit" => '赠送余额比例',
- ];
- }
- public function scenarios()
- {
- $scenarios = parent::scenarios();
- return $scenarios;
- }
- public function saveRechargeSetting()
- {
- if ($this->validate()) {
- $t = \Yii::$app->db->beginTransaction();
- $store = Store::findOne($this->store_id);
- $store->lg_cash = $this->lg_cash;
- $store->save();
- $data = self::getGroup(self::OPTION_KEY, get_store_id());
- foreach($data as &$val){
- if(isset($this->attributes[$val['name']])){
- $val['value'] = $this->attributes[$val['name']];
- }
- }
- $result = self::setGroup(self::OPTION_KEY, $data, get_store_id());
- if (!$result) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '保存失败'
- ];
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- } else {
- // 验证失败:$errors 是一个包含错误信息的数组
- return [
- 'code' => 1,
- "msg" => $this->getErrorSummary(false)[0]
- ];
- }
- }
- public static function getRechargeSettingList()
- {
- $result = self::getGroup(self::OPTION_KEY, get_store_id());
- $data = [];
- foreach($result as $val){
- $data[$val['name']] = (string)$val['value'];
- }
- $store = Store::findOne(get_store_id());
- $data['balance'] = $store->balance == 1 ? 1 : 0;
- $data['lg_cash'] = $store->lg_cash == 1 ? 1 : 0;
- return $data;
- }
- /**
- * 根据类获取数据
- * @param $group
- * @param $store_id
- * @return array
- */
- public static function getGroup ($group, $store_id = 1)
- {
- $group_setting = self::OPTION_SETTING;
- $group_arr = $group_setting[$group]['list'] ?: [];
- $data = [];
- foreach ($group_arr as $val) {
- $group_data = Option::findOne(['store_id' => $store_id, 'group' => $group, 'name' => $val['name']]);
- if ($val['type'] == 'checkbox') {
- if ($group_data) {
- $attr = $val['default'];
- $data_val = json_decode($group_data->value,true);
- foreach ($attr as $k => &$v) {
- $v = $data_val[$k] ? $data_val[$k] : $v;
- }
- }
- }
- $data[] = [
- 'name' => $group_data ? $group_data->name : $val['name'],
- 'text' => $val['text'],
- 'group' => $group_data ? $group_data->group : $group,
- 'type' => $val['type'],
- 'size' => isset($val['size']) ? $val['size'] : '',
- 'title' => isset($val['title']) ? $val['title'] : '',
- 'required' => $val['required'],
- 'muted' => isset($val['muted']) ? $val['muted'] : '',
- 'value' => $group_data ? ($val['type'] == 'checkbox' ? $attr : $group_data->value ) : $val['default'],
- 'updated_at' => $group_data ? $group_data->updated_at : 0,
- 'unit' => isset($val['unit']) ? $val['unit'] : '',
- ];
- }
- return $data;
- }
- /**
- * 根据类型批量设置数据
- * @param $group
- * @param $set_data
- * @param int $store_id
- * @return bool
- */
- public static function setGroup ($group, $set_data, $store_id = 1)
- {
- foreach ($set_data as $val) {
- if ($val['name']) {
- $group_data = Option::findOne(['store_id' => $store_id, 'group' => $group, 'name' => $val['name']]);
- if (!$group_data) {
- $group_data = new Option();
- $group_data->store_id = $store_id;
- $group_data->group = $group;
- }
- $group_data->name = $val['name'];
- if ($val['type'] == 'checkbox') {
- $group_data->value = json_encode($val['value']);
- }else {
- $group_data->value = "{$val['value']}";
- }
- if (!$group_data->save()) {
- return false;
- }
- }
- }
- return true;
- }
- }
|