DouHuoMallFilmApiRequest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. namespace app\common\repositories\movie;
  3. use think\facade\Cache;
  4. /**
  5. * 微唯宝电影
  6. */
  7. class DouHuoMallFilmApiRequest
  8. {
  9. //接口文档:https://docs.qq.com/doc/DWkJQS0h3WENvVmdG?u=a6a6ad6c89714f6394f5fd13fcffc2ac
  10. private static $path = 'http://wmh5.douhuomall.com';
  11. private static $app_id = 'YS3a8ab661406e47bb86';
  12. private static $app_secret = '41d8b8e1dd9a454a1abc4534297e33c3';
  13. private static $mobile = '17855366921';
  14. private static $successCode = 200;
  15. private static $douHuoMallFilmAccessTokenCache = 'DouHuoMallFilmAccessToken';
  16. /**
  17. *
  18. *
  19. * @return false|mixed
  20. */
  21. public static function request_access_token()
  22. {
  23. $url = '/openapi/Auth/accessToken';
  24. $time = date('Y-m-d H:i:s');
  25. $body = [
  26. 'app_id' => self::$app_id,
  27. 'sign' => self::getSign($time),
  28. 'mobile' => self::$mobile,
  29. 'datetime' => $time
  30. ];
  31. $res = self::curl_post(self::$path . $url, $body);
  32. if ($res['code'] == self::$successCode) {
  33. $expire_time = strtotime($res['data']['expire_time']) - 5 - time();
  34. $expire_time = $expire_time > 0 ? $expire_time : 3600;
  35. Cache::set(self::$douHuoMallFilmAccessTokenCache, $res['data']['access_token'], $expire_time);// 将取到的token存入redis方便下次使用
  36. return $res['data']['access_token'];
  37. }
  38. return '';
  39. }
  40. /**
  41. * 获取token
  42. *
  43. * @return mixed
  44. */
  45. public static function get_access_token()
  46. {
  47. $access_token = Cache::get(self::$douHuoMallFilmAccessTokenCache);
  48. return $access_token ?? self::request_access_token();
  49. }
  50. /**
  51. * 获取城市列表
  52. * @return false|int|mixed
  53. */
  54. public static function getMovieCity($page = 1, $limit = 10, $initial = '', $city_name = '')
  55. {
  56. $url = '/openapi/Movie/getCity';
  57. $body = [
  58. 'access_token' => self::get_access_token(),
  59. 'page' => $page,
  60. 'limit' => $limit
  61. ];
  62. if (!empty($initial)) $body['initial'] = $initial;
  63. if (!empty($city_name)) $body['city_name'] = $city_name;
  64. $res = self::curl_post(self::$path . $url, $body);
  65. return $res;
  66. }
  67. /**
  68. * 获取城市分区列表
  69. * @return false|int|mixed
  70. */
  71. public static function getMovieArea($city_sign = '')
  72. {
  73. $url = '/openapi/Movie/getArea';
  74. $body = [
  75. 'access_token' => self::get_access_token(),
  76. 'city_sign' => $city_sign
  77. ];
  78. $res = self::curl_post(self::$path . $url, $body);
  79. return $res;
  80. }
  81. /**
  82. * 获取影院列表
  83. * @param $page
  84. * @param $limit
  85. * @param $city_sign
  86. * @param $area_sign
  87. * @return mixed|string
  88. */
  89. public static function getCinema($page = 1, $limit = 10, $city_sign = '', $area_sign = '')
  90. {
  91. $url = '/openapi/Movie/getCinema';
  92. $body = [
  93. 'access_token' => self::get_access_token(),
  94. 'page' => $page,
  95. 'limit' => $limit
  96. ];
  97. if (!empty($city_sign)) $body['city_sign'] = $city_sign;
  98. if (!empty($area_sign)) $body['area_sign'] = $area_sign;
  99. $res = self::curl_post(self::$path . $url, $body);
  100. return $res;
  101. }
  102. /**
  103. * 获取影片列表
  104. * @param $page
  105. * @param $limit
  106. * @param $cinema_sign
  107. * @param $film_sign
  108. * @return mixed|string
  109. */
  110. public static function getFilm($page = 1, $limit = 10, $cinema_sign = '', $film_sign = '')
  111. {
  112. $url = '/openapi/Movie/getFilm';
  113. $body = [
  114. 'access_token' => self::get_access_token(),
  115. 'page' => $page,
  116. 'limit' => $limit
  117. ];
  118. if (!empty($cinema_sign)) $body['cinema_sign'] = $cinema_sign;
  119. if (!empty($film_sign)) $body['film_sign'] = $film_sign;
  120. $res = self::curl_post(self::$path . $url, $body);
  121. return $res;
  122. }
  123. /**
  124. * 获取排片影院列表(废弃)
  125. * @param $page
  126. * @param $limit
  127. * @param $cinema_sign
  128. * @param $film_sign
  129. * @return mixed|string
  130. */
  131. public static function getFilmSched($page = 1, $limit = 10, $film_sign = '', $longitude = '', $latitude = '', $city_sign = '')
  132. {
  133. $url = '/openapi/Movie/getFilmSched';
  134. $body = [
  135. 'access_token' => self::get_access_token(),
  136. 'page' => $page,
  137. 'limit' => $limit,
  138. 'film_sign' => $film_sign,
  139. 'longitude' => $longitude,
  140. 'latitude' => $latitude,
  141. 'city_sign' => $city_sign
  142. ];
  143. $res = self::curl_post(self::$path . $url, $body);
  144. return $res;
  145. }
  146. /**
  147. * 获取影院场次列表
  148. *
  149. * @param $film_sign
  150. * @param $cinema_sign
  151. * @return mixed|string
  152. */
  153. public static function getCinemaSched($film_sign = '', $cinema_sign = '')
  154. {
  155. $url = '/openapi/Movie/getCinemaSched';
  156. $body = [
  157. 'access_token' => self::get_access_token(),
  158. 'film_sign' => $film_sign,
  159. 'cinema_sign' => $cinema_sign
  160. ];
  161. $res = self::curl_post(self::$path . $url, $body);
  162. return $res;
  163. }
  164. /**
  165. * 获取场次座位
  166. *
  167. * @param $relation_id
  168. * @param $sched_sign
  169. * @return mixed|string
  170. */
  171. public static function getSchedSeat($relation_id = '', $sched_sign = '')
  172. {
  173. $url = '/openapi/Movie/getSchedSeat';
  174. $body = [
  175. 'access_token' => self::get_access_token(),
  176. 'relation_id' => $relation_id,
  177. 'sched_sign' => $sched_sign
  178. ];
  179. $res = self::curl_post(self::$path . $url, $body);
  180. return $res;
  181. }
  182. /**
  183. * 锁座订单
  184. *
  185. * @param $phone
  186. * @param $channel_type
  187. * @param $sched_sign
  188. * @param $seat_sign
  189. * @param $third_order
  190. * @param $order_price
  191. * @return mixed|string
  192. */
  193. public static function submitOrder($phone, $channel_type, $sched_sign, $seat_sign, $third_order, $order_price)
  194. {
  195. $url = '/openapi/Movie/submitOrder';
  196. $body = [
  197. 'access_token' => self::get_access_token(),
  198. 'channel_type' => $channel_type,
  199. 'sched_sign' => $sched_sign,
  200. 'third_order' => $third_order,
  201. 'order_price' => $order_price,
  202. 'phone' => $phone,
  203. 'seat_sign' => $seat_sign
  204. ];
  205. $res = self::curl_post(self::$path . $url, $body);
  206. return $res;
  207. }
  208. /**
  209. * 确认订单
  210. *
  211. * @param $third_order
  212. * @param $order_price
  213. * @return mixed|string
  214. */
  215. public static function confirmOrder($third_order, $order_price)
  216. {
  217. $url = '/openapi/Movie/confirmOrder';
  218. $body = [
  219. 'access_token' => self::get_access_token(),
  220. 'third_order' => $third_order,
  221. 'order_price' => $order_price
  222. ];
  223. $res = self::curl_post(self::$path . $url, $body);
  224. return $res;
  225. }
  226. /**
  227. * 查询订单信息
  228. *
  229. * @param $third_order
  230. * @return mixed|string
  231. */
  232. public static function getOrderInfo($third_order)
  233. {
  234. $url = '/openapi/Movie/getOrderInfo';
  235. $body = [
  236. 'access_token' => self::get_access_token(),
  237. 'third_order' => $third_order
  238. ];
  239. $res = self::curl_post(self::$path . $url, $body);
  240. return $res;
  241. }
  242. /**
  243. * 签名
  244. *
  245. * @param $time
  246. * @return string
  247. */
  248. public static function getSign($time)
  249. {
  250. //按照以下顺序将字符串拼接起来 app_id+mobile+timestamp+app_secret 再用md5加密
  251. return md5(self::$app_id . self::$mobile . $time . self::$app_secret);
  252. }
  253. //通过curl发送post请求带body传参
  254. public static function curl_post($url, $postFields = null, $header = array())
  255. {
  256. $curl = curl_init();
  257. //设置抓取的url
  258. curl_setopt($curl, CURLOPT_URL, $url);
  259. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
  260. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  261. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postFields));
  262. // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
  263. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  264. // curl_setopt($curl, CURLOPT_HEADER, 1);
  265. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  266. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  267. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  268. //执行命令
  269. $data = curl_exec($curl);
  270. // if (is_null(json_decode($data))) {
  271. // $data = strstr($data, '{');
  272. // }
  273. // 显示错误信息
  274. if (curl_error($curl)) {
  275. return curl_error($curl);
  276. } else {
  277. curl_close($curl);
  278. return json_decode($data, true);
  279. }
  280. }
  281. }