ShiHuiController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: PHP
  5. * Date: 2016/11/3
  6. * Time: 13:55
  7. */
  8. class ShiHuiController extends O2oBaseController {
  9. //访问接口前进行签名验证
  10. public function beforeAction($action){
  11. if(isset($_GET['no_sign'])){
  12. return true;
  13. }else{
  14. unset($_GET['no_sign']);
  15. return $this->shiHuiCheckSign();
  16. }
  17. }
  18. //取消订单
  19. public function actionDel(){
  20. $data['key'] = Yii::app()->params['shKey'];
  21. $data['version'] = '1.0';
  22. $data['serviceType'] = (int)26;
  23. $data['orderId'] = '57fde8059f5160c4048b4aeb';
  24. ksort($data);
  25. $str = '';
  26. $dat = '';
  27. foreach ($data as $key => $value) {
  28. $str .= $key.'='.$value.'&';
  29. $dat .= $key.'='.urlencode($value).'&';
  30. }
  31. $str = rtrim(trim($str),'&');
  32. $str .= $str.Yii::app()->params['shMd5Key'];
  33. $data['sign'] = strtoupper(md5($str));
  34. $dat .= 'sign='.urlencode($data['sign']);//构建post 参数
  35. $url = 'http://test2.app.hiwemeet.com/v2/openpf/home/order/thirdOrder/cancel'; //调用接口的平台服务地址
  36. $ch = curl_init();
  37. curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
  38. curl_setopt($ch, CURLOPT_URL, $url);
  39. curl_setopt($ch, CURLOPT_POST, 1);
  40. curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
  41. curl_setopt($ch, CURLOPT_POSTFIELDS, $dat);
  42. $result = curl_exec($ch);
  43. curl_close($ch);
  44. echo $result;
  45. }
  46. //更新订单状态
  47. public function actionUpdateStatus(){
  48. $data['key'] = Yii::app()->params['shKey'];
  49. $data['version'] = '1.0';
  50. $data['serviceType'] = (int)26;
  51. $data['orderId'] = '57fde8059f5160c4048b4aeb';
  52. $data['status'] = '-1';
  53. ksort($data);
  54. $str = '';
  55. $dat = '';
  56. foreach ($data as $key => $value) {
  57. $str .= $key.'='.$value.'&';
  58. $dat .= $key.'='.urlencode($value).'&';
  59. }
  60. $str = rtrim(trim($str),'&');
  61. $str .= $str.Yii::app()->params['shMd5Key'];
  62. $data['sign'] = strtoupper(md5($str));
  63. $dat .= 'sign='.urlencode($data['sign']);//构建post 参数
  64. $url = 'http://test2.app.hiwemeet.com/v2/openpf/home/order/thirdOrder/update'; //调用接口的平台服务地址
  65. $ch = curl_init();
  66. curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
  67. curl_setopt($ch, CURLOPT_URL, $url);
  68. curl_setopt($ch, CURLOPT_POST, 1);
  69. curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
  70. curl_setopt($ch, CURLOPT_POSTFIELDS, $dat);
  71. $result = curl_exec($ch);
  72. curl_close($ch);
  73. echo $result;
  74. }
  75. // 获取服务时间接口
  76. public function actionQueryTime(){
  77. $latitude = Yii::app()->getRequest()->getParam("latitude"); //服务地址经纬度(火星坐标系)
  78. $longitude = Yii::app()->getRequest()->getParam("longitude"); //服务地址经纬度(火星坐标系)
  79. $position = CommonFn::GCJTobaidu($latitude,$longitude);
  80. $location = $position['lat'].','.$position['lng'];
  81. $res = CommonFn::simple_http('http://api.map.baidu.com/geocoder/v2/?ak=B349f0b32ef6e78b2e678f45cb9fddaf&location='.$location.'&output=json&pois=0');
  82. $info = json_decode($res);
  83. if($info||$info->status==0){
  84. $info = $info->result->addressComponent;
  85. if(!empty($info->province) && !empty($info->city) && $info->province == '上海市' && $info->district != '金山区' && $info->district != '松江区' && $info->district != '奉贤区' && $info->district != '青浦区' && $info->district != '崇明县'){
  86. $cityinfo['province'] = $info->province;
  87. $cityinfo['city'] = $info->city;
  88. $cityinfo['area'] = $info->district;
  89. }else{
  90. echo '{
  91. "code": "20001",
  92. "msg": "地址不在服务范围1"
  93. }';
  94. die();
  95. }
  96. }else{
  97. echo '{
  98. "code": "20001",
  99. "msg": "地址不在服务范围2"
  100. }';
  101. die();
  102. }
  103. $date[] = date('Y-m-d',time()+86400);
  104. $date[] = date('Y-m-d',time()+86400*2);
  105. $date[] = date('Y-m-d',time()+86400*3);
  106. $date[] = date('Y-m-d',time()+86400*4);
  107. $date[] = date('Y-m-d',time()+86400*5);
  108. $date[] = date('Y-m-d',time()+86400*6);
  109. $data['code'] = '0';
  110. $data['msg'] = 'success';
  111. $data['body'] = array();
  112. $timelist = array();
  113. foreach ($date as $day) {
  114. $day_res['date'] = $day;
  115. $time = strtotime($day);
  116. $holiday_start = strtotime('2016-01-01');
  117. $holiday_end = strtotime('2016-01-01');
  118. if( $time>=$holiday_start && $time<=$holiday_end ){
  119. $day_res['timeslot'] = "000000000000000000000000000000000000000000000000";
  120. }else{
  121. $day_res['timeslot'] = "000000000000000000111111111111111111111111100000";
  122. }
  123. $timelist[] = $day_res;
  124. }
  125. $data['body']['timeList'] = $timelist;
  126. echo json_encode($data);
  127. }
  128. // 服务是否可用
  129. public function actionQueryProductIsAvailable(){
  130. echo '{"code": "0","msg": "服务可用"}';
  131. }
  132. //支付通知
  133. public function actionQueryPay()
  134. {
  135. $orderId = Yii::app()->getRequest()->getParam("orderId");
  136. if (!CommonFn::isMongoId($orderId)) {
  137. echo '{"code": "1001","msg": "订单id不正确!"}';
  138. die();
  139. }
  140. $criteria = new EMongoCriteria();
  141. $criteria->_id('==', new MongoId($orderId));
  142. $order = ROrder::model()->find($criteria);
  143. //订单不存在
  144. if (!$order) {
  145. echo '{"code": "3001","msg": "订单不存在!"}';
  146. die();
  147. } else if(intval($order->status) != 0){
  148. echo '{"code": "3001","msg": "已经通知过啦!!"}';
  149. die();
  150. }else {
  151. $order->status = 1;//修改订单状态
  152. //修改成功
  153. if($order->save()){
  154. echo '{"code": "0","msg": "通知成功"}';
  155. } else {
  156. echo '{"code": "1003","msg": "状态修改失败!"}';
  157. die();
  158. }
  159. }
  160. }
  161. //订单评价
  162. public function actionOrderComment(){
  163. $orderId = Yii::app()->getRequest()->getParam("orderId");
  164. $comment = Yii::app()->getRequest()->getParam("comments");//评价
  165. $score = initval(Yii::app()->getRequest()->getParam("comments"));//评分
  166. }
  167. //创建订单
  168. public function actionCreateOrder(){
  169. $cityId = Yii::app()->getRequest()->getParam("cityId");
  170. $productId = Yii::app()->getRequest()->getParam("productId");
  171. $product_const = Yii::app()->params['ProductId'];
  172. foreach ($product_const as $key => $value) {
  173. $flag =1;
  174. foreach ($value as $k => $v) {
  175. if ($productId == $k){
  176. $extra_type = $v;
  177. if ($key == 2){
  178. $productId = '57e0e0189f5160dc048b4568';//深度清洁id
  179. $service_type = 2; $flag =0;break;
  180. } else if($key == 1){
  181. $productId = '57e0dffc9f5160dd048b4568';//日常清洁id
  182. $service_type = 1; $flag =0;break;
  183. }
  184. }
  185. }
  186. if (!$flag) break;
  187. }
  188. $criteria = new EMongoCriteria();
  189. $criteria->_id('==',new MongoId($productId));
  190. //$criteria->addCond('extra.type','==',$extra_type);
  191. $product_obj = Product::model()->find($criteria);
  192. $products = array();
  193. $price = 0;
  194. if($product_obj){
  195. $products[0]['product'] = $product_obj->_id;
  196. $products[0]['count'] = 1;
  197. $products[0]['extra'] = $product_obj->extra[$extra_type];
  198. }else{
  199. echo '{"code": "1003","msg": "product_not_exist!"}';
  200. die();
  201. }
  202. $serviceTime = Yii::app()->getRequest()->getParam("serviceStartTime"); //服务开始时间,格式为2015-04-18 10:30:00
  203. $final_price = Yii::app()->getRequest()->getParam("price"); //产品价格,以元为单位,精确到分
  204. $latitude = Yii::app()->getRequest()->getParam("latitude"); //服务地址经纬度(火星坐标系)
  205. $counts = Yii::app()->getRequest()->getParam("amount");
  206. $longitude = Yii::app()->getRequest()->getParam("longitude"); //服务地址经纬度(火星坐标系)
  207. $serviceAddress = Yii::app()->getRequest()->getParam("serviceAddress"); //服务地址,精确到小区
  208. $houseNumber = Yii::app()->getRequest()->getParam("detailAddress"); //服务地址,门牌号等详细信息
  209. $cellphone = Yii::app()->getRequest()->getParam("phone"); //用户电话
  210. $comment = Yii::app()->getRequest()->getParam("remark"); //用户备注
  211. $name = Yii::app()->getRequest()->getParam("contactName");//用户姓名
  212. $position = CommonFn::GCJTobaidu($latitude,$longitude);
  213. $location = $position['lat'].','.$position['lng'];
  214. $res = CommonFn::simple_http('http://api.map.baidu.com/geocoder/v2/?ak=B349f0b32ef6e78b2e678f45cb9fddaf&location='.$location.'&output=json&pois=0');
  215. $info = json_decode($res);
  216. if($info||$info->status==0){
  217. $info = $info->result->addressComponent;
  218. if(!empty($info->province) && !empty($info->city) && $info->province == '上海市'){
  219. $cityinfo['province'] = $info->province;
  220. $cityinfo['city'] = $info->city;
  221. $cityinfo['area'] = $info->district;
  222. }else{
  223. echo '{
  224. "code": "20001",
  225. "msg": "地址不在服务范围"
  226. }';
  227. die();
  228. }
  229. }else{
  230. echo '{
  231. "code": "20001",
  232. "msg": "地址不在服务范围"
  233. }';
  234. die();
  235. }
  236. $order = new ROrder();
  237. $order->channel = 'shihui';
  238. $order->order_time = time();
  239. $order->booking_time = strtotime($serviceTime);
  240. $order->products = $products;
  241. // $order->precedence = $precedence; //加急状态
  242. $order->price = intval($price);
  243. $order->final_price = intval($final_price);
  244. $address = array();
  245. $address['province'] = $cityinfo['province'];
  246. $address['city'] = $cityinfo['city'];
  247. $address['area'] = $cityinfo['area'];
  248. $address['name'] = $name;
  249. if($serviceAddress == $houseNumber){
  250. $address['detail'] = $serviceAddress;
  251. }else{
  252. $address['detail'] = $serviceAddress.$houseNumber;
  253. }
  254. $address['poi']['name'] = $address[detail];
  255. $address['mobile'] = $cellphone;
  256. $positions[0] = isset($position['lng'])?floatval($position['lng']):0;
  257. $positions[1] = isset($position['lat'])?floatval($position['lat']):0;
  258. $address['position'] = $positions;
  259. $address['position'] = $positions;
  260. $order->address = $address;
  261. $order->memo = $comment;
  262. $order->station = new MongoId('5548b05e0eb9fbc5728b51ea');
  263. // $order->coupons = $used_coupon;
  264. $order->status = 0;
  265. $order->user = '';
  266. $order->pay_channel = 'shihui';
  267. $order->type = $service_type;
  268. $order->price = $products[0]['extra']['price'];
  269. $order->counts = $counts;
  270. if($order->save()){
  271. echo '{
  272. "code": "1",
  273. "msg": "创建订单成功",
  274. "result": {
  275. "orderId": "'.(string)$order->_id.'"
  276. }
  277. }';
  278. }else{
  279. echo '{
  280. "code": "1004",
  281. "msg": "创建订单失败",
  282. }';
  283. }
  284. }
  285. //取消订单
  286. public function actionQueryDel(){
  287. $order_id = Yii::app()->getRequest()->getParam("orderId");
  288. if( !$order_id || !CommonFn::isMongoId($order_id)){
  289. CommonFn::requestAjax(false,CommonFn::getMessage('message','params_illegal'));
  290. echo '{"code": "1001","msg": "params_illegal"}';die();
  291. }
  292. $order = ROrder::get(new MongoId($order_id));
  293. $status = -1;
  294. if(!$order){
  295. echo '{"code": "1001","msg": "params_illegal"}';die();
  296. }
  297. if( $order->status!=-1 && $order->status!=-2 ){
  298. foreach ($order->coupons as $user_coupon) {
  299. $user_coupon = UserCoupon::get($user_coupon);
  300. $user_coupon->status = 1;
  301. $user_coupon->update(array('status'),true);
  302. }
  303. }
  304. $order->status = $status;
  305. $arr_order = array('status');
  306. $success = $order->update($arr_order,true);
  307. if($success){
  308. echo '{"code": "0","msg": "订单取消成功"}';
  309. }
  310. }
  311. }