// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\MemberModel; use App\Models\MemberPaymentModel; use App\Models\User; use App\Models\UserModel; use App\Services\BaseService; use App\Services\ConfigService; use Earnp\GoogleAuthenticator\GoogleAuthenticator; /** * 交易员收款方式-服务类 * Class MemberPaymentService * @package App\Services\Common */ class MemberPaymentService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @since 2020/11/10 */ public function __construct() { $this->model = new MemberPaymentModel(); $this->memberModel = new MemberModel(); $this->userModel = new UserModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * 获取选项列表 * @param $userId * @return mixed */ public function getOptionList($userId, $type = 1, $pageSize = 9999) { $list = $this->model->where(function ($query) use ($userId, $type) { $query->where(['user_id' => $userId,'type'=> $type,'mark'=>1])->where('status','>',0); }) ->selectRaw('id,type,real_name,logo,bank_name,branch_name,account,is_default,bank_card,trade_num,used_num,trade_quota,used_quota,(trade_quota - used_quota) as credit,status') ->orderBy('is_default','asc') ->orderBy('id','desc') ->paginate($pageSize>0? $pageSize : 9999999); $list = $list? $list->toArray() :[]; if($list){ foreach($list['data'] as &$item){ $item['logo'] = $item['logo'] ? get_image_url($item['logo']) : ''; $item['realname_text'] = $item['real_name'] ? format_realname($item['real_name']) : ''; $item['account_text'] = $item['account'] ? format_account($item['account']) : ''; $item['bank_card_text'] = $item['bank_card'] ? format_bank_card($item['bank_card']) : ''; if ($item['type'] == 1) { $item['show_text'] = $item['bank_name'] . ' ' . $item['realname_text'] . ' ' . $item['bank_card_text']; } else { $item['show_text'] = $item['realname_text'] . ' ' . $item['account_text']; } } } return [ 'pageSize'=> $pageSize, 'total'=>isset($list['total'])? $list['total'] : 0, 'list'=> isset($list['data'])? $list['data'] : [] ]; } /** * 是否已经设置了收款方式 * @param $userId * @return mixed */ public function checkHasByUser($userId) { return $this->model->where(['user_id' => $userId, 'status' => 1,'mark'=>1])->count('id'); } /** * 更新状态 * @param $id * @param $status * @param $userId * @return mixed */ public function setPayment($id, $status, $userId){ $status = $status? 1 : 2; return $this->model->where(['user_id' => $userId, 'id' => $id,'mark'=>1])->update(['status'=> $status]); } /** * 删除 * @param $id * @param $userId * @return mixed */ public function delPayment($id, $userId){ // 永久删除 $this->model->where(['user_id' => $userId, 'id' => $id,'status'=> 0,'mark'=>1]) ->where('update_time','<', time() - 2 * 86400) ->delete(); return $this->model->where(['user_id' => $userId, 'id' => $id])->update(['status'=> 0,'mark'=>1]); } /** * 详情 * @param $id * @return mixed */ public function getInfo($id) { $info = $this->model->where(['id' => $id,'mark'=>1])->where('status','>', 0)->first(); if($info){ $info['logo'] = $info['logo']? get_image_url($info['logo']) : ''; $info['status'] = $info['status']==1? true : false; } return $info; } /** * 获取默认的收款方式,若没有则返回其他一个 * @param $userId * @return array */ public function getPayment($userId) { $info = $this->model->where(['user_id' => $userId,'mark'=>1,'status'=> 1]) ->whereRaw('(trade_num<=0 or trade_quota <= 0 or (trade_num-used_num>0 and trade_quota-used_quota>0))') ->select(['id','type','logo','real_name','bank_name','branch_name','bank_card','qrcode','account']) ->orderBy('is_default','asc') ->orderBy('id','desc') ->first(); $info = $info? $info->toArray() : []; if($info){ $info['logo'] = $info['logo']? get_image_url($info['logo']) : ''; } return $info; } /** * 后台添加/编辑 * @return array */ public function saveData($userId){ $data = request()->all(); $id = isset($data['id'])? intval($data['id']) : 0; $googleCode = isset($data['google_code'])? $data['google_code'] : ''; $type = isset($data['type'])? $data['type'] : 1; $bankCard = isset($data['bank_card'])? $data['bank_card'] : ''; $isDefault = isset($data['is_default'])? intval($data['is_default']) : 2; if($type ==1){ if(empty($bankCard) || !preg_match('/^[1-9][0-9]{12,19}$/', $bankCard)){ return returnJson('银行卡号格式不正确,请输入13-19位银行卡号', false); } if($this->model->where(['bank_card'=> $bankCard])->whereNotIn('id', [$id])->value('id')){ return returnJson('您添加的银行卡号已存在,请核对正确或联系客服处理', false); } } if(empty($googleCode)){ return returnJson('请输入您的谷歌验证码,或先绑定再来操作', false); } $userInfo = $this->userModel->where(['user_id'=> $userId,'mark'=>1]) ->select(['id','user_id','google_secret','status']) ->first(); if(empty($userInfo)){ return returnJson('您的账号异常无法操作,请联系客服处理', false); } $googleSecret = isset($userInfo['google_secret'])? $userInfo['google_secret'] : ''; if(empty($googleSecret)){ return returnJson(2017, false); } if (!GoogleAuthenticator::CheckCode($googleSecret, $googleCode)) { return returnJson(2018, false); } $limitCount = ConfigService::make()->getConfigByCode('payment_limit_num'); $limitCount = $limitCount>0? $limitCount : 30; if($this->model->where(['user_id'=> $userId, 'mark'=> 1])->count('id') >= $limitCount){ return returnJson('您当前的收款账号数量已经超出限制,请删除无用账号或联系客服处理', false); } // LOGO if(isset($data['logo'])){ $logo = trim($data['logo']); if (strpos($logo, "temp")) { $data['logo'] = save_image($logo, 'business'); } else { $data['logo'] = str_replace(IMG_URL, "", $data['logo']); } } // QRCODE if(isset($data['qrcode'])){ $qrcode = trim($data['qrcode']); if (strpos($qrcode, "temp")) { $data['qrcode'] = save_image($qrcode, 'business'); } else { $data['qrcode'] = str_replace(IMG_URL, "", $data['qrcode']); } } if($isDefault == 1){ $this->model->where(['user_id'=> $userId])->update(['is_default'=> 2]); } $data['user_id'] = $userId; if(isset($data['used_at'])){ $data['used_at'] = $data['used_at']? $data['used_at'] : time(); } return parent::edit($data); } }