Order.php 8.9 KB

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