O2oAccess.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. * O2oAccess.php
  4. *
  5. * o2o平台接入抽象类
  6. *
  7. * @author 2015-11-04
  8. */
  9. abstract class O2oAccess {
  10. private static $_instances = array(); // 单例保存子类的实例化
  11. protected $request = array(); // 对方服务器的请求参数
  12. protected $params = array(); // 请求对方服务器时的参数数组
  13. protected $url = ''; // 请求对方服务器时的接地址
  14. protected $access_key = ''; // 对方提供的access_key
  15. protected $secret_key = ''; // 对方提供的secret_key
  16. const CURL_GET = -1;
  17. const CURL_POST = 1;
  18. /**
  19. * 强制要求子类实现的方法,命名参考目前已接入平台
  20. *
  21. * ------ 签名及加密部分 ------
  22. * method checkSignature 检查签名
  23. * method makeSignature 生成签名
  24. * method makeParams 加密并生成参数
  25. *
  26. * ------- 响应部分 ------
  27. * method queryAvaliableTimeslots 生成可预约时间列表
  28. * method queryAvaliableSource 生成可预约服务人员列表
  29. * method updatePaiedInfo 更新订单支付状态
  30. * method updateOrderInfo 更新订单状态
  31. *
  32. * ------ 订单部分 -------
  33. * method parseForCreate 整理创建订单数据
  34. * method updateToPlatform 将订单信息更新到平台
  35. */
  36. abstract protected function checkSignature();
  37. abstract protected function makeSignature();
  38. abstract protected function makeParams();
  39. abstract protected function queryStaticSource();
  40. abstract protected function queryAvailableTimeslots();
  41. abstract protected function queryAvailableSource();
  42. abstract protected function updatePaiedInfo();
  43. abstract protected function updateOrderInfo();
  44. abstract protected function parseForCreate();
  45. abstract protected function updateToPlatform();
  46. /**
  47. * 父类的构造方法
  48. */
  49. private function __construct() {}
  50. /**
  51. * 子类的构造方法
  52. * 实例化后默认获取Request数据、Token及api_url
  53. */
  54. public static function getInstance($class = __CLASS__) {
  55. if (isset(self::$_instances[$class])) {
  56. return self::$_instances[$class];
  57. } else {
  58. $instance = self::$_instances[$class] = new $class(null);
  59. $instance->setKey()->getRequest()->setUrl();
  60. return $instance;
  61. }
  62. }
  63. /**
  64. * 设置ak、sk的方法
  65. */
  66. public function setKey($access_key='', $secret_key='') {
  67. $this->access_key = $access_key;
  68. $this->secret_key = $secret_key;
  69. return $this;
  70. }
  71. /**
  72. * 获取secret
  73. */
  74. public function getSecret() {
  75. return $this->secret_key;
  76. }
  77. /**
  78. * 获取access
  79. */
  80. public function getAccess() {
  81. return $this->access_key;
  82. }
  83. /**
  84. * 设置请求参数的方法
  85. */
  86. public function setParams($args) {
  87. $this->params = $args;
  88. return $this;
  89. }
  90. /**
  91. * 获取对方服务器请求数据,包括POST及GET,并销毁传入的控制器地址(r)
  92. */
  93. public function getRequest() {
  94. if (is_array($_POST)) {
  95. foreach ($_POST as $key => $value) {
  96. $this->request[$key] = $value;
  97. }
  98. }
  99. if (is_array($_GET)) {
  100. foreach ($_GET as $key => $value) {
  101. $this->request[$key] = $value;
  102. }
  103. }
  104. if (isset($this->request['r'])) {
  105. unset($this->request['r']);
  106. }
  107. return $this;
  108. }
  109. /**
  110. * 获取请求数据字符串
  111. */
  112. public function getReuqestArray() {
  113. return $this->request;
  114. }
  115. /**
  116. * 设置request数据中的值
  117. */
  118. public function setRequestValue($key, $value) {
  119. $this->request[$key] = $value;
  120. return $this;
  121. }
  122. /**
  123. * 获取request数据中的值
  124. * @param string $key : 参数键名
  125. * @param mix $default : 默认值
  126. * @return mix $value : 获取的值
  127. */
  128. public function getRequestValue($key, $default) {
  129. $value = isset($this->request[$key]) ? $this->request[$key] : $default;
  130. return $value;
  131. }
  132. /**
  133. * 设置请求的接口地址
  134. */
  135. public function setUrl($urlString='') {
  136. $this->url = $urlString;
  137. return $this;
  138. }
  139. /**
  140. * 检查预约地址是否在服务范围内
  141. * @param float $latitude : 纬度(默认为百度坐标)
  142. * @param float $longitude : 经度(默认为百度坐标)
  143. * @param boolean $gcjTrans : 是否需要从火星系坐标转换为百度坐标(默认否)
  144. * @param boolean $returnInfo : 是否需要返回地址信息(默认否)
  145. * @return mix(bool) $flag : 地址是否在服务范围内
  146. * @return mix(obj) $info : 反向查询获得的地址信息
  147. */
  148. public function checkLocation($latitude, $longitude, $gcjTrans=false, $returnInfo=false) {
  149. if ($gcjTrans) {
  150. $position = CommonFn::GCJTobaidu($latitude, $longitude);
  151. $latitude = $position['lat'];
  152. $longitude = $position['lng'];
  153. }
  154. $location = $latitude.','.$longitude;
  155. $res = CommonFn::simple_http('http://api.map.baidu.com/geocoder/v2/?ak=B349f0b32ef6e78b2e678f45cb9fddaf&location='.$location.'&output=json&pois=0');
  156. $info = json_decode($res);
  157. if($info||$info->status==0){
  158. $addressInfo = $info->result->addressComponent;
  159. $flag = empty($addressInfo->province) || empty($addressInfo->city) ||
  160. $addressInfo->province != '上海市' || $addressInfo->district == '金山区' ||
  161. $addressInfo->district == '松江区' || $addressInfo->district == '奉贤区' ||
  162. $addressInfo->district == '青浦区' || $addressInfo->district == '崇明县';
  163. if($flag) {
  164. return false;
  165. }
  166. } else {
  167. return false;
  168. }
  169. if ($returnInfo) {
  170. return $info;
  171. } else {
  172. return true;
  173. }
  174. }
  175. /**
  176. * 插入新的订单数据并返回订单ID,若插入失败则返回空
  177. * @param array $array : 需要插入的数据 array(key => value, ...)
  178. */
  179. public function createOrder($array) {
  180. $order = new ROrder();
  181. foreach ($array as $key => $value) {
  182. $order->$key = $value;
  183. }
  184. if ($order->save()) {
  185. $order_id = (string)$order->_id;
  186. } else {
  187. $order_id = '';
  188. }
  189. return $order_id;
  190. }
  191. /**
  192. * 发送请求
  193. * @param string $url : 请求的URL,若请求方式为GET则包括请求参数
  194. * @param number $method : 请求的方式
  195. * @return string $result : 响应结果
  196. */
  197. public function sendRequest($url, $method=self::CURL_GET) {
  198. if ($method == self::CURL_GET) {
  199. $ch = curl_init($url);
  200. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  201. curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
  202. $result = curl_exec($ch);
  203. curl_close($ch);
  204. return $result;
  205. // POST方法暂时未封装
  206. } else {
  207. return '';
  208. }
  209. }
  210. }