JingBaiController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: PHP
  5. * Date: 2016/12/12
  6. * Time: 16:48
  7. */
  8. class JingBaiController extends O2oBaseController
  9. {
  10. public function beforeAction($action)
  11. {
  12. if (isset($_POST['type'])){
  13. $type = $_POST['type'];
  14. }
  15. if (isset($_GET['type'])){
  16. $type = $_GET['type'];
  17. }
  18. if (isset($type)){
  19. if($type == Yii::app()->params['JingBai']){
  20. return true;
  21. }
  22. }
  23. echo '{
  24. "code": "10001",
  25. "msg": "缺少必要参数type",
  26. }';
  27. die();
  28. }
  29. //获取产品列表
  30. public function actionGetAllProduct()
  31. {
  32. $criteria = new EMongoCriteria();
  33. $criteria->status('==', 1);
  34. $cursor = Product::model()->findAll($criteria);
  35. $products = CommonFn::getRowsFromCursor($cursor);
  36. $data = array();
  37. $data['code'] = '0';
  38. $data['message'] = 'success';
  39. foreach ($products as $key => $value) {
  40. $data['data'][$key]['productId'] = (string)$value['_id'];
  41. $data['data'][$key]['name'] = $value['name'];
  42. }
  43. echo json_encode($data);
  44. }
  45. //获取预约时间
  46. public function actionGetStationTime()
  47. {
  48. echo '{
  49. "code": "0",
  50. "message": "success",
  51. "data": [
  52. {
  53. "AvailableMaternityAppointmentTimeId": "1",
  54. "Time": "8:00"
  55. },
  56. {
  57. "AvailableMaternityAppointmentTimeId": "2",
  58. "Time": "8:30"
  59. },
  60. {
  61. "AvailableMaternityAppointmentTimeId": "3",
  62. "Time": "09:00"
  63. },
  64. {
  65. "AvailableMaternityAppointmentTimeId": "4",
  66. "Time": "09:30"
  67. },
  68. {
  69. "AvailableMaternityAppointmentTimeId": "5",
  70. "Time": "10:00"
  71. },
  72. {
  73. "AvailableMaternityAppointmentTimeId": "6",
  74. "Time": "10:30"
  75. },{
  76. "AvailableMaternityAppointmentTimeId": "7",
  77. "Time": "11:00"
  78. },
  79. {
  80. "AvailableMaternityAppointmentTimeId": "8",
  81. "Time": "11:30"
  82. },
  83. {
  84. "AvailableMaternityAppointmentTimeId": "9",
  85. "Time": "12:00"
  86. },
  87. {
  88. "AvailableMaternityAppointmentTimeId": "10",
  89. "Time": "12:30"
  90. },
  91. {
  92. "AvailableMaternityAppointmentTimeId": "11",
  93. "Time": "13:00"
  94. },
  95. {
  96. "AvailableMaternityAppointmentTimeId": "12",
  97. "Time": "13:30"
  98. },
  99. {
  100. "AvailableMaternityAppointmentTimeId": "13",
  101. "Time": "14:00"
  102. },
  103. {
  104. "AvailableMaternityAppointmentTimeId": "14",
  105. "Time": "14:30"
  106. },
  107. {
  108. "AvailableMaternityAppointmentTimeId": "15",
  109. "Time": "15:00"
  110. },
  111. {
  112. "AvailableMaternityAppointmentTimeId": "16",
  113. "Time": "15:30"
  114. },
  115. {
  116. "AvailableMaternityAppointmentTimeId": "17",
  117. "Time": "16:00"
  118. },
  119. {
  120. "AvailableMaternityAppointmentTimeId": "18",
  121. "Time": "16:30"
  122. },
  123. {
  124. "AvailableMaternityAppointmentTimeId": "19",
  125. "Time": "17:00"
  126. },
  127. {
  128. "AvailableMaternityAppointmentTimeId": "20",
  129. "Time": "17:30"
  130. },
  131. {
  132. "AvailableMaternityAppointmentTimeId": "21",
  133. "Time": "18:00"
  134. }
  135. ]
  136. }';
  137. }
  138. //获取产品下的服务 类型和价格
  139. public function actionGetExtraForProduct()
  140. {
  141. if (!$_POST) {
  142. echo '
  143. {
  144. "code" : "1001",
  145. "message": "请求方式不正确"
  146. }
  147. ';
  148. die();
  149. }
  150. $productId = Yii::app()->getRequest()->getParam('ProductId', '');
  151. if (!CommonFn::isMongoId($productId)) {
  152. echo '
  153. {
  154. "code" : "1001",
  155. "message": "请求的产品id不正确"
  156. }
  157. ';
  158. die();
  159. }
  160. $data['code'] = '0';
  161. $data['message'] = 'success';
  162. $product = Product::model()->get(new MongoId($productId));
  163. $data['data']['productId'] = (string)$product->_id;
  164. $data['data']['name'] = $product->name;
  165. $data['data']['status'] = $product->status;
  166. if (empty($product->extra)) {
  167. $data['data']['product']['price'] = $product->price;
  168. $data['data']['product']['type'] = $product->name;
  169. } else {
  170. foreach ($product->extra as $key => $value) {
  171. $data['data']['extra'][$key]['price'] = $value['price'];
  172. $data['data']['extra'][$key]['type'] = $value['type'];
  173. }
  174. }
  175. echo json_encode($data);
  176. }
  177. //获取区
  178. public function actionGetCoverage()
  179. {
  180. $station = Station::model()->get(new MongoId("57db39709f5160bb048b456a"));
  181. $coverage = $station->coverage;
  182. $data = array();
  183. $data['code'] = '0';
  184. $data['message'] = 'success';
  185. foreach ($coverage as $key => $value) {
  186. $data['data'][$key]['Id'] = $key;
  187. $data['data'][$key]['Name'] = $value['area'];
  188. }
  189. echo json_encode($data);
  190. }
  191. //创建订单
  192. public function actionCreateOrder()
  193. {
  194. if (!$_POST) {
  195. echo '
  196. {
  197. "code" : "1001",
  198. "message": "请求方式不正确"
  199. }
  200. ';
  201. die();
  202. }
  203. //创建订单必需参数
  204. $OrderNo = Yii::app()->getRequest()->getParam('OrderNo','');//Jingbai订单id
  205. $ProductId = Yii::app()->getRequest()->getParam('ProductId','');
  206. $name = Yii::app()->getRequest()->getParam('ExpectedMotherName','');
  207. $mobile = Yii::app()->getRequest()->getParam('Mobile','');
  208. $address = Yii::app()->getRequest()->getParam('Address','');
  209. $extra = json_decode(Yii::app()->getRequest()->getParam('extra',''),true);
  210. $memo = Yii::app()->getRequest()->getParam('Memo','');
  211. $areaId = intval(Yii::app()->getRequest()->getParam('areaId'));
  212. $count = intval(Yii::app()->getRequest()->getParam('Quantity',''));
  213. $finalPrice = intval(Yii::app()->getRequest()->getParam('Price',''));
  214. $booking_time = Yii::app()->getRequest()->getParam('AppointmentTime','');
  215. $invoice = intval(Yii::app()->getRequest()->getParam('invoice',0));//默认不需要
  216. $invoice_price = floatval(Yii::app()->getRequest()->getParam('invoicePrice',0));//发票价格
  217. //参数不能为空
  218. if (empty($OrderNo) || empty($ProductId) ||empty($name) ||empty($mobile) ||empty($address) ||empty($count) ||empty($finalPrice) ||empty($booking_time) ) {
  219. if(empty($OrderNo)){
  220. $tmp = 'orderNo';
  221. } else if(empty($ProductId)){
  222. $tmp = 'ProductId';
  223. }else if(empty($name)){
  224. $tmp = 'ExpectedMotherName';
  225. }else if(empty($mobile)){
  226. $tmp = 'Mobile';
  227. }else if(empty($address)){
  228. $tmp = 'Address';
  229. }else if(empty($count)){
  230. $tmp = 'Quantity';
  231. }else if(empty($finalPrice)){
  232. $tmp = 'Price';
  233. }else if(empty($booking_time)){
  234. $tmp = 'AppointmentTime';
  235. }
  236. echo '
  237. {
  238. "code": "10001",
  239. "message": "缺少参数,请检查'.$tmp.'"
  240. }
  241. ';
  242. die();
  243. }
  244. $rOrder = new ROrder();
  245. $rOrder->channel = 'jingbai';
  246. $rOrder->booking_time = intval($booking_time);
  247. $rOrder->order_time = time();
  248. $products[0]['product'] = new MongoId($ProductId);
  249. $products[0]['count'] = $count;
  250. if (!empty($extra)){
  251. $products[0]['extra'] = (object)$extra;
  252. } else {
  253. $extra =array();
  254. $products[0]['extra'] = (object)$extra;
  255. }
  256. $rOrder->products = $products;
  257. if (isset($extra['price'])) {
  258. $rOrder->price = floatval($extra['price']);
  259. }else{
  260. $rOrder->price = floatval($finalPrice);
  261. }
  262. $rOrder->final_price = floatval($finalPrice);
  263. if($invoice == 1){
  264. $rOrder->remark = '需要发票,发票价格为'.$invoice_price;
  265. }
  266. $rOrder->counts = $count;
  267. $rOrder->status = intval(1);
  268. $rOrder->memo = $memo;
  269. $rOrder->station = new MongoId("57db39709f5160bb048b456a");
  270. $station = Station::model()->get(new MongoId("57db39709f5160bb048b456a"));
  271. $coverage = $station->coverage[$areaId];
  272. $add['province'] = $coverage['province'];
  273. $add['city'] = $coverage['city'];
  274. $add['area'] = $coverage['area'];
  275. $add['detail'] = $address;
  276. $add['mobile'] = $mobile;
  277. $add['name'] = $name;
  278. $rOrder->OrderNo = $OrderNo;
  279. $rOrder->address = (object)$add;
  280. $rOrder->pay_channel = "jingbai";
  281. $addROrder_arr = array('OrderNo','channel', 'booking_time', 'order_time', 'price','products', 'final_price', 'counts','precedence', 'coupons', 'user', 'status', 'memo', 'remark', 'type', 'have_comment', 'station', 'address', 'pay_channel');
  282. $success = $rOrder->save(true, $addROrder_arr);
  283. if ($success) {
  284. echo '
  285. {
  286. "code": "0",
  287. "message": "success"
  288. }
  289. ';
  290. die();
  291. } else {
  292. echo '
  293. {
  294. "code": "10001",
  295. "message": "订单创建失败,请检查数据格式"
  296. }
  297. ';
  298. }
  299. }
  300. }