ThirdOrderLogic.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. use think\facade\Db;
  16. use app\common\model\service_area\ServiceArea;
  17. /**
  18. * 第三方平台下单逻辑层
  19. * Class UserLogic
  20. * @package app\shopapi\logic
  21. */
  22. class ThirdOrderLogic extends BaseLogic
  23. {
  24. protected const user_token = [
  25. "accessToken"=>"V2-29d801e316e9ec4eb88b3b9abd8dbda490367af539d97d754420056dc5c1329a2c1aff7d4d54729ff622d96ff7c9910aa930f7b7ad27910d199c15029ffc71aa4a84142d75b60f4a34b71a8390a89d8c01a4dd385232e4e9c9204fb5934eda89",
  26. "expireIn"=> 2592000,
  27. "opBizCode"=> "AG5MALO18KG6I5DL8LVONRSRF5G",
  28. "opBizName"=> "武汉开源节流科技有限公司",
  29. "refreshToken"=> "ab8d15b4028b7bd6aca383d230b87450436d301a2996b6bb3151d668dd2d015d68d8393e66fb2a59aa10146c70dd30728b473358ec0d156dc0bc5cc5e574a109a5680ab49c393fa9d2693ef5519ff41a16115b81259b58c71a54d79e23f26381",
  30. "scope"=> "dingdan,generalreserve,merchantreceipt,shangpin"
  31. ];
  32. protected const shop_token1 = [
  33. "accessToken"=> "V2-9667b1ce4ffaabe453d4ce847888c788c9c61762df9230ab342caf33e2b4a735a384288379641f64bb2f4272c88d44db42b080ef237cb0c5e3ae49466d72f5c1b17e8a34991e131e91a24e9947f046027af6bbbbc24e0155f6cd4d738e19b5c0",
  34. "expireIn"=> 2592000,
  35. "opBizCode"=> "AH14KGRJ76O8PFH4GCO67F0CUKC",
  36. "opBizName"=> "众盾闪修 家电清洗 武汉",
  37. "refreshToken"=> "fd3da11174eed4c3d6b4d117f87ab7b62a72603627d2d597bed82dddb1036943b8e76e2959c0c4b3cf7b33ec4664bedbca3ef14dd070f662582a1b6695077577e2268c6a42ca519fda331c7e1cdbc0919af8052151ce9ae0a658d346307ca0c1",
  38. "scope"=> "merchantdata,poiqrcode,tuangou,ugc,yuding"
  39. ];
  40. protected const shop_token2 = [
  41. "accessToken"=> "V2-26f91ef8b515c59b97b9b4404e77dc2b2fc14b9715e3be7edb9f3f86178e193ec380e1ef5a9ab7a877120a94bcae4586707c51ab5e56cfeb4cd91d4fb2983babbbfd2fda0f77353eb21b105fd14299aea392a706589a422baec961b6cc255191",
  42. "expireIn"=> 2592000,
  43. "opBizCode"=> "AE7MKOJAV67338LIC3UD0K5TGIO",
  44. "opBizName"=> "众盾闪修 家电维修 武汉",
  45. "refreshToken"=> "bd57846c8817125f8ad7f4eb4ccad4af80c808f30c8b22e38476fc78c008b568ca8bcd8a027740023f2bcab5c0f6f062fb8a6e8f588cff53512a057c2e97a7b1789fb9fe9dc4b3fc2abf42dd753341c455e6eacb7a5e19dd4894bf1d93db9abf",
  46. "scope"=> "merchantdata,poiqrcode,tuangou,ugc,yuding"
  47. ];
  48. protected const developerId = 114657;
  49. protected const assessKey = '8471lgvnv0qcjpx6';
  50. /**
  51. * 处理订单
  52. * @param array $order
  53. * @return bool
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\DbException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. */
  58. public static function orderhandle(array $order)
  59. {
  60. $message = json_decode($order['message'], true);
  61. //查询用户是否存在
  62. $userName = $message['userName'];
  63. $mobile = $message['mobile'];
  64. $userAddress = json_decode($message['userAddress']);
  65. $user = User::where(['mobile' =>$mobile])->findOrEmpty();
  66. $product_message = json_decode($message['products'],true);
  67. if($user->isEmpty()){
  68. $user = LoginLogic::register([
  69. 'password'=>'123456',
  70. 'account'=>$userName,
  71. 'mobile'=>$mobile,
  72. 'channel'=>1,
  73. 'user_type'=>0
  74. ]);
  75. }
  76. //查询美团商品和平台商品的对应关系
  77. $productId = $message['productId'];
  78. $product = ThirdGoods::where(['third_type'=>1,'product_id'=>$productId])->findOrEmpty();
  79. //生成第三方订单信息
  80. $orderData = [
  81. 'orderId'=>$message['orderId'],
  82. 'uni_order_id'=>$product_message[0]['uni_order_id'],
  83. 'productId'=>$message['productId'],
  84. 'productName'=>$message['productName'],
  85. 'request_msg'=>json_encode($order,JSON_UNESCAPED_UNICODE),
  86. 'bookStatus'=>1
  87. ];
  88. //判断美团的预约订单是否生成
  89. if(!$product->isEmpty()){
  90. //查询美团预约订单是否生成
  91. $thirdOrder = ThirdOrders::where(['uni_order_id'=>$product_message[0]['uni_order_id']])->findOrEmpty();
  92. if(!$thirdOrder->isEmpty() and $thirdOrder['work_id'] != 0){
  93. //生成第三方订单信息
  94. $orderData['sync_status'] = 1;
  95. $orderData['work_id'] = $thirdOrder['work_id'];
  96. }
  97. }
  98. $thirdOrders = ThirdOrders::where('orderId',$message['orderId'])->findOrEmpty();
  99. //生成未同步预约订单信息
  100. if($thirdOrders->isEmpty()){
  101. $thirdOrders = ThirdOrders::create($orderData);
  102. }
  103. if(!$thirdOrders->isEmpty()){
  104. $orderData['work_id'] = $thirdOrders->work_id;
  105. }
  106. if(empty($orderData['work_id']) and !$product->isEmpty()){
  107. $goods = Goods::findOrEmpty($product['goods_id']);
  108. $pattern = '/(?:省|市)([\w\s]+市)/u';
  109. Db::startTrans();
  110. try {
  111. if (preg_match($pattern, $userAddress->receiver_address, $matches)) {
  112. $city = $matches[1];
  113. }
  114. $area_city = PostageRegion::where(['level'=>2,'name'=>$city])->value('id');
  115. //生成服务工单
  116. $work_data = [
  117. 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
  118. 'real_name' => $userName,
  119. 'mobile' => $mobile,
  120. 'address' => $userAddress->receiver_address,
  121. 'province' => 0,
  122. 'city' => $area_city??0,
  123. 'area_name' => $userAddress->receiver_address,
  124. 'title' => $goods->goods_name,
  125. 'category_type' => $goods['category_type'],
  126. 'goods_category_ids' => $goods['goods_category_ids'],
  127. 'goods_category_id' => $goods['goods_category_id'],
  128. 'base_service_fee' => $goods['base_service_fee'],
  129. 'service_fee' => $product['product_price'],
  130. 'work_pay_status'=>WorkEnum::IS_PAY_STATUS,
  131. 'appointment_time' => strtotime($message['begintime']),
  132. 'user_id'=>$user['id'],
  133. 'lon'=>!empty($userAddress->longitude)?$userAddress->longitude:0,
  134. 'lat'=>!empty($userAddress->latitude)?$userAddress->latitude:0,
  135. 'property_activity_id'=>0,
  136. 'user_equity_id'=>0,
  137. 'third_type'=>1,
  138. 'work_total'=>$goods['base_service_fee'],
  139. 'work_amount'=>$goods['base_service_fee']
  140. ];
  141. $service_area_id = ServiceArea::serviceAreaId(['lon' => $work_data['lon'], 'lat' => $work_data['lat']]);
  142. $work_data['service_area_id'] = $service_area_id;
  143. $service_work = ServiceWork::create($work_data);
  144. //生成服务订单
  145. $data = [
  146. 'work_id'=> $service_work['id'],
  147. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  148. 'order_type'=>0,//服务订单
  149. 'order_terminal' => 1,
  150. 'payment_type'=>$goods['goods_payment_type']==GoodsEnum::ISGOODS_PAYMENT_TYPE?0:1,
  151. 'user_id' => $user['id'],
  152. 'pay_status' => PayEnum::ISPAID,
  153. 'coupon_id'=>0,
  154. 'coupon_price'=>0,
  155. 'pay_way' => 4,
  156. 'order_total' => $product['product_price'],
  157. 'order_amount' => $product['product_price'],
  158. ];
  159. $order = RechargeOrder::create($data);
  160. //生成订单服务详情
  161. OrderGoods::create([
  162. 'sn' => $order['sn'],
  163. 'goods_id' => $goods['id'],
  164. 'category_type' => $goods['category_type'],
  165. 'goods_category_ids' => $goods['goods_category_ids'],
  166. 'goods_category_id' => $goods['goods_category_id'],
  167. 'goods_name' => $goods['goods_name'],
  168. 'goods_image' => $goods['goods_image'],
  169. 'goods_video' => $goods['goods_video'],
  170. 'goods_number' => $goods['goods_number'],
  171. 'good_unit' => $goods['good_unit'],
  172. 'goods_size' => $goods['goods_size'],
  173. 'goods_type' => $goods['goods_type'],
  174. 'goods_brand' => $goods['goods_brand'],
  175. 'install_guide' => $goods['install_guide'],
  176. 'goods_payment_type'=>$goods['goods_payment_type'],
  177. 'base_service_fee' => $goods['base_service_fee'],
  178. 'service_total' => $goods['service_total'],
  179. 'service_fee' => $product['product_price'],
  180. 'service_image' => $goods['service_image'],
  181. 'warranty_period'=>$goods['warranty_period'],
  182. 'fee_schedule' => $goods['fee_schedule'],
  183. 'goods_status' => $goods['goods_status'],
  184. ]);
  185. //判断是否是一口价预支付订单,支付成功后生成尾款订单
  186. $order_goods = OrderGoods::where('sn',$order->sn)->findOrEmpty();
  187. if(!$goods->isEmpty() and $goods->goods_payment_type == 3 and bcsub($goods->base_service_fee,$goods->service_fee,2)>=0){
  188. //新增待支付尾款
  189. $order_weikuan_data = [
  190. 'order_type'=>$order['order_type'],
  191. 'sn'=>generate_sn(\app\common\model\orders\RechargeOrder::class, 'sn'),
  192. 'work_id'=>$order['work_id'],
  193. 'user_id'=>$order['user_id'],
  194. 'payment_type'=>2,
  195. 'order_total'=>bcsub($goods->base_service_fee,$product['product_price'],2),
  196. 'order_amount'=>bcsub($goods->base_service_fee,$product['product_price'],2),
  197. 'order_terminal'=>$order['order_terminal']
  198. ];
  199. \app\common\model\recharge\RechargeOrder::create($order_weikuan_data);
  200. }
  201. $thirdOrders->work_id = $service_work->id;
  202. $thirdOrders->save();
  203. self::bookresultcallback($message['orderId'],2);
  204. Db::commit();
  205. } catch (\Exception $e) {
  206. Db::rollback();
  207. self::setError($e->getMessage());
  208. return false;
  209. }
  210. }
  211. return true;
  212. }
  213. //手动添加订单
  214. public static function submitOrders(array $message)
  215. {
  216. //查询用户是否存在
  217. $userName = $message['userName'];
  218. $mobile = $message['mobile'];
  219. $user = User::where(['mobile' =>$mobile])->findOrEmpty();
  220. if($user->isEmpty()){
  221. $user = LoginLogic::register([
  222. 'password'=>'123456',
  223. 'account'=>$userName,
  224. 'mobile'=>$mobile,
  225. 'channel'=>1,
  226. 'user_type'=>0,
  227. ]);
  228. }
  229. $goods = Goods::findOrEmpty($message['goods_id']);
  230. $pattern = '/(?:省|市)([\w\s]+市)/u';
  231. Db::startTrans();
  232. try {
  233. if (preg_match($pattern, $message['userAddress'], $matches)) {
  234. $city = $matches[1];
  235. }
  236. $area_city = PostageRegion::where(['level'=>2,'name'=>$city])->value('id');
  237. //生成服务工单
  238. $work_data = [
  239. 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
  240. 'real_name' => $userName,
  241. 'mobile' => $mobile,
  242. 'address' => $message['userAddress'],
  243. 'province' => 0,
  244. 'city' => $area_city??0,
  245. 'area_name' => $area_city??'',
  246. 'title' => $goods->goods_name,
  247. 'category_type' => $goods['category_type'],
  248. 'goods_category_ids' => $goods['goods_category_ids'],
  249. 'goods_category_id' => $goods['goods_category_id'],
  250. 'base_service_fee' => $goods['base_service_fee'],
  251. 'service_fee' => $message['amount'],
  252. 'work_pay_status'=>WorkEnum::IS_PAY_STATUS,
  253. 'appointment_time' => strtotime($message['appointment_time']),
  254. 'user_id'=>$user['id'],
  255. 'lon'=>$message['lon'],
  256. 'lat'=>$message['lat'],
  257. 'property_activity_id'=>0,
  258. 'user_equity_id'=>0,
  259. 'third_type'=>1,
  260. 'work_total'=>$goods['base_service_fee'],
  261. 'work_amount'=>$goods['base_service_fee']
  262. ];
  263. $service_area_id = ServiceArea::serviceAreaId(['lon' => $work_data['lon'], 'lat' => $work_data['lat']]);
  264. $work_data['service_area_id'] = $service_area_id;
  265. $service_work = ServiceWork::create($work_data);
  266. //生成服务订单
  267. $data = [
  268. 'work_id'=> $service_work['id'],
  269. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  270. 'order_type'=>0,//服务订单
  271. 'order_terminal' => 1,
  272. 'payment_type'=>$goods['goods_payment_type']==GoodsEnum::ISGOODS_PAYMENT_TYPE?0:1,
  273. 'user_id' => $user['id'],
  274. 'pay_status' => PayEnum::ISPAID,
  275. 'coupon_id'=>0,
  276. 'coupon_price'=>0,
  277. 'pay_way' => 4,
  278. 'order_total' => $message['amount'],
  279. 'order_amount' => $message['amount'],
  280. ];
  281. $order = RechargeOrder::create($data);
  282. //生成订单服务详情
  283. OrderGoods::create([
  284. 'sn' => $order['sn'],
  285. 'goods_id' => $goods['id'],
  286. 'category_type' => $goods['category_type'],
  287. 'goods_category_ids' => $goods['goods_category_ids'],
  288. 'goods_category_id' => $goods['goods_category_id'],
  289. 'goods_name' => $goods['goods_name'],
  290. 'goods_image' => $goods['goods_image'],
  291. 'goods_video' => $goods['goods_video'],
  292. 'goods_number' => $goods['goods_number'],
  293. 'good_unit' => $goods['good_unit'],
  294. 'goods_size' => $goods['goods_size'],
  295. 'goods_type' => $goods['goods_type'],
  296. 'goods_brand' => $goods['goods_brand'],
  297. 'install_guide' => $goods['install_guide'],
  298. 'goods_payment_type'=>$goods['goods_payment_type'],
  299. 'base_service_fee' => $goods['base_service_fee'],
  300. 'service_total' => $goods['service_total'],
  301. 'service_fee' => $message['amount'],
  302. 'service_image' => $goods['service_image'],
  303. 'warranty_period'=>$goods['warranty_period'],
  304. 'fee_schedule' => $goods['fee_schedule'],
  305. 'goods_status' => $goods['goods_status'],
  306. ]);
  307. //判断是否是一口价预支付订单,支付成功后生成尾款订单
  308. if(!$goods->isEmpty() and $goods->goods_payment_type == 3 and bcsub($goods->base_service_fee,$goods->service_fee,2)>=0){
  309. //新增待支付尾款
  310. $order_weikuan_data = [
  311. 'order_type'=>$order['order_type'],
  312. 'sn'=>generate_sn(\app\common\model\orders\RechargeOrder::class, 'sn'),
  313. 'work_id'=>$order['work_id'],
  314. 'user_id'=>$order['user_id'],
  315. 'payment_type'=>2,
  316. 'order_total'=>bcsub($goods->base_service_fee,$message['amount'],2),
  317. 'order_amount'=>bcsub($goods->base_service_fee,$message['amount'],2),
  318. 'order_terminal'=>$order['order_terminal']
  319. ];
  320. \app\common\model\recharge\RechargeOrder::create($order_weikuan_data);
  321. }
  322. Db::commit();
  323. } catch (\Exception $e) {
  324. Db::rollback();
  325. self::setError($e->getMessage());
  326. return false;
  327. }
  328. return true;
  329. }
  330. public static function get_sign($sign_key, $data)
  331. {
  332. if ($data == null) {
  333. return null;
  334. }
  335. ksort($data);
  336. $result_str = "";
  337. foreach ($data as $key => $val) {
  338. if ($key != "sign" && $val != null && $val != "") {
  339. $result_str = $result_str . $key . $val;
  340. }
  341. }
  342. $result_str = $sign_key . $result_str;
  343. $ret = bin2hex(sha1($result_str, true));
  344. return $ret;
  345. }
  346. /**
  347. * 商家接单接口
  348. * @param $orderId
  349. * @param $bookStatus
  350. * @return string
  351. */
  352. public static function bookresultcallback($orderId,$bookStatus=2)
  353. {
  354. //预订结果,2-预订成功,3-预订失败
  355. $url = 'https://api-open-cater.meituan.com/ddzh/yuding/bookresultcallback';
  356. $data = [
  357. 'timestamp'=>time(),
  358. 'appAuthToken'=>self::shop_token2['accessToken'],
  359. 'charset'=>'utf-8',
  360. 'version'=>'2',
  361. 'developerId'=>self::developerId,
  362. 'businessId'=>58,
  363. 'biz'=>json_encode([
  364. 'orderId'=>$orderId,
  365. 'bookStatus'=>$bookStatus,
  366. "code"=>200,
  367. "type"=>2
  368. ],JSON_UNESCAPED_UNICODE)
  369. ];
  370. $sign_data = self::get_sign(self::assessKey, $data);
  371. $data['sign'] = $sign_data;
  372. return http_request($url,http_build_query($data));
  373. }
  374. }