YqlHandle.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace app\common\logic;
  3. // 176
  4. use think\Env;
  5. use think\Request;
  6. use think\response\Json;
  7. class YqlHandle
  8. {
  9. protected $api_url;
  10. protected $api_sign_key;
  11. public function __construct()
  12. {
  13. $this->api_url = Env::get("yql.api_url");
  14. $this->api_sign_key = Env::get("yql.api_sign_key");
  15. trace(json_encode(input()), 'YqlHandle@__construct.input');
  16. }
  17. /**
  18. * sign
  19. *
  20. * @param $data
  21. *
  22. * @return string
  23. */
  24. private function sign($data)
  25. {
  26. unset($data['sign']);
  27. $data['key'] = $this->api_sign_key;
  28. $string = '';
  29. foreach ($data as $k => $v) {
  30. if ($string) {
  31. $string .= '&' . $k . "=" . $v;
  32. } else {
  33. $string = $k . "=" . $v;
  34. }
  35. }
  36. // dump(md5($string), $string);
  37. return md5($string);
  38. }
  39. /**
  40. * 代金卷使用通知 - 请求176
  41. *
  42. * @param $coupon_member_id 祈盟平台的用户代金卷ID
  43. *
  44. * @return Json
  45. */
  46. public function notifyCoupon($coupon_member_id)
  47. {
  48. if (!$coupon_member_id) {
  49. return json(["code" => 0, "message" => "非法操作:代金卷不存在!", "data" => []]);
  50. }
  51. // 176 平台的用户代金卷ID
  52. $yql_id = model('common/CouponMember')->where(['id' => $coupon_member_id])->value('yql_id');
  53. if (empty($yql_id)) {
  54. return json(["code" => 0, "message" => "代金卷不存在!", "data" => []]);
  55. }
  56. $data = ['id' => $yql_id];
  57. $data['sign'] = $this->sign($data);
  58. $url = $this->api_url . '/serve/v1/notify/vouchers';
  59. $res = getHttpPost($url.'?'.http_build_query($data), [], ['Content-Type' => 'application/json']);
  60. trace("url: ".$url." - body: ".json_encode($data).' - res:'.json_encode($res), 'YqlHandle@notifyCoupon.getHttpPost');
  61. if ($res) {
  62. return json(["code" => 1, "message" => "通知成功!", "data" => []]);
  63. }
  64. return json(["code" => 0, "message" => "通知失败!", "data" => []]);
  65. }
  66. }