ChannelWithdraw.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /**
  3. * 提现控制器
  4. *
  5. */
  6. namespace app\guildapi\controller;
  7. use app\common\library\MakeReportGo;
  8. use app\guildapi\controller\Guild;
  9. use think\Db;
  10. use think\Config;
  11. use app\common\model\Setting;
  12. use think\Env;
  13. use think\Exception;
  14. use app\common\library\MakeReport;
  15. class ChannelWithdraw extends Guild {
  16. protected $nowTime;
  17. protected $payRequestLimit = 5; //重复下单的限制时间
  18. protected $redis; //redis的句柄对象
  19. /**
  20. * 不进行父类的登录验证,所以增加构造方法重写了父类的初始化方法
  21. */
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->nowTime = NOW_TIMESTAMP;
  26. $this->redis = \think\Cache::store('default')->handler();
  27. }
  28. /**
  29. * 提现列表
  30. * @return [type] [description]
  31. */
  32. public function index()
  33. {
  34. //判断当前账号是否有此功能
  35. if(!in_array($this->_channelLevel,[1])){
  36. $this->jsonResult('', 0, '您无权限使用该功能');
  37. }
  38. $list_rows = $this->input('list_rows',10);
  39. $page = $this->input('page',1);
  40. $status = $this->input('status','0','intval');
  41. $begin_time = $this->input('begin_time','','trim');
  42. $end_time = $this->input('end_time','','trim');
  43. $download = $this->input('download',0,'intval');
  44. $condition = [];
  45. $condition['channel_id'] = $this->_channelId;
  46. if(input('status') <> ''){
  47. $condition['status'] = intval($status);
  48. }
  49. //申请开始时间和结束时间不为空时
  50. if ($begin_time != '' && $end_time != '') {
  51. $condition['create_time'] = [
  52. ['>=', strtotime($begin_time)],
  53. ['<=', strtotime($end_time . ' 23:59:59')],
  54. ];
  55. } //开始时间不为空时
  56. elseif ($begin_time != '') {
  57. $condition['create_time'] = ['>=', strtotime($begin_time)];
  58. } //结束时间不为空时
  59. elseif ($end_time != '') {
  60. $condition['create_time'] = ['<=', strtotime($end_time . ' 23:59:59')];
  61. }
  62. // var_dump($condition);
  63. if($download){
  64. $sql = model('ChannelWithdraw')
  65. ->where($condition)
  66. ->field("id,orderid,channel_id,channel_name,withdraw_amt,remark,create_time,status")
  67. ->order("id desc")
  68. ->fetchSql(true)
  69. ->select();
  70. // echo $sql;
  71. // if ((new MakeReport())->addTask('guild.ChannelWithdrawIndex', $sql, 'cps'.session('guild_info')['id'])){
  72. if ((new MakeReportGo())->addTask('guild.ChannelWithdrawIndex', $sql, 'cps'.session('guild_info')['id'])){
  73. $this->jsonResult('', 20000, '报表生成的任务已经提交, 报表生成完成后,会及时通知您,请耐心稍等');
  74. }
  75. else{
  76. $this->jsonResult('', 20013, '报表生成任务不可重复提交,如遇到无法导出情况,建议修改查询条件解除当前状态,提交重新生成报表任务!');
  77. }
  78. }
  79. $withdrawList = model('ChannelWithdraw')
  80. ->where($condition)
  81. ->field("id,orderid,channel_id,channel_name,withdraw_amt,remark,create_time,status")
  82. ->order("id desc")
  83. ->paginate(['list_rows'=>$list_rows,'page'=>$page])
  84. ->toArray();
  85. // echo model('ChannelWithdraw')->getLastSql()."-----lastsql------<br>";
  86. $this->jsonResult($withdrawList, 20000, '获取列表成功');
  87. }
  88. /**
  89. * 提现信息展示
  90. */
  91. public function withdraw()
  92. {
  93. if ($this->request->isPost()) {
  94. //判断当前账号是否有充值权限
  95. if(!in_array($this->_channelLevel,[1])){
  96. $this->jsonResult('', 0, '您无权限进行提现相关操作');
  97. }
  98. else{
  99. $channelInfo = model("Channel")->field("id,name,level,parent_id,js_amount")->where(['id'=>$this->_channelId])->find();
  100. if(!$channelInfo){
  101. $this->jsonResult('', 0, '账号异常,请重新登录后再试');
  102. }
  103. $adminInfo = model("ChannelAdmin")->field("id,username,channel_id,pay_password,secondary_password")->where(['id'=>session('guild_info')['id'],'channel_id'=>$this->_channelId])->find();
  104. if(!$adminInfo){
  105. $this->jsonResult('', 0, '账号异常,请重新登录后再试');
  106. }
  107. else if(!$adminInfo['pay_password'] || !$adminInfo['secondary_password']){
  108. $this->jsonResult('', 0, '请设置交易密码和二级密码后才能进行提现');
  109. }
  110. $relChannelInfo = model("ChannelInfo")->where(['channel_id'=>$this->_channelId])->find();
  111. if(!$relChannelInfo){
  112. $this->jsonResult('', 0, '请先补充完提现相关信息后再进行提现');
  113. }
  114. $channelInfoApply = model("ChannelInfo")->field("id,channel_id,real_name,apply_status,type")->where(['channel_id'=>$this->_channelId,'apply_status'=>1])->find();
  115. if(empty($channelInfoApply)){
  116. $this->jsonResult('', 0, '您的身份尚未认证通过,不能进行提现');
  117. }
  118. $this->jsonResult($channelInfo, 20000, '获取提现信息成功');
  119. }
  120. exit;
  121. }
  122. else{
  123. $this->jsonResult('', 0, '非法请求');
  124. exit;
  125. }
  126. }
  127. /**
  128. * 提现处理
  129. */
  130. public function doWithdraw()
  131. {
  132. if ($this->request->isPost()) {
  133. $amount = $this->input('amount',0,'floatval'); //提现金额
  134. $pay_password = $this->input('pay_password','','trim'); //交易密码
  135. $remark = $this->input('remark','','trim'); //备注
  136. $orderid = 'W'.makeOrderid();
  137. //判断当前账号是否有充值权限
  138. if(!in_array($this->_channelLevel,[1])){
  139. $this->jsonResult('', 0, '您无权限进行提现操作');
  140. }
  141. //外放的无法结算
  142. if (isset(session('guild_info')['roles'][0]) && session('guild_info')['roles'][0] == 4) {
  143. $this->jsonResult('', 0, '您无权限使用该功能');
  144. }
  145. if(!$amount){
  146. $this->jsonResult('', 0, '请填写提现金额!');
  147. }
  148. else if(abs(intval($amount*100)/100-$amount) > 0.01){
  149. $this->jsonResult('', 0, '请正确填写提现金额!');
  150. }
  151. else if($amount < 100){
  152. $this->jsonResult('', 0, '提现金额不能小于100元!');
  153. }
  154. $channelInfo = model("Channel")->field("id,name,level,parent_id,js_amount")->where(['id'=>$this->_channelId])->find();
  155. if(!$channelInfo){
  156. $this->jsonResult('', 0, '账号异常,请重新登录后再试');
  157. }
  158. else if($channelInfo['js_amount']<$amount){
  159. $this->jsonResult('', 0, '您的结算余额不足');
  160. }
  161. $relChannelInfo = model("ChannelInfo")->where(['channel_id'=>$this->_channelId])->find();
  162. if(!$relChannelInfo){
  163. $this->jsonResult('', 0, '请先补充完提现相关信息后再进行提现');
  164. }
  165. $channelInfoApply = model("ChannelInfo")->field("id,channel_id,real_name,apply_status,type,person_id,zfb_account,bank_ban_mobile,bank_number,bank_province_city,bank_name,bank_open_name,bank_open_number")->where(['channel_id'=>$this->_channelId,'apply_status'=>1])->find();
  166. if(empty($channelInfoApply)){
  167. $this->jsonResult('', 0, '您的身份尚未认证通过,不能进行提现');
  168. }
  169. // if(!(date("w")==1 && (date('Hi')>='0200' && date('Hi')<'2359') || date("w")==2 && (date('Hi')>='0200' && date('Hi')<'1200'))){
  170. // $this->jsonResult('', 0, '不在提现时间内,请在每周一02:00~23:59 或 每周二02:00~12:00 内提交!');
  171. // }
  172. //指定时间内,禁止重复下单
  173. if(!requestDuplicateCheck('channel_withdraw_duplicate_'.$this->_channelId,$this->payRequestLimit)){
  174. $this->jsonResult('', 0, '提现请求过多,请于'.$this->payRequestLimit.'s以后,再次进行提现操作');
  175. }
  176. $adminInfo = model("ChannelAdmin")->field("id,username,channel_id,pay_password,secondary_password")->where(['id'=>session('guild_info')['id'],'channel_id'=>$this->_channelId])->find();
  177. if(!$adminInfo){
  178. $this->jsonResult('', 0, '账号异常,请重新登录后再试');
  179. }
  180. else if(!$adminInfo['pay_password'] || !$adminInfo['secondary_password']){
  181. $this->jsonResult('', 0, '请设置交易密码和二级密码后才能进行提现');
  182. }
  183. else if($adminInfo['pay_password'] != mg_password($pay_password)){
  184. $this->jsonResult('', 0, '交易密码错误!');
  185. }
  186. $withdrawData = array();
  187. $withdrawData['orderid'] = $orderid;
  188. $withdrawData['channel_id'] = $this->_channelId;
  189. $withdrawData['channel_name'] = $this->_channelName;
  190. $withdrawData['withdraw_amt'] = $amount;
  191. $withdrawData['remark'] = $remark;
  192. $withdrawData['status'] = 0;
  193. $withdrawData['create_time'] = time();
  194. $withdrawData['real_name'] = $channelInfoApply['real_name'];
  195. $withdrawData['person_id'] = $channelInfoApply['person_id'];
  196. $withdrawData['zfb_account'] = $channelInfoApply['zfb_account'];
  197. $withdrawData['bank_ban_mobile']= $channelInfoApply['bank_ban_mobile'];
  198. $withdrawData['bank_number'] = $channelInfoApply['bank_number'];
  199. $withdrawData['bank_province_city'] = $channelInfoApply['bank_province_city'];
  200. $withdrawData['bank_name'] = $channelInfoApply['bank_name'];
  201. $withdrawData['bank_open_name'] = $channelInfoApply['bank_open_name'];
  202. $withdrawData['bank_open_number'] = $channelInfoApply['bank_open_number'];
  203. // 启动事务
  204. Db::startTrans();
  205. try{
  206. $result = ''; //返回值
  207. $insertWithdrawId = model("ChannelWithdraw")->insert($withdrawData);
  208. if(!$insertWithdrawId){
  209. throw new Exception("创建提现订单失败! ");
  210. }
  211. $detData = array();
  212. $detData['channel_id'] = $this->_channelId;
  213. $detData['channel_name'] = $this->_channelName;
  214. $detData['change_amount'] = -$amount;
  215. $detData['account_type'] = 2; //结算账户
  216. $detData['type'] = 9; //提现
  217. $detData['out_orderid'] = $orderid;
  218. $detData['create_time'] = NOW_TIMESTAMP;
  219. $insertDetId = model('ChannelAccountDet')->insertGetId($detData);
  220. if ( !$insertDetId) {
  221. throw new Exception("添加账户变动明细失败");
  222. }
  223. $updData = array();
  224. $updData['js_amount'] = Db::raw("js_amount-".$amount);
  225. $updData['update_time'] = time();
  226. $updResult = model('Channel')->where(['id'=>$this->_channelId,'js_amount'=>array('egt',$amount)])->update($updData);
  227. if (!$updResult) {
  228. throw new Exception("账户金额变动失败");
  229. }
  230. $this->redis->del('channel_withdraw_duplicate_'.$this->_channelId);
  231. // 提交事务
  232. Db::commit();
  233. } catch (\Exception $e) {
  234. // 回滚事务
  235. Db::rollback();
  236. $this->jsonResult('', 0, '提现订单生成失败'.$e->getMessage());
  237. }
  238. $result['orderid'] = $orderid;
  239. $template = '有新的提现申请:会长"'.$this->_channelName.'" 发起了提现申请,订单号:'.$orderid.' 。请及时安排处理,时间:'.date('Y-m-d H:i:s');
  240. $ddurl = Env::get('operat_url');
  241. curlDD($template, $ddurl,true);
  242. $this->jsonResult($result, 20000, '提现订单生成成功');
  243. exit;
  244. }
  245. else{
  246. $this->jsonResult('', 0, '非法请求');
  247. exit;
  248. }
  249. }
  250. }