| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- <?php
- /**
- * api平台币控制器
- */
- namespace app\api\controller\v1;
- use app\api\controller\Api;
- //use app\common\model\MemberCoin;
- //use app\common\model\MemberCoinLog;
- use app\common\model\PayCpinfo;
- use app\common\model\Pay;
- use app\common\model\TtbHistory;
- use think\Db;
- use think\Env;
- class Coin extends Api
- {
- // protected $coinModel;
- /**
- * 不进行父类的登录验证,所以增加构造方法重写了父类的初始化方法
- */
- public function _initialize()
- {
- parent::_initialize();
- // $this->coinModel = new MemberCoin;
- // $this->releaseCoin();
- }
- /**
- * 平台币劵列表
- * return array
- */
- public function index()
- {
- $game_id= $this->input('gameid',0); //游戏ID
- $userid = $this->input('userid'); //用户ID
- $index = $this->input('index',0); //分页索引
- $offset = 30; //分页偏移量
- if (empty($game_id)) {
- $this->jsonResult('', 0, '游戏ID不能为空');
- }
- if (empty($userid)) {
- $this->jsonResult('', 0, '用户ID不能为空');
- }
- $ttbHistoryModel = new TtbHistory;
- $where = ['mc_userid'=>$userid,'mc_gameid'=>$game_id,'mc_coin_residue'=>['>',0]];
- $count = $this->coinModel->field('mc_id,mc_coin_residue,mc_expire_time')
- ->where($where)
- ->where('(mc_expire_time=0 or mc_expire_time>'.NOW_TIMESTAMP.')')
- ->order('mc_expire_time asc')->count();
- $list = $this->coinModel->field('mc_id,coin_type,mc_coin_residue,RTRIM(mc_expire_time) as mc_expire_time,history_id')
- ->where($where)
- ->where('(mc_expire_time=0 or mc_expire_time>'.NOW_TIMESTAMP.')')
- ->order('mc_expire_time asc')->limit($index.','.$offset)->select();
- if(!empty($list)){
- foreach($list as $key => $value){
- $list[$key]['title'] = '';
- if($value['coin_type']=='act'){
- if($beizhu = $ttbHistoryModel->where(['id'=>$value['history_id']])->value('beizhu')){
- $list[$key]['title'] = $beizhu;
- }
- }
- }
- }
- $this->jsonResult(['data'=>$list,'total'=>$count,'offset'=>$offset], 1, '');
- }
- /**
- * 充值中心,默认的一笔平台币信息
- *
- */
- public function defaultCoin()
- {
- $game_id = $this->input('gameid',0); //游戏ID
- $userid = $this->input('userid'); //用户ID
- if (empty($game_id)) {
- $this->jsonResult('', 0, '游戏ID不能为空');
- }
- if (empty($userid)) {
- $this->jsonResult('', 0, '用户ID不能为空');
- }
- $info = $this->coinModel->field('mc_id,mc_coin_residue')
- ->where(['mc_userid'=>$userid,'mc_gameid'=>$game_id,'mc_coin_residue'=>['>',0]])
- ->where('(mc_expire_time=0 or mc_expire_time>'.NOW_TIMESTAMP.')')
- ->order('mc_expire_time asc')->find();
- $this->jsonResult($info, 1, '');
- }
- /**
- * 平台币余额
- * return array
- */
- public function remain()
- {
- $game_id= $this->input('gameid',0); //游戏ID
- $userid = $this->input('userid'); //用户ID
- // $game_id = 20;
- // $userid = 2;
- if (empty($game_id)) {
- $this->jsonResult('', 0, '游戏ID不能为空');
- }
- if (empty($userid)) {
- $this->jsonResult('', 0, '用户ID不能为空');
- }
- $memberInfo = model('Members')->field('id,username')->where(['id'=>$userid])->find();
- $gameInfo = model('Game')->field('id,name')->where(['id'=>$game_id])->find();
- if($memberInfo && $gameInfo){
- $userCoinInfo = model('MemberZscoin')->field('id,userid,username,game_id,amount,status')->where(['userid'=>$userid,'game_id'=>$game_id])->find();
- $retData = array();
- $retData['userid'] = $userid;
- $retData['game_id'] = $game_id;
- if($userCoinInfo){
- $retData['amount'] = floatval($userCoinInfo['amount']);
- $retData['status'] = $userCoinInfo['status'];
- }
- else{
- $retData['amount'] = 0;
- $retData['status'] = 1;
- }
- $this->jsonResult($retData, 1, '');
- }
- else{
- $this->jsonResult('', 0, '用户或游戏不存在');
- }
- }
- /**
- * 待支付订单,交易取消释放平台币
- *
- */
- private function releaseCoin()
- {
- $payModel = new Pay;
- // $coinLogModel = new MemberCoinLog;
- $payCpinfoModel = new PayCpinfo;
- $userid = $this->input('userid'); //用户ID
- //下单已经超过30分钟的待支付订单
- $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();
- //订单信息不为空时
- if(!empty($payList))
- {
- foreach($payList as $key=>$value){
- $memberInfo = model('Members')->where(['id'=>$userid])->find();
- $gameInfo = model('Game')->where(['id'=>$value['gameid']])->find();
- if($memberInfo && $gameInfo){
- // 启动事务
- Db::startTrans();
- try{
- $userCoinInfo = model('MemberZscoin')->field('id,userid,username,game_id,amount,status')->where(['userid'=>$userid,'game_id'=>$value['gameid']])->find();
- if(!empty($userCoinInfo)){
- $coinData = array();
- $coinData['amount'] = Db::raw("amount+".$value['real_ptb']);
- $coinData['update_time'] = time();
- $result = model('MemberZscoin')->where(['id'=>$userCoinInfo['id'],'userid'=>$userCoinInfo['userid'],'game_id'=>$userCoinInfo['game_id']])->update($coinData);
- if (!$result) {
- throw new Exception("用户游戏平台币账户金额变动失败");
- }
- $coinDetData = array();
- $coinDetData['userid'] = $userid;
- $coinDetData['username'] = $memberInfo['username'];
- $coinDetData['game_id'] = $value['gameid'];
- $coinDetData['type'] = 4;
- $coinDetData['prev_amount'] = 0;
- $coinDetData['change_amount'] = $value['real_ptb'];
- $coinDetData['after_amount'] = $coinDetData['prev_amount'] + $value['real_ptb'];
- $coinDetData['out_orderid'] = $value['orderid'];
- $coinDetData['create_time'] = time();
- $coinDetData['update_time'] = time();
- $insertDetId = model('MemberZscoinDet')->insertGetId($coinDetData);
- if ( !$insertDetId) {
- throw new Exception("添加用户游戏平台币变动明细失败");
- }
- }
- else{
- $coinData = array();
- $coinData['userid'] = $userid;
- $coinData['username'] = $memberInfo['username'];
- $coinData['game_id'] = $value['gameid'];
- $coinData['amount'] = $value['real_ptb'];
- $coinData['status'] = 1;
- $coinData['create_time'] = time();
- $coinData['update_time'] = time();
- $result = model('MemberZscoin')->insertGetId($coinData);
- if (!$result) {
- throw new Exception("添加用户游戏平台币账户失败");
- }
- $coinDetData = array();
- $coinDetData['userid'] = $userid;
- $coinDetData['username'] = $memberInfo['username'];
- $coinDetData['game_id'] = $value['gameid'];
- $coinDetData['type'] = 4;
- $coinDetData['prev_amount'] = 0;
- $coinDetData['change_amount'] = $value['real_ptb'];
- $coinDetData['after_amount'] = $coinDetData['prev_amount'] + $value['real_ptb'];
- $coinDetData['out_orderid'] = $value['orderid'];
- $coinDetData['create_time'] = time();
- $coinDetData['update_time'] = time();
- $insertDetId = model('MemberZscoinDet')->insertGetId($coinDetData);
- if ( !$insertDetId) {
- throw new Exception("添加用户游戏平台币变动明细失败");
- }
- }
- //更新订单状态
- $updPayResult = model('Pay')->where(['id'=>$value['id'],'userid'=>$userid,'status'=>0])->update(['status'=>2]);
- if ( !$updPayResult) {
- throw new Exception("订单消费释放状态更改失败");
- }
- // 提交事务
- Db::commit();
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- $template = "订单 ".$value['orderid']." 平台币消费释放存在异常:".$e->getMessage()."。请及时核实,时间:".date('Y-m-d H:i:s');
- $ddurl = Env::get('dingtalk.warning_url');
- $result = curlDD($template, $ddurl,true);
- }
- }
- }
- }
- }
- /**
- * 平台币说明
- */
- public function coinInstruction()
- {
- $instructionModel = model('Common/CoinInstruction');
- $info = $instructionModel->getInstruction();
- // 加meta标签,使ios中的wkwebview字体正常
- $info = '<head><meta name="viewport" content="width=device-width"></head>' . $info;
- $this->jsonResult(['content'=>$info], 1, '');
- }
- }
|