Order.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\service\OrderService;
  4. use app\api\validate\BaseApiValidate;
  5. use app\api\validate\OrderValidate;
  6. use app\common\controller\Api;
  7. use think\Request;
  8. /**
  9. * 订单
  10. */
  11. class Order extends Api
  12. {
  13. private $user;
  14. private $service;
  15. protected $noNeedLogin = ["getOrder", "virtualMobile"];
  16. public function __construct(Request $request = null)
  17. {
  18. parent::__construct($request);
  19. $this->user = $this->auth->getUser();
  20. $this->service = new OrderService();
  21. }
  22. private function check_option_services($option_services)
  23. {
  24. $option_services = json_decode(str_ireplace("&quot;", "\"", $option_services), true);
  25. if (null === $option_services || !is_array($option_services))
  26. $this->error("选择的服务数据传输格式错误!");
  27. if (0 === count($option_services))
  28. $this->error("请选择服务!");
  29. $s_ids = [];
  30. $fmt_services = [];
  31. foreach ($option_services as $item) {
  32. $item_id = (int)$item['service_id'];
  33. $s_ids[] = $item_id;
  34. $fmt_services[$item_id] = [
  35. "service_id" => $item_id,
  36. "quantity" => (int)$item['quantity']
  37. ];
  38. }
  39. return ['s_ids' => $s_ids, "fmt_services" => $fmt_services];
  40. }
  41. public function fetchOrder($status = null, $page = 1, $size = 10)
  42. {
  43. $array = $status ? explode(",", $status) : [];
  44. foreach ($array as $item) {
  45. if (!in_array($item, array_merge(ALL_ORDER_STATUS, ["*"])))
  46. $this->error("状态错误!");
  47. }
  48. $r = $this->service->fetchOrder($this->user->id, in_array("*", $array) ? ALL_ORDER_STATUS : $array, $page, $size);
  49. $this->success($r);
  50. }
  51. /**
  52. * 获取订单详情
  53. */
  54. public function getOrder()
  55. {
  56. $params = (new BaseApiValidate(["order_id" => "require|number"]))->checkBody();
  57. $r = $this->service->getOrder($params["order_id"]);
  58. if (null === $r)
  59. $this->error("订单不存在!");
  60. $this->success($r);
  61. }
  62. /**
  63. * @throws \think\exception\DbException
  64. */
  65. public function create()
  66. {
  67. $params = (new OrderValidate())->checkBody();
  68. $store_id = isset($params['store_id']) && $params['store_id'] > 0 ? $params['store_id'] : null;
  69. $massager_id = isset($params['massager_id']) && $params['massager_id'] > 0 ? $params['massager_id'] : null;
  70. if ($store_id == null && $massager_id == null)
  71. $this->error("下单主体错误!");
  72. $option_services = $this->check_option_services($params['option_services']);
  73. $r = $this->service->create($this->user, $option_services['s_ids'], $option_services['fmt_services'], $massager_id, $store_id, $massager_id > 0);
  74. $r->code() ? $this->success($r->data()) : $this->error($r->msg());
  75. }
  76. public function reorder()
  77. {
  78. $params = (new BaseApiValidate(["order_id" => "require|number"]))->checkBody();
  79. $r = $this->service->reorder($params["order_id"]);
  80. $r->code() ? $this->success($r->data()) : $this->error($r->msg());
  81. }
  82. /**
  83. * 变更服务项
  84. * @throws \think\exception\DbException
  85. */
  86. public function changeOrderService()
  87. {
  88. $params = (new BaseApiValidate([
  89. "order_id" => "require|number",
  90. "option_services" => "require"
  91. ]))->checkBody();
  92. $option_services = $this->check_option_services($params['option_services']);
  93. $r = $this->service->changeOrderService($params["order_id"], $option_services['s_ids'], $option_services['fmt_services']);
  94. $r->code() ? $this->success($r->data()) : $this->error($r->msg());
  95. }
  96. /**
  97. * 选择用户地址
  98. */
  99. public function bindUserArea()
  100. {
  101. $params = (new BaseApiValidate([
  102. "order_id" => "require|number",
  103. "user_area_id" => "require|number"
  104. ]))->checkBody();
  105. $r = $this->service->bindUserArea($this->user->id, $params['order_id'], $params['user_area_id']);
  106. $r->code() ? $this->success($r->data()) : $this->error($r->msg());
  107. }
  108. /**
  109. * 选择优惠券
  110. * @throws \think\db\exception\DataNotFoundException
  111. * @throws \think\db\exception\ModelNotFoundException
  112. * @throws \think\exception\DbException
  113. */
  114. public function bindVoucher()
  115. {
  116. $params = (new BaseApiValidate([
  117. "order_id" => "require|number",
  118. "user_voucher_id" => "require|number"
  119. ]))->checkBody();
  120. $r = $this->service->bindVoucher($this->user->id, $params['order_id'], $params['user_voucher_id']);
  121. $r->code() ? $this->success($r->data()) : $this->error($r->msg());
  122. }
  123. /**
  124. * @throws \think\exception\DbException
  125. */
  126. public function modifyOrder()
  127. {
  128. $params = (new BaseApiValidate([
  129. "order_id" => "require|number",
  130. ]))->checkBody();
  131. $description = isset($params['description']) && strlen($params['description']) > 0 ? $params['description'] : null;
  132. $trip_type = null;
  133. $service_start_date = null;
  134. if (isset($params["trip_type"]) && in_array($params["trip_type"], [\E_TRIP_TYPE::Taxi, \E_TRIP_TYPE::Bus]))
  135. $trip_type = $params['trip_type'];
  136. if (isset($params["service_start_date"]) && strtotime($params["service_start_date"]) > 0)
  137. $service_start_date = $params['service_start_date'];
  138. $r = $this->service->modifyOrder(
  139. $this->user->id,
  140. $params['order_id'],
  141. $description,
  142. $trip_type,
  143. $service_start_date,
  144. isset($params["balance_deduction"]) && $params["balance_deduction"] >= 0 ? $params["balance_deduction"] : null
  145. );
  146. $r->code() ? $this->success($r->data()) : $this->error($r->msg());
  147. }
  148. public function updateOrderOpenMembership()
  149. {
  150. $params = (new BaseApiValidate([
  151. "order_id" => "require|number",
  152. "open_membership" => "require|number",
  153. ]))->checkBody();
  154. $r = $this->service->updateOrderOpenMembership($this->user->id, $params["order_id"], 1 == $params["open_membership"]);
  155. $r->code() ? $this->success($r->data()) : $this->error($r->msg());
  156. }
  157. /**
  158. * @throws \think\exception\DbException
  159. */
  160. public function calculateTripAmount()
  161. {
  162. $params = (new BaseApiValidate([
  163. "order_id" => "require|number",
  164. ]))->checkBody();
  165. $r = $this->service->calculateTripAmount($this->user->id, $params['order_id']);
  166. $r->code() ? $this->success($r->data()) : $this->error($r->msg());
  167. }
  168. /**
  169. * @throws \think\exception\DbException
  170. */
  171. public function payment()
  172. {
  173. $params = (new BaseApiValidate([
  174. "order_id" => "require|number",
  175. "payment_type" => "require",
  176. "platform" => "require",
  177. ]))->checkBody();
  178. if (!in_array($params['payment_type'], ALL_ORDER_PAY_TYPE))
  179. $this->error("支付方式错误!");
  180. if (!in_array($params['platform'], ["web", "applet", "app"]))
  181. $this->error("平台错误!");
  182. $r = $this->service->payment($this->user->id, $params['order_id'], $params['payment_type'], $params["platform"], $params["description"]);
  183. $r->code() ? $this->success($r->data()) : $this->error($r->msg(), null, $r->code());
  184. }
  185. /**
  186. * @throws \think\exception\DbException
  187. */
  188. public function cancel()
  189. {
  190. $params = (new BaseApiValidate([
  191. "order_id" => "require|number",
  192. "reason" => "require"
  193. ]))->checkBody();
  194. $r = $this->service->cancel($params["order_id"], $params["reason"]);
  195. $r->code() ? $this->success($r->data()) : $this->error($r->msg());
  196. }
  197. public function comment()
  198. {
  199. $params = (new BaseApiValidate([
  200. "order_id" => "require|number",
  201. "is_anonymity" => "require|between:0,1",
  202. "star" => "between:0,5",
  203. "content" => "length:1,1000",
  204. "negative" => "between:0,1"
  205. ]))->checkBody();
  206. if (!isset($params["content"])) {
  207. $params["content"] = "服务好素质高";
  208. }
  209. $r = $this->service->comment($params["order_id"], $params);
  210. $r->code() ? $this->success($r->data()) : $this->error($r->msg());
  211. }
  212. public function appeal()
  213. {
  214. $params = (new BaseApiValidate([
  215. "order_id" => "require|number",
  216. "appeal_reason" => "require",
  217. ]))->checkBody();
  218. $r = $this->service->appeal($this->user->id, $params["order_id"], $params["appeal_reason"]);
  219. $r->code() ? $this->success($r->data()) : $this->error($r->msg());
  220. }
  221. public function virtualMobile()
  222. {
  223. $params = (new BaseApiValidate([
  224. "order_id" => "require|number",
  225. "type" => "require",
  226. ]))->checkBody();
  227. $this->success("15574920253");
  228. }
  229. }