ThirdOrderLogic.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. <?php
  2. namespace app\common\logic;
  3. use app\api\logic\LoginLogic;
  4. use app\common\{enum\GoodsEnum,
  5. enum\PayEnum,
  6. enum\WorkEnum,
  7. model\goods\Goods,
  8. model\orders\RechargeOrder,
  9. model\recharge\OrderGoods,
  10. model\setting\PostageRegion,
  11. model\third\ThirdGoods,
  12. model\third\ThirdOrders,
  13. model\user\User,
  14. model\works\ServiceWork,
  15. model\works\ServiceWorkAppointmentLog,
  16. model\works\ServiceWorkLog};
  17. use think\facade\Db;
  18. use app\common\model\service_area\ServiceArea;
  19. use think\facade\Log;
  20. /**
  21. * 第三方平台下单逻辑层
  22. * Class UserLogic
  23. * @package app\shopapi\logic
  24. */
  25. class ThirdOrderLogic extends BaseLogic
  26. {
  27. protected const user_token = [
  28. "accessToken"=>"V2-29d801e316e9ec4eb88b3b9abd8dbda490367af539d97d754420056dc5c1329a2c1aff7d4d54729ff622d96ff7c9910aa930f7b7ad27910d199c15029ffc71aa4a84142d75b60f4a34b71a8390a89d8c01a4dd385232e4e9c9204fb5934eda89",
  29. "expireIn"=> 2592000,
  30. "opBizCode"=> "AG5MALO18KG6I5DL8LVONRSRF5G",
  31. "opBizName"=> "武汉开源节流科技有限公司",
  32. "refreshToken"=> "ab8d15b4028b7bd6aca383d230b87450436d301a2996b6bb3151d668dd2d015d68d8393e66fb2a59aa10146c70dd30728b473358ec0d156dc0bc5cc5e574a109a5680ab49c393fa9d2693ef5519ff41a16115b81259b58c71a54d79e23f26381",
  33. "scope"=> "dingdan,generalreserve,merchantreceipt,shangpin"
  34. ];
  35. protected const shop_token1 = [
  36. "accessToken"=> "V2-9667b1ce4ffaabe453d4ce847888c788c9c61762df9230ab342caf33e2b4a735ade021c559312195b7f01f30f3bdd44cf6f8528b1fd1ad1541ceabeb7da61156c852e314aae26eef9d52fb41fda585c566c18e05dc3cbb61733b2f71c05a78f6",
  37. "expireIn"=> 2592000,
  38. "opBizCode"=> "AH14KGRJ76O8PFH4GCO67F0CUKC",
  39. "opBizName"=> "众盾闪修 家电清洗 武汉",
  40. "refreshToken"=> "fd3da11174eed4c3d6b4d117f87ab7b62a72603627d2d597bed82dddb103694375ed9cab1944bc7437a204b171303cb3c0976fbddc88708e75f2169c532a28614ff36c68486a75cb9dbf0391e39d6dfa8bec0bf18b54b5d2119d895788a2281c",
  41. "scope"=> "merchantdata,poiqrcode,tuangou,ugc,yuding"
  42. ];
  43. protected const shop_token2 = [
  44. "accessToken"=> "V2-26f91ef8b515c59b97b9b4404e77dc2b2fc14b9715e3be7edb9f3f86178e193e21513ea76f021c503890172cad1863ea544d6b4c7b8b3790374ba912c15ad9e71e32ba0b195d38131da3584a59603ea55e81d1b3203a57ae280c36cbb67f5ffc",
  45. "expireIn"=> 2592000,
  46. "opBizCode"=> "AE7MKOJAV67338LIC3UD0K5TGIO",
  47. "opBizName"=> "众盾闪修 家电维修 武汉",
  48. "refreshToken"=> "bd57846c8817125f8ad7f4eb4ccad4af80c808f30c8b22e38476fc78c008b568376db34916f190e086272f6da315ac2ce4daefac6e61e4ce8889500f293e95537f6939f810f0466c8181086b052907c68b01a6d59ac5fe78abbae0ecd54578f5",
  49. "scope"=> "merchantdata,poiqrcode,tuangou,ugc,yuding"
  50. ];
  51. protected const developerId = 114657;
  52. protected const assessKey = '8471lgvnv0qcjpx6';
  53. /**
  54. * 处理订单
  55. * @param array $order
  56. * @return bool
  57. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\DbException
  59. * @throws \think\db\exception\ModelNotFoundException
  60. */
  61. public static function orderhandle(array $order)
  62. {
  63. $message = json_decode($order['message'], true);
  64. //查询用户是否存在
  65. $userName = $message['userName'];
  66. $mobile = $message['mobile'];
  67. $userAddress = json_decode($message['userAddress']);
  68. $user = User::where(['mobile' =>$mobile])->findOrEmpty();
  69. $product_message = json_decode($message['products'],true);
  70. if($user->isEmpty()){
  71. $user = LoginLogic::register([
  72. 'password'=>'123456',
  73. 'nickname'=>$userName,
  74. 'account'=>$mobile,
  75. 'mobile'=>$mobile,
  76. 'channel'=>1,
  77. 'user_type'=>0
  78. ]);
  79. }
  80. //查询美团商品和平台商品的对应关系
  81. $productId = $message['productId'];
  82. $product = ThirdGoods::where(['third_type'=>1,'product_id'=>$productId])->findOrEmpty();
  83. //生成第三方订单信息
  84. $orderData = [
  85. 'orderId'=>$message['orderId'],
  86. 'uni_order_id'=>$product_message[0]['uni_order_id'],
  87. 'productId'=>$message['productId'],
  88. 'productName'=>$message['productName'],
  89. 'request_msg'=>json_encode($order,JSON_UNESCAPED_UNICODE),
  90. 'bookStatus'=>1,
  91. 'third_type'=>1,//美团
  92. ];
  93. //判断美团的预约订单是否生成
  94. if(!$product->isEmpty()){
  95. //查询美团预约订单是否生成
  96. $thirdOrder = ThirdOrders::where(['uni_order_id'=>$product_message[0]['uni_order_id']])->findOrEmpty();
  97. if(!$thirdOrder->isEmpty() and $thirdOrder['work_id'] != 0){
  98. //生成第三方订单信息
  99. $orderData['sync_status'] = 1;
  100. $orderData['work_id'] = $thirdOrder['work_id'];
  101. }
  102. }
  103. $thirdOrders = ThirdOrders::where('orderId',$message['orderId'])->findOrEmpty();
  104. //生成未同步预约订单信息
  105. if($thirdOrders->isEmpty()){
  106. $thirdOrders = ThirdOrders::create($orderData);
  107. }
  108. if(!$thirdOrders->isEmpty()){
  109. $orderData['work_id'] = $thirdOrders->work_id;
  110. }
  111. if(empty($orderData['work_id']) and !$product->isEmpty() and !empty($product['goods_id'])){
  112. $goods = Goods::findOrEmpty($product['goods_id']);
  113. $pattern = '/(?:省|市)([\w\s]+市)/u';
  114. Db::startTrans();
  115. try {
  116. if (preg_match($pattern, $userAddress->receiver_address, $matches)) {
  117. $city = $matches[1];
  118. }
  119. $area_city = PostageRegion::where(['level'=>2,'name'=>$city])->value('id');
  120. //生成服务工单
  121. $work_data = [
  122. 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
  123. 'real_name' => $userName,
  124. 'mobile' => $mobile,
  125. 'address' => $userAddress->receiver_address,
  126. 'province' => 0,
  127. 'city' => $area_city??0,
  128. 'area_name' => $userAddress->receiver_address,
  129. 'title' => $goods->goods_name,
  130. 'category_type' => $goods['category_type'],
  131. 'goods_category_ids' => $goods['goods_category_ids'],
  132. 'goods_category_id' => $goods['goods_category_id'],
  133. 'base_service_fee' => $goods['base_service_fee'],
  134. 'service_fee' => $product['product_price'],
  135. 'work_pay_status'=>WorkEnum::IS_PAY_STATUS,
  136. 'appointment_time' => strtotime($message['begintime']),
  137. 'user_id'=>$user['id'],
  138. 'lon'=>!empty($userAddress->longitude)?$userAddress->longitude:0,
  139. 'lat'=>!empty($userAddress->latitude)?$userAddress->latitude:0,
  140. 'property_activity_id'=>0,
  141. 'user_equity_id'=>0,
  142. 'third_type'=>1,
  143. 'work_total'=>$product['product_price'],
  144. 'work_amount'=>$product['product_price']
  145. ];
  146. $service_area_id = ServiceArea::serviceAreaId(['lon' => $work_data['lon'], 'lat' => $work_data['lat']]);
  147. $work_data['service_area_id'] = $service_area_id;
  148. $service_work = ServiceWork::create($work_data);
  149. //生成服务订单
  150. $data = [
  151. 'work_id'=> $service_work['id'],
  152. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  153. 'order_type'=>0,//服务订单
  154. 'order_terminal' => 1,
  155. 'payment_type'=>$goods['goods_payment_type']==GoodsEnum::ISGOODS_PAYMENT_TYPE?0:1,
  156. 'user_id' => $user['id'],
  157. 'pay_status' => PayEnum::ISPAID,
  158. 'coupon_id'=>0,
  159. 'coupon_price'=>0,
  160. 'pay_way' => 4,
  161. 'order_total' => $product['product_price'],
  162. 'order_amount' => $product['product_price'],
  163. 'paid_amount'=> $product['product_price'],
  164. ];
  165. $order = RechargeOrder::create($data);
  166. //生成订单服务详情
  167. OrderGoods::create([
  168. 'sn' => $order['sn'],
  169. 'goods_id' => $goods['id'],
  170. 'category_type' => $goods['category_type'],
  171. 'goods_category_ids' => $goods['goods_category_ids'],
  172. 'goods_category_id' => $goods['goods_category_id'],
  173. 'goods_name' => $goods['goods_name'],
  174. 'goods_image' => $goods['goods_image'],
  175. 'goods_video' => $goods['goods_video'],
  176. 'goods_number' => $goods['goods_number'],
  177. 'good_unit' => $goods['good_unit'],
  178. 'goods_size' => $goods['goods_size'],
  179. 'goods_type' => $goods['goods_type'],
  180. 'goods_brand' => $goods['goods_brand'],
  181. 'install_guide' => $goods['install_guide'],
  182. 'goods_payment_type'=>$goods['goods_payment_type'],
  183. 'base_service_fee' => $goods['base_service_fee'],
  184. 'service_total' => $goods['service_total'],
  185. 'service_fee' => $product['product_price'],
  186. 'service_image' => $goods['service_image'],
  187. 'warranty_period'=>$goods['warranty_period'],
  188. 'fee_schedule' => $goods['fee_schedule'],
  189. 'goods_status' => $goods['goods_status'],
  190. ]);
  191. //判断是否是一口价预支付订单,支付成功后生成尾款订单
  192. if(!$goods->isEmpty() and $goods->goods_payment_type == 3 and bcsub($goods->base_service_fee,$goods->service_fee,2)>=0){
  193. //新增待支付尾款
  194. $order_weikuan_data = [
  195. 'order_type'=>$order['order_type'],
  196. 'sn'=>generate_sn(\app\common\model\orders\RechargeOrder::class, 'sn'),
  197. 'work_id'=>$order['work_id'],
  198. 'user_id'=>$order['user_id'],
  199. 'payment_type'=>2,
  200. 'order_total'=>bcsub($goods->base_service_fee,$product['product_price'],2),
  201. 'order_amount'=>bcsub($goods->base_service_fee,$product['product_price'],2),
  202. 'order_terminal'=>$order['order_terminal']
  203. ];
  204. \app\common\model\recharge\RechargeOrder::create($order_weikuan_data);
  205. }
  206. $thirdOrders->work_id = $service_work->id;
  207. $thirdOrders->save();
  208. self::bookresultcallback($order['opBizCode'],$message['orderId'],2);
  209. Db::commit();
  210. } catch (\Exception $e) {
  211. Db::rollback();
  212. self::setError($e->getMessage());
  213. return false;
  214. }
  215. }
  216. if(!empty($orderData['work_id']) and !$product->isEmpty() and !empty($product['goods_id'])) {
  217. //更改预约时间
  218. $serviceWorkInfo = ServiceWork::find($orderData['work_id']);
  219. if(!empty($serviceWorkInfo)){
  220. $appointment_time = $message['begintime'];
  221. ServiceWorkAppointmentLog::create([
  222. 'work_id'=>$serviceWorkInfo->id,
  223. 'last_appointment_time'=>strtotime($serviceWorkInfo->appointment_time),
  224. 'this_appointment_time'=>strtotime($appointment_time),
  225. ]);
  226. //修改预计完成时间
  227. if ($serviceWorkInfo->estimated_finish_time) {
  228. $serviceWorkInfo->estimated_finish_time = strtotime($appointment_time) + (strtotime($serviceWorkInfo->estimated_finish_time) - strtotime($serviceWorkInfo->appointment_time));
  229. }
  230. $serviceWorkInfo->appointment_time = strtotime($appointment_time);
  231. $serviceWorkInfo->save();
  232. }
  233. }
  234. return true;
  235. }
  236. //手动添加订单
  237. public static function submitOrders(array $message)
  238. {
  239. //查询用户是否存在
  240. $userName = $message['userName'];
  241. $mobile = $message['mobile'];
  242. $user = User::where(['mobile' =>$mobile])->findOrEmpty();
  243. if($user->isEmpty()){
  244. $user = LoginLogic::register([
  245. 'password'=>'123456',
  246. 'nickname'=>$userName,
  247. 'account'=>$mobile,
  248. 'mobile'=>$mobile,
  249. 'channel'=>1,
  250. 'user_type'=>0,
  251. ]);
  252. }
  253. $goods = Goods::findOrEmpty($message['goods_id']);
  254. $pattern = '/(?:省|市)([\w\s]+市)/u';
  255. Db::startTrans();
  256. try {
  257. if (preg_match($pattern, $message['userAddress'], $matches)) {
  258. $city = $matches[1];
  259. }
  260. $area_city = PostageRegion::where(['level'=>2,'name'=>$city])->value('id');
  261. //生成服务工单
  262. $work_data = [
  263. 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
  264. 'real_name' => $userName,
  265. 'mobile' => $mobile,
  266. 'address' => $message['userAddress'],
  267. 'province' => 0,
  268. 'city' => $area_city??0,
  269. 'area_name' => $area_city??'',
  270. 'title' => $goods->goods_name,
  271. 'category_type' => $goods['category_type'],
  272. 'goods_category_ids' => $goods['goods_category_ids'],
  273. 'goods_category_id' => $goods['goods_category_id'],
  274. 'base_service_fee' => $goods['base_service_fee'],
  275. 'service_fee' => $message['amount'],
  276. 'work_pay_status'=>WorkEnum::IS_PAY_STATUS,
  277. 'appointment_time' => strtotime($message['appointment_time']),
  278. 'user_id'=>$user['id'],
  279. 'lon'=>$message['lon'],
  280. 'lat'=>$message['lat'],
  281. 'property_activity_id'=>0,
  282. 'user_equity_id'=>0,
  283. 'third_type'=>1,
  284. 'work_total'=>$goods['base_service_fee'],
  285. 'work_amount'=>$goods['base_service_fee']
  286. ];
  287. $service_area_id = ServiceArea::serviceAreaId(['lon' => $work_data['lon'], 'lat' => $work_data['lat']]);
  288. $work_data['service_area_id'] = $service_area_id;
  289. $service_work = ServiceWork::create($work_data);
  290. //生成服务订单
  291. $data = [
  292. 'work_id'=> $service_work['id'],
  293. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  294. 'order_type'=>0,//服务订单
  295. 'order_terminal' => 1,
  296. 'payment_type'=>$goods['goods_payment_type']==GoodsEnum::ISGOODS_PAYMENT_TYPE?0:1,
  297. 'user_id' => $user['id'],
  298. 'pay_status' => PayEnum::ISPAID,
  299. 'coupon_id'=>0,
  300. 'coupon_price'=>0,
  301. 'pay_way' => 4,
  302. 'order_total' => $message['amount'],
  303. 'order_amount' => $message['amount'],
  304. 'paid_amount'=>$message['amount']
  305. ];
  306. $order = RechargeOrder::create($data);
  307. //生成订单服务详情
  308. OrderGoods::create([
  309. 'sn' => $order['sn'],
  310. 'goods_id' => $goods['id'],
  311. 'category_type' => $goods['category_type'],
  312. 'goods_category_ids' => $goods['goods_category_ids'],
  313. 'goods_category_id' => $goods['goods_category_id'],
  314. 'goods_name' => $goods['goods_name'],
  315. 'goods_image' => $goods['goods_image'],
  316. 'goods_video' => $goods['goods_video'],
  317. 'goods_number' => $goods['goods_number'],
  318. 'good_unit' => $goods['good_unit'],
  319. 'goods_size' => $goods['goods_size'],
  320. 'goods_type' => $goods['goods_type'],
  321. 'goods_brand' => $goods['goods_brand'],
  322. 'install_guide' => $goods['install_guide'],
  323. 'goods_payment_type'=>$goods['goods_payment_type'],
  324. 'base_service_fee' => $goods['base_service_fee'],
  325. 'service_total' => $goods['service_total'],
  326. 'service_fee' => $message['amount'],
  327. 'service_image' => $goods['service_image'],
  328. 'warranty_period'=>$goods['warranty_period'],
  329. 'fee_schedule' => $goods['fee_schedule'],
  330. 'goods_status' => $goods['goods_status'],
  331. ]);
  332. //判断是否是一口价预支付订单,支付成功后生成尾款订单
  333. if(!$goods->isEmpty() and $goods->goods_payment_type == 3 and bcsub($goods->base_service_fee,$goods->service_fee,2)>=0){
  334. //新增待支付尾款
  335. $order_weikuan_data = [
  336. 'order_type'=>$order['order_type'],
  337. 'sn'=>generate_sn(\app\common\model\orders\RechargeOrder::class, 'sn'),
  338. 'work_id'=>$order['work_id'],
  339. 'user_id'=>$order['user_id'],
  340. 'payment_type'=>2,
  341. 'order_total'=>bcsub($goods->base_service_fee,$message['amount'],2),
  342. 'order_amount'=>bcsub($goods->base_service_fee,$message['amount'],2),
  343. 'order_terminal'=>$order['order_terminal']
  344. ];
  345. \app\common\model\recharge\RechargeOrder::create($order_weikuan_data);
  346. }
  347. Db::commit();
  348. } catch (\Exception $e) {
  349. Db::rollback();
  350. self::setError($e->getMessage());
  351. return false;
  352. }
  353. return true;
  354. }
  355. public static function get_sign($sign_key, $data)
  356. {
  357. if ($data == null) {
  358. return null;
  359. }
  360. ksort($data);
  361. $result_str = "";
  362. foreach ($data as $key => $val) {
  363. if ($key != "sign" && $val != null && $val != "") {
  364. $result_str = $result_str . $key . $val;
  365. }
  366. }
  367. $result_str = $sign_key . $result_str;
  368. $ret = bin2hex(sha1($result_str, true));
  369. return $ret;
  370. }
  371. /**
  372. * 商家接单接口
  373. * @param $orderId
  374. * @param $bookStatus
  375. * @return string
  376. */
  377. public static function bookresultcallback($opBizCode,$orderId,$bookStatus=2)
  378. {
  379. //预订结果,2-预订成功,3-预订失败
  380. $url = 'https://api-open-cater.meituan.com/ddzh/yuding/bookresultcallback';
  381. $accessToken = $opBizCode == 'AE7MKOJAV67338LIC3UD0K5TGIO' ? self::shop_token2['accessToken'] : self::shop_token1['accessToken'];
  382. $data = [
  383. 'timestamp'=>time(),
  384. 'appAuthToken'=>$accessToken,
  385. 'charset'=>'utf-8',
  386. 'version'=>'2',
  387. 'developerId'=>self::developerId,
  388. 'businessId'=>58,
  389. 'biz'=>json_encode([
  390. 'orderId'=>$orderId,
  391. 'bookStatus'=>$bookStatus,
  392. "code"=>200,
  393. "type"=>2
  394. ],JSON_UNESCAPED_UNICODE)
  395. ];
  396. $sign_data = self::get_sign(self::assessKey, $data);
  397. $data['sign'] = $sign_data;
  398. return http_request($url,http_build_query($data));
  399. }
  400. /**
  401. * 取消预订
  402. * @param array
  403. * $order
  404. * @return bool
  405. */
  406. public static function cancelOrderHandle(array $order)
  407. {
  408. $message = json_decode($order['message'], true);
  409. Db::startTrans();
  410. try {
  411. $thirdOrders = ThirdOrders::where('orderId',$message['orderId'])->findOrEmpty();
  412. if(!$thirdOrders->isEmpty()){
  413. $thirdOrders->bookStatus = 3;
  414. $thirdOrders->save();
  415. $serviceWorkInfo = ServiceWork::find($thirdOrders->work_id);
  416. if(!empty($serviceWorkInfo)){
  417. $appointment_time = '2099-12-31 23:59:59';
  418. ServiceWorkAppointmentLog::create([
  419. 'work_id'=>$serviceWorkInfo->id,
  420. 'last_appointment_time'=>strtotime($serviceWorkInfo->appointment_time),
  421. 'this_appointment_time'=>strtotime($appointment_time),
  422. ]);
  423. //修改预计完成时间
  424. if ($serviceWorkInfo->estimated_finish_time) {
  425. $serviceWorkInfo->estimated_finish_time = strtotime($appointment_time) + (strtotime($serviceWorkInfo->estimated_finish_time) - strtotime($serviceWorkInfo->appointment_time));
  426. }
  427. $serviceWorkInfo->appointment_time = strtotime($appointment_time);
  428. $serviceWorkInfo->save();
  429. }
  430. }
  431. Db::commit();
  432. return true;
  433. } catch (\Exception $e) {
  434. Db::rollback();
  435. self::setError($e->getMessage());
  436. return false;
  437. }
  438. }
  439. /**
  440. * 美团改约通知
  441. * @param array $order
  442. * @return bool
  443. */
  444. public static function updateOrderHandle(array $order)
  445. {
  446. $message = json_decode($order['message'], true);
  447. Db::startTrans();
  448. try {
  449. $thirdOrders = ThirdOrders::where('orderId',$message['orderId'])->findOrEmpty();
  450. if(!$thirdOrders->isEmpty()){
  451. $thirdOrders->bookStatus = 2;
  452. $thirdOrders->save();
  453. $serviceWorkInfo = ServiceWork::find($thirdOrders->work_id);
  454. if(!empty($serviceWorkInfo)){
  455. $appointment_time = $message['reserveEndtime'];
  456. ServiceWorkAppointmentLog::create([
  457. 'work_id'=>$serviceWorkInfo->id,
  458. 'last_appointment_time'=>strtotime($serviceWorkInfo->appointment_time),
  459. 'this_appointment_time'=>strtotime($appointment_time),
  460. ]);
  461. //修改预计完成时间
  462. if ($serviceWorkInfo->estimated_finish_time) {
  463. $serviceWorkInfo->estimated_finish_time = strtotime($appointment_time) + (strtotime($serviceWorkInfo->estimated_finish_time) - strtotime($serviceWorkInfo->appointment_time));
  464. }
  465. $serviceWorkInfo->appointment_time = strtotime($appointment_time);
  466. $serviceWorkInfo->save();
  467. }
  468. }
  469. Db::commit();
  470. return true;
  471. } catch (\Exception $e) {
  472. Db::rollback();
  473. self::setError($e->getMessage());
  474. return false;
  475. }
  476. }
  477. /**
  478. * 获取美团商品信息
  479. * @param $offset
  480. * @return string
  481. */
  482. public static function queryshopdeal($offset=1)
  483. {
  484. $url = 'https://api-open-cater.meituan.com/ddzh/tuangou/deal/queryshopdeal';
  485. $data = [
  486. 'timestamp'=>time(),
  487. 'appAuthToken'=>self::shop_token2['accessToken'],
  488. 'charset'=>'utf-8',
  489. 'version'=>'2',
  490. 'developerId'=>self::developerId,
  491. 'businessId'=>58,
  492. 'biz'=>json_encode([
  493. 'offset'=>$offset,
  494. 'limit'=>100,
  495. "source"=>2,
  496. ],JSON_UNESCAPED_UNICODE)
  497. ];
  498. $sign_data = self::get_sign(self::assessKey, $data);
  499. $data['sign'] = $sign_data;
  500. return http_request($url,http_build_query($data));
  501. }
  502. /**
  503. * 获取美团商品信息
  504. * @param $offset
  505. * @return string
  506. */
  507. public static function queryshopdeal1($offset=1)
  508. {
  509. $url = 'https://api-open-cater.meituan.com/ddzh/tuangou/deal/queryshopdeal';
  510. $data = [
  511. 'timestamp'=>time(),
  512. 'appAuthToken'=>self::shop_token1['accessToken'],
  513. 'charset'=>'utf-8',
  514. 'version'=>'2',
  515. 'developerId'=>self::developerId,
  516. 'businessId'=>58,
  517. 'biz'=>json_encode([
  518. 'offset'=>$offset,
  519. 'limit'=>100,
  520. "source"=>2,
  521. ],JSON_UNESCAPED_UNICODE)
  522. ];
  523. $sign_data = self::get_sign(self::assessKey, $data);
  524. $data['sign'] = $sign_data;
  525. return http_request($url,http_build_query($data));
  526. }
  527. /**
  528. * 商家改约
  529. * @param $offset
  530. * @return string
  531. */
  532. public static function merchantmodify($data)
  533. {
  534. $url = 'https://api-open-cater.meituan.com/ddzh/yuding/lifereserve/merchantmodify';
  535. $opBizCode = !empty($data['request_msg'])?json_decode($data['request_msg'],true)['opBizCode']:'';
  536. $accessToken = $opBizCode == 'AE7MKOJAV67338LIC3UD0K5TGIO' ? self::shop_token2['accessToken'] : self::shop_token1['accessToken'];
  537. $data = [
  538. 'timestamp'=>time(),
  539. 'appAuthToken'=>$accessToken,
  540. 'charset'=>'utf-8',
  541. 'version'=>'2',
  542. 'developerId'=>self::developerId,
  543. 'businessId'=>58,
  544. 'biz'=>json_encode([
  545. 'reserveOrderId'=>$data['reserveOrderId'],
  546. 'reserveUserName'=>$data['reserveUserName'],
  547. "reservePhone"=>$data['reservePhone'],
  548. "reserveAddress"=>$data['reserveAddress'],
  549. "bookStartTime"=>$data['bookStartTime'],
  550. "bookEndTime"=>$data['bookEndTime'],
  551. ],JSON_UNESCAPED_UNICODE)
  552. ];
  553. $sign_data = self::get_sign(self::assessKey, $data);
  554. $data['sign'] = $sign_data;
  555. return http_request($url,http_build_query($data));
  556. }
  557. /**
  558. * 商家取消预定
  559. * @param $params
  560. * @return string|true
  561. */
  562. public static function merchantcancelorder($work_id)
  563. {
  564. $third_order = ThirdOrders::where('work_id',$work_id)->findOrEmpty();
  565. if(!$third_order->isEmpty() && !empty($third_order['request_msg']) && $third_order['third_type']==1){
  566. Log::write('取消美团订单'.$work_id);
  567. $third_order->bookStatus = 3;
  568. $third_order->save();
  569. $url = 'https://api-open-cater.meituan.com/ddzh/yuding/merchantcancelorder';
  570. $opBizCode = !empty($third_order['request_msg'])?json_decode($third_order['request_msg'],true)['opBizCode']:'';
  571. $accessToken = $opBizCode == 'AE7MKOJAV67338LIC3UD0K5TGIO' ? self::shop_token2['accessToken'] : self::shop_token1['accessToken'];
  572. $data = [
  573. 'timestamp'=>time(),
  574. 'appAuthToken'=>$accessToken,
  575. 'charset'=>'utf-8',
  576. 'version'=>'2',
  577. 'developerId'=>self::developerId,
  578. 'businessId'=>58,
  579. 'biz'=>json_encode([
  580. 'orderId'=>$third_order['orderId'],
  581. 'cancelReason'=>'取消预定',
  582. "type"=>2,
  583. ],JSON_UNESCAPED_UNICODE)
  584. ];
  585. $sign_data = self::get_sign(self::assessKey, $data);
  586. $data['sign'] = $sign_data;
  587. return http_request($url,http_build_query($data));
  588. }
  589. return true;
  590. }
  591. /**
  592. * 更新履约状态
  593. * @param $work
  594. * @return string|void
  595. */
  596. public static function updateorderfulfillinfo($work,$orderFulfillStatus)
  597. {
  598. $third_order = ThirdOrders::where('work_id',$work['id'])->findOrEmpty();
  599. if(!$third_order->isEmpty() && !empty($third_order['request_msg'])) {
  600. // 家政/维修更新顺序:已分配(40001)→ 已到达(40002)→ 履约完成(41001)
  601. $app_sku_id = ThirdGoods::where('product_id',$third_order['productId'])->value('app_sku_id');
  602. //判断当前履约状态
  603. $pic_url = 'https://cdnweixiu.kyjlkj.com/uploads/images/20240914/2024091414471041c336653.png';
  604. switch ($orderFulfillStatus){
  605. case '40001':
  606. $describe = '已分配';
  607. break;
  608. case '40002':
  609. $describe = '已到达';
  610. break;
  611. case '41001':
  612. $describe = '履约完成';
  613. $pic_url = !empty($work['finished_images'][0])?$work['finished_images'][0]:$pic_url;
  614. break;
  615. default:
  616. $describe = '未知';
  617. break;
  618. }
  619. $url = 'https://api-open-cater.meituan.com/ddzh/yuding/updateorderfulfillinfo';
  620. $opBizCode = !empty($third_order['request_msg']) ? json_decode($third_order['request_msg'], true)['opBizCode'] : '';
  621. $accessToken = $opBizCode == 'AE7MKOJAV67338LIC3UD0K5TGIO' ? self::shop_token2['accessToken'] : self::shop_token1['accessToken'];
  622. $data = [
  623. 'timestamp' => time(),
  624. 'appAuthToken' => $accessToken,
  625. 'charset' => 'utf-8',
  626. 'version' => '2',
  627. 'developerId' => self::developerId,
  628. 'businessId' => 58,
  629. 'biz' => json_encode([
  630. 'orderId' => $third_order['orderId'],
  631. 'attrKeyValue' => [
  632. 'service_voucher' => [
  633. 'detail_list' => [
  634. [
  635. 'app_sku_id' => $app_sku_id,
  636. 'describe' => $describe,
  637. 'pic_url' => $pic_url
  638. ]
  639. ]
  640. ],
  641. 'delivery_info' => '无',
  642. 'delivery_type' => 3
  643. ],
  644. "type" => 2,
  645. "orderFulfillStatus" => $orderFulfillStatus
  646. ], JSON_UNESCAPED_UNICODE)
  647. ];
  648. $sign_data = self::get_sign(self::assessKey, $data);
  649. $data['sign'] = $sign_data;
  650. return http_request($url, http_build_query($data));
  651. }
  652. }
  653. }