WithdrawServer.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace App\Servers;
  3. use App\Jobs\AutoWithdrawJob;
  4. use App\Models\ErrorRecord;
  5. use App\Models\Goods;
  6. use App\Models\GoodsBrowse;
  7. use App\Models\GoodsImg;
  8. use App\Models\GoodsSearch;
  9. use App\Models\GoodsSpec;
  10. use App\Models\Member;
  11. use App\Models\MemberAddress;
  12. use App\Models\MemberOpenId;
  13. use App\Models\SearchStr;
  14. use App\Models\Shop;
  15. use App\Models\ShopWithdraw;
  16. use App\Models\SpecClass;
  17. use App\Models\SpecKey;
  18. use App\Models\Withdraw;
  19. use App\Models\WithdrawConfig;
  20. use Illuminate\Support\Facades\DB;
  21. /**
  22. * Redis数据缓存类
  23. */
  24. class WithdrawServer
  25. {
  26. private $status_arr = [
  27. 1 => '待审核',
  28. 2 => '已驳回',
  29. 3 => '已完成',
  30. ];
  31. private $withdraw_type = [
  32. 1 => '微信转账',
  33. 2 => '线下转账'
  34. ];
  35. static private $server = '';
  36. private function __construct()
  37. {
  38. }
  39. /**
  40. * 创建对象
  41. * @return WithdrawServer
  42. */
  43. static function creatServer()
  44. {
  45. if (empty(self::$server)) {
  46. self::$server = new WithdrawServer();
  47. }
  48. return self::$server;
  49. }
  50. /**
  51. * 获取订单状态
  52. * @param $status
  53. * @return string
  54. */
  55. public function getStatusArr($status)
  56. {
  57. return empty($this->status_arr[$status]) ? '' : $this->status_arr[$status];
  58. }
  59. /**
  60. * 获取转账类型
  61. * @param $type
  62. * @return string
  63. */
  64. function getType($type)
  65. {
  66. return empty($this->withdraw_type[$type]) ? '无' : $this->withdraw_type[$type];
  67. }
  68. function setWithdraw(Member $member, $money)
  69. {
  70. $withdraw_config = WithdrawConfig::where('coin_id', 1)->first();
  71. if ($withdraw_config->{'type'} == 2) {
  72. $service_money = $withdraw_config->{'proportion'};
  73. } else {
  74. $service_money = round($money * $withdraw_config->{'proportion'} / 100, 6);
  75. }
  76. $withdraw_info = [
  77. 'm_id' => $member->{'id'},
  78. 'money' => $money,
  79. 'status' => 1,
  80. 'wx_data' => '',
  81. 'service_money' => $service_money,
  82. 'withdraw_money' => $money - $service_money,
  83. ];
  84. DB::beginTransaction();
  85. $withdraw_info = Withdraw::create($withdraw_info);//记录提现信息
  86. MoneyDetailServer::creatServer()->write(1, 9, $withdraw_info->{'money'}, 0, $withdraw_info->{'m_id'}, '余额提现', $withdraw_info->{'id'});
  87. //更新会员余额信息
  88. $this->setIsAuto($withdraw_config,$withdraw_info,1);
  89. DB::commit();
  90. return $withdraw_info;
  91. }
  92. /**
  93. * 店铺提现
  94. * @param Shop $shop
  95. * @param $money
  96. * @param $bank_id
  97. * @return ShopWithdraw|false|\Illuminate\Database\Eloquent\Model
  98. * @throws \Exception
  99. */
  100. function setShopWithdraw(Shop $shop, $money, $bank_id)
  101. {
  102. $withdraw_config = WithdrawConfig::where('coin_id', 2)->first();
  103. if ($withdraw_config->{'type'} == 2) {
  104. $service_money = $withdraw_config->{'proportion'};
  105. } else {
  106. $service_money = round($money * $withdraw_config->{'proportion'} / 100, 6);
  107. }
  108. $withdraw_info = [
  109. 'shop_id' => $shop->{'id'},
  110. 'bank_id' => $bank_id,
  111. 'money' => $money,
  112. 'status' => 1,
  113. 'service_money' => $service_money,
  114. 'withdraw_money' => $money - $service_money,
  115. ];
  116. DB::beginTransaction();
  117. $withdraw_info = ShopWithdraw::create($withdraw_info);//记录提现信息
  118. $ret = ShopMoneyServer::creatServer()->write(1, 4, $withdraw_info->{'money'}, 0, $withdraw_info->{'shop_id'}, '余额提现', $withdraw_info->{'id'});
  119. if (empty($ret)) {
  120. DB::rollBack();
  121. return false;
  122. }
  123. //更新会员余额信息
  124. $this->setIsAuto($withdraw_config,$withdraw_info,2);
  125. DB::commit();
  126. return $withdraw_info;
  127. }
  128. /**
  129. * 验证是否满足自动提现
  130. * @param WithdrawConfig $config
  131. * @param $withdraw_info
  132. * @param $type
  133. */
  134. function setIsAuto(WithdrawConfig $config, $withdraw_info, $type)
  135. {
  136. if ($config->{'auto_money'} > 0 && $config->{'auto_day'} > 0 && $config->{'auto_num'} > 0) {
  137. if ($withdraw_info->{'money'} > $config->{'auto_money'}) {
  138. return;
  139. }
  140. $start_date = date('Y-m-d H:i:s', strtotime('-' . $config->{'auto_day'} . ' day'));
  141. if ($type == 1) {
  142. $withdraw_num = Withdraw::where('m_id', $withdraw_info->{'m_id'})->where('created_at', '>=', $start_date)->where('status', '<>', 3)->count();
  143. } else {
  144. $withdraw_num = ShopWithdraw::where('shop_id', $withdraw_info->{'shop_id'})->where('created_at', '>=', $start_date)->where('status', '<>', 3)->count();
  145. }
  146. if ($withdraw_num > $config->{'auto_num'}) {
  147. return;
  148. }
  149. AutoWithdrawJob::dispatch($type,$withdraw_info->{'id'})->onConnection('redis')->onQueue('auto_withdraw');
  150. }
  151. }
  152. /**
  153. * 自动提现功能
  154. * @param $type
  155. * @param $withdraw_id
  156. */
  157. function sysAutoWithdraw($type,$withdraw_id){
  158. if($type==1){
  159. $withdraw_info = Withdraw::where('id',$withdraw_id)->where('status', 1)->first();
  160. if(empty($withdraw_info)){
  161. ErrorRecord::create(['m_id' => 0, 'msg' => '提现信息错误', 'data' => $withdraw_id]);
  162. return;
  163. }
  164. $openid = MemberOpenId::where('m_id', $withdraw_info->{'m_id'})->where('type', 1)->value('openid');
  165. if (empty($openid)) {
  166. ErrorRecord::create(['m_id' => $withdraw_info->{'m_id'}, 'msg' => '当前用户未绑定微信,无法自动提现', 'data' => $withdraw_id]);
  167. return;
  168. }
  169. //转账到微信零钱
  170. $request = WeixinServer::creatServer()->setVerified('TX' . $withdraw_info->{'id'} . date('YmdHis'), $withdraw_info->{'withdraw_money'}, $openid, '');
  171. if (empty($request['code'])) {
  172. ErrorRecord::create(['m_id' => $withdraw_info->{'m_id'}, 'msg' => '微信转账失败:'.$withdraw_info->{'id'}, 'data' => json_encode($request)]);
  173. return;
  174. } else {
  175. $wx_data = json_encode($request);
  176. }
  177. $withdraw_info->update(['status' => 2, 'withdraw_type' => 1, 'wx_data' => $wx_data]);
  178. }else{
  179. //店铺自动审核暂未功能
  180. }
  181. }
  182. function autoWithdraw()
  183. {
  184. $list = Withdraw::where('status', 1)->get();
  185. foreach ($list as $info) {
  186. $openid = MemberOpenId::where('m_id', $info->{'m_id'})->where('type', 1)->value('openid');
  187. if (empty($openid)) {
  188. continue;
  189. }
  190. //转账到微信零钱
  191. $request = WeixinServer::creatServer()->setVerified('TX' . $info->{'id'} . date('YmdHis'), $info->{'withdraw_money'}, $openid, '');
  192. if (empty($request['code'])) {
  193. continue;
  194. } else {
  195. $wx_data = json_encode($request);
  196. }
  197. $info->update(['status' => 2, 'withdraw_type' => 1, 'wx_data' => $wx_data]);
  198. }
  199. }
  200. }