Coin.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * api平台币控制器
  4. */
  5. namespace app\api\controller\v1;
  6. use app\api\controller\Api;
  7. //use app\common\model\MemberCoin;
  8. //use app\common\model\MemberCoinLog;
  9. use app\common\model\PayCpinfo;
  10. use app\common\model\Pay;
  11. use app\common\model\TtbHistory;
  12. use think\Db;
  13. use think\Env;
  14. class Coin extends Api
  15. {
  16. // protected $coinModel;
  17. /**
  18. * 不进行父类的登录验证,所以增加构造方法重写了父类的初始化方法
  19. */
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. // $this->coinModel = new MemberCoin;
  24. // $this->releaseCoin();
  25. }
  26. /**
  27. * 平台币劵列表
  28. * return array
  29. */
  30. public function index()
  31. {
  32. $game_id= $this->input('gameid',0); //游戏ID
  33. $userid = $this->input('userid'); //用户ID
  34. $index = $this->input('index',0); //分页索引
  35. $offset = 30; //分页偏移量
  36. if (empty($game_id)) {
  37. $this->jsonResult('', 0, '游戏ID不能为空');
  38. }
  39. if (empty($userid)) {
  40. $this->jsonResult('', 0, '用户ID不能为空');
  41. }
  42. $ttbHistoryModel = new TtbHistory;
  43. $where = ['mc_userid'=>$userid,'mc_gameid'=>$game_id,'mc_coin_residue'=>['>',0]];
  44. $count = $this->coinModel->field('mc_id,mc_coin_residue,mc_expire_time')
  45. ->where($where)
  46. ->where('(mc_expire_time=0 or mc_expire_time>'.NOW_TIMESTAMP.')')
  47. ->order('mc_expire_time asc')->count();
  48. $list = $this->coinModel->field('mc_id,coin_type,mc_coin_residue,RTRIM(mc_expire_time) as mc_expire_time,history_id')
  49. ->where($where)
  50. ->where('(mc_expire_time=0 or mc_expire_time>'.NOW_TIMESTAMP.')')
  51. ->order('mc_expire_time asc')->limit($index.','.$offset)->select();
  52. if(!empty($list)){
  53. foreach($list as $key => $value){
  54. $list[$key]['title'] = '';
  55. if($value['coin_type']=='act'){
  56. if($beizhu = $ttbHistoryModel->where(['id'=>$value['history_id']])->value('beizhu')){
  57. $list[$key]['title'] = $beizhu;
  58. }
  59. }
  60. }
  61. }
  62. $this->jsonResult(['data'=>$list,'total'=>$count,'offset'=>$offset], 1, '');
  63. }
  64. /**
  65. * 充值中心,默认的一笔平台币信息
  66. *
  67. */
  68. public function defaultCoin()
  69. {
  70. $game_id = $this->input('gameid',0); //游戏ID
  71. $userid = $this->input('userid'); //用户ID
  72. if (empty($game_id)) {
  73. $this->jsonResult('', 0, '游戏ID不能为空');
  74. }
  75. if (empty($userid)) {
  76. $this->jsonResult('', 0, '用户ID不能为空');
  77. }
  78. $info = $this->coinModel->field('mc_id,mc_coin_residue')
  79. ->where(['mc_userid'=>$userid,'mc_gameid'=>$game_id,'mc_coin_residue'=>['>',0]])
  80. ->where('(mc_expire_time=0 or mc_expire_time>'.NOW_TIMESTAMP.')')
  81. ->order('mc_expire_time asc')->find();
  82. $this->jsonResult($info, 1, '');
  83. }
  84. /**
  85. * 平台币余额
  86. * return array
  87. */
  88. public function remain()
  89. {
  90. $game_id= $this->input('gameid',0); //游戏ID
  91. $userid = $this->input('userid'); //用户ID
  92. // $game_id = 20;
  93. // $userid = 2;
  94. if (empty($game_id)) {
  95. $this->jsonResult('', 0, '游戏ID不能为空');
  96. }
  97. if (empty($userid)) {
  98. $this->jsonResult('', 0, '用户ID不能为空');
  99. }
  100. $memberInfo = model('Members')->field('id,username')->where(['id'=>$userid])->find();
  101. $gameInfo = model('Game')->field('id,name')->where(['id'=>$game_id])->find();
  102. if($memberInfo && $gameInfo){
  103. $userCoinInfo = model('MemberZscoin')->field('id,userid,username,game_id,amount,status')->where(['userid'=>$userid,'game_id'=>$game_id])->find();
  104. $retData = array();
  105. $retData['userid'] = $userid;
  106. $retData['game_id'] = $game_id;
  107. if($userCoinInfo){
  108. $retData['amount'] = floatval($userCoinInfo['amount']);
  109. $retData['status'] = $userCoinInfo['status'];
  110. }
  111. else{
  112. $retData['amount'] = 0;
  113. $retData['status'] = 1;
  114. }
  115. $this->jsonResult($retData, 1, '');
  116. }
  117. else{
  118. $this->jsonResult('', 0, '用户或游戏不存在');
  119. }
  120. }
  121. /**
  122. * 待支付订单,交易取消释放平台币
  123. *
  124. */
  125. private function releaseCoin()
  126. {
  127. $payModel = new Pay;
  128. // $coinLogModel = new MemberCoinLog;
  129. $payCpinfoModel = new PayCpinfo;
  130. $userid = $this->input('userid'); //用户ID
  131. //下单已经超过30分钟的待支付订单
  132. $payList = $payModel->field('gameid,real_ptb,orderid,id,coupon_amount,coupon_member_id')->where(['create_time'=>['<=',NOW_TIMESTAMP-(60*30)],'userid'=>$userid,'status'=>0,'real_ptb'=>['>',0]])->select();
  133. //订单信息不为空时
  134. if(!empty($payList))
  135. {
  136. foreach($payList as $key=>$value){
  137. $memberInfo = model('Members')->where(['id'=>$userid])->find();
  138. $gameInfo = model('Game')->where(['id'=>$value['gameid']])->find();
  139. if($memberInfo && $gameInfo){
  140. // 启动事务
  141. Db::startTrans();
  142. try{
  143. $userCoinInfo = model('MemberZscoin')->field('id,userid,username,game_id,amount,status')->where(['userid'=>$userid,'game_id'=>$value['gameid']])->find();
  144. if(!empty($userCoinInfo)){
  145. $coinData = array();
  146. $coinData['amount'] = Db::raw("amount+".$value['real_ptb']);
  147. $coinData['update_time'] = time();
  148. $result = model('MemberZscoin')->where(['id'=>$userCoinInfo['id'],'userid'=>$userCoinInfo['userid'],'game_id'=>$userCoinInfo['game_id']])->update($coinData);
  149. if (!$result) {
  150. throw new Exception("用户游戏平台币账户金额变动失败");
  151. }
  152. $coinDetData = array();
  153. $coinDetData['userid'] = $userid;
  154. $coinDetData['username'] = $memberInfo['username'];
  155. $coinDetData['game_id'] = $value['gameid'];
  156. $coinDetData['type'] = 4;
  157. $coinDetData['prev_amount'] = 0;
  158. $coinDetData['change_amount'] = $value['real_ptb'];
  159. $coinDetData['after_amount'] = $coinDetData['prev_amount'] + $value['real_ptb'];
  160. $coinDetData['out_orderid'] = $value['orderid'];
  161. $coinDetData['create_time'] = time();
  162. $coinDetData['update_time'] = time();
  163. $insertDetId = model('MemberZscoinDet')->insertGetId($coinDetData);
  164. if ( !$insertDetId) {
  165. throw new Exception("添加用户游戏平台币变动明细失败");
  166. }
  167. }
  168. else{
  169. $coinData = array();
  170. $coinData['userid'] = $userid;
  171. $coinData['username'] = $memberInfo['username'];
  172. $coinData['game_id'] = $value['gameid'];
  173. $coinData['amount'] = $value['real_ptb'];
  174. $coinData['status'] = 1;
  175. $coinData['create_time'] = time();
  176. $coinData['update_time'] = time();
  177. $result = model('MemberZscoin')->insertGetId($coinData);
  178. if (!$result) {
  179. throw new Exception("添加用户游戏平台币账户失败");
  180. }
  181. $coinDetData = array();
  182. $coinDetData['userid'] = $userid;
  183. $coinDetData['username'] = $memberInfo['username'];
  184. $coinDetData['game_id'] = $value['gameid'];
  185. $coinDetData['type'] = 4;
  186. $coinDetData['prev_amount'] = 0;
  187. $coinDetData['change_amount'] = $value['real_ptb'];
  188. $coinDetData['after_amount'] = $coinDetData['prev_amount'] + $value['real_ptb'];
  189. $coinDetData['out_orderid'] = $value['orderid'];
  190. $coinDetData['create_time'] = time();
  191. $coinDetData['update_time'] = time();
  192. $insertDetId = model('MemberZscoinDet')->insertGetId($coinDetData);
  193. if ( !$insertDetId) {
  194. throw new Exception("添加用户游戏平台币变动明细失败");
  195. }
  196. }
  197. //更新订单状态
  198. $updPayResult = model('Pay')->where(['id'=>$value['id'],'userid'=>$userid,'status'=>0])->update(['status'=>2]);
  199. if ( !$updPayResult) {
  200. throw new Exception("订单消费释放状态更改失败");
  201. }
  202. // 提交事务
  203. Db::commit();
  204. } catch (\Exception $e) {
  205. // 回滚事务
  206. Db::rollback();
  207. $template = "订单 ".$value['orderid']." 平台币消费释放存在异常:".$e->getMessage()."。请及时核实,时间:".date('Y-m-d H:i:s');
  208. $ddurl = Env::get('dingtalk.warning_url');
  209. $result = curlDD($template, $ddurl,true);
  210. }
  211. }
  212. }
  213. }
  214. }
  215. /**
  216. * 平台币说明
  217. */
  218. public function coinInstruction()
  219. {
  220. $instructionModel = model('Common/CoinInstruction');
  221. $info = $instructionModel->getInstruction();
  222. // 加meta标签,使ios中的wkwebview字体正常
  223. $info = '<head><meta name="viewport" content="width=device-width"></head>' . $info;
  224. $this->jsonResult(['content'=>$info], 1, '');
  225. }
  226. }