UserExtract.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2018/3/3
  6. */
  7. namespace app\models\user;
  8. use crmeb\basic\BaseModel;
  9. use crmeb\services\workerman\ChannelService;
  10. use crmeb\traits\ModelTrait;
  11. /**
  12. * TODO 用户提现
  13. * Class UserExtract
  14. * @package app\models\user
  15. */
  16. class UserExtract extends BaseModel
  17. {
  18. /**
  19. * 数据表主键
  20. * @var string
  21. */
  22. protected $pk = 'id';
  23. /**
  24. * 模型名称
  25. * @var string
  26. */
  27. protected $name = 'user_extract';
  28. use ModelTrait;
  29. //审核中
  30. const AUDIT_STATUS = 0;
  31. //未通过
  32. const FAIL_STATUS = -1;
  33. //已提现
  34. const SUCCESS_STATUS = 1;
  35. protected static $extractType = ['alipay', 'bank', 'weixin'];
  36. protected static $extractTypeMsg = ['alipay' => '支付宝', 'bank' => '银行卡', 'weixin' => '微信'];
  37. protected static $status = array(
  38. -1 => '未通过',
  39. 0 => '审核中',
  40. 1 => '已提现'
  41. );
  42. /**
  43. * 用户自主提现记录提现记录,后台执行审核
  44. * @param array $userInfo 用户个人信息
  45. * @param array $data 提现详细信息
  46. * @return bool
  47. */
  48. public static function userExtract($userInfo, $data)
  49. {
  50. // if(!in_array($data['extract_type'],self::$extractType))
  51. // return self::setErrorInfo('提现方式不存在');
  52. $userInfo = User::get($userInfo['uid']);
  53. if ($data['money_type'] == 'brokerage') {
  54. // var_dump(1111);
  55. $extractPrice = $userInfo['brokerage_price'];
  56. if ($extractPrice < 0) return self::setErrorInfo('提现佣金不足' . $data['money']);
  57. if ($data['money'] > $extractPrice) return self::setErrorInfo('提现佣金不足' . $data['money']);
  58. if ($data['money'] <= 0) return self::setErrorInfo('提现佣金大于0');
  59. $balance = bcsub($userInfo['brokerage_price'], $data['money'], 2);
  60. if ($balance < 0) $balance = 0;
  61. $insertData = [
  62. 'uid' => $userInfo['uid'],
  63. 'extract_type' => $data['extract_type'],
  64. 'extract_price' => $data['money'],
  65. 'add_time' => time(),
  66. 'balance' => $balance,
  67. 'status' => self::AUDIT_STATUS
  68. ];
  69. if (isset($data['name']) && strlen(trim($data['name']))) $insertData['real_name'] = $data['name'];
  70. else $insertData['real_name'] = $userInfo['nickname'];
  71. if (isset($data['cardnum'])) $insertData['bank_code'] = $data['cardnum'];
  72. else $insertData['bank_code'] = '';
  73. if (isset($data['bankname'])) $insertData['bank_address'] = $data['bankname'];
  74. else $insertData['bank_address'] = '';
  75. if (isset($data['weixin'])) $insertData['wechat'] = $data['weixin'];
  76. else $insertData['wechat'] = $userInfo['nickname'];
  77. if ($data['extract_type'] == 'alipay') {
  78. if (!$data['alipay_code']) return self::setErrorInfo('请输入支付宝账号');
  79. $insertData['alipay_code'] = $data['alipay_code'];
  80. $mark = '使用支付宝提现' . $insertData['extract_price'] . '元';
  81. } else if ($data['extract_type'] == 'bank') {
  82. if (!$data['cardnum']) return self::setErrorInfo('请输入银行卡账号');
  83. if (!$data['bankname']) return self::setErrorInfo('请输入开户行信息');
  84. $mark = '使用银联卡' . $insertData['bank_code'] . '提现' . $insertData['extract_price'] . '元';
  85. } else if ($data['extract_type'] == 'weixin') {
  86. if (!$data['weixin']) return self::setErrorInfo('请输入微信账号');
  87. $mark = '使用微信提现' . $insertData['extract_price'] . '元';
  88. }
  89. self::beginTrans();
  90. try {
  91. $res1 = self::create($insertData);
  92. if (!$res1) return self::setErrorInfo('提现失败');
  93. $res2 = User::edit(['brokerage_price' => $balance], $userInfo['uid'], 'uid');
  94. $res3 = UserBill::expend('余额提现', $userInfo['uid'], 'now_money', 'extract', $data['money'], $res1['id'], $balance, $mark);
  95. $res = $res2 && $res3;
  96. if ($res) {
  97. self::commitTrans();
  98. try {
  99. ChannelService::instance()->send('WITHDRAW', ['id' => $res1->id]);
  100. } catch (\Exception $e) {
  101. }
  102. event('AdminNewPush');
  103. //发送模板消息
  104. return true;
  105. } else return self::setErrorInfo('提现失败!');
  106. } catch (\Exception $e) {
  107. self::rollbackTrans();
  108. return self::setErrorInfo('提现失败!');
  109. }
  110. }
  111. // var_dump(2222);
  112. $userMoney = UserMoney::initialUserMoney($userInfo['uid'], $data['money_type']);
  113. $money2 = UserMoney::initialUserMoney($userInfo['uid'], $data['service_type']);
  114. if ($data['money_type'] != $data['service_type']) {
  115. if ($data['money'] > $userMoney['money']) return self::setErrorInfo('可提现佣金不足');
  116. if ($data['service'] > $money2['money']) return self::setErrorInfo('用于支付手续费的' . init_money_type()[$data['service_type']] . '不足');
  117. } else {
  118. // if (bcadd($data['money'], $data['service'], 8) > $userMoney['money']) return app('json')->fail('可提现佣金不足');
  119. if ($data['money'] > $userMoney['money']) return self::setErrorInfo('可提现佣金不足');
  120. }
  121. if ($data['money'] > $userMoney['money']) return self::setErrorInfo('提现佣金不足' . $data['money']);
  122. if ($data['money'] <= 0) return self::setErrorInfo('提现佣金大于0');
  123. $balance = bcsub($userMoney['money'], $data['money'], 8);
  124. if ($balance < 0) $balance = 0;
  125. if ($data['money_type'] != $data['service_type']) {
  126. $data['extract_price'] = $data['money'];
  127. } else {
  128. $data['extract_price'] = bcsub($data['money'], $data['service'], 8);
  129. }
  130. $insertData = [
  131. 'uid' => $userInfo['uid'],
  132. 'extract_type' => $data['extract_type'] ?? '',
  133. 'extract_price' => $data['money'],
  134. 'add_time' => time(),
  135. 'balance' => $balance,
  136. 'money_type' => $data['money_type'],
  137. 'address' => $data['address'],
  138. 'service' => $data['service'],
  139. 'service_type' => $data['service_type'],
  140. 'real_get' => $data['extract_price'],
  141. ];
  142. // var_dump($insertData);
  143. // exit;
  144. if (isset($data['name']) && strlen(trim($data['name']))) $insertData['real_name'] = $data['name'];
  145. else $insertData['real_name'] = $userInfo['nickname'];
  146. if (isset($data['cardnum'])) $insertData['bank_code'] = $data['cardnum'];
  147. else $insertData['bank_code'] = '';
  148. if (isset($data['bankname'])) $insertData['bank_address'] = $data['bankname'];
  149. else $insertData['bank_address'] = '';
  150. if (isset($data['weixin'])) $insertData['wechat'] = $data['weixin'];
  151. else $insertData['wechat'] = $userInfo['nickname'];
  152. if ($data['extract_type'] == 'alipay') {
  153. if (!$data['alipay_code']) return self::setErrorInfo('请输入支付宝账号');
  154. $insertData['alipay_code'] = $data['alipay_code'];
  155. $mark = '使用支付宝提现' . $insertData['extract_price'] . get_money_name($insertData['money_type']);
  156. } else if ($data['extract_type'] == 'bank') {
  157. if (!$data['cardnum']) return self::setErrorInfo('请输入银行卡账号');
  158. if (!$data['bankname']) return self::setErrorInfo('请输入开户行信息');
  159. $mark = '使用银联卡' . $insertData['bank_code'] . '提现' . $insertData['extract_price'] . get_money_name($insertData['money_type']);
  160. } else if ($data['extract_type'] == 'weixin') {
  161. if (!$data['weixin']) return self::setErrorInfo('请输入微信账号');
  162. $mark = '使用微信提现' . $insertData['extract_price'] . get_money_name($insertData['money_type']);
  163. } else {
  164. $mark = '提币' . $insertData['extract_price'] . init_money_type()[$data['money_type']];
  165. }
  166. $mark2 = '提币' . $insertData['extract_price'] . init_money_type()[$data['money_type']] . '支付手续费' . $insertData['service'] . init_money_type()[$data['service_type']];
  167. // }
  168. self::beginTrans();
  169. try {
  170. $res1 = self::create($insertData);
  171. if (!$res1) return self::setErrorInfo('提现失败');
  172. // $res2 = User::edit(['brokerage_price' => $balance], $userInfo['uid'], 'uid');
  173. // $res3 = UserBill::expend('余额提现', $userInfo['uid'], 'now_money', 'extract', $data['money'], $res1['id'], $balance, $mark);
  174. $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);
  175. if ($res) {
  176. self::commitTrans();
  177. // try {
  178. // ChannelService::instance()->send('WITHDRAW', ['id' => $res1->id]);
  179. // } catch (\Exception $e) {
  180. // }
  181. // event('AdminNewPush');
  182. //发送模板消息
  183. return $res1;
  184. } else return self::setErrorInfo('提币失败!');
  185. } catch (\Exception $e) {
  186. self::rollbackTrans();
  187. return self::setErrorInfo('提币失败!');
  188. }
  189. }
  190. /**
  191. * 获得用户最后一次提现信息
  192. * @param $openid
  193. * @return mixed
  194. */
  195. public static function userLastInfo($uid)
  196. {
  197. return self::where(compact('uid'))->order('add_time DESC')->find();
  198. }
  199. /**
  200. * 获得用户提现总金额
  201. * @param $uid
  202. * @return mixed
  203. */
  204. public static function userExtractTotalPrice($uid, $status = self::SUCCESS_STATUS)
  205. {
  206. return self::where('uid', $uid)->where('status', $status)->value('SUM(extract_price)') ?: 0;
  207. }
  208. /**
  209. * 用户提现记录列表
  210. * @param int $uid 用户uid
  211. * @param int $first 截取行数
  212. * @param int $limit 截取数
  213. * @return \think\Collection
  214. * @throws \think\db\exception\DataNotFoundException
  215. * @throws \think\db\exception\ModelNotFoundException
  216. * @throws \think\exception\DbException
  217. */
  218. public static function extractList($uid, $first = 0, $limit = 8)
  219. {
  220. $list = UserExtract::where('uid', $uid)->order('add_time desc')->limit($first, $limit)->select();
  221. foreach ($list as &$v) {
  222. $v['add_time'] = date('Y/m/d', $v['add_time']);
  223. }
  224. return $list;
  225. }
  226. /**
  227. * 获取累计已提现佣金
  228. * @param $uid
  229. * @return float
  230. */
  231. public static function extractSum($uid)
  232. {
  233. return self::where('uid', $uid)->where('status', 1)->sum('extract_price');
  234. }
  235. }