AppController.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. /**
  10. * 第三方管理类
  11. */
  12. class AppController extends Controller
  13. {
  14. /**
  15. * 构造函数
  16. *
  17. * @access public
  18. * @param mixed $req 数据传输
  19. * @return array JsonString
  20. */
  21. public function __construct() {
  22. }
  23. /**
  24. * 接收app发来的下线通知
  25. */
  26. public function loginOut(Request $req){
  27. $input = $req->input();
  28. if(!isset($input['money']) || !preg_match('/^[0-9]{1,8}(.[0-9]{1,2})?$/', $input['money'])){
  29. $arr['msg'] = '请传入正确的金额';
  30. return $arr;
  31. }
  32. $money = $req->input('money');
  33. $appUsername = $req->input('username');
  34. $appUserModel = new Models\AppUser();
  35. $appUserInfo = $appUserModel->where('app_username',$appUsername)->first();
  36. $mathches = array();
  37. preg_match('/^t(\d+)_(\w+\W+)_a(\d+)$/', $appUsername, $mathches);
  38. $partyId = $mathches[1];
  39. $appId = $mathches[3];
  40. $partyUserName = $mathches[2];
  41. $appModel = new Models\App;
  42. $appInfo = $appModel->where('id', $appId)->first();
  43. DB::beginTransaction();
  44. //更新额度
  45. /*$result = $partyModel->where('id', $partyInfo['id'])->update(array(
  46. 'balance' => $partyInfo['balance'] - $money
  47. ));
  48. if($result === false){
  49. $appUserModel->rollBack();
  50. return toJson(-4, '系统繁忙', []);
  51. }*/
  52. //更新额度
  53. $result = $appModel->where('id', $appId)->update(array(
  54. 'balance' => $appInfo['balance'] - $money
  55. ));
  56. if($result === false){
  57. DB::rollBack();
  58. return toJson(-4, '系统繁忙', []);
  59. }
  60. //记录日志
  61. $result = $balanceLogModel = new Models\BalanceLog();
  62. $balanceLogModel->insert([
  63. 'type' => 2,
  64. 'money' => $money,
  65. 'app_id' => $appInfo['id'],
  66. 'app_username' => $appUsername,
  67. 'party_id' =>$partyId,
  68. 'party_username' => $partyUserName,
  69. //'ctime' =>now()
  70. ]);
  71. if($result === false){
  72. $appUserModel->rollBack();
  73. return toJson(-4, '系统繁忙', []);
  74. }
  75. DB::commit();
  76. //TODO 调用第三方平台推出接口,更新第三方额度
  77. }
  78. }