ChannelWithdraw.php 11 KB

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