WithdrawsController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace App\Http\Controllers\Withdraw;
  3. use App\Http\Controllers\AdminBaseController;
  4. use App\Http\Requests\WithdrawCreateRequest;
  5. use App\Http\Requests\WithdrawUpdateRequest;
  6. use App\Models\MemberOpenId;
  7. use App\Models\Withdraw;
  8. use App\Repositories\Eloquent\WithdrawRepositoryEloquent;
  9. use App\Servers\MoneyDetailServer;
  10. use App\Servers\WeixinServer;
  11. use App\Servers\WithdrawServer;
  12. use App\Validators\WithdrawValidator;
  13. use function Couchbase\defaultDecoder;
  14. /**
  15. * Class WithdrawsController.
  16. *
  17. * @package namespace App\Http\Controllers;
  18. */
  19. class WithdrawsController extends AdminBaseController
  20. {
  21. /**
  22. * @var WithdrawRepositoryEloquent
  23. */
  24. protected $repository;
  25. /**
  26. * @var WithdrawValidator
  27. */
  28. protected $validator;
  29. /**
  30. * WithdrawsController constructor.
  31. *
  32. * @param WithdrawRepositoryEloquent $repository
  33. * @param WithdrawValidator $validator
  34. */
  35. public function __construct(WithdrawRepositoryEloquent $repository, WithdrawValidator $validator)
  36. {
  37. parent::__construct($repository, $validator);
  38. }
  39. /**
  40. * 数据检索
  41. */
  42. public function _indexScopeQuery()
  43. {
  44. $where = [];
  45. $search = explode(";", request()->input('search', ""));
  46. $start = $end = null;
  47. $fieldSearchable = $this->repository->getFieldsSearchable();
  48. foreach ($search as $value) {
  49. if (!empty($value)) {
  50. list($one, $tow) = explode(":", $value);
  51. if (!empty($fieldSearchable[$one])) {
  52. if ($fieldSearchable[$one] == 'like') $tow = "%{$tow}%";
  53. $where[] = [$one, $fieldSearchable[$one], $tow];
  54. } elseif ($one == 'start') {
  55. $start = $tow . " 00:00:00";
  56. } elseif ($one == 'end') {
  57. $end = $tow . " 23:59:59";
  58. } else {
  59. continue;
  60. }
  61. }
  62. }
  63. return function ($query) use ($where, $start, $end) {
  64. if ($start) $where[] = ['created_at', '>=', $start];
  65. if ($end) $where[] = ['created_at', '<=', $end];
  66. if ($where) {
  67. $query->where($where);
  68. }
  69. };
  70. }
  71. /**
  72. * 数据更新
  73. */
  74. protected function _indexPost($datum)
  75. {
  76. foreach ($datum as $value){
  77. if ($value->status ==1) {
  78. $value->skip = '';
  79. } else {
  80. $value->skip = 'skip';
  81. }
  82. $value->{'withdraw_type_str'}=WithdrawServer::creatServer()->getType($value->{'withdraw_type'});
  83. $value->{'status_str'}=WithdrawServer::creatServer()->getStatusArr($value->{'status'});
  84. }
  85. return $datum;
  86. }
  87. /**
  88. * 查询组合
  89. * @return \Illuminate\Database\Query\Builder
  90. */
  91. function _indexJoin()
  92. {
  93. return Withdraw::from('withdraws as w')
  94. ->leftJoin('members as m', 'm.id', '=', 'w.m_id');
  95. }
  96. function _indexSelect(){
  97. return ['w.*','m.phone', 'm.nickname'];
  98. }
  99. function _updatePost(){
  100. $status=request()->input('status');
  101. $id=request()->input('id');
  102. $withdraw_type=request()->input('withdraw_type');
  103. $info=Withdraw::where('id',$id)->first();
  104. if(empty($withdraw_type) && !in_array($withdraw_type,[1,2])){
  105. $this->errorMsg='审核类型错误';
  106. return false;
  107. }
  108. if(empty($info)){
  109. $this->errorMsg='审核信息不存在';
  110. return false;
  111. }
  112. if($info->{'status'}!=1){
  113. $this->errorMsg='状态异常';
  114. return false;
  115. }
  116. if($status==2){
  117. $wx_data='';
  118. if($withdraw_type==1){
  119. $openid=MemberOpenId::where('m_id',$info->{'m_id'})->where('type',1)->value('openid');
  120. if(empty($openid)){
  121. $this->errorMsg='当前用户未绑定微信';
  122. return false;
  123. }
  124. //转账到微信零钱
  125. $request=WeixinServer::creatServer()->setVerified('TX'.$info->{'id'}.date('YmdHis'),$info->{'withdraw_money'},$openid,'');
  126. if(empty($request['code'])){
  127. $this->errorMsg=empty($request['msg'])?'微信零钱划转失败':$request['msg'];
  128. return false;
  129. }else{
  130. $wx_data=json_encode($request);
  131. }
  132. }
  133. $ret=$info->update(['status'=>2,'withdraw_type'=>$withdraw_type,'wx_data'=>$wx_data]);
  134. }else{
  135. MoneyDetailServer::creatServer()->write(1, 14, $info->{'money'}, 1, $info->{'m_id'}, '余额提现驳回', $info->{'id'});
  136. $ret=$info->update(['status'=>3]);
  137. }
  138. if($ret){
  139. $this->successMsg='操作成功';
  140. return true;
  141. }else{
  142. $this->errorMsg='操作失败';
  143. return false;
  144. }
  145. }
  146. /**
  147. * 提现审核
  148. */
  149. function audit(){
  150. $status=request()->input('status');
  151. $id=request()->input('id');
  152. $withdraw_type=request()->input('withdraw_type');
  153. $info=Withdraw::where('id',$id)->first();
  154. if(empty($info)){
  155. return $this->error('审核信息不存在');
  156. }
  157. if($info->{'status'}!=1){
  158. return $this->response('状态异常', 'error', '', '');
  159. }
  160. if($status==2){
  161. if($withdraw_type==1){
  162. $openid=MemberOpenId::where('m_id',$info->{'m_id'})->where('type',1)->value('openid');
  163. if(empty($openid)){
  164. return $this->error('当前用户未绑定微信');
  165. }
  166. }
  167. $ret=$info->update(['status'=>2,'withdraw_type'=>$withdraw_type]);
  168. //转账到微信零钱
  169. }else{
  170. MoneyDetailServer::creatServer()->write(1, 14, $info->{'money'}, 1, $info->{'m_id'}, '余额提现驳回', $info->{'id'});
  171. $ret=$info->update(['status'=>3]);
  172. }
  173. if($ret){
  174. return $this->success('操作成功');
  175. }else{
  176. return $this->error('操作失败');
  177. }
  178. }
  179. }