UserMonthService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: z-yang
  5. * Date: 2019/7/27
  6. * Time: 18:01
  7. */
  8. namespace App\Modules\User\Services;
  9. use App\Modules\User\Models\UserMonthOrder;
  10. use App\Modules\User\Models\UserMonthSign;
  11. use GuzzleHttp\Client;
  12. class UserMonthService
  13. {
  14. public static function createSign(int $uid, int $plan_id, string $change_type,string $openid,string $type)
  15. {
  16. $model = new UserMonthSign();
  17. $info = $model->where('uid', $uid)->where('plan_id', $plan_id)->first();
  18. if ($info) {
  19. $info->change_type = $change_type;
  20. $info->save();
  21. } else {
  22. $model->uid = $uid;
  23. $model->plan_id = $plan_id;
  24. $model->change_type = $change_type;
  25. $model->openid = $openid;
  26. $model->type = $type;
  27. $model->save();
  28. }
  29. return;
  30. }
  31. public static function isSignMonth($openid){
  32. $model = new UserMonthSign();
  33. return $model->where('openid',$openid)->where('change_type','ADD')->select('id')->first();
  34. }
  35. public static function getLastOrder(int $uid){
  36. $model = new UserMonthOrder();
  37. return $model->where('uid',$uid)->orderBy('id','desc')->first();
  38. }
  39. public static function createLOrder(int $uid, int $plan_id, int $total_fee, string $trade_no,string $out_trade_no,string $type)
  40. {
  41. $model = new UserMonthOrder();
  42. $model->uid = $uid;
  43. $model->plan_id = $plan_id;
  44. $model->total_fee = $total_fee;
  45. $model->trade_no = $trade_no;
  46. $model->out_trade_no = $out_trade_no;
  47. $model->type = $type;
  48. $model->save();
  49. return;
  50. }
  51. public static function getOrderByOrder($trade_no,$out_trade_no){
  52. $model = new UserMonthOrder();
  53. $info = $model->where('trade_no',$trade_no)->first();
  54. return $info;
  55. }
  56. public static function getOrderAndSignStatusByUid($uid){
  57. $result = UserMonthOrder::join('user_month_sign','user_month_sign.uid','=','user_month_order.uid')
  58. ->select('user_month_order.type','user_month_sign.change_type')
  59. ->where('user_month_order.uid',$uid)
  60. ->where('user_month_sign.change_type','DELETE')
  61. ->get();
  62. if($result->isEmpty()) return [];
  63. $return_result = [];
  64. foreach ($result as $item){
  65. $return_result[] = $item->type;
  66. }
  67. return $return_result;
  68. }
  69. public static function checkOrderStatus(int $user_id,int $plan_id,string $app_id,string $key,string $app_secret){
  70. $pay_year = date('Y');
  71. $pay_month = date('m');
  72. $sign = strtoupper(_sign(compact('app_id','app_secret','user_id','plan_id','pay_year','pay_month'),$key.$key));
  73. $client = new Client();
  74. $url = 'http://pap.manyuedu.org/checkpay.php?';
  75. $url .= http_build_query(compact('user_id','app_secret','app_id','plan_id','pay_year','pay_month','sign'));
  76. $result = $client->request('post',$url)->getBody()->getContents();
  77. return json_decode($result,true);
  78. }
  79. public static function isMonthOrderUser($uid){
  80. $info = UserMonthOrder::where('uid',$uid)
  81. ->where('type','MONTH')
  82. ->orderBy('id','desc')
  83. ->select('created_at')
  84. ->first();
  85. if($info && strtotime($info->created_at)+365*86400 >time() ){
  86. return true;
  87. }
  88. return false;
  89. }
  90. }