Coin.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /**
  3. * 官网的公共控制器类
  4. *
  5. */
  6. namespace app\mobile\controller;
  7. use app\common\logic\PayCallback;
  8. use app\common\model\GameServer;
  9. use app\common\model\MemberCoinPay;
  10. use app\common\model\MemberGameServer;
  11. use app\common\model\Members;
  12. use app\common\model\PayType;
  13. use app\common\model\Subaccount;
  14. use app\service\GamePayService;
  15. use app\service\MemberCoinService;
  16. use app\common\model\Game;
  17. use app\common\logic\Pay as PayLogic;
  18. use think\Cache;
  19. use think\Env;
  20. use think\Exception;
  21. class Coin extends Mobile
  22. {
  23. //v1.game_pay_type
  24. /**
  25. * 获取支付方式
  26. */
  27. public function getPayType()
  28. {
  29. $denyPayType = [
  30. 'wxsmzf', 'zfbsmzf',
  31. ];
  32. //wx-wap
  33. //xzzfbzf 新支付宝
  34. //old-zfb-wap 旧支付宝
  35. $payTypeModel = new PayType();
  36. $arrPayType = $payTypeModel->field('paytype,payname')
  37. ->where(['paytype' => ['in', $denyPayType]])
  38. ->select();
  39. $this->result($arrPayType, 1, '成功', 'json');
  40. }
  41. //v1.pay
  42. public function pay()
  43. {
  44. $data['type'] = input('type'); //类型 1充值平台币 2 充值游戏
  45. $data['paytype'] = input('paytype'); //支付类型
  46. $data['amount'] = input('amount'); //支付金额
  47. $data['accountType'] = input('accountType'); //账号类型1当前账号2其他账号
  48. $data['username'] = input('username');
  49. $data['gameid'] = input('gameid');
  50. $data['serverid'] = input('serverid');
  51. $data['roleid'] = input('roleid');
  52. $data['ip'] = request()->ip();
  53. $checkResult = $this->validate($data,
  54. [
  55. ['type', 'require|integer|in:1,2', '类型不能为空|必须为整型'],
  56. ['paytype', 'require', '支付类型不能为空'],
  57. ['amount', 'require|integer|egt:0', '支付金额不能为空|支付金额必须为整型|支付金额大于等于0的整型'],
  58. ['accountType', 'require|in:1,2', '账号类型不能为空|账号类型错误'],
  59. ['username', 'requireIf:accountType,2', '账号必填'],
  60. ['gameid', 'requireIf:type,2|integer', '游戏必填|游戏必须为整型'],
  61. ['serverid', 'requireIf:type,2', '区服必填'],
  62. ['roleid', 'requireIf:type,2', '角色必填'],
  63. ['coin_amt', 'float|egt:0', '金额必须为数字|支付金额大于等于0'],
  64. ]);
  65. if (true !== $checkResult) {
  66. $this->result('', 0, $checkResult, 'json');
  67. }
  68. if ($data['accountType'] == 2) {
  69. if (!$user = (new Members())->getUseridByName($data)) {
  70. $this->result([], 0, '账号不存在');
  71. }
  72. $data['member_id'] = $user['id'];
  73. } else {
  74. $data['member_id'] = session('front_userid');
  75. if(!session('front_userid')){
  76. $this->result([], 0, '未登录请先登录');
  77. }
  78. if (!$user = (new Members())->getUseridById($data)) {
  79. $this->result([], 0, '账号不存在');
  80. }
  81. }
  82. if(!in_array($data['paytype'],['old-zfb-wap','kdh5','coinpay','coin-old-zfb-wap','coin-kdh5'])){
  83. $this->jsonResult('', 0, '支付类型错误');
  84. }
  85. if (in_array($data['paytype'], ['coinpay','coin-old-zfb-wap','coin-kdh5']) && $data['accountType'] != 1) {
  86. $this->jsonResult('', 0, '支付方式错误');
  87. }
  88. if (session('front_userid')) {
  89. $data['place_user_id'] = session('front_userid');
  90. } else {
  91. $data['place_user_id'] = 0;
  92. }
  93. if ($data['type'] == 1) {//充值平台币
  94. $result = (new MemberCoinService())->payCoin($data);
  95. } else {
  96. // $data['gameid'] = $this->input('gameid'); //游戏ID
  97. // $data['member_id'] = $this->input('userid'); //用户ID
  98. // $data['serverid'] = $this->input('serverid'); //服务器ID
  99. // $data['servername'] = $this->input('servername'); //区服名称
  100. // $data['amount'] = $this->input('amount'); //充值金额
  101. // $data['roleid'] = $this->input('roleid'); //角色ID
  102. $data['attach'] = makeOrderid('CP'); //上一级的扩展参数,多数是订单编号[麻花网络自己聚合自己时,会加个aoyou###前缀]
  103. $data['ptb_amt'] = 0; //平台币使用金额
  104. $data['imeil'] = 'pc'; //手机imeil码
  105. $data['productname'] = '游戏充值'; //消费的商品名称
  106. // $data['paytype'] = $this->input('paytype'); //支付类型
  107. // $data['rolename'] = $this->input('rolename', ''); //角色名
  108. // $data['rolelevel'] = $this->input('rolelevel', ''); //角色角色等级
  109. // $data['channel_id'] = $user['channel_id'];
  110. // $data['sub_username'] = $this->input('sub_username');
  111. if($data['accountType'] == 2 || $data['type'] == 1){
  112. $data['coin_amt'] = 0;
  113. }else{
  114. $data['coin_amt'] = input('coin_amt'); //平台币使用金额
  115. }
  116. $result = (new GamePayService())->gamePay($data,2);
  117. if ($result['code'] == 1) {
  118. $this->jsonResult($result['data'], 1, '订单生成成功', false);
  119. } else {
  120. $this->jsonResult('', 0, $result['msg']);
  121. }
  122. //全部平台币支付,并且支付成功时
  123. if ($result['code'] == 1 && (isset($result['data']['pay_status'])&& $result['data']['pay_status'] == 1)) {
  124. // 通知CP,支付成功
  125. (new PayCallback())->callBackToCp($result['data']['orderid']);
  126. (new PayLogic())->payWarning($data['member_id'], $data['channel_id'], $data['gameid'], $data['amount']);
  127. }
  128. }
  129. if ($result['code'] == 1) {
  130. $this->jsonResult($result['data'], 1, (isset($result['data']['pay_status'])&&$result['data']['pay_status'] == 1) ? '付款成功' : '订单生成成功', false);
  131. } else {
  132. $this->jsonResult('', 0, $result['msg']);
  133. }
  134. }
  135. /**
  136. * 获取可支付游戏
  137. */
  138. public function getGameList()
  139. {
  140. $this->result((new Game())->getGameListPay(), 1, '成功');
  141. }
  142. public function getServerList()
  143. {
  144. $data['gameid'] = input('gameid'); //游戏id
  145. $checkResult = $this->validate($data,
  146. [
  147. ['gameid', 'require|integer|gt:0', '游戏id不能为空|游戏id必须为整型|游戏id大于0的整型'],
  148. ]);
  149. if (true !== $checkResult) {
  150. $this->result('', 0, $checkResult, 'json');
  151. }
  152. $this->result((new GameServer())->getServerList($data), 1, '成功');
  153. }
  154. public function getRoleList()
  155. {
  156. $data['gameid'] = input('gameid'); //游戏id
  157. $data['serverid'] = input('serverid'); //游戏id
  158. $data['accountType'] = input('accountType'); //账号类型1当前账号2其他账号
  159. $data['username'] = input('username');
  160. $checkResult = $this->validate($data,
  161. [
  162. ['gameid', 'require|integer|gt:0', '游戏id不能为空|游戏id必须为整型|游戏id大于0的整型'],
  163. ['serverid', 'require|integer|gt:0', '区服id不能为空|区服id必须为整型区服id大于0的整型'],
  164. ['accountType', 'require|in:1,2', '账号类型不能为空|账号类型错误'],
  165. ['username', 'requireIf:accountType,2', '账号必填']
  166. ]);
  167. if (true !== $checkResult) {
  168. $this->result('', 0, $checkResult, 'json');
  169. }
  170. if ($data['accountType'] == 2) {
  171. if (!$user = (new Members())->getUseridByName($data)) {
  172. $this->result([], 0, '账号不存在');
  173. }
  174. $data['userid'] = $user['id'];
  175. } else {
  176. $data['userid'] = session('front_userid');
  177. }
  178. $this->result((new MemberGameServer())->getRoleList($data), 1, '成功');
  179. }
  180. public function verifyUserName()
  181. {
  182. $data['username'] = input('username'); //游戏id
  183. $checkResult = $this->validate($data,
  184. [
  185. ['username', 'require', '账号不能为空'],
  186. ]);
  187. if (true !== $checkResult) {
  188. $this->result('', 0, $checkResult, 'json');
  189. }
  190. $user = (new Members())->getUserName($data);
  191. if ($user) {
  192. $this->result($user, 1, '成功');
  193. } else {
  194. $this->result([], 0, '账号不存在');
  195. }
  196. }
  197. public function payHtml()
  198. {
  199. $data['data'] = input('data');
  200. $checkResult = $this->validate($data,
  201. [
  202. ['data', 'require', '参数不能为空'],
  203. ]);
  204. if (true !== $checkResult) {
  205. $this->result('', 0, $checkResult, 'json');
  206. }
  207. if (!$data = json_decode(opensslDecrypt($data['data']), true)) {
  208. $this->result('', 0, '订单不存在', 'json');
  209. }
  210. $this->assign('data', $data);
  211. return $this->fetch();
  212. }
  213. public function recharge()
  214. {
  215. $token = input('token');
  216. $token = str_ireplace(" ", "+", $token);
  217. if($token){
  218. //聚合渠道时,$sub_username对应的值是mg_username
  219. try {
  220. list($userid2, $username2, $gameid2,$sub_username,$token_random,$type) = explode('|', auth_code($token,"DECODE",Env::get('auth_key')));
  221. // var_dump($userid2, $username2, $gameid2,$sub_username,$token_random,$type);die();
  222. $redisTokenRandom = Cache::store('default')->get('token|'.$type.'|'.$userid2.'|'.$gameid2);
  223. $this->assign('username2', $username2);
  224. }catch (Exception $e){
  225. $username2 = '';
  226. }
  227. }else{
  228. $username2 = '';
  229. }
  230. $this->assign('username2',$username2 );
  231. return $this->fetch();
  232. }
  233. public function getCoin(){
  234. if (session('front_userid')){
  235. model('MemberCoinInfo')->releaseCoin(session('front_userid'));
  236. $userinfo = model('members')->field('id,amount')->where('id',session('front_userid'))->find();
  237. $this->jsonResult($userinfo, 1,'获取成功');
  238. }else{
  239. $this->jsonResult(['id'=>'','amount'=>'0'], 1,'获取成功');
  240. }
  241. }
  242. }