BalanceSetController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /****************************************************************
  3. * Author: libaojia
  4. * Date: 2017/12/4 下午2:11
  5. * Email: livsyitian@163.com
  6. * QQ: 995265288
  7. * User: 芸众商城 www.yunzshop.com
  8. ****************************************************************/
  9. namespace app\backend\modules\finance\controllers;
  10. use app\common\components\BaseController;
  11. use app\common\exceptions\ShopException;
  12. use app\common\facades\Setting;
  13. use app\common\helpers\Url;
  14. class BalanceSetController extends BaseController
  15. {
  16. private $balance_set;
  17. /**
  18. * 查看余额设置
  19. * @return string
  20. */
  21. public function see()
  22. {
  23. if (request()->ajax()) {
  24. !$this->balance_set && $this->setBalanceSet();
  25. if (!is_null(\app\common\modules\shop\ShopConfig::current()->get('love_form'))) {
  26. $class = array_get(\app\common\modules\shop\ShopConfig::current()->get('love_form'), 'class');
  27. $function = array_get(\app\common\modules\shop\ShopConfig::current()->get('love_form'), 'function');
  28. $ret = $class::$function();
  29. }
  30. $this->balance_set['level_limit'] = (int)$this->balance_set['level_limit'];
  31. $this->balance_set['group_type'] = (int)$this->balance_set['group_type'];
  32. $this->balance_set['recharge_activity_start'] = $this->balance_set['recharge_activity_start'] * 1000;
  33. $this->balance_set['recharge_activity_end'] = $this->balance_set['recharge_activity_end'] * 1000;
  34. return $this->successJson('ok', [
  35. 'balance' => $this->balance_set,
  36. 'is_open' => $ret ?: false,
  37. 'high_light_open' => app('plugins')->isEnabled('high-light') ? 1 : 0,
  38. // 'day_data' => $this->getDayData(),
  39. 'love_name' => app('plugins')->isEnabled('love') ? LOVE_NAME : '爱心值',
  40. 'memberLevels' => \app\backend\modules\member\models\MemberLevel::getMemberLevelList(),
  41. 'group_type' => \app\common\models\MemberGroup::uniacid()->select('id', 'group_name')->get()
  42. ]);
  43. }
  44. return view('finance.balance.index');
  45. }
  46. /**
  47. * 返回一天24时,对应key +1, 例:1 => 0:00
  48. * @return array
  49. */
  50. private function getDayData()
  51. {
  52. $dayData = [];
  53. for ($i = 1; $i <= 23; $i++) {
  54. $dayData += [
  55. $i => "每天" . $i . ":00",
  56. ];
  57. }
  58. return $dayData;
  59. }
  60. /**
  61. *
  62. * @return mixed|string更新余额设置数据
  63. */
  64. public function store()
  65. {
  66. $request_data = $this->getPostValue();
  67. if (Setting::set('finance.balance', $request_data)) {
  68. (new \app\common\services\operation\BalanceSetLog(['old' => $this->balance_set, 'new' => $request_data], 'update'));
  69. return $this->successJson('余额基础设置保存成功');
  70. }
  71. return $this->see();
  72. }
  73. private function getPostValue()
  74. {
  75. $this->validate($this->rules(), request(), [], $this->customAttributes());
  76. $request_data = \YunShop::request()->balance;
  77. //$request_data['sale'] = $this->rechargeSale($request_data);
  78. $request_data['recharge_activity_start'] = (int) ($request_data['recharge_activity_time']['start'] / 1000);
  79. $request_data['recharge_activity_end'] = (int) ($request_data['recharge_activity_time']['end'] / 1000);
  80. //顺序不能打乱,需要判断是否重置重置活动
  81. $request_data['recharge_activity_count'] = $this->getRechargeActivityCount($request_data['recharge_activity']);
  82. $request_data['recharge_activity'] = ($request_data['recharge_activity'] >= 1) ? 1 : 0;
  83. unset($request_data['recharge_activity_time']);
  84. return $request_data;
  85. }
  86. /**
  87. * 余额基础设置,附值 $this->balance_set
  88. */
  89. private function setBalanceSet()
  90. {
  91. $this->balance_set = Setting::get('finance.balance');
  92. if ($this->balance_set['uid']) {
  93. $this->balance_set['member'] = \app\backend\modules\member\models\Member::select('uid', 'mobile', 'nickname', 'realname', 'avatar')->find($this->balance_set['uid'])->toArray();
  94. }
  95. }
  96. private function getRechargeActivityCount($recharge_activity_status)
  97. {
  98. $this->setBalanceSet();
  99. $activity_count = !empty($this->balance_set['recharge_activity_count']) ? $this->balance_set['recharge_activity_count'] : 1;
  100. if ($recharge_activity_status == 2) {
  101. $activity_count += 1;
  102. }
  103. return $activity_count;
  104. }
  105. /**
  106. * 处理充值赠送数据,满额赠送数据
  107. *
  108. * @param $data
  109. * @return array
  110. * @Author yitian
  111. */
  112. private function rechargeSale($data)
  113. {
  114. $sale = array();
  115. $array = is_array($data['enough']) ? $data['enough'] : array();
  116. foreach ($array as $key => $value) {
  117. $enough = trim($value);
  118. if ($enough) {
  119. $sale[] = array(
  120. 'enough' => trim($data['enough'][$key]),
  121. 'give' => trim($data['give'][$key])
  122. );
  123. }
  124. }
  125. foreach ($sale as $key => $item) {
  126. $this->validatorCustomRules($item, $this->saleRules(), [], $this->saleCustomAttributes());
  127. }
  128. return $sale;
  129. }
  130. private function validatorCustomRules($array, $rules, $messages, $customAttributes)
  131. {
  132. $validator = $this->getValidationFactory()->make($array, $rules, $messages, $customAttributes);
  133. if ($validator->fails()) {
  134. throw new ShopException($validator->errors()->first());
  135. }
  136. }
  137. private function saleRules()
  138. {
  139. return [
  140. 'enough' => 'numeric|min:0',
  141. 'give' => 'numeric|min:0',
  142. ];
  143. }
  144. private function saleCustomAttributes()
  145. {
  146. return [
  147. 'enough' => "满足金额值",
  148. 'give' => "赠送金额",
  149. ];
  150. }
  151. private function rules()
  152. {
  153. return [
  154. 'balance.recharge' => 'required|numeric|regex:/^[01]$/',
  155. 'balance.recharge_activity' => 'required|numeric|regex:/^[012]$/',
  156. 'balance.recharge_activity_fetter' => 'required|numeric|integer|min:-1|max:99999999',
  157. 'balance.recharge_activity_time' => '',
  158. 'balance.proportion_status' => 'required|numeric|regex:/^[01]$/',
  159. 'balance.transfer' => 'required|numeric|regex:/^[01]$/',
  160. ];
  161. }
  162. private function customAttributes()
  163. {
  164. return [
  165. 'balance.recharge' => '开启充值',
  166. 'balance.recharge_activity' => '充值活动',
  167. 'balance.recharge_activity_fetter' => '会员参与充值活动次数',
  168. 'recharge_activity_time.start' => '充值活动开始时间',
  169. 'recharge_activity_time.end' => '充值活动开始时间',
  170. 'balance.proportion_status' => '充值赠送类型',
  171. 'balance.transfer' => '转让开关',
  172. ];
  173. }
  174. }