123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- /**
- *
- * @author: xaboy<365615158@qq.com>
- * @day: 2018/3/3
- */
- namespace app\models\user;
- use crmeb\basic\BaseModel;
- use crmeb\services\workerman\ChannelService;
- use crmeb\traits\ModelTrait;
- /**
- * TODO 用户提现
- * Class UserExtract
- * @package app\models\user
- */
- class UserExtract extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'user_extract';
- use ModelTrait;
- //审核中
- const AUDIT_STATUS = 0;
- //未通过
- const FAIL_STATUS = -1;
- //已提现
- const SUCCESS_STATUS = 1;
- protected static $extractType = ['alipay', 'bank', 'weixin'];
- protected static $extractTypeMsg = ['alipay' => '支付宝', 'bank' => '银行卡', 'weixin' => '微信'];
- protected static $status = array(
- -1 => '未通过',
- 0 => '审核中',
- 1 => '已提现'
- );
- /**
- * 用户自主提现记录提现记录,后台执行审核
- * @param array $userInfo 用户个人信息
- * @param array $data 提现详细信息
- * @return bool
- */
- public static function userExtract($userInfo, $data)
- {
- // if(!in_array($data['extract_type'],self::$extractType))
- // return self::setErrorInfo('提现方式不存在');
- $userInfo = User::get($userInfo['uid']);
- if ($data['money_type'] == 'brokerage') {
- // var_dump(1111);
- $extractPrice = $userInfo['brokerage_price'];
- if ($extractPrice < 0) return self::setErrorInfo('提现佣金不足' . $data['money']);
- if ($data['money'] > $extractPrice) return self::setErrorInfo('提现佣金不足' . $data['money']);
- if ($data['money'] <= 0) return self::setErrorInfo('提现佣金大于0');
- $balance = bcsub($userInfo['brokerage_price'], $data['money'], 2);
- if ($balance < 0) $balance = 0;
- $insertData = [
- 'uid' => $userInfo['uid'],
- 'extract_type' => $data['extract_type'],
- 'extract_price' => $data['money'],
- 'add_time' => time(),
- 'balance' => $balance,
- 'status' => self::AUDIT_STATUS
- ];
- if (isset($data['name']) && strlen(trim($data['name']))) $insertData['real_name'] = $data['name'];
- else $insertData['real_name'] = $userInfo['nickname'];
- if (isset($data['cardnum'])) $insertData['bank_code'] = $data['cardnum'];
- else $insertData['bank_code'] = '';
- if (isset($data['bankname'])) $insertData['bank_address'] = $data['bankname'];
- else $insertData['bank_address'] = '';
- if (isset($data['weixin'])) $insertData['wechat'] = $data['weixin'];
- else $insertData['wechat'] = $userInfo['nickname'];
- if ($data['extract_type'] == 'alipay') {
- if (!$data['alipay_code']) return self::setErrorInfo('请输入支付宝账号');
- $insertData['alipay_code'] = $data['alipay_code'];
- $mark = '使用支付宝提现' . $insertData['extract_price'] . '元';
- } else if ($data['extract_type'] == 'bank') {
- if (!$data['cardnum']) return self::setErrorInfo('请输入银行卡账号');
- if (!$data['bankname']) return self::setErrorInfo('请输入开户行信息');
- $mark = '使用银联卡' . $insertData['bank_code'] . '提现' . $insertData['extract_price'] . '元';
- } else if ($data['extract_type'] == 'weixin') {
- if (!$data['weixin']) return self::setErrorInfo('请输入微信账号');
- $mark = '使用微信提现' . $insertData['extract_price'] . '元';
- }
- self::beginTrans();
- try {
- $res1 = self::create($insertData);
- if (!$res1) return self::setErrorInfo('提现失败');
- $res2 = User::edit(['brokerage_price' => $balance], $userInfo['uid'], 'uid');
- $res3 = UserBill::expend('余额提现', $userInfo['uid'], 'now_money', 'extract', $data['money'], $res1['id'], $balance, $mark);
- $res = $res2 && $res3;
- if ($res) {
- self::commitTrans();
- try {
- ChannelService::instance()->send('WITHDRAW', ['id' => $res1->id]);
- } catch (\Exception $e) {
- }
- event('AdminNewPush');
- //发送模板消息
- return true;
- } else return self::setErrorInfo('提现失败!');
- } catch (\Exception $e) {
- self::rollbackTrans();
- return self::setErrorInfo('提现失败!');
- }
- }
- // var_dump(2222);
- $userMoney = UserMoney::initialUserMoney($userInfo['uid'], $data['money_type']);
- $money2 = UserMoney::initialUserMoney($userInfo['uid'], $data['service_type']);
- if ($data['money_type'] != $data['service_type']) {
- if ($data['money'] > $userMoney['money']) return self::setErrorInfo('可提现佣金不足');
- if ($data['service'] > $money2['money']) return self::setErrorInfo('用于支付手续费的' . init_money_type()[$data['service_type']] . '不足');
- } else {
- // if (bcadd($data['money'], $data['service'], 8) > $userMoney['money']) return app('json')->fail('可提现佣金不足');
- if ($data['money'] > $userMoney['money']) return self::setErrorInfo('可提现佣金不足');
- }
- if ($data['money'] > $userMoney['money']) return self::setErrorInfo('提现佣金不足' . $data['money']);
- if ($data['money'] <= 0) return self::setErrorInfo('提现佣金大于0');
- $balance = bcsub($userMoney['money'], $data['money'], 8);
- if ($balance < 0) $balance = 0;
- if ($data['money_type'] != $data['service_type']) {
- $data['extract_price'] = $data['money'];
- } else {
- $data['extract_price'] = bcsub($data['money'], $data['service'], 8);
- }
- $insertData = [
- 'uid' => $userInfo['uid'],
- 'extract_type' => $data['extract_type'] ?? '',
- 'extract_price' => $data['money'],
- 'add_time' => time(),
- 'balance' => $balance,
- 'money_type' => $data['money_type'],
- 'address' => $data['address'],
- 'service' => $data['service'],
- 'service_type' => $data['service_type'],
- 'real_get' => $data['extract_price'],
- ];
- // var_dump($insertData);
- // exit;
- if (isset($data['name']) && strlen(trim($data['name']))) $insertData['real_name'] = $data['name'];
- else $insertData['real_name'] = $userInfo['nickname'];
- if (isset($data['cardnum'])) $insertData['bank_code'] = $data['cardnum'];
- else $insertData['bank_code'] = '';
- if (isset($data['bankname'])) $insertData['bank_address'] = $data['bankname'];
- else $insertData['bank_address'] = '';
- if (isset($data['weixin'])) $insertData['wechat'] = $data['weixin'];
- else $insertData['wechat'] = $userInfo['nickname'];
- if ($data['extract_type'] == 'alipay') {
- if (!$data['alipay_code']) return self::setErrorInfo('请输入支付宝账号');
- $insertData['alipay_code'] = $data['alipay_code'];
- $mark = '使用支付宝提现' . $insertData['extract_price'] . get_money_name($insertData['money_type']);
- } else if ($data['extract_type'] == 'bank') {
- if (!$data['cardnum']) return self::setErrorInfo('请输入银行卡账号');
- if (!$data['bankname']) return self::setErrorInfo('请输入开户行信息');
- $mark = '使用银联卡' . $insertData['bank_code'] . '提现' . $insertData['extract_price'] . get_money_name($insertData['money_type']);
- } else if ($data['extract_type'] == 'weixin') {
- if (!$data['weixin']) return self::setErrorInfo('请输入微信账号');
- $mark = '使用微信提现' . $insertData['extract_price'] . get_money_name($insertData['money_type']);
- } else {
- $mark = '提币' . $insertData['extract_price'] . init_money_type()[$data['money_type']];
- }
- $mark2 = '提币' . $insertData['extract_price'] . init_money_type()[$data['money_type']] . '支付手续费' . $insertData['service'] . init_money_type()[$data['service_type']];
- // }
- self::beginTrans();
- try {
- $res1 = self::create($insertData);
- if (!$res1) return self::setErrorInfo('提现失败');
- // $res2 = User::edit(['brokerage_price' => $balance], $userInfo['uid'], 'uid');
- // $res3 = UserBill::expend('余额提现', $userInfo['uid'], 'now_money', 'extract', $data['money'], $res1['id'], $balance, $mark);
- $res = $res1 && ($data['service'] > 0 ? (UserMoney::expendMoney($userInfo['uid'], $data['service_type'], $data['service'], 'extract_service', '提币手续费', $mark2)) : true) && UserMoney::expendMoney($userInfo['uid'], $data['money_type'], $data['extract_price'], 'extract', '申请提币', $mark);
- if ($res) {
- self::commitTrans();
- // try {
- // ChannelService::instance()->send('WITHDRAW', ['id' => $res1->id]);
- // } catch (\Exception $e) {
- // }
- // event('AdminNewPush');
- //发送模板消息
- return $res1;
- } else return self::setErrorInfo('提币失败!');
- } catch (\Exception $e) {
- self::rollbackTrans();
- return self::setErrorInfo('提币失败!');
- }
- }
- /**
- * 获得用户最后一次提现信息
- * @param $openid
- * @return mixed
- */
- public static function userLastInfo($uid)
- {
- return self::where(compact('uid'))->order('add_time DESC')->find();
- }
- /**
- * 获得用户提现总金额
- * @param $uid
- * @return mixed
- */
- public static function userExtractTotalPrice($uid, $status = self::SUCCESS_STATUS)
- {
- return self::where('uid', $uid)->where('status', $status)->value('SUM(extract_price)') ?: 0;
- }
- /**
- * 用户提现记录列表
- * @param int $uid 用户uid
- * @param int $first 截取行数
- * @param int $limit 截取数
- * @return \think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public static function extractList($uid, $first = 0, $limit = 8)
- {
- $list = UserExtract::where('uid', $uid)->order('add_time desc')->limit($first, $limit)->select();
- foreach ($list as &$v) {
- $v['add_time'] = date('Y/m/d', $v['add_time']);
- }
- return $list;
- }
- /**
- * 获取累计已提现佣金
- * @param $uid
- * @return float
- */
- public static function extractSum($uid)
- {
- return self::where('uid', $uid)->where('status', 1)->sum('extract_price');
- }
- }
|