UserExtract.php 7.6 KB

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