RechargeSettingForm.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\constants\OptionSetting;
  9. use app\models\Option;
  10. use app\models\Store;
  11. use yii\base\Model;
  12. class RechargeSettingForm extends Model
  13. {
  14. public $recharge_wallet_status;
  15. public $recharge_custom_status;
  16. public $balance_to_commission;
  17. public $combine_pay;
  18. public $recharge_pic_url;
  19. public $recharge_ad_pic_url;
  20. public $recharge_page_url;
  21. public $recharge_p_pic_url;
  22. public $recharge_help;
  23. public $commission_to_balance;
  24. public $give_integral_profit;
  25. public $give_balance_profit;
  26. public $store_id;
  27. public $lg_cash;
  28. public $balance_to_cash;
  29. public $balance_to_cash_setting;
  30. //搜索
  31. const OPTION_KEY = 'recharge';
  32. const SCENARIO_ADD = 'add';
  33. const SCENARIO_EDIT = 'edit';
  34. const SCENARIO_DEL = 'del';
  35. const SCENARIO_LIST = 'list';
  36. const OPTION_SETTING = [
  37. 'recharge' => [
  38. 'name' => '充值设置',
  39. 'list' => [
  40. [
  41. 'name' => OptionSetting::RECHARGE_WALLET_STATUS,
  42. 'text' => '开启余额',
  43. 'type' => 'radio',
  44. 'required' => true,
  45. 'default' => '0',
  46. 'unit' => '',
  47. 'muted' => '开启余额功能'
  48. ],
  49. [
  50. 'name' => OptionSetting::RECHARGE_CUSTOM_STATUS,
  51. 'text' => '自定义金额',
  52. 'type' => 'radio',
  53. 'required' => true,
  54. 'default' => '0',
  55. 'unit' => '',
  56. 'muted' => '是否开放自定义金额'
  57. ],
  58. [
  59. 'name' => OptionSetting::STORE_COMBINE_PAY,
  60. 'text' => '是否开启余额抵扣',
  61. 'type' => 'radio',
  62. 'required' => true,
  63. 'default' => '1',
  64. 'unit' => '',
  65. 'muted' => '是否开启余额抵扣'
  66. ],
  67. [
  68. 'name' => OptionSetting::BALANCE_TO_COMMISSION,
  69. 'text' => '是否开启余额转佣金',
  70. 'type' => 'radio',
  71. 'required' => true,
  72. 'default' => '0',
  73. 'unit' => '',
  74. 'muted' => '是否开启余额转佣金'
  75. ],
  76. [
  77. 'name' => 'give_integral_profit',
  78. 'text' => '积分转赠手续费比例',
  79. 'type' => 'number',
  80. 'required' => true,
  81. 'default' => '0.00',
  82. 'unit' => '%',
  83. 'muted' => '佣金转余额比例'
  84. ],
  85. [
  86. 'name' => 'give_balance_profit',
  87. 'text' => '余额转赠手续费比例',
  88. 'type' => 'number',
  89. 'required' => true,
  90. 'default' => '0.00',
  91. 'unit' => '%',
  92. 'muted' => '佣金转余额比例'
  93. ],
  94. [
  95. 'name' => OptionSetting::COMMISSION_TO_BALANCE,
  96. 'text' => '佣金转余额比例',
  97. 'type' => 'number',
  98. 'required' => true,
  99. 'default' => '0.00',
  100. 'unit' => '%',
  101. 'muted' => '佣金转余额比例'
  102. ],
  103. [
  104. 'name' => OptionSetting::BALANCE_TO_CASH,
  105. 'text' => '是否开启余额提现',
  106. 'type' => 'radio',
  107. 'required' => true,
  108. 'default' => '0',
  109. 'unit' => '',
  110. 'muted' => '是否开启余额提现'
  111. ],
  112. [
  113. 'name' => OptionSetting::BALANCE_TO_CASH_SETTING,
  114. 'text' => '余额提现设置',
  115. 'type' => 'text',
  116. 'required' => true,
  117. 'default' => '',
  118. 'unit' => '',
  119. 'muted' => '是否开启余额提现'
  120. ],
  121. [
  122. 'name' => 'cash_service_charge',
  123. 'text' => '提现手续费',
  124. 'type' => 'text',
  125. 'required' => true,
  126. 'default' => '0.00',
  127. 'unit' => '',
  128. 'muted' => ''
  129. ],
  130. // [
  131. // 'name' => OptionSetting::RECHARGE_PIC_URL,
  132. // 'text' => '背景图片',
  133. // 'type' => 'image',
  134. // 'size' => '200*50',
  135. // 'required' => false,
  136. // 'default' => '',
  137. // 'unit' => ''
  138. // ],
  139. // [
  140. // 'name' => OptionSetting::RECHARGE_AD_PIC_URL,
  141. // 'text' => '广告图片',
  142. // 'type' => 'image',
  143. // 'size' => '200*50',
  144. // 'required' => false,
  145. // 'default' => '',
  146. // 'unit' => ''
  147. // ],
  148. // [
  149. // 'name' => OptionSetting::RECHARGE_PAGE_URL,
  150. // 'text' => '跳转链接',
  151. // 'type' => 'text',
  152. // 'required' => true,
  153. // 'default' => 0,
  154. // 'unit' => '',
  155. // 'muted' => '广告图片跳转链接'
  156. // ],
  157. // [
  158. // 'name' => OptionSetting::RECHARGE_P_PIC_URL,
  159. // 'text' => '说明图标',
  160. // 'type' => 'image',
  161. // 'size' => '200*50',
  162. // 'required' => false,
  163. // 'default' => '',
  164. // 'unit' => '充值说明图标'
  165. // ],
  166. // [
  167. // 'name' => OptionSetting::RECHARGE_HELP,
  168. // 'text' => '充值说明',
  169. // 'type' => 'text',
  170. // 'required' => true,
  171. // 'default' => '',
  172. // 'unit' => '',
  173. // 'muted' => ''
  174. // ],
  175. ]
  176. ]
  177. ];
  178. /**
  179. * {@inheritdoc}
  180. */
  181. public function rules()
  182. {
  183. return [
  184. // [['recharge_pic_url', 'recharge_ad_pic_url', 'recharge_page_url', 'recharge_p_pic_url', 'recharge_help'], 'string', 'max' => 255],
  185. [['recharge_wallet_status', 'recharge_custom_status', 'balance_to_commission', 'combine_pay'], 'in', 'range' => [0, 1]],
  186. // [['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]],
  187. [['commission_to_balance', 'give_integral_profit', 'give_balance_profit','lg_cash'], 'number'],
  188. [['balance_to_cash', 'balance_to_cash_setting'], 'safe'],
  189. ];
  190. }
  191. /**
  192. * {@inheritdoc}
  193. */
  194. public function attributeLabels()
  195. {
  196. return [
  197. 'recharge_wallet_status' => '开启余额功能',
  198. 'recharge_custom_status' => '是否开放自定义金额',
  199. 'balance_to_commission' => '是否开启余额转佣金',
  200. 'combine_pay' => '是否开启余额抵扣',
  201. 'recharge_pic_url' => '背景图片',
  202. 'recharge_ad_pic_url' => '广告图片',
  203. // 'recharge_page_url' => '广告图片跳转链接',
  204. 'recharge_p_pic_url' => '充值说明图标',
  205. 'recharge_help' => '充值说明',
  206. "commission_to_balance" => '佣金转余额比例',
  207. "give_integral_profit" => '赠送积分比例',
  208. "give_balance_profit" => '赠送余额比例',
  209. ];
  210. }
  211. public function scenarios()
  212. {
  213. $scenarios = parent::scenarios();
  214. return $scenarios;
  215. }
  216. public function saveRechargeSetting()
  217. {
  218. if ($this->validate()) {
  219. $t = \Yii::$app->db->beginTransaction();
  220. $store = Store::findOne($this->store_id);
  221. $store->lg_cash = $this->lg_cash;
  222. $store->save();
  223. $data = self::getGroup(self::OPTION_KEY, get_store_id());
  224. foreach($data as &$val){
  225. if(isset($this->attributes[$val['name']])){
  226. $val['value'] = $this->attributes[$val['name']];
  227. }
  228. }
  229. $result = self::setGroup(self::OPTION_KEY, $data, get_store_id());
  230. if (!$result) {
  231. $t->rollBack();
  232. return [
  233. 'code' => 1,
  234. 'msg' => '保存失败'
  235. ];
  236. }
  237. $t->commit();
  238. return [
  239. 'code' => 0,
  240. 'msg' => '保存成功'
  241. ];
  242. } else {
  243. // 验证失败:$errors 是一个包含错误信息的数组
  244. return [
  245. 'code' => 1,
  246. "msg" => $this->getErrorSummary(false)[0]
  247. ];
  248. }
  249. }
  250. public static function getRechargeSettingList()
  251. {
  252. $result = self::getGroup(self::OPTION_KEY, get_store_id());
  253. $data = [];
  254. foreach($result as $val){
  255. $data[$val['name']] = (string)$val['value'];
  256. }
  257. $store = Store::findOne(get_store_id());
  258. $data['balance'] = $store->balance == 1 ? 1 : 0;
  259. $data['lg_cash'] = $store->lg_cash == 1 ? 1 : 0;
  260. return $data;
  261. }
  262. /**
  263. * 根据类获取数据
  264. * @param $group
  265. * @param $store_id
  266. * @return array
  267. */
  268. public static function getGroup ($group, $store_id = 1)
  269. {
  270. $group_setting = self::OPTION_SETTING;
  271. $group_arr = $group_setting[$group]['list'] ?: [];
  272. $data = [];
  273. foreach ($group_arr as $val) {
  274. $group_data = Option::findOne(['store_id' => $store_id, 'group' => $group, 'name' => $val['name']]);
  275. if ($val['type'] == 'checkbox') {
  276. if ($group_data) {
  277. $attr = $val['default'];
  278. $data_val = json_decode($group_data->value,true);
  279. foreach ($attr as $k => &$v) {
  280. $v = $data_val[$k] ? $data_val[$k] : $v;
  281. }
  282. }
  283. }
  284. $data[] = [
  285. 'name' => $group_data ? $group_data->name : $val['name'],
  286. 'text' => $val['text'],
  287. 'group' => $group_data ? $group_data->group : $group,
  288. 'type' => $val['type'],
  289. 'size' => isset($val['size']) ? $val['size'] : '',
  290. 'title' => isset($val['title']) ? $val['title'] : '',
  291. 'required' => $val['required'],
  292. 'muted' => isset($val['muted']) ? $val['muted'] : '',
  293. 'value' => $group_data ? ($val['type'] == 'checkbox' ? $attr : $group_data->value ) : $val['default'],
  294. 'updated_at' => $group_data ? $group_data->updated_at : 0,
  295. 'unit' => isset($val['unit']) ? $val['unit'] : '',
  296. ];
  297. }
  298. return $data;
  299. }
  300. /**
  301. * 根据类型批量设置数据
  302. * @param $group
  303. * @param $set_data
  304. * @param int $store_id
  305. * @return bool
  306. */
  307. public static function setGroup ($group, $set_data, $store_id = 1)
  308. {
  309. foreach ($set_data as $val) {
  310. if ($val['name']) {
  311. $group_data = Option::findOne(['store_id' => $store_id, 'group' => $group, 'name' => $val['name']]);
  312. if (!$group_data) {
  313. $group_data = new Option();
  314. $group_data->store_id = $store_id;
  315. $group_data->group = $group;
  316. }
  317. $group_data->name = $val['name'];
  318. if ($val['type'] == 'checkbox') {
  319. $group_data->value = json_encode($val['value']);
  320. }else {
  321. $group_data->value = "{$val['value']}";
  322. }
  323. if (!$group_data->save()) {
  324. return false;
  325. }
  326. }
  327. }
  328. return true;
  329. }
  330. }