Order.php 12 KB

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