DouHuoMallFilmApiRequest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 = '')
  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. ];
  142. $res = self::curl_post(self::$path . $url, $body);
  143. return $res;
  144. }
  145. /**
  146. * 获取影院场次列表
  147. *
  148. * @param $film_sign
  149. * @param $cinema_sign
  150. * @return mixed|string
  151. */
  152. public static function getCinemaSched($film_sign = '', $cinema_sign = '')
  153. {
  154. $url = '/openapi/Movie/getCinemaSched';
  155. $body = [
  156. 'access_token' => self::get_access_token(),
  157. 'film_sign' => $film_sign,
  158. 'cinema_sign' => $cinema_sign
  159. ];
  160. $res = self::curl_post(self::$path . $url, $body);
  161. return $res;
  162. }
  163. /**
  164. * 获取场次座位
  165. *
  166. * @param $relation_id
  167. * @param $sched_sign
  168. * @return mixed|string
  169. */
  170. public static function getSchedSeat($relation_id = '', $sched_sign = '')
  171. {
  172. $url = '/openapi/Movie/getSchedSeat';
  173. $body = [
  174. 'access_token' => self::get_access_token(),
  175. 'relation_id' => $relation_id,
  176. 'sched_sign' => $sched_sign
  177. ];
  178. $res = self::curl_post(self::$path . $url, $body);
  179. return $res;
  180. }
  181. /**
  182. * 锁座订单
  183. *
  184. * @param $phone
  185. * @param $channel_type
  186. * @param $sched_sign
  187. * @param $seat_sign
  188. * @param $third_order
  189. * @param $order_price
  190. * @return mixed|string
  191. */
  192. public static function submitOrder($phone, $channel_type, $sched_sign, $seat_sign, $third_order, $order_price)
  193. {
  194. $url = '/openapi/Movie/submitOrder';
  195. $body = [
  196. 'access_token' => self::get_access_token(),
  197. 'channel_type' => $channel_type,
  198. 'sched_sign' => $sched_sign,
  199. 'third_order' => $third_order,
  200. 'order_price' => $order_price,
  201. 'phone' => $phone,
  202. 'seat_sign' => $seat_sign
  203. ];
  204. $res = self::curl_post(self::$path . $url, $body);
  205. return $res;
  206. }
  207. /**
  208. * 确认订单
  209. *
  210. * @param $third_order
  211. * @param $order_price
  212. * @return mixed|string
  213. */
  214. public static function confirmOrder($third_order, $order_price)
  215. {
  216. $url = '/openapi/Movie/confirmOrder';
  217. $body = [
  218. 'access_token' => self::get_access_token(),
  219. 'third_order' => $third_order,
  220. 'order_price' => $order_price
  221. ];
  222. $res = self::curl_post(self::$path . $url, $body);
  223. return $res;
  224. }
  225. /**
  226. * 查询订单信息
  227. *
  228. * @param $third_order
  229. * @return mixed|string
  230. */
  231. public static function getOrderInfo($third_order)
  232. {
  233. $url = '/openapi/Movie/getOrderInfo';
  234. $body = [
  235. 'access_token' => self::get_access_token(),
  236. 'third_order' => $third_order
  237. ];
  238. $res = self::curl_post(self::$path . $url, $body);
  239. return $res;
  240. }
  241. /**
  242. * 签名
  243. *
  244. * @param $time
  245. * @return string
  246. */
  247. public static function getSign($time)
  248. {
  249. //按照以下顺序将字符串拼接起来 app_id+mobile+timestamp+app_secret 再用md5加密
  250. return md5(self::$app_id . self::$mobile . $time . self::$app_secret);
  251. }
  252. //通过curl发送post请求带body传参
  253. public static function curl_post($url, $postFields = null, $header = array())
  254. {
  255. $curl = curl_init();
  256. //设置抓取的url
  257. curl_setopt($curl, CURLOPT_URL, $url);
  258. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
  259. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  260. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postFields));
  261. // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
  262. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  263. // curl_setopt($curl, CURLOPT_HEADER, 1);
  264. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  265. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  266. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  267. //执行命令
  268. $data = curl_exec($curl);
  269. // if (is_null(json_decode($data))) {
  270. // $data = strstr($data, '{');
  271. // }
  272. // 显示错误信息
  273. if (curl_error($curl)) {
  274. return curl_error($curl);
  275. } else {
  276. curl_close($curl);
  277. return json_decode($data, true);
  278. }
  279. }
  280. }