Order.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. <?php
  2. namespace app\api\controller\v2;
  3. use app\api\controller\Api;
  4. use app\common\model\Members;
  5. use app\common\model\MembersTwo;
  6. use app\common\model\Game;
  7. use think\Cache;
  8. use think\Config;
  9. use think\Db;
  10. use think\Model;
  11. use app\common\model\PayType;
  12. use app\common\model\Payrule;
  13. use think\Request;
  14. class Order extends Api
  15. {
  16. private $status = [
  17. 0 => '待支付',
  18. 1 => '支付成功',
  19. 2 => '已取消',
  20. 3 => '超时取消',
  21. ];
  22. private $payType = [];
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->payType = config('order_pay_type');
  27. }
  28. /**
  29. * 订单列表
  30. * @throws \think\db\exception\DataNotFoundException
  31. * @throws \think\db\exception\ModelNotFoundException
  32. * @throws \think\exception\DbException
  33. */
  34. public function orderList()
  35. {
  36. $checkResult = $this->validate($this->input,
  37. [
  38. ['page', 'require|integer|egt:1', '页数不能为空|页数必须是整数|页数必须大于1'],
  39. ['pageSize', 'require|integer|egt:10', '页码不能为空|页码必须是整数|页码必须大于10'],
  40. ['month', 'dateFormat:Y-m', '日期格式错误'],
  41. ['type', 'require|integer|in:1,2', '类型不能为空|请输入类型|请输入类型'],
  42. ]);
  43. if (true !== $checkResult) {
  44. $this->jsonResult('', 0, $checkResult);
  45. }
  46. $data['page'] = $this->input['page'];
  47. $data['pageSize'] = $this->input['pageSize'];
  48. $data['month'] = $this->input['month'];
  49. $data['type'] = $this->input['type']; //1 游戏充值 2 平台币充值
  50. $where = [
  51. 'userid' => $this->input['userid'],
  52. ];
  53. if ($data['month']) {
  54. $where['a.create_time'] = ['between', [strtotime(getMonthRange($data['month'])), strtotime(getMonthRange($data['month'], false))]];
  55. }
  56. if ($data['type'] == 1) { //充值游戏
  57. $platform = model('GameInfo')->field('platform')->cache('order:orderlist:' . $this->input['gameid'], 180)->where(['game_id' => $this->input['gameid']])->find();
  58. if ($platform['platform'] == 0) {
  59. $gameBand = model('gameBand')->cache('order:orderlist:gameBand:' . $this->input['gameid'], 180)->where('android_game_id', $this->input['gameid'])->find();
  60. if ($gameBand) {
  61. $where['gameid'] = ['in', [$gameBand['android_game_id'], $gameBand['ios_game_id']]];
  62. } else {
  63. $where['gameid'] = $this->input['gameid'];
  64. }
  65. } else {
  66. $gameBand = model('gameBand')->cache('order:orderlist:gameBand:' . $this->input['gameid'], 180)->where('ios_game_id', $this->input['gameid'])->find();
  67. if ($gameBand) {
  68. $where['gameid'] = ['in', [$gameBand['android_game_id'], $gameBand['ios_game_id']]];
  69. } else {
  70. $where['gameid'] = $this->input['gameid'];
  71. }
  72. }
  73. $gameInfo = (new Game())->getGameInfoList();
  74. $list = model('pay')->alias('a')
  75. ->field('a.amount,a.gameid,a.create_time,a.status,a.orderid,a.paytype')
  76. ->where($where)
  77. ->where('a.create_time', '>', strtotime('-3 month'))
  78. ->order('a.id desc')
  79. ->paginate(['list_rows' => $data['pageSize'], 'page' => $data['page']])
  80. ->toArray();
  81. foreach ($list['data'] as $k => $v) {
  82. $list['data'][$k]['game_name'] = isset($gameInfo[$v['gameid']]['nickname']) ? $gameInfo[$v['gameid']]['nickname'] : (isset($gameInfo[$v['gameid']]['nname']) ? $gameInfo[$v['gameid']]['name'] : '');
  83. $list['data'][$k]['logo_img'] = isset($gameInfo[$v['gameid']]['mobileicon']) ? STATIC_DOMAIN . '/' . $gameInfo[$v['gameid']]['mobileicon'] : '';
  84. $list['data'][$k]['statusStr'] = isset($this->status[$v['status']]) ? $this->status[$v['status']] : '未知';
  85. $list['data'][$k]['payTypeStr'] = isset($this->payType[$v['paytype']]) ? $this->payType[$v['paytype']] : '未知';
  86. }
  87. } else { // 充值平台币
  88. $list = model('memberCoinPay')->alias('a')
  89. ->field('a.amount,a.create_time,a.status,a.orderid,a.paytype,a.place_user_id,a.userid')
  90. ->where($where)
  91. ->order('a.id desc')
  92. ->paginate(['list_rows' => $data['pageSize'], 'page' => $data['page']])
  93. ->toArray();
  94. foreach ($list['data'] as $k => $v) {
  95. $list['data'][$k]['gameid'] = 0;
  96. $list['data'][$k]['game_name'] = '平台币';
  97. $list['data'][$k]['logo_img'] = config('PTB_LOGO');
  98. $list['data'][$k]['statusStr'] = isset($this->status[$v['status']]) ? $this->status[$v['status']] : '未知';
  99. if ($v['place_user_id'] == $v['userid']) {
  100. $list['data'][$k]['payTypeStr'] = isset($this->payType[$v['paytype']]) ? $this->payType[$v['paytype']] : '未知';
  101. } else {
  102. $list['data'][$k]['payTypeStr'] = '未知';
  103. }
  104. unset($list['data'][$k]['place_user_id'], $list['data'][$k]['userid']);
  105. }
  106. }
  107. $this->jsonResult($list, 1, '发送成功');
  108. }
  109. /**
  110. * 订单详情
  111. * @throws \think\db\exception\DataNotFoundException
  112. * @throws \think\db\exception\ModelNotFoundException
  113. * @throws \think\exception\DbException
  114. */
  115. public function orderInfo()
  116. {
  117. $checkResult = $this->validate($this->input,
  118. [
  119. ['orderid', 'require', '订单号不能为空'],
  120. ['type', 'require|integer|in:1,2', '类型不能为空|请输入类型|请输入类型'],
  121. ]);
  122. if (true !== $checkResult) {
  123. $this->jsonResult('', 0, $checkResult);
  124. }
  125. $data['orderid'] = $this->input['orderid'];
  126. $data['type'] = $this->input['type']; //1 游戏充值 2 平台币充值
  127. $where = [
  128. 'a.userid' => $this->input['userid'],
  129. ];
  130. if ($data['type'] == 1) {
  131. $gameInfo = (new Game())->getGameInfoList();
  132. $info = model('pay')->alias('a')
  133. ->field('a.amount,a.gameid,a.create_time,a.status,a.orderid,a.paytype,b.status as cp_status')
  134. ->join('cy_paycpinfo b', 'a.orderid=b.orderid', 'left')
  135. ->where(['a.orderid' => $data['orderid']])
  136. ->where($where)
  137. ->find();
  138. if (!$info) {
  139. $this->jsonResult('', 0, '当前订单号不存在!');
  140. }
  141. $info['game_name'] = isset($gameInfo[$info['gameid']]['nickname']) ? $gameInfo[$info['gameid']]['nickname'] : (isset($gameInfo[$info['gameid']]['nname']) ? $gameInfo[$info['gameid']]['name'] : '');
  142. $info['logo_img'] = STATIC_DOMAIN . '/' . $gameInfo[$info['gameid']]['mobileicon'];
  143. $info['statusStr'] = isset($this->status[$info['status']]) ? $this->status[$info['status']] : '未知';
  144. $info['payTypeStr'] = isset($this->payType[$info['paytype']]) ? $this->payType[$info['paytype']] : '未知';
  145. $info['cpStatusStr'] = $info['cp_status'] == 1 ? '已发货' : '未发货';
  146. } else {
  147. $info = model('memberCoinPay')->alias('a')
  148. ->field('a.amount,a.create_time,a.status,a.orderid,a.paytype,a.place_user_id,a.userid')
  149. ->where(['a.orderid' => $data['orderid']])
  150. ->where($where)
  151. ->find();
  152. if (!$info) {
  153. $this->jsonResult('', 0, '当前订单号不存在!');
  154. }
  155. $info['gameid'] = 0;
  156. $info['game_name'] = '平台币';
  157. $info['logo_img'] = config('PTB_LOGO');
  158. $info['statusStr'] = isset($this->status[$info['status']]) ? $this->status[$info['status']] : '未知';
  159. if ($info['place_user_id'] == $info['userid']) {
  160. $info['payTypeStr'] = isset($this->payType[$info['paytype']]) ? $this->payType[$info['paytype']] : '未知';
  161. } else {
  162. $info['payTypeStr'] = '未知';
  163. }
  164. $info['cpStatusStr'] = $info['status'] == 1 ? '已发货' : '未发货';
  165. unset($info['place_user_id'], $info['userid']);
  166. }
  167. $this->jsonResult($info, 1, '发送成功');
  168. }
  169. /**
  170. * 取消订单
  171. * @throws \think\db\exception\DataNotFoundException
  172. * @throws \think\db\exception\ModelNotFoundException
  173. * @throws \think\exception\DbException
  174. */
  175. public function cancelOrder()
  176. {
  177. $checkResult = $this->validate($this->input,
  178. [
  179. ['orderid', 'require', '订单号不能为空'],
  180. ['type', 'require|integer|in:1,2', '类型不能为空|请输入类型|请输入类型'],
  181. ]);
  182. if (true !== $checkResult) {
  183. $this->jsonResult('', 0, $checkResult);
  184. }
  185. $data['orderid'] = $this->input['orderid'];
  186. $data['type'] = $this->input['type']; //1 游戏充值 2 平台币充值
  187. $where = [
  188. 'userid' => $this->input['userid'],
  189. ];
  190. // 适配老接口
  191. $result = [];
  192. if(version_compare($this->input['version'], 'v3.6.3') >= 0){
  193. $result = ['status' => 0];
  194. }
  195. if ($data['type'] == 1) {
  196. $info = model('pay')
  197. ->field('status,orderid,paytype,real_ptb,real_coin,coupon_amount,coupon_member_id')
  198. ->where(['orderid' => $data['orderid']])
  199. ->where($where)
  200. ->find();
  201. if(!isset($info)){
  202. $this->jsonResult($result, 0, '取消失败,当前订单不存在!');
  203. }
  204. if ($info['status'] === 0) {
  205. if (model('MemberCoinInfo')->releaseCoin($this->input['userid'], $data['orderid'])) {
  206. $result['status'] = 2;
  207. $this->jsonResult($result, 1, '取消成功');
  208. } else {
  209. $this->jsonResult($result, 0, '取消失败,请稍后再试');
  210. }
  211. } elseif ($info['status'] == 1) {
  212. $result['status'] = 1;
  213. $this->jsonResult($result, 1, '取消失败,当前订单已支付');
  214. } else {
  215. $this->jsonResult($result, 0, '取消失败,订单不是待支付状态');
  216. }
  217. } else {
  218. $info = model('memberCoinPay')
  219. ->field('orderid,paytype,status')
  220. ->where(['orderid' => $data['orderid']])
  221. ->where($where)
  222. ->find();
  223. if ($info['status'] == 0) {
  224. if (model('common/MemberCoinPay')
  225. ->where(['orderid' => $data['orderid']])
  226. ->where($where)->update(['status' => 2])) {
  227. $this->jsonResult([], 1, '取消成功');
  228. } else {
  229. $this->jsonResult([], 0, '取消失败');
  230. }
  231. } else {
  232. $this->jsonResult([], 0, '取消失败,订单不是待支付状态');
  233. }
  234. $this->jsonResult($info, 1, '发送成功');
  235. }
  236. }
  237. /**
  238. * 平台币明细
  239. */
  240. public function getMemberCoinList()
  241. {
  242. $checkResult = $this->validate($this->input,
  243. [
  244. ['page', 'require|integer|egt:1', '页数不能为空|页数必须是整数|页数必须大于1'],
  245. ['pageSize', 'require|integer|egt:10', '页码不能为空|页码必须是整数|页码必须大于10'],
  246. ['month', 'dateFormat:Y-m', '日期格式错误'],
  247. ['type', 'require|integer|in:0,1,2,3', '类型不能为空|请输入类型|请输入类型'],
  248. ]);
  249. if (true !== $checkResult) {
  250. $this->jsonResult('', 0, $checkResult);
  251. }
  252. $data['page'] = $this->input['page'];
  253. $data['pageSize'] = $this->input['pageSize'];
  254. $data['month'] = $this->input['month'];
  255. $data['type'] = $this->input['type']; //0 全部 类型1充值2消费3回退
  256. $where = [
  257. 'a.userid' => $this->input['userid'],
  258. ];
  259. if ($data['type'] > 0) {
  260. $where['a.type'] = ['in', [$data['type']]];
  261. }
  262. if ($data['month']) {
  263. $where['a.create_time'] = ['between', [strtotime(getMonthRange($data['month'])), strtotime(getMonthRange($data['month'], false))]];
  264. }
  265. $sql = Db::field('id,orderid,amount,type,"0" as gameid,create_time')
  266. ->table('cy_member_coin_info a')->where($where)->where(['a.type' => 3])
  267. ->union(function ($query) use ($where) {
  268. $query->field('a.id,a.orderid,a.amount,a.type,"0" as gameid,a.create_time')->table('cy_member_coin_info a')
  269. ->where($where)->where(['a.type' => 1]);
  270. })
  271. ->union(function ($query) use ($where) {
  272. $query->field('a.id,a.orderid,a.amount,a.type,b.gameid,a.create_time')->table('cy_member_coin_info a')
  273. ->join('cy_pay b', 'a.orderid=b.orderid')
  274. ->where($where)->where(['a.type' => 2]);
  275. })->buildSql();
  276. $list = Db::table($sql . ' a')->order('id desc')->paginate(['list_rows' => $this->input['pageSize'], 'page' => $this->input['page']])->toArray();
  277. $gameInfo = (new Game())->getGameInfoList();
  278. foreach ($list['data'] as $k => $v) {
  279. $list['data'][$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
  280. if ($v['type'] == 2) {
  281. $list['data'][$k]['game_name'] = isset($gameInfo[$v['gameid']]['name']) ? $gameInfo[$v['gameid']]['name'] : '';
  282. $list['data'][$k]['logo_img'] = isset($gameInfo[$v['gameid']]['mobileicon']) ? APK_DOWN_DOMAIN . '/' . $gameInfo[$v['gameid']]['mobileicon'] : '';
  283. $list['data'][$k]['typeStr'] = '消费';
  284. } else if ($v['type'] == 3) {
  285. $list['data'][$k]['orderid'] = str_ireplace('_T', '', $v['orderid']);
  286. $list['data'][$k]['game_name'] = '平台币';
  287. $list['data'][$k]['logo_img'] = config('PTB_LOGO');
  288. $list['data'][$k]['typeStr'] = '未消费释放';
  289. } else {
  290. $list['data'][$k]['game_name'] = '平台币';
  291. $list['data'][$k]['logo_img'] = config('PTB_LOGO');
  292. $list['data'][$k]['typeStr'] = '充值';
  293. }
  294. }
  295. $this->jsonResult($list, 1, '发送成功');
  296. }
  297. // 获取支付配置
  298. public function getPayInfo()
  299. {
  300. $list['pay_type'] = $this->payType();
  301. $list['member_coin'] = $this->getCoin();
  302. $list['member_zs_coin'] = $this->getZsCoin();
  303. $list['coupon'] = $this->getPayCoupon();
  304. $this->jsonResult($list, 1, '发送成功');
  305. }
  306. private function payType()
  307. {
  308. $gameModel = new Game;
  309. $payRuleModel = new Payrule;
  310. $payTypeModel = new PayType;
  311. $sdkModel = model('common/AndroidSdk');
  312. $game_id = $this->input('gameid'); //游戏ID
  313. $version = $this->input('version'); //sdk版本号 [v3.0.2 参数]
  314. $headerDevice = Request::instance()->header('device', 0);
  315. $handleQrVersion = trim(config('pay_qr_handle_version'));
  316. if($headerDevice == 4){
  317. $payType = [
  318. ["paytype" => "wx_h5", "payname" => "微信", "pay_scene" => "wx_h5", "sort" => 1],
  319. ["paytype" => "ali_h5", "payname" => "支付宝", "pay_scene" => "ali_h5", "sort" => 2],
  320. ["paytype" => "coinpay", "payname" => "平台币", "pay_scene" => "ptb", "sort" => 5],
  321. ];
  322. // if(version_compare(trim($version), $handleQrVersion, '>=')){
  323. // $payTypeNew = [
  324. // ["paytype" => "wx_h5", "payname" => "微信(扫码)", "pay_scene" => "wx_h5_qr", "sort" => 3],
  325. // ["paytype" => "ali_h5", "payname" => "支付宝(扫码)", "pay_scene" => "ali_h5_qr", "sort" => 4],
  326. // ];
  327. // $payType = array_merge($payType, $payTypeNew);
  328. // usort($payType, function($a, $b) {
  329. // return $a['sort'] <=> $b['sort'];
  330. // });
  331. // }
  332. return $payType;
  333. }
  334. $handleVersion = trim(config('pay_handle_version'));
  335. if (version_compare(trim($version), $handleVersion, '>')) {
  336. $payType = [
  337. ["paytype" => "ybzf_wxmp_h5", "payname" => "微信支付", "pay_scene" => "wx_h5", "sort" => 1],
  338. ["paytype" => "zfb", "payname" => "支付宝(快捷)", "pay_scene" => "ali_app", "sort" => 2],
  339. ["paytype" => "old-zfb-wap", "payname" => "支付宝", "pay_scene" => "ali_h5", "sort" => 5],
  340. ["paytype" => "coinpay", "payname" => "平台币", "pay_scene" => "ptb", "sort" => 6],
  341. ];
  342. if(version_compare(trim($version), $handleQrVersion, '>=') && $this->input('device') == 2){
  343. $payTypeNew = [
  344. ["paytype" => "ybzf_wxmp_h5", "payname" => "微信(扫码)", "pay_scene" => "wx_h5_qr", "sort" => 3],
  345. ["paytype" => "old-zfb-wap", "payname" => "支付宝(扫码)", "pay_scene" => "ali_h5_qr", "sort" => 4],
  346. ];
  347. $payType = array_merge($payType, $payTypeNew);
  348. usort($payType, function($a, $b) {
  349. return $a['sort'] <=> $b['sort'];
  350. });
  351. }
  352. return $payType;
  353. }
  354. // TODO:萌战无双游戏微信支付申请-01.02
  355. if ($game_id == Config::get('gb_game')) {
  356. return [["paytype" => "ybzf_wxmp_h5", "payname" => "微信"]];
  357. }
  358. if (empty($game_id)) {
  359. $this->jsonResult('', 0, '游戏ID不能为空');
  360. } elseif (!($gameInfo = $gameModel->field('id,android_sdk_id,name,game_kind')->where(['id' => $game_id, 'isdelete' => 0])->find())) {
  361. $this->jsonResult('', 0, '传入游戏参数错误');
  362. }
  363. //游戏禁止的支付方式
  364. $denyPayType = $payRuleModel->where(["game_id" => $game_id])->column('deny_paytype');
  365. //专服游戏禁用平台币支付
  366. if ($gameInfo['game_kind'] == 1) {
  367. $denyPayType[] = 'ptb';
  368. }
  369. if (!empty($version)) {
  370. $android_sdk_id = $sdkModel->where(['version' => $version])->value('id');
  371. if (empty($android_sdk_id)) {
  372. $this->jsonResult('', 0, 'sdk版本信息不存在');
  373. }
  374. $payType = $payTypeModel->field('paytype,payname')->where(['status' => 1, 'paytype' => ['not in', $denyPayType], 'used_sdk_id' => ['like', '%,' . $android_sdk_id . ',%']])->order('sort asc')->select();
  375. } else {
  376. $payType = $payTypeModel->field('paytype,payname')->where(['status' => 1, 'paytype' => ['not in', $denyPayType], 'used_sdk_id' => ['like', '%,' . $gameInfo['android_sdk_id'] . ',%']])->order('sort asc')->select();
  377. }
  378. return $payType;
  379. }
  380. // 获取玩家平台币和折扣
  381. private function getCoin()
  382. {
  383. $userid = $this->input('userid'); //用户ID
  384. if (empty($userid)) {
  385. $this->jsonResult('', 0, '用户ID不能为空');
  386. }
  387. // 待支付订单,交易取消释放平台币
  388. model('MemberCoinInfo')->releaseCoin($userid);
  389. $memberInfo = model('Members')->field('id,amount,channel_id')->where(['id' => $userid])->find();
  390. if ($memberInfo) {
  391. $discount = getDiscount($memberInfo['id'], $this->input('gameid'), $memberInfo['channel_id']);
  392. $memberInfo['discount'] = $discount;
  393. $memberInfo['discount_str'] = ($discount * 10) . '折';
  394. return $memberInfo;
  395. } else {
  396. $this->jsonResult('', 0, '用户不存在');
  397. }
  398. }
  399. // 获取玩家游戏专属币
  400. private function getZsCoin()
  401. {
  402. $game_id = $this->input('gameid', 0); //游戏ID
  403. $userid = $this->input('userid'); //用户ID
  404. // $game_id = 20;
  405. // $userid = 2;
  406. if (empty($game_id)) {
  407. $this->jsonResult('', 0, '游戏ID不能为空');
  408. }
  409. if (empty($userid)) {
  410. $this->jsonResult('', 0, '用户ID不能为空');
  411. }
  412. $memberInfo = model('Members')->field('id,username')->where(['id' => $userid])->find();
  413. $gameInfo = model('Game')->field('id,name')->where(['id' => $game_id])->find();
  414. if ($memberInfo && $gameInfo) {
  415. $userCoinInfo = model('MemberZscoin')->field('id,userid,username,game_id,amount,status')->where(['userid' => $userid, 'game_id' => $game_id])->find();
  416. $retData = array();
  417. $retData['userid'] = $userid;
  418. $retData['game_id'] = $game_id;
  419. if ($userCoinInfo) {
  420. $retData['amount'] = floatval($userCoinInfo['amount']);
  421. $retData['status'] = $userCoinInfo['status'];
  422. } else {
  423. $retData['amount'] = 0;
  424. $retData['status'] = 1;
  425. }
  426. return $retData;
  427. } else {
  428. $this->jsonResult('', 0, '用户或游戏不存在');
  429. }
  430. }
  431. // 用户代金券列表
  432. private function getPayCoupon()
  433. {
  434. $checkResult = $this->validate($this->input,
  435. [
  436. ['money', 'require|float|egt:0', '金额不能为空|金额数据有误|金额必须大于0'],
  437. ['gameid', 'require|integer|gt:1', '游戏ID不能为空|游戏ID必须是整数|游戏ID必须大于1'],
  438. ]);
  439. if (true !== $checkResult) {
  440. $this->jsonResult('', 0, $checkResult);
  441. }
  442. $list = model('common/CouponMember')->getPayCoupon($this->input['userid'], $this->input['gameid'], $this->input['money']);
  443. return $list;
  444. }
  445. }