ChannelWithdraw.php.bak 11 KB

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