Order.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 < 10')
  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. // 写入数据
  170. Db::startTrans();
  171. $info['status'] = 3;
  172. $info['served'] = 1;
  173. $info['taxi_id'] = $taxi['id'];
  174. $info['taxi_uid'] = $taxiUser['id'];
  175. $info['settle_price'] = $info['price'];
  176. if (!$info->save()) {
  177. Db::rollback();
  178. return IResponse::failure('接单失败');
  179. }
  180. if (!Taxi::where(['taxi_user_id' => $taxiUser['id'], 'id' => $taxi['id']])->update(['status' => 2])) {
  181. Db::rollback();
  182. return IResponse::failure('接单失败');
  183. }
  184. push_socket_data('motor', [
  185. 'id' => $info['id'],
  186. 'msg' => '有新的摩的订单等待处理,点击前往!'
  187. ]);
  188. Db::commit();
  189. // 模板消息
  190. try {
  191. $tplTitle = "人人接 - 摩的服务";
  192. $thing7 = '电话请保持通畅,师傅正在赶往路上,请稍候';
  193. $user = model('common/Users')->where(['id' => $info['user_id']])->find();
  194. if ($user && $user['open_id']) {
  195. $tpl1 = [
  196. 'template_id' => 'bhkIzzwYpjjXJJ9lPdfXIsUYRrfSz52aJel1n74AA7A', // 所需下发的订阅模板id
  197. 'touser' => 'o11PJ5QDWJnIa1kPKsvvStXj243U',
  198. 'data' => [
  199. ]
  200. ];
  201. $tpl1['touser'] = $user['open_id'];
  202. $tpl1['data'] = [
  203. 'character_string1' => [
  204. 'value' => $info['order_no'],
  205. ],
  206. 'thing3' => [
  207. 'value' => $tplTitle,
  208. ],
  209. 'amount4' => [
  210. 'value' => $info['price'],
  211. ],
  212. 'date2' => [
  213. 'value' => date("Y/m/d H:i:s"),
  214. ],
  215. 'thing7' => [
  216. 'value' => $thing7,
  217. ]
  218. ];
  219. // 模板消息
  220. $wechat = sys_config('', 'wechat');
  221. $config = [
  222. 'app_id' => $wechat['mini_appid'],
  223. 'secret' => $wechat['mni_secret_key'],
  224. 'response_type' => 'array',
  225. 'log' => [
  226. 'level' => 'debug',
  227. 'file' => app()->getRuntimePath() . 'log/' . date('Ym') . '/wechat_debug.log',
  228. ],
  229. ];
  230. $wechat = Factory::miniProgram($config);
  231. $wechat->subscribe_message->send($tpl1);
  232. }
  233. } catch (\Exception $exception) {
  234. }
  235. return IResponse::success('接单成功,请尽快前往客户所在地接驾');
  236. }
  237. /**
  238. * 已送达
  239. * @return mixed
  240. * @throws \Lettered\Support\Exceptions\FailedException
  241. */
  242. public function delivered()
  243. {
  244. $param = $this->request->param();
  245. $id = isset($param['id']) ? $param['id'] : 0;
  246. $taxiUser = $this->auth->guard('taxi_user')->user();
  247. if (!$taxiUser) {
  248. return IResponse::failure('用户不存在,或已被冻结');
  249. }
  250. if ($taxiUser['user_id'] <= 0) {
  251. return IResponse::failure('账户未绑定用户');
  252. }
  253. $info = model('common/TaxiOrder')
  254. ->where(['taxi_uid' => $taxiUser['id'], 'id' => $id, 'status' => 3])
  255. ->order('created_at', 'desc')
  256. ->find();
  257. if (empty($info)) {
  258. return IResponse::failure('订单不存在,或已处理');
  259. }
  260. if ($info['taxi_uid'] <= 0 || $info['taxi_id'] <= 0) {
  261. return IResponse::failure('参数错误');
  262. }
  263. // 写入数据
  264. Db::startTrans();
  265. $info['status'] = 4;
  266. if (!$info->save()) {
  267. Db::rollback();
  268. return IResponse::failure('确认失败');
  269. }
  270. if (!Taxi::where(['taxi_user_id' => $taxiUser['id'], 'id' => $info['taxi_id']])->update(['status' => 1])) {
  271. Db::rollback();
  272. return IResponse::failure('确认失败');
  273. }
  274. // 到账
  275. if ($info['settle_price']) {
  276. if (!model('common/Users')->changePartnership(
  277. $taxiUser['user_id'],
  278. $info['settle_price'],
  279. "订单完成,金额到账【" . $info['settle_price'] . "】",
  280. 30,
  281. true
  282. )) {
  283. Db::rollback();
  284. return IResponse::failure('确认失败');
  285. }
  286. }
  287. // 订单结算给摩的代理
  288. model('\app\api\model\taxi\Award')->send($info);
  289. Db::commit();
  290. return IResponse::success('确认成功');
  291. }
  292. }