BankService.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. namespace App\Services\Bank;
  3. use App\Cache\FinanceCache;
  4. use App\Consts\ErrorConst;
  5. use App\Consts\FinanceConsts;
  6. use App\Dao\Bank\BankDao;
  7. use App\Dao\Bank\WithdrawDao;
  8. use App\Exceptions\ApiException;
  9. use App\Facade\Site;
  10. use App\Libs\Utils;
  11. use App\Services\Sms\SmsService;
  12. class BankService
  13. {
  14. private $bankDao;
  15. private $withdrawDao;
  16. private $smsService;
  17. public function __construct(
  18. BankDao $bankDao,
  19. WithdrawDao $withdrawDao,
  20. SmsService $smsService
  21. )
  22. {
  23. $this->bankDao = $bankDao;
  24. $this->withdrawDao = $withdrawDao;
  25. $this->smsService = $smsService;
  26. }
  27. /**
  28. * 下拉银行列表
  29. *
  30. * @return mixed
  31. */
  32. public function bankList()
  33. {
  34. return $this->bankDao->bankList();
  35. }
  36. /**
  37. * 站点银行卡列表
  38. *
  39. * @return array
  40. */
  41. public function channelBankAccounts($params): array
  42. {
  43. // 账号id
  44. $channelUserId = Site::getUid();
  45. // 对公对私
  46. $isCompany = (int)getProp($params, 'is_company', -1);
  47. // 获取账号下全部有效银行卡
  48. $accounts = $this->bankDao->getValidCashAccountsByChannelUserId($channelUserId, $isCompany);
  49. if (empty($accounts)) {
  50. return [];
  51. }
  52. // 获取银行卡提现记录
  53. $cardNumbers = array_column($accounts, 'card_number');
  54. [$startTime, $endTime] = [date('Y-m-d'), date('Y-m-d') . ' 23:59:59'];
  55. $withDrawOrders = $this->withdrawDao->getWithDrawLogsByBankAccountIds($cardNumbers, $startTime, $endTime);
  56. // 计算各个银行卡可提余额
  57. foreach ($accounts as &$account) {
  58. // 卡号
  59. $cardNumber = getProp($account, 'card_number');
  60. $isCompany = getProp($account, 'is_company');
  61. // 当前卡今日提现总额
  62. $amount = collect($withDrawOrders)->where('bank_account', $cardNumber)->sum('amount');
  63. // 计算剩余额度
  64. $account['remain_enable_amount'] = -1;
  65. if (!$isCompany) {
  66. $account['remain_enable_amount'] = FinanceConsts::WITH_DRAW_PRIVATE_MAX - $amount;
  67. if ($account['remain_enable_amount'] <= 0) {
  68. $account['remain_enable_amount'] = 0;
  69. }
  70. }
  71. }
  72. return $accounts;
  73. }
  74. /**
  75. * 添加银行卡账号
  76. *
  77. * @param $params
  78. * @return bool|int
  79. * @throws ApiException
  80. */
  81. public function addBankAccount($params)
  82. {
  83. // 基本参数
  84. $accountName = getProp($params, 'account_name');
  85. $identityCard = getProp($params, 'identity_card');
  86. $cardNumber = trim(getProp($params, 'card_number'));
  87. $accountBank = getProp($params, 'account_bank');
  88. $bankId = getProp($params, 'bank_id', 0);
  89. $isCompany = (int)getProp($params, 'is_company', 0);
  90. $province = getProp($params, 'province');
  91. $code = getProp($params, 'sms_code');
  92. $channelId = Site::getCurrentChannelId();
  93. $channelUserId = Site::getUid();
  94. $phone = Site::getPhone();
  95. // 参数简易判断
  96. if (empty($accountName) || empty($cardNumber) || empty($bankId) || !is_numeric($bankId) || empty($channelUserId)
  97. || !in_array($isCompany, [FinanceConsts::COMPANY_NO, FinanceConsts::COMPANY_YES], true)) {
  98. Utils::throwError(ErrorConst::PARAM_ERROR_CODE);
  99. }
  100. // 判断手机号是否合理设置
  101. if (empty($phone) || !preg_match('/^1[34578]\d{9}$/', $phone)) {
  102. Utils::throwError(ErrorConst::FINANCE_PHONE_ERROR);
  103. }
  104. // 检验银行卡号
  105. if (!preg_match('/^[0-9]{14,19}$/', $cardNumber)) {
  106. Utils::throwError(ErrorConst::FINANCE_CARD_NUMBER_ERROR);
  107. }
  108. // 校验手机验证码
  109. $isValid = $this->bankDao->checkSmsCode($channelId, $phone, $code);
  110. if (!$isValid) {
  111. Utils::throwError(ErrorConst::FINANCE_SMS_CHECK_FAILED);
  112. }
  113. // 获取银行信息
  114. $bank = $this->bankDao->getBank($bankId);
  115. if (!getProp($bank, 'id')) {
  116. Utils::throwError(ErrorConst::PARAM_ERROR_CODE);
  117. }
  118. // 获取全部账号列表,判断重复提交
  119. $cashAccounts = $this->bankDao->getValidCashAccountsByChannelUserId($channelUserId);
  120. if (collect($cashAccounts)->where('card_number', $cardNumber)->all()) {
  121. Utils::throwError(ErrorConst::FINANCE_ACCOUNT_EXISTED);
  122. }
  123. // 写入数据库
  124. $insertData = [
  125. 'channel_user_id' => $channelUserId,
  126. 'account_name' => $accountName,
  127. 'identity_card' => $identityCard,
  128. 'card_number' => $cardNumber,
  129. 'account_bank' => $accountBank,
  130. 'bank' => getProp($bank, 'name'),
  131. 'province' => $province,
  132. 'phone' => $phone,
  133. 'bank_id' => $bankId,
  134. 'is_company' => $isCompany,
  135. 'status' => FinanceConsts::STATUS_FORBID_UPDATE,
  136. 'updated_at' => date('Y-m-d H:i:s'),
  137. ];
  138. $insertId = $this->bankDao->addCashAccount($insertData);
  139. if (!$insertId) {
  140. Utils::throwError(ErrorConst::SYS_EXCEPTION);
  141. }
  142. // 删除验证码
  143. FinanceCache::delSmsCode($channelId, $phone);
  144. return $insertId;
  145. }
  146. /**
  147. * 删除银行卡账号
  148. *
  149. * @param $params
  150. * @return bool
  151. * @throws ApiException
  152. */
  153. public function delBankAccount($params): bool
  154. {
  155. // 基本参数
  156. $id = getProp($params, 'id');
  157. $deleteAll = (int)getProp($params, 'delete_all');
  158. $channelUserId = Site::getUid();
  159. // 删除全部
  160. if ($deleteAll) {
  161. return $this->bankDao->deleteCashAccountByChannelUserId($channelUserId);
  162. }
  163. // 参数简易判断
  164. if (!$id || !is_numeric($id)) {
  165. Utils::throwError(ErrorConst::PARAM_ERROR_CODE);
  166. }
  167. // 校验银行卡
  168. $cardInfo = $this->bankDao->getValidCashAccountById($id);
  169. if (!$cardInfo) {
  170. Utils::throwError(ErrorConst::RECORD_NOT_EXIST);
  171. }
  172. // 权限
  173. if ((int)getProp($cardInfo, 'channel_user_id') !== (int)$channelUserId) {
  174. Utils::throwError(ErrorConst::NOT_ACCESS);
  175. }
  176. // 执行更新
  177. $result = $this->bankDao->deleteCashAccountByCardId($id);
  178. if (!$result) {
  179. Utils::throwError(ErrorConst::SYS_EXCEPTION);
  180. }
  181. return true;
  182. }
  183. /**
  184. * 发送验证码
  185. *
  186. * @return bool
  187. * @throws ApiException
  188. */
  189. public function getBankAccountSms(): bool
  190. {
  191. $channelId = Site::getCurrentChannelId();
  192. $phone = Site::getPhone();
  193. // 获取当前手机号的验证码
  194. $code = FinanceCache::getSmsCode($channelId, $phone);
  195. if ($code) {
  196. Utils::throwError(ErrorConst::FINANCE_SMS_EXIST);
  197. }
  198. // 生成新的验证码并发送
  199. $newCode = mt_rand(1000, 9999);
  200. // 记录缓存
  201. FinanceCache::setSmsCode($channelId, $phone, $newCode);
  202. // 发送验证码
  203. $sendRes = $this->smsService->sendSms($phone, 'update_withdraw_cash', ['code' => $newCode]);
  204. if (!$sendRes) {
  205. Utils::throwError(ErrorConst::FINANCE_SMS_FAILED);
  206. }
  207. return true;
  208. }
  209. }