AppController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Http\Controllers\Sys;
  3. use App\Http\Appadapter\Adapter;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. use \App\Http\Models;
  7. use Illuminate\Support\Facades\DB;
  8. use \Exception;
  9. use App\Http\Appadapter\PartySign;
  10. use Monolog\Logger;
  11. use Monolog\Handler\StreamHandler;
  12. /**
  13. * 第三方管理类
  14. */
  15. class AppController extends Controller
  16. {
  17. public $logger;
  18. /**
  19. * 构造函数
  20. *
  21. * @access public
  22. * @param mixed $req 数据传输
  23. * @return array JsonString
  24. */
  25. public function __construct() {
  26. $this->logger = new Logger('app');
  27. $this->logger->pushHandler(new StreamHandler(storage_path('logs/app.log'), Logger::WARNING));
  28. }
  29. /**
  30. * 接收app发来的下线通知
  31. */
  32. public function loginOut(Request $req){
  33. $input = $req->input();
  34. $this->logger->addWarning('loginOut get:'.json_encode($input));
  35. if(!isset($input['money']) || !preg_match('/^[0-9]{1,8}(.[0-9]{1,2})?$/', $input['money'])){
  36. $this->logger->addError('loginOut out: status -201;请传入正确的金额');
  37. return toJson(-201, '请传入正确的金额', []);
  38. }
  39. $money = $req->input('money');
  40. $appUsername = $req->input('username');
  41. $appUserModel = new Models\AppUser();
  42. $appUserInfo = $appUserModel->where('app_username',$appUsername)->first();
  43. $mathches = array();
  44. preg_match('/^t(\d+)_(\w+\W+)_a(\d+)$/', $appUsername, $mathches);
  45. $partyId = $mathches[1];
  46. $appId = $mathches[3];
  47. $partyUserName = $mathches[2];
  48. $appModel = new Models\App;
  49. $appInfo = $appModel->where('id', $appId)->first();
  50. DB::beginTransaction();
  51. //更新额度
  52. /*$result = $partyModel->where('id', $partyInfo['id'])->update(array(
  53. 'balance' => $partyInfo['balance'] - $money
  54. ));
  55. if($result === false){
  56. $appUserModel->rollBack();
  57. return toJson(-4, '系统繁忙', []);
  58. }*/
  59. //更新额度
  60. $result = $appModel->where('id', $appId)->update(array(
  61. 'balance' => $appInfo['balance'] - $money
  62. ));
  63. if($result === false){
  64. DB::rollBack();
  65. $this->logger->addError('loginOut out:-104, 更新额度失败');
  66. return toJson(-104, '系统繁忙', []);
  67. }
  68. //记录日志
  69. $result = $balanceLogModel = new Models\BalanceLog();
  70. $balanceLogModel->insert([
  71. 'type' => 2,
  72. 'money' => $money,
  73. 'app_id' => $appInfo['id'],
  74. 'app_username' => $appUsername,
  75. 'party_id' =>$partyId,
  76. 'party_username' => $partyUserName,
  77. //'ctime' =>now()
  78. ]);
  79. if($result === false){
  80. $appUserModel->rollBack();
  81. $this->logger->addError('loginOut out:-104, 记录日志失败');
  82. return toJson(-104, '系统繁忙', []);
  83. }
  84. DB::commit();
  85. //TODO 调用第三方平台推出接口,更新第三方额度
  86. }
  87. public function getToken(Request $request){
  88. $data = $request->input();
  89. $secret = 'abcds';
  90. $partySign = new PartySign($secret);
  91. $data = $partySign->signString($data);
  92. return $data;
  93. }
  94. }