Coin.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. * 官网的公共控制器类
  4. *
  5. */
  6. namespace app\home\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 Home
  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 (!in_array($data['paytype'], ['zfbsmzf', 'kdsm', 'coinpay', 'coin-zfbsmzf', 'coin-kdsm'])) {
  69. $this->jsonResult('', 0, '支付类型错误');
  70. }
  71. if (in_array($data['paytype'], ['coinpay', 'coin-zfbsmzf', 'coin-kdsm']) && $data['accountType'] != 1) {
  72. $this->jsonResult('', 0, '支付方式错误');
  73. }
  74. if ($data['accountType'] == 2) {
  75. if (!$user = (new Members())->getUseridByName($data)) {
  76. $this->result([], 0, '账号不存在');
  77. }
  78. $data['member_id'] = $user['id'];
  79. } else {
  80. if(!session('front_userid')){
  81. $this->result([], 0, '未登录请先登录');
  82. }
  83. $data['member_id'] = session('front_userid');
  84. if (!$user = (new Members())->getUseridById($data)) {
  85. $this->result([], 0, '账号不存在');
  86. }
  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. $data['coin_amt'] = 0;
  95. $result = (new MemberCoinService())->payCoin($data);
  96. } else {
  97. // $data['gameid'] = $this->input('gameid'); //游戏ID
  98. // $data['member_id'] = $this->input('userid'); //用户ID
  99. // $data['serverid'] = $this->input('serverid'); //服务器ID
  100. // $data['servername'] = $this->input('servername'); //区服名称
  101. // $data['amount'] = $this->input('amount'); //充值金额
  102. // $data['roleid'] = $this->input('roleid'); //角色ID
  103. $data['attach'] = makeOrderid('CP'); //上一级的扩展参数,多数是订单编号[祈盟文化自己聚合自己时,会加个aoyou###前缀]
  104. $data['ptb_amt'] = 0; //专属币使用金额
  105. $data['imeil'] = 'pc'; //手机imeil码
  106. $data['productname'] = '游戏充值'; //消费的商品名称
  107. // $data['paytype'] = $this->input('paytype'); //支付类型
  108. // $data['rolename'] = $this->input('rolename', ''); //角色名
  109. // $data['rolelevel'] = $this->input('rolelevel', ''); //角色角色等级
  110. // $data['channel_id'] = $user['channel_id'];
  111. // $data['sub_username'] = $this->input('sub_username');
  112. if($data['accountType'] == 2 || $data['type'] == 1){
  113. $data['coin_amt'] = 0;
  114. }else{
  115. $data['coin_amt'] = input('coin_amt'); //平台币使用金额
  116. }
  117. $result = (new GamePayService())->gamePay($data,2);
  118. //全部平台币支付,并且支付成功时
  119. if ($result['code'] == 1 && (isset($result['data']['pay_status'])&& $result['data']['pay_status'] == 1)) {
  120. // 通知CP,支付成功
  121. (new PayCallback())->callBackToCp($result['data']['orderid']);
  122. (new PayLogic())->payWarning($data['member_id'], $result['data']['channel_id'], $data['gameid'], $data['amount']);
  123. }
  124. }
  125. if ($result['code'] == 1) {
  126. $this->jsonResult($result['data'], 1, (isset($result['data']['pay_status'])&&$result['data']['pay_status'] == 1) ? '付款成功' : '订单生成成功', false);
  127. } else {
  128. $this->jsonResult('', 0, $result['msg']);
  129. }
  130. }
  131. /**
  132. * 获取可支付游戏
  133. */
  134. public function getGameList()
  135. {
  136. $this->result((new Game())->getGameListPay(), 1, '成功');
  137. }
  138. public function getServerList()
  139. {
  140. $data['gameid'] = input('gameid'); //游戏id
  141. $checkResult = $this->validate($data,
  142. [
  143. ['gameid', 'require|integer|gt:0', '游戏id不能为空|游戏id必须为整型|游戏id大于0的整型'],
  144. ]);
  145. if (true !== $checkResult) {
  146. $this->result('', 0, $checkResult, 'json');
  147. }
  148. $this->result((new GameServer())->getServerList($data), 1, '成功');
  149. }
  150. public function getRoleList()
  151. {
  152. $data['gameid'] = input('gameid'); //游戏id
  153. $data['serverid'] = input('serverid'); //游戏id
  154. $data['accountType'] = input('accountType'); //账号类型1当前账号2其他账号
  155. $data['username'] = input('username');
  156. $checkResult = $this->validate($data,
  157. [
  158. ['gameid', 'require|integer|gt:0', '游戏id不能为空|游戏id必须为整型|游戏id大于0的整型'],
  159. ['serverid', 'require|integer|gt:0', '区服id不能为空|区服id必须为整型区服id大于0的整型'],
  160. ['accountType', 'require|in:1,2', '账号类型不能为空|账号类型错误'],
  161. ['username', 'requireIf:accountType,2', '账号必填']
  162. ]);
  163. if (true !== $checkResult) {
  164. $this->result('', 0, $checkResult, 'json');
  165. }
  166. if ($data['accountType'] == 2) {
  167. if (!$user = (new Members())->getUseridByName($data)) {
  168. $this->result([], 0, '账号不存在');
  169. }
  170. $data['userid'] = $user['id'];
  171. } else {
  172. $data['userid'] = session('front_userid');
  173. }
  174. $this->result((new MemberGameServer())->getRoleList($data), 1, '成功');
  175. }
  176. public function verifyUserName()
  177. {
  178. $data['username'] = input('username'); //游戏id
  179. $checkResult = $this->validate($data,
  180. [
  181. ['username', 'require', '账号不能为空'],
  182. ]);
  183. if (true !== $checkResult) {
  184. $this->result('', 0, $checkResult, 'json');
  185. }
  186. $user = (new Members())->getUserName($data);
  187. if ($user) {
  188. $this->result($user, 1, '成功');
  189. } else {
  190. $this->result([], 0, '账号不存在');
  191. }
  192. }
  193. public function payHtml()
  194. {
  195. $data['data'] = input('data');
  196. // $data = [
  197. // 'real_amount' => 'xxx',
  198. // 'orderid' => 'D011001',
  199. // 'create_time' => '2024-03-09 12:00',
  200. // 'goods' => '商品名',
  201. // 'kdsm_param' => ['mweb_url' => 'https://dev.weixin.qq.com/docs/f'],
  202. // ];
  203. $checkResult = $this->validate($data,
  204. [
  205. ['data', 'require', '参数不能为空'],
  206. ]);
  207. if (true !== $checkResult) {
  208. $this->result('', 0, $checkResult, 'json');
  209. }
  210. if (!$data = json_decode(opensslDecrypt($data['data']), true)) {
  211. $this->result('', 0, '订单不存在', 'json');
  212. }
  213. $this->assign('data', $data);
  214. return $this->fetch();
  215. }
  216. public function recharge()
  217. {
  218. if (is_mobile_request()) {
  219. $url = MOBILE_SITE_DOMAIN . '/coin/recharge.html?' . http_build_query(input());
  220. header("Location: $url");
  221. exit();
  222. }
  223. $token = input('token');
  224. $token = str_ireplace(" ", "+", $token);
  225. if ($token) {
  226. try {
  227. //聚合渠道时,$sub_username对应的值是mg_username
  228. list($userid2, $username2, $gameid2, $sub_username, $token_random, $type) = explode('|', auth_code($token, "DECODE", Env::get('auth_key')));
  229. // var_dump($userid2, $username2, $gameid2,$sub_username,$token_random,$type);die();
  230. $redisTokenRandom = Cache::store('default')->get('token|' . $type . '|' . $userid2 . '|' . $gameid2);
  231. $this->assign('username2', $username2);
  232. } catch (Exception $e) {
  233. $username2 = '';
  234. }
  235. } else {
  236. $username2 = '';
  237. }
  238. $this->assign('username2', $username2);
  239. return $this->fetch();
  240. }
  241. public function getCoin(){
  242. if (session('front_userid')){
  243. model('MemberCoinInfo')->releaseCoin(session('front_userid'));
  244. $userinfo = model('members')->field('id,amount')->where('id',session('front_userid'))->find();
  245. $this->jsonResult($userinfo, 1,'获取成功');
  246. }else{
  247. $this->jsonResult(['id'=>'','amount'=>'0'], 1,'获取成功');
  248. }
  249. }
  250. }