CoinTransfer.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /**
  3. * 转账接口控制器
  4. *
  5. */
  6. namespace app\guildapi\controller;
  7. use app\guildapi\controller\Guild;
  8. use app\common\model\CoinTransfer as CoinTransferModel;
  9. use app\common\library\WeixinPay;
  10. use think\Db;
  11. use think\Config;
  12. use app\common\model\Setting;
  13. use think\Exception;
  14. class CoinTransfer 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,2])){
  35. $this->jsonResult('', 0, '您无权限使用该功能');
  36. }
  37. $list_rows = $this->input('list_rows',10);
  38. $page = $this->input('page',1);
  39. $type = $this->input('type','0','intval'); //1(转账收入),2(转账支出)
  40. $account = $this->input('account','','trim'); //收入账号
  41. $apply_begin_time = $this->input('apply_begin_time','','trim');
  42. $apply_end_time = $this->input('apply_end_time','','trim');
  43. $finish_begin_time = $this->input('finish_begin_time','','trim');
  44. $finish_end_time = $this->input('finish_end_time','','trim');
  45. $condition = [];
  46. if($account){
  47. $condition['det_username'] = $account;
  48. }
  49. if($type==1){
  50. $condition['det_userid'] = $this->_channelId;
  51. }
  52. else if($type==2){
  53. $condition['src_channel_id'] = $this->_channelId;
  54. }
  55. //申请开始时间和结束时间不为空时
  56. if ($apply_begin_time != '' && $apply_end_time != '') {
  57. $condition['create_time'] = [
  58. ['>=', strtotime($apply_begin_time)],
  59. ['<=', strtotime($apply_end_time . ' 23:59:59')],
  60. ];
  61. } //开始时间不为空时
  62. elseif ($apply_begin_time != '') {
  63. $condition['create_time'] = ['>=', strtotime($apply_begin_time)];
  64. } //结束时间不为空时
  65. elseif ($apply_end_time != '') {
  66. $condition['create_time'] = ['<=', strtotime($apply_end_time . ' 23:59:59')];
  67. }
  68. //到账开始时间和结束时间不为空时
  69. if ($finish_begin_time != '' && $finish_end_time != '') {
  70. $condition['create_time'] = [
  71. ['>=', strtotime($finish_begin_time)],
  72. ['<=', strtotime($finish_end_time . ' 23:59:59')],
  73. ];
  74. } //开始时间不为空时
  75. elseif ($finish_begin_time != '') {
  76. $condition['create_time'] = ['>=', strtotime($finish_begin_time)];
  77. } //结束时间不为空时
  78. elseif ($finish_end_time != '') {
  79. $condition['create_time'] = ['<=', strtotime($finish_end_time . ' 23:59:59')];
  80. }
  81. // var_dump($condition);
  82. $rechargeList = model('CoinTransfer')
  83. ->where($condition)
  84. ->whereRaw('src_channel_id='.$this->_channelId.' or det_userid='.$this->_channelId.' and transfer_kind=1')
  85. ->paginate(['list_rows'=>$list_rows,'page'=>$page])
  86. ->toArray();
  87. // echo model('ChannelRecharge')->getLastSql()."-----lastsql------<br>";
  88. $this->jsonResult($rechargeList, 20000, '获取列表成功');
  89. }
  90. /**
  91. * 转账处理
  92. */
  93. public function transfer()
  94. {
  95. if ($this->request->isPost()) {
  96. $account = $this->input('account','','trim'); //转账账号
  97. $amount = $this->input('amount',0,'intval'); //转账金额
  98. $type = $this->input('type','','trim'); //转账方式:single(单个转账),batch(批量转账)
  99. $transfer_kind = $this->input('transfer_kind','','trim'); //收款对象:1(转给玩家),2(转给渠道)
  100. $game_id = $this->input('game_id',0,'intval'); //游戏ID
  101. $server_id = $this->input('server_id','','trim'); //区服ID
  102. $server_name = $this->input('server_name','','trim'); //区服名称
  103. $remark = $this->input('remark','','trim'); //备注
  104. $orderid = 'T'.makeOrderid();
  105. $batchNo = 'Batch-'.$this->_channelId.'-'.date('YmdHis');
  106. //判断当前账号是否有充值权限
  107. if(!in_array($this->_channelLevel,[1,2])){
  108. $this->jsonResult('', 0, '您无权限进行转账操作');
  109. }
  110. if(!in_array($type,['single','batch'])){
  111. $this->jsonResult('', 0, '请正确选择转账方式');
  112. }
  113. else{
  114. if(!in_array($transfer_kind,[1,2])){
  115. $this->jsonResult('', 0, '请选择收款对象');
  116. }
  117. }
  118. if($type=='single'){ //单个转账
  119. if(!$account){
  120. $this->jsonResult('', 0, '请输入收款账号');
  121. }
  122. if($transfer_kind==1){ //转给玩家
  123. if(!$game_id){
  124. $this->jsonResult('', 0, '请选择要充值的游戏');
  125. }
  126. else{
  127. $gameInfo = model('Game')->where(['id'=>$game_id])->find();
  128. if(!$gameInfo){
  129. $this->jsonResult('', 0, '该游戏不存在');
  130. }
  131. else{ //判断能否对该玩家进行充值
  132. $chkHasPriv = chkTransferPlayer($this->_channelId,$account);
  133. }
  134. }
  135. } else{ //转给渠道
  136. $chkHasPriv = chkTransferChannel($this->_channelId,$account);
  137. }
  138. } else { //批量转账
  139. }
  140. if(abs(intval($amount*100)/100-$amount) > 0.01){
  141. $this->jsonResult('', 0, '请正确填写支付金额!');
  142. } else {
  143. //判断支付金额
  144. $condis = array();
  145. $condis['channel_id'] = $this->_channelId;
  146. $condis['begin_time'] = array('elt',$this->nowTime);
  147. $condis['end_time'] = array('egt',$this->nowTime);
  148. $rechargeRatio = model("ChannelRechargeRatio")->where($condis)->find();
  149. if(!empty($rechargeRatio) && $rechargeRatio['ratio']){
  150. $rechargeRatio = $rechargeRatio['ratio'];
  151. }
  152. else{
  153. $rechargeRatio = 100;
  154. }
  155. $discountAmt = floatval($amount*$rechargeRatio/100);
  156. if($discountAmt <> $amount){
  157. $this->jsonResult('', 0, '充值金额异常,请刷新页面后重新充值');
  158. }
  159. }
  160. $channelInfo = model("Channel")->where(['id'=>$this->_channelId])->find();
  161. $recharge_type = 3; // 结算币充值
  162. if($channelInfo && $channelInfo['parent_id']){
  163. if($recharge_type==3 && $channelInfo['js_amount']<$amount){
  164. $this->jsonResult('', 0, '您的结算币余额不足');
  165. }
  166. }
  167. else{
  168. $this->jsonResult('', 0, '您的账号有异常,请联系管理员');
  169. }
  170. //指定时间内,禁止重复下单
  171. if(!requestDuplicateCheck('recharge_pay_duplicate_'.$this->_channelId,$this->payRequestLimit)){
  172. $this->jsonResult('', 0, '充值请求过多,请于'.$this->payRequestLimit.'s以后,再次进行充值操作');
  173. }
  174. $rechargeData = array();
  175. $rechargeData['orderid'] = $orderid;
  176. $rechargeData['channel_id'] = $this->_channelId;
  177. $rechargeData['channel_name'] = $this->_channelName;
  178. if($this->_channelLevel==1){
  179. $rechargeData['send_channel_id'] = '0';
  180. $rechargeData['send_channel_name'] = '平台';
  181. } else if($this->_channelLevel==2) {
  182. $parentChannelInfo = model("Channel")->where(['id'=>$channelInfo['parent_id']])->find();
  183. $rechargeData['send_channel_id'] = $parentChannelInfo['id'];
  184. $rechargeData['send_channel_name'] = $parentChannelInfo['name'];
  185. }
  186. $paytype = 1;
  187. $rechargeData['amount'] = $amount;
  188. $rechargeData['real_amount'] = $amount;
  189. $rechargeData['recharge_type'] = $recharge_type;
  190. $rechargeData['paytype'] = $paytype;
  191. $rechargeData['out_order_no'] = $batchNo;
  192. $rechargeData['remark'] = $remark;
  193. $rechargeData['status'] = 0;
  194. $rechargeData['create_time'] = time();
  195. // 启动事务
  196. Db::startTrans();
  197. try{
  198. $result = []; //返回值
  199. //结算币充值
  200. if($recharge_type==3){
  201. $rechargeData['status'] = 1;
  202. $insertRechargeId = model("ChannelRecharge")->insert($rechargeData);
  203. if(!$insertRechargeId){
  204. throw new Exception("创建订单失败! ");
  205. }
  206. $detData = array();
  207. $detData['channel_id'] = $this->_channelId;
  208. $detData['channel_name'] = $this->_channelName;
  209. $detData['change_amount'] = -$amount;
  210. $detData['account_type'] = 2; //结算账户
  211. $detData['type'] = 7; //结算充值
  212. $detData['out_orderid'] = $orderid;
  213. $detData['create_time'] = NOW_TIMESTAMP;
  214. $insertDetId = model('ChannelAccountDet')->insertGetId($detData);
  215. if ( !$insertDetId) {
  216. throw new Exception("添加账户变动明细失败");
  217. }
  218. $updData = array();
  219. $updData['js_amount'] = Db::raw("js_amount-".$amount);
  220. $updData['update_time'] = time();
  221. $updResult = model('Channel')->where(['id'=>$this->_channelId,'js_amount'=>array('egt',$amount)])->update($updData);
  222. if (!$updResult) {
  223. throw new Exception("账户金额变动失败");
  224. }
  225. $detData = array();
  226. $detData['channel_id'] = $this->_channelId;
  227. $detData['channel_name'] = $this->_channelName;
  228. $detData['change_amount'] = $amount;
  229. $detData['account_type'] = 1; //通用账户
  230. $detData['type'] = 4; //充值收入
  231. $detData['out_orderid'] = $orderid;
  232. $detData['create_time'] = NOW_TIMESTAMP;
  233. $insertDetId = model('ChannelAccountDet')->insertGetId($detData);
  234. if ( !$insertDetId) {
  235. throw new Exception("添加账户变动明细失败");
  236. }
  237. $updData = array();
  238. $updData['amount'] = Db::raw("amount+".$amount);
  239. $updData['update_time'] = time();
  240. $updResult = model('Channel')->where(['id'=>$this->_channelId])->update($updData);
  241. if (!$updResult) {
  242. throw new Exception("账户金额变动失败");
  243. }
  244. }
  245. else{
  246. $insertRechargeId = model("ChannelRecharge")->insert($rechargeData);
  247. if(!$insertRechargeId){
  248. throw new Exception("创建订单失败! ");
  249. }
  250. }
  251. $this->redis->del('recharge_pay_duplicate_'.$this->_channelId);
  252. // 提交事务
  253. Db::commit();
  254. } catch (\Exception $e) {
  255. // 回滚事务
  256. Db::rollback();
  257. $this->jsonResult('', 0, '订单生成失败'.$e->getMessage());
  258. }
  259. $result['orderid'] = $orderid;
  260. $this->jsonResult($result, 20000, '订单生成成功');
  261. exit;
  262. } else {
  263. $this->jsonResult('', 0, '非法请求');
  264. exit;
  265. }
  266. }
  267. }