AppController.php 2.8 KB

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