Order.php 10 KB

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