$this->user_id]); $data['score'] = empty($user_wallet['score']) ? 0 : $user_wallet['score']; // 积分配置 $setting = Yii::$app->services->setting->mall->get($this->mall_id, 'score'); $data['is_score_transfer'] = $setting['is_score_transfer']; $data['is_score_balance'] = $setting['is_score_balance']; // 判断是否有等级限制 $data['is_hide_score_details'] = $setting['is_hide_score_details'] ?? 0; // 前端隐藏结算明细是否开启 // 判断是否有积分转余额权限 if (!empty($data['is_score_balance']) && !empty($setting['level_limit_type']) && !empty($setting['level_limit_levels'])) { $user = User::findOne(['mall_id' => $this->mall_id, 'id' => $this->user_id]); $user_level = !empty($user) ? $user->level : 0; $level_limit_levels = json_decode($setting['level_limit_levels'], true); !in_array($user_level, $level_limit_levels) && $data['is_score_balance'] = 0; } // 待返积分 $data['wait_return_score'] = 0; if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_POINT_UPPER_LIMIT)) { if (!empty((new SettingService())->isEnable($this->mall_id))) { // 取出用户待返积分 try { $data['wait_return_score'] = (new UserService())->getUserQuota($this->mall_id, $this->user_id, false); } catch (\Exception $e) { return $this->error($e->getMessage()); } } } CapitalLog::$capitalType = 'score'; //获取用户累计增值金额 $total_added_num = CapitalLog::find()->where(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'from_type' => 'ScoreGame', 'capital_type' => 'score_game_increment'])->sum('change_num'); $yesterDay = DateHelper::yesterDay(); $yesteday_added_num = CapitalLog::find() ->where(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'from_type' => 'ScoreGame', 'capital_type' => 'score_game_increment']) ->andWhere(['>=', 'created_at', $yesterDay['start']]) ->andWhere(['<=', 'created_at', $yesterDay['end']]) ->sum('change_num'); $data['total_added_num'] = $total_added_num ?? 0; //总增值 $data['yesteday_added_num'] = $yesteday_added_num ?? 0; //昨日增值 $data['is_score_cash'] = $setting['score_cash_status'] ?? 0; $this->success('操作成功', $data); } /** * 余额充值/消费/提现记录 * @return mixed|void */ public function actionListOld() { $params = \Yii::$app->request->get(); $where = []; $where[] = ['mall_id' => $this->mall_id]; $where[] = ['user_id' => $this->user_id]; $params['select'] = 'id,change_type,change_num,current_num,desc,created_at,from_type,capital_type'; $params['order'] = 'id desc'; $params['where'] = $where; $list = ScoreLog::listPage($params); $list['from_type_map'] = UserWalletEnum::getFromTypeMapByScore(); $list['capital_type_map'] = UserWalletEnum::getCapitalTypeMapByScore(); return $this->success('操作成功', $list); } public function actionList() { $params = \Yii::$app->request->get(); CapitalLog::$capitalType = 'score'; $status = $params['status']??1; $where = []; if ($status == 0) { CapitalLog::$capitalType = 'score_frozen'; $where[] = ['status'=>$status]; } $where[] = ['mall_id' => $this->mall_id]; $where[] = ['user_id' => $this->user_id]; $params['select'] = 'id,change_type,change_num,current_num,desc,created_at,from_type,capital_type'; $params['order'] = 'id desc'; $params['where'] = $where; $params['page'] = empty($get['page']) ? 1 : $get['page']; $list = CapitalLog::listPage($params, false); $list['from_type_map'] = GlobalVarMap::getEnumMapByType('FromTypeMap', $this->mall_id); foreach ($list['from_type_map'] as $key => $item) { if ($key == 'ScoreBonus') { $mall_config = Yii::$app->services->setting->mall->get($this->mall_id, 'score'); $list['from_type_map'][$key] = !empty($mall_config['score_name']) ? $mall_config['score_name'] . '分红' : '积分分红'; } } $list['capital_type_map'] = GlobalVarMap::getEnumMapByType('CapitalTypeMap', $this->mall_id); // $list['from_type_map'] = UserWalletEnum::getFromTypeMapByScore(); // $list['capital_type_map'] = UserWalletEnum::getCapitalTypeMapByScore(); if(!empty($list)){ $score_name = \Yii::$app->services->setting->mall->get($this->mall_id, 'score.score_name'); $balance_name = \Yii::$app->services->setting->mall->get($this->mall_id, 'balance.balance_name'); foreach ($list['capital_type_map'] as &$val){ $val = str_replace('积分',$score_name,$val); $val = str_replace('余额',$balance_name,$val); } foreach ($list['list'] as &$item){ $item['desc'] = str_replace('积分',$score_name,$item['desc']); $item['desc'] = str_replace('金额',$score_name,$item['desc']); } } if ($params['page'] == 1) { $setting = \Yii::$app->services->setting->mall->get($this->mall_id, 'score'); $list['tab_alias'] = [ 'wait_settlement' => !empty($setting['wait_settlement']) ? $setting['wait_settlement'] : '未结算', 'finish_settlement' => !empty($setting['finish_settlement']) ? $setting['finish_settlement'] : '已结算', ]; } return $this->success('操作成功', $list); } /** * 积分转送/转入页面 * @Author: lun * @DateTime: 2021/11/24 0024 11:04 * @Copyright: copyright (c) 2021 广东七件事集团 * @return mixed|void */ public function actionTransfer() { $form = Yii::$app->request->get(); $rules = [[['type'], 'required'], [['type'], 'in', 'range' => ['score', 'balance']]]; $res = Yii::$app->services->validate->validate($form, $rules); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); // 查询用户积分余额 $score = UserWallet::find()->select('score')->where(['mall_id'=>$this->mall_id,'user_id'=>$this->user_id,'status'=>StatusEnum::ENABLED])->scalar(); // 获取转送配置 $config = Yii::$app->services->setting->mall->get($this->mall_id, 'score'); if ($form['type'] == 'score') { $ratio = $config['transfer_poundage']; $explain = "转送手续费:{$ratio}%"; } elseif ($form['type'] == 'balance') { $balance_name = Yii::$app->services->setting->mall->get($this->mall_id, 'balance.balance_name'); $ratio = $config['score_balance_ratio']; $explain = "{$ratio}{$config['score_name']}转换1{$balance_name}"; if(!empty($config['score_balance_poundage'])){ $explain .= ",手续费:{$config['score_balance_poundage']}%"; } } $pay_password_status = $config['enable_password'] ?? 0; $transaction_password = User::find()->select('transaction_password')->where(['mall_id'=>$this->mall_id,'id'=>$this->user_id,'status'=>StatusEnum::ENABLED])->scalar(); $is_transaction_password = empty(trim($transaction_password)) ? false : true; $data = ['capital' => $score, 'explain' => $explain, 'ratio' => $ratio]; $data = array_merge($data, compact('pay_password_status', 'is_transaction_password')); return $this->success('操作成功', $data); } /** * 积分转送提交 * @Author: lun * @DateTime: 2021/11/24 0024 11:08 * @Copyright: copyright (c) 2021 广东七件事集团 * @return mixed|void */ public function actionTransferScore() { if (Yii::$app->request->isPost) { $form = Yii::$app->request->post(); $rules = [ [['score','user_id'], 'required'], [['score','user_id'], 'integer'], [['score'], 'compare', 'compareValue' => 0, 'operator' => '>'], [['transaction_password'], 'string'], ]; $res = Yii::$app->services->validate->validate($form, $rules); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $key = sprintf('TransferScore:%s', $this->user_id); try { if (LockHelper::fLock($key)) throw new Exception('请勿频繁请求'); // 检测转送用户是否是本人 if ($form['user_id'] == $this->user_id) throw new Exception('赠送的用户不能是本人哦'); // 检查用户是否存在 $receive_user = User::getUser([['id' => $form['user_id']]], 'id'); if (empty($receive_user)) throw new Exception('赠送的用户不存在'); // 获取配置 $config = Yii::$app->services->setting->mall->get($this->mall_id, 'score'); if (empty($config['is_score_transfer'])) throw new Exception('积分转增功能尚未开启'); $check_password_result = UserWithdrawLog::checkPassword($this->user_id, $form['transaction_password'], $config); if(is_array($check_password_result)){ throw new Exception($check_password_result['msg']); } if (!empty($config['source_transfer_permission'])) { // $parent_ids = UserRelationship::getParentIdArr($this->user_id);// 获取上级 $parent_ids = UserPartitionRelationship::getParentIdArr($this->user_id);// 获取上级 // $child_ids = UserRelationship::getChildIdArr($this->user_id);// 获取下级 $child_ids = UserPartitionRelationship::getChildIdArr($this->user_id);// 获取下级 if (!in_array($receive_user->id, array_merge($parent_ids, $child_ids))) throw new Exception('转送会员不在你的团队中'); } $fee = bcdiv($form['score'] * $config['transfer_poundage'], 100, 2);// 手续费 $sub_score = bcadd($form['score'], $fee, 2);// 最终扣减积分额度 // 查询用户积分余额 $user_wallet = UserWallet::find()->select('id,score')->where(['mall_id'=>$this->mall_id,'user_id'=>$this->user_id,'status'=>StatusEnum::ENABLED])->asArray()->one(); if (empty($user_wallet) || ($user_wallet['score'] < $sub_score)) throw new Exception('积分额度不足'); // 组合用户 $list = [['user_id'=>$this->user_id,'type' => 'sub'], ['user_id'=>$receive_user->id,'type'=>'add']]; $give_user = $this->user_id;// 转出用户 $receive_user = '用户ID:'.$receive_user->id;// 接收用户 $fee_str = $fee <= 0 ? '' : ',手续费'.$fee; // 循环生成数据 foreach ($list as $item) { $insert_wallet[] = [ 'mall_id' => $this->mall_id, 'user_id' => $item['user_id'], 'is_frozen' => false, 'wallet_type' => UserWalletService::WALLET_TYPE_SCORE, 'money' => $item['type'] == 'sub' ? -$sub_score : $form['score'], 'desc' => $item['type'] == 'sub' ? '积分转给'.$receive_user.$fee_str : '用户ID'.$give_user.'积分转入', 'source_table' => UserWallet::tableName(), 'source_table_id' => $user_wallet['id'], 'from_type' => UserWalletEnum::TRANSFER, 'capital_type'=> UserWalletEnum::TRANSFER_GIVE ]; } // 异步处理积分 if ($insert_wallet) { $res = UserWalletService::batchHandleByQueue($insert_wallet); if (empty($res)) throw new Exception(UserWalletService::getStaticError()); } LockHelper::unFLock($key); $this->success('转送成功'); } catch (Exception $e) { LockHelper::unFLock($key); return $this->error('赠送失败:' . $e->getMessage()); } } } /** * 积分转入余额 * @Author: lun * @DateTime: 2021/11/24 0024 14:58 * @Copyright: copyright (c) 2021 广东七件事集团 * @return mixed|void * @throws \Exception */ public function actionTransferBalance() { if (Yii::$app->request->isPost) { $form = Yii::$app->request->post(); $rules = [ [['score'], 'required'], [['score'], 'integer'], [['score'], 'compare', 'compareValue' => 0, 'operator' => '>'], [['transaction_password'], 'string'], ]; $res = Yii::$app->services->validate->validate($form, $rules); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $key = sprintf('TransferBalance:%s', $this->user_id); try { if (LockHelper::fLock($key)) throw new Exception('请勿频繁请求'); // 获取配置 $config = Yii::$app->services->setting->mall->get($this->mall_id, 'score'); if (empty($config['is_score_balance'])) throw new Exception('积分转余额功能尚未开启'); if (empty($config['score_balance_ratio'])) throw new Exception('积分转余额功能尚未开启'); // 判断是否有积分转余额权限 if (!empty($config['level_limit_type']) && !empty($config['level_limit_levels'])) { $user = User::findOne(['mall_id' => $this->mall_id, 'id' => $this->user_id]); $user_level = !empty($user) ? $user->level : 0; $level_limit_levels = json_decode($config['level_limit_levels'], true); if(!in_array($user_level, $level_limit_levels)) throw new Exception('您暂无积分转余额权限');; } $check_password_result = UserWithdrawLog::checkPassword($this->user_id, $form['transaction_password'], $config); if(is_array($check_password_result)){ throw new Exception($check_password_result['msg']); } $balance = BcHelper::floorFloat(($form['score']/$config['score_balance_ratio']),2);// 保留两位小数,向下取整 //是否设置手续费 $poundage = 0; if(!empty($config['score_balance_poundage'])){ $poundage = BcHelper::floorFloat(($balance * $config['score_balance_poundage'] / 100),2); $balance = bcsub($balance,$poundage,2); } // 查询用户积分余额 $user_wallet = UserWallet::find()->select('id,score')->where(['mall_id'=>$this->mall_id,'user_id'=>$this->user_id,'status'=>StatusEnum::ENABLED])->asArray()->one(); if (empty($user_wallet) || ($user_wallet['score'] < $form['score'])) throw new Exception('积分额度不足'); // 扣减积分 $insert_wallet[] = [ 'mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'is_frozen' => false, 'wallet_type' => UserWalletService::WALLET_TYPE_SCORE, 'money' => -$form['score'], 'desc' => '积分转入余额', 'source_table' => UserWallet::tableName(), 'source_table_id' => $user_wallet['id'], 'from_type' => UserWalletEnum::TRANSFER, 'capital_type'=> UserWalletEnum::TRANSFER_BALANCE ]; // 增加余额 $desc = '积分转入余额'; if($poundage > 0){ $desc .= ',手续费扣除:'.$poundage; } $insert_wallet[] = [ 'mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'is_frozen' => false, 'wallet_type' => UserWalletService::WALLET_TYPE_BALANCE, 'money' => $balance, 'desc' => $desc, 'source_table' => UserWallet::tableName(), 'source_table_id' => $user_wallet['id'], 'from_type' => UserWalletEnum::RECHARGE, 'capital_type'=> UserWalletEnum::RECHARGE_SCORE ]; // 异步处理积分 if ($insert_wallet) { $res = UserWalletService::batchHandleByQueue($insert_wallet); if (empty($res)) throw new Exception(UserWalletService::getStaticError()); } LockHelper::unFLock($key); $this->success('转入成功'); } catch (Exception $e) { LockHelper::unFLock($key); return $this->error('转入失败:' . $e->getMessage()); } } } public function actionWithdrawList() { if (!Yii::$app->request->isGet) { return $this->error('请求方式非法'); } $where[] = ['mall_id' => $this->mall_id]; $where[] = ['user_id' => $this->user_id]; $where[] = ['from' => UserWithdrawLog::FROM_SCORE]; $where[]=['!=','status',-1]; $params['where'] = $where; $params['select'] = 'id,order_no,num,actual_num,poundage_num,poundage_ratio,status,type,created_at'; $params['order'] = 'id desc'; $list = UserWithdrawLog::listPage($params); if($list && $list['list']){ $ids = array_column($list['list'],'id'); if ($this->platform == 'mp-wx') { $mini_program = Yii::$app->services->setting->mall->get($this->mall_id, 'mini_program', true); $appId = $mini_program['app_id'] ?? ''; } else { $wechat_conf = Yii::$app->services->setting->mall->get($this->mall_id, 'wechat', true); $appId = $wechat_conf['app_id'] ?? ''; } $status_msg = UserWithdrawLog::$status_msg; //查询对应的积分记录 $log_list = ScoreLog::find()->where(['source_table' => 'user_wallet','source_table_id' => $ids])->indexBy('source_table_id')->asArray()->all(); $wechat_transfer_log_list = \common\models\wechat\WechatTransferLog::lists(['where' => [['cash_id' => $ids]], 'index' => 'cash_id']); $list['list'] = array_map(function($item) use ($log_list,$status_msg,$appId,$wechat_transfer_log_list) { $v['transfer_result'] = []; if (isset($wechat_transfer_log_list[$item['id']])) { $payment_conf = \Yii::$app->services->setting->mall->get($this->mall_id, 'pay.wechat', true); $payment_conf = json_decode($payment_conf, true); $mchId = $payment_conf['wechat_mch_id'] ?? ''; $wechat_transfer = $wechat_transfer_log_list[$item['id']]; $transfer_result = json_decode($wechat_transfer['transfer_result'], true); $transfer_result['mchId'] = $mchId; $transfer_result['appId'] = $appId; $item['transfer_result'] = $transfer_result; } $log = $log_list[$item['id']] ?? []; $log['change_num'] = $log['change_num'] ?? 0; $item['desc'] = '提现'.$log['change_num'].'商城积分'; $item['created_at'] = date("Y-m-d H:i:s",$item["created_at"]); $item['change_num'] = $item['num']; $item['status_text'] = $status_msg[$item['status']] ?? ''; return $item; },$list['list']); } return $this->success('操作成功', $list); } }