Order.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. namespace app\api\controller\v1\taxiUser;
  3. use app\api\controller\ApiController;
  4. use app\api\controller\v1\Wechat;
  5. use app\common\model\Taxi;
  6. use app\http\IResponse;
  7. use EasyWeChat\Factory;
  8. use think\App;
  9. use app\api\service\JWTAuth as IAuth;
  10. use think\Db;
  11. class Order extends ApiController
  12. {
  13. protected $model;
  14. public function __construct(App $app = null, IAuth $auth)
  15. {
  16. parent::__construct($app, $auth);
  17. $this->model = new \app\common\model\TaxiOrder();
  18. $this->taxiUserModel = new \app\api\model\taxi\User();
  19. }
  20. /**
  21. * 接单大厅订单列表
  22. * @return mixed|\think\response\Json
  23. * @throws \Lettered\Support\Exceptions\FailedException
  24. */
  25. public function index()
  26. {
  27. // 1. 传入用户位置
  28. $param = $this->request->param();
  29. // 数据校验
  30. $valid = $this->validate($param, [
  31. 'lng' => ['require', 'regex|-?((0|1?[0-7]?[0-9]?)(([.][0-9]{1,4})?)|180(([.][0]{1,4})?))'],
  32. 'lat' => ['require', 'regex|-?((0|1?[0-7]?[0-9]?)(([.][0-9]{1,4})?)|180(([.][0]{1,4})?))'],
  33. 'page' => 'require',
  34. ], [
  35. 'lng.require' => '缺少经度参数',
  36. 'lng.regex' => '经度参数有误',
  37. 'lat.require' => '缺少维度参数',
  38. 'lat.regex' => '维度参数有误'
  39. ]);
  40. // 错误
  41. if (true !== $valid) {
  42. return IResponse::failure($valid);
  43. }
  44. $limit = isset($param['pageSize']) ? $param['pageSize'] : 20;
  45. $taxiUser = $this->auth->guard('taxi_user')->user();
  46. if (!$taxiUser) {
  47. return IResponse::failure('用户不存在,或已被冻结');
  48. }
  49. $categoryIds = Taxi::where(['taxi_user_id' => $taxiUser['id']])->column('category_id');
  50. $categoryIds = array_unique($categoryIds);
  51. // 经纬度升序
  52. $lists = $this->model
  53. ->fieldRaw("* , TRUNCATE(( 6371 * acos (
  54. cos ( radians(" . $param['lat'] . ") )
  55. * cos( radians( lat ) )
  56. * cos( radians( lng ) - radians(" . $param['lng'] . ") )
  57. + sin ( radians(" . $param['lat'] . ") )
  58. * sin( radians( lat ) )
  59. )
  60. ), 2) AS distance")
  61. ->having('distance < 10')
  62. ->where(['status' => 2])
  63. ->where('lat', '>', 0)
  64. ->where('lng', '>', 0)
  65. ->where(function ($query) use ($categoryIds) {
  66. if ($categoryIds) {
  67. $query->whereIn('category_id', $categoryIds);
  68. }
  69. })
  70. ->order(['distance' => 'ASC'])
  71. ->limit((($param['page'] - 1) * $limit) . "," . $limit)
  72. ->select();
  73. $order = isset($lists[0]) ? $lists[0] : [];
  74. $lastOrderTime = isset($order['created_at']) ? $order['created_at']['val'] : time();
  75. $hasNewOrder = $order && $lastOrderTime != $taxiUser->last_order_time ? true : false;
  76. $taxiUser->last_order_time = $hasNewOrder ? $lastOrderTime : $taxiUser->last_order_time;
  77. $taxiUser->save();
  78. $count = $lists->count();
  79. return IResponse::success(['list' => $lists, 'total' => $count, 'hasNewOrder' => $hasNewOrder], '获取成功');
  80. }
  81. /**
  82. * 我的订单
  83. * @return mixed
  84. * @throws \Lettered\Support\Exceptions\FailedException
  85. */
  86. public function myOrder()
  87. {
  88. // 1. 传入用户位置
  89. $param = $this->request->param();
  90. $limit = isset($param['pageSize']) ? $param['pageSize'] : 20;
  91. $taxiUser = $this->auth->guard('taxi_user')->user();
  92. if (!$taxiUser) {
  93. return IResponse::failure('用户不存在,或已被冻结');
  94. }
  95. // 经纬度升序
  96. $lists = $this->model->with(['paylog', 'user', 'taxi', 'taxiUser'])
  97. ->where(['taxi_uid' => $taxiUser['id']])
  98. ->order(['created_at' => 'desc'])
  99. ->limit((($param['page'] - 1) * $limit) . "," . $limit)
  100. ->select();
  101. return IResponse::success($lists, '获取成功');
  102. }
  103. /**
  104. * 等待服务订单
  105. * @return mixed|\think\response\Json
  106. * @throws \Lettered\Support\Exceptions\FailedException
  107. */
  108. public function waitOrder()
  109. {
  110. $taxiUser = $this->auth->guard('taxi_user')->user();
  111. if (!$taxiUser) {
  112. return IResponse::failure('用户不存在,或已被冻结');
  113. }
  114. $info = model('common/TaxiOrder')->with(['paylog', 'user', 'taxi', 'taxiUser'])
  115. ->where(['taxi_uid' => $taxiUser['id']])
  116. ->whereIn('status', [2, 3])
  117. ->order('created_at', 'desc')
  118. ->find();
  119. if ($info) {
  120. return IResponse::success(!is_null($info) ? $info : [], '获取成功');
  121. } else {
  122. return IResponse::failure('获取失败');
  123. }
  124. }
  125. /**
  126. * 接单
  127. * @return mixed
  128. * @throws \Lettered\Support\Exceptions\FailedException
  129. */
  130. public function receive(App $app = null)
  131. {
  132. $param = $this->request->param();
  133. $id = isset($param['id']) ? $param['id'] : 0;
  134. $taxiUser = $this->auth->guard('taxi_user')->user();
  135. if (!$taxiUser) {
  136. return IResponse::failure('用户不存在,或已被冻结');
  137. }
  138. $info = model('common/TaxiOrder')
  139. ->where(['id' => $id, 'status' => 2])
  140. ->where('deleted_at', '<=', 0)
  141. ->order('created_at', 'desc')
  142. ->find();
  143. if (empty($info)) {
  144. return IResponse::failure('订单不存在,或已处理');
  145. }
  146. if (empty($info['category_id'])) {
  147. return IResponse::failure('车型参数错误或不匹配');
  148. }
  149. $taxi = Taxi::where(['taxi_user_id' => $taxiUser['id'], 'category_id' => $info['category_id']])
  150. ->where('status', '>', 0)
  151. ->order('status', 'asc')
  152. ->order('created_at', 'desc')
  153. ->find();
  154. if (empty($taxi)) {
  155. return IResponse::failure('车型参数错误或您的车型不匹配');
  156. }
  157. if ($taxi['status'] != 1) {
  158. return IResponse::failure('您的车辆当前不可再接单,请先完成订单或联系客服');
  159. }
  160. // 写入数据
  161. Db::startTrans();
  162. $info['status'] = 3;
  163. $info['served'] = 1;
  164. $info['taxi_id'] = $taxi['id'];
  165. $info['taxi_uid'] = $taxiUser['id'];
  166. $info['settle_price'] = $info['price'];
  167. if (!$info->save()) {
  168. Db::rollback();
  169. return IResponse::failure('接单失败');
  170. }
  171. if (!Taxi::where(['taxi_user_id' => $taxiUser['id'], 'id' => $taxi['id']])->update(['status' => 2])) {
  172. Db::rollback();
  173. return IResponse::failure('接单失败');
  174. }
  175. push_socket_data('motor', [
  176. 'id' => $info['id'],
  177. 'msg' => '有新的摩的订单等待处理,点击前往!'
  178. ]);
  179. Db::commit();
  180. // 模板消息
  181. try {
  182. $tplTitle = "人人接 - 摩的服务";
  183. $thing7 = '电话请保持通畅,师傅正在赶往路上,请稍候';
  184. $user = model('common/Users')->where(['id' => $info['user_id']])->find();
  185. if ($user && $user['open_id']) {
  186. $tpl1 = [
  187. 'template_id' => 'bhkIzzwYpjjXJJ9lPdfXIsUYRrfSz52aJel1n74AA7A', // 所需下发的订阅模板id
  188. 'touser' => 'o11PJ5QDWJnIa1kPKsvvStXj243U',
  189. 'data' => [
  190. ]
  191. ];
  192. $tpl1['touser'] = $user['open_id'];
  193. $tpl1['data'] = [
  194. 'character_string1' => [
  195. 'value' => $info['order_no'],
  196. ],
  197. 'thing3' => [
  198. 'value' => $tplTitle,
  199. ],
  200. 'amount4' => [
  201. 'value' => $info['price'],
  202. ],
  203. 'date2' => [
  204. 'value' => date("Y/m/d H:i:s"),
  205. ],
  206. 'thing7' => [
  207. 'value' => $thing7,
  208. ]
  209. ];
  210. // 模板消息
  211. $wechat = sys_config('', 'wechat');
  212. $config = [
  213. 'app_id' => $wechat['mini_appid'],
  214. 'secret' => $wechat['mni_secret_key'],
  215. 'response_type' => 'array',
  216. 'log' => [
  217. 'level' => 'debug',
  218. 'file' => app()->getRuntimePath() . 'log/' . date('Ym') . '/wechat_debug.log',
  219. ],
  220. ];
  221. $wechat = Factory::miniProgram($config);
  222. $wechat->subscribe_message->send($tpl1);
  223. }
  224. } catch (\Exception $exception) {
  225. }
  226. return IResponse::success('接单成功,请尽快前往客户所在地接驾');
  227. }
  228. /**
  229. * 已送达
  230. * @return mixed
  231. * @throws \Lettered\Support\Exceptions\FailedException
  232. */
  233. public function delivered()
  234. {
  235. $param = $this->request->param();
  236. $id = isset($param['id']) ? $param['id'] : 0;
  237. $taxiUser = $this->auth->guard('taxi_user')->user();
  238. if (!$taxiUser) {
  239. return IResponse::failure('用户不存在,或已被冻结');
  240. }
  241. if ($taxiUser['user_id'] <= 0) {
  242. return IResponse::failure('账户未绑定用户');
  243. }
  244. $info = model('common/TaxiOrder')
  245. ->where(['taxi_uid' => $taxiUser['id'], 'id' => $id, 'status' => 3])
  246. ->order('created_at', 'desc')
  247. ->find();
  248. if (empty($info)) {
  249. return IResponse::failure('订单不存在,或已处理');
  250. }
  251. if ($info['taxi_uid'] <= 0 || $info['taxi_id'] <= 0) {
  252. return IResponse::failure('参数错误');
  253. }
  254. // 写入数据
  255. Db::startTrans();
  256. $info['status'] = 4;
  257. if (!$info->save()) {
  258. Db::rollback();
  259. return IResponse::failure('确认失败');
  260. }
  261. if (!Taxi::where(['taxi_user_id' => $taxiUser['id'], 'id' => $info['taxi_id']])->update(['status' => 1])) {
  262. Db::rollback();
  263. return IResponse::failure('确认失败');
  264. }
  265. // 到账
  266. if ($info['settle_price']) {
  267. if (!model('common/Users')->changePartnership(
  268. $taxiUser['user_id'],
  269. $info['settle_price'],
  270. "订单完成,金额到账【" . $info['settle_price'] . "】",
  271. 30,
  272. true
  273. )) {
  274. Db::rollback();
  275. return IResponse::failure('确认失败');
  276. }
  277. }
  278. // 订单结算给摩的代理
  279. model('\app\api\model\taxi\Award')->send($info);
  280. Db::commit();
  281. return IResponse::success('确认成功');
  282. }
  283. }