DouYinService.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <?php
  2. namespace app\api\service;
  3. use app\adminapi\logic\external\ExternalConsultationLogic;
  4. use app\api\logic\ServiceOrderLogic;
  5. use app\common\model\Config;
  6. use app\common\model\external\DouyinOrder;
  7. use app\common\model\external\DouyinRefundOrder;
  8. use app\common\model\external\ExternalConsultation;
  9. use app\common\model\external\ExternalConsultationOrder;
  10. use app\common\model\goods\Goods;
  11. use app\common\model\recharge\RechargeOrder;
  12. use app\common\model\user\User;
  13. use app\common\model\user\UserAuth;
  14. use app\common\model\works\ServiceWork;
  15. use app\common\service\ConfigService;
  16. use app\common\service\FileService;
  17. use think\facade\Db;
  18. use think\facade\Log;
  19. class DouYinService
  20. {
  21. protected static int $terminal = \app\common\enum\user\UserTerminalEnum::DOUYIN;
  22. protected static int $external_platform_id = 6;
  23. public static function register(array $params)
  24. {
  25. $userSn = User::createUserSn();
  26. $params['password'] = !empty($params['password'])?$params['password']:rand(100000,999999);
  27. $passwordSalt = \think\facade\Config::get('project.unique_identification');
  28. $password = create_password($params['password'], $passwordSalt);
  29. $avatar = ConfigService::get('default_image', 'user_avatar');
  30. $user = User::create([
  31. 'sn' => $userSn,
  32. 'avatar' => $avatar,
  33. 'nickname' => '用户' . $userSn,
  34. 'account' => $params['account'],
  35. 'mobile' => !empty($params['mobile'])?$params['mobile']:'',
  36. 'password' => $password,
  37. 'channel' => self::$terminal,
  38. 'user_type' => $params['user_type']??0,
  39. ]);
  40. return $user;
  41. }
  42. public static function phoneLogin(array $params)
  43. {
  44. try {
  45. $where = ['mobile' => $params['mobile']];
  46. $params['account'] = $params['mobile'];
  47. $user = User::where($where)->findOrEmpty();
  48. if ($user->isEmpty()) {
  49. //直接注册用户
  50. $params['channel'] = self::$terminal;
  51. $user = self::register($params);
  52. }
  53. //更新登录信息
  54. $user->login_time = time();
  55. $user->login_ip = request()->ip();
  56. $user->save();
  57. $userInfo = UserTokenService::setToken($user->id, self::$terminal);
  58. //返回登录信息
  59. $avatar = $user->avatar ?: Config::get('project.default_image.user_avatar');
  60. $avatar = FileService::getFileUrl($avatar);
  61. return [
  62. 'nickname' => $userInfo['nickname'],
  63. 'sn' => $userInfo['sn'],
  64. 'mobile' => $userInfo['mobile'],
  65. 'avatar' => $avatar,
  66. 'token' => $userInfo['token'],
  67. ];
  68. } catch (\Exception $e) {
  69. throw new \Exception($e->getMessage());
  70. }
  71. }
  72. /**
  73. * 提交订单
  74. * @param array $params
  75. * @return array|false
  76. */
  77. public static function submitOrder($params)
  78. {
  79. Db::startTrans();
  80. try {
  81. $goods = Goods::findOrEmpty($params['goods_id']);
  82. if($goods->isEmpty()){
  83. throw new \Exception('产品不存在!');
  84. }
  85. if(empty($params['user_info']['mobile'])){
  86. throw new \Exception('请先补充您的联系方式后在提交订单');
  87. }
  88. // TODO tmp防抖1m
  89. $isExist = DouyinOrder::where(['user_id'=>$params['user_id'],'goods_id'=>$goods['id']])->where('create_time','>',(time() - 60))->findOrEmpty();
  90. if(!$isExist->isEmpty()){
  91. throw new \Exception('请勿重复下单!');
  92. }
  93. $quantity = $params['quantity']??1;
  94. //生成订单
  95. $create_data = [
  96. 'user_id' => $params['user_id'],
  97. 'mobile' => $params['user_info']['mobile'],
  98. 'title' => $goods['goods_name'],
  99. 'goods_id'=>$goods['id'],
  100. 'unit_price' => $goods['service_fee'],
  101. 'quantity' => $quantity,
  102. 'total_amount' => $goods['service_fee'] * $quantity,
  103. 'order_number' => generate_sn(DouyinOrder::class, 'order_number'),
  104. ];
  105. $order = DouyinOrder::create($create_data);
  106. Db::commit();
  107. return $create_data['order_number'];
  108. } catch (\Exception $e) {
  109. Db::rollback();
  110. throw new \Exception($e->getMessage());
  111. }
  112. }
  113. public static function getByteAuthorization($order_number)
  114. {
  115. /*{
  116. "skuList": [{
  117. "skuId": "商品ID",
  118. "price": 100,//单价-分
  119. "quantity": 1,
  120. "title": "商品标题",
  121. "imageList": ["https://cdn.weixiu.kyjlkj.com/uploads/images/20240914/202409141528015aeaa2357.png"],
  122. "type": 701,
  123. "tagGroupId": "tag_group_7272625659887960076"
  124. }],
  125. "outOrderNo": "202411121413333930",
  126. "totalAmount": 100,//分
  127. "orderEntrySchema": {
  128. "path": "page/path/index",
  129. "params": '{"id":1234, "name":"hello"}'
  130. },
  131. "payNotifyUrl": "https://weixiudev.kyjlkj.com/api/dou_yin/payNotify"
  132. }*/
  133. try {
  134. $douyinOrder = DouyinOrder::where('order_number',$order_number)->findOrEmpty();
  135. if($douyinOrder->isEmpty()){
  136. throw new \Exception('订单不存在!');
  137. }
  138. $order = $douyinOrder->toArray();
  139. $goods_image = Goods::where('id',$order['goods_id'])->value('goods_image')??'';
  140. $data = [
  141. "skuList" => [
  142. [
  143. "skuId" => (string)$order['goods_id'],
  144. "price" => $order['unit_price'] * 100,
  145. "quantity" => $order['quantity'],
  146. "title" => $order['title'],
  147. "imageList" => [$goods_image],
  148. "type" => 701,
  149. "tagGroupId" => "tag_group_7272625659887960076"
  150. ]
  151. ],
  152. "outOrderNo" => $order['order_number'],
  153. "totalAmount" => $order['total_amount'] * 100,
  154. "orderEntrySchema" => [
  155. "path" => "page/index/index",
  156. "params" => json_encode(['order_number' => $order['order_number']])
  157. ],
  158. "payNotifyUrl" => config('douyin.payNotifyUrl'),
  159. ];
  160. $byteAuthorization = self::byteAuthorization(config('douyin.privateKeyStr'), json_encode($data), config('douyin.appId'), self::randStr(10), time(), 1);
  161. return ['byteAuthorization'=>$byteAuthorization,'data'=>json_encode($data)];
  162. } catch (\Exception $e) {
  163. throw new \Exception($e->getMessage());
  164. }
  165. }
  166. public static function cancelOrder($params)
  167. {
  168. // $params['order_number']
  169. Db::startTrans();
  170. try {
  171. $order = DouyinOrder::where('order_number', $params['order_number'])->findOrEmpty();
  172. if(!$order->isEmpty()){
  173. if($order->order_status == 1 && $order->pay_status == 0){
  174. $order->order_status = 4;
  175. $order->save();
  176. }else{
  177. throw new \Exception('订单状态不可取消!');
  178. }
  179. }
  180. Db::commit();
  181. return $order['id'];
  182. } catch (\Exception $e) {
  183. Db::rollback();
  184. throw new \Exception($e->getMessage());
  185. }
  186. }
  187. public static function payNotify($params)
  188. {
  189. Log::write(json_encode($params));
  190. // 查询抖音订单是否完成支付
  191. if ($params['status'] === 'SUCCESS') {
  192. $transaction_id = $params['order_id']??'';
  193. $paid_amount = bcdiv(bcsub($params['total_amount'] ,$params['discount_amount']), '100', 2)??0;
  194. $out_order_no = $params['out_order_no'];
  195. $pay_time = $params['event_time']??time();
  196. $order = DouyinOrder::where('order_number', $out_order_no)->findOrEmpty();
  197. if(!$order->isEmpty()){
  198. // 更新充值订单状态
  199. $order->transaction_id = $transaction_id;
  200. $order->order_status = 2;
  201. $order->pay_time = $pay_time;
  202. $order->paid_amount = $paid_amount;
  203. $user = User::where('id',$order->user_id)->findOrEmpty()->toArray();
  204. $form_detail = [
  205. 'user_name' => $user['real_name']??'',
  206. 'mobile' => $user['mobile'],
  207. 'transaction_id' => $transaction_id,
  208. 'out_trade_no' => $out_order_no,
  209. 'paid_amount' => $paid_amount,
  210. 'params' => $params,
  211. ];
  212. $consultation = ExternalConsultation::create([
  213. 'external_platform_id' => self::$external_platform_id,
  214. 'form_detail' => json_encode($form_detail),
  215. 'user_name' => $user['real_name']??'',
  216. 'mobile' => $user['mobile'],
  217. 'goods_id' => $order->goods_id,
  218. 'amount' => $paid_amount
  219. ]);
  220. $order->consultation_id = $consultation->id;
  221. $order->save();
  222. return true;
  223. }
  224. }
  225. return false;
  226. }
  227. public static function reservation($params)
  228. {
  229. /*$lon_lat = get_address_lat_lng($params['user_address']);
  230. $params['lon'] = $lon_lat['lon'];
  231. $params['lat'] = $lon_lat['lat'];*/
  232. // $params['order_number']
  233. Db::startTrans();
  234. try {
  235. $order = DouyinOrder::where('order_number', $params['order_number'])->findOrEmpty();
  236. if(!$order->isEmpty()){
  237. $consultation = ExternalConsultation::where('id', $order->consultation_id)->findOrEmpty()->toArray();
  238. $consultation['user_name'] = $params['user_name']??$consultation['user_name'];
  239. $consultation['mobile'] = $params['mobile']??$consultation['mobile'];
  240. $consultation['user_address'] = $params['user_address'];
  241. $consultation['lon'] = $params['lon'];
  242. $consultation['lat'] = $params['lat'];
  243. $consultation['appointment_time'] = $params['appointment_time'];
  244. $result = ExternalConsultationLogic::order($consultation);
  245. if (false === $result) {
  246. throw new \Exception('预约失败');
  247. }
  248. $consultationOrder = ExternalConsultationOrder::where('consultation_id', $order->consultation_id)->where('goods_id', $order->goods_id)->where('amount', $order->paid_amount)
  249. ->findOrEmpty()->toArray();
  250. $work_status = ServiceWork::where('id', $consultationOrder['work_id'])->value('work_status');
  251. $order->work_id = $consultationOrder['work_id'];
  252. $order->fulfillment_status = $work_status;
  253. $order->save();
  254. }
  255. Db::commit();
  256. return $order['id'];
  257. } catch (\Exception $e) {
  258. Db::rollback();
  259. throw new \Exception($e->getMessage());
  260. }
  261. }
  262. public static function upReservation($params)
  263. {
  264. // $params['order_number']
  265. Db::startTrans();
  266. try {
  267. $order = DouyinOrder::where('order_number', $params['order_number'])->findOrEmpty();
  268. if(!$order->isEmpty()){
  269. // sn appointment_time
  270. $result = ServiceOrderLogic::approvalChangeAppointment(['sn'=>RechargeOrder::where('work_id', $order->work_id)->value('sn'),'appointment_time'=>$params['appointment_time']]);
  271. if (false === $result) {
  272. throw new \Exception(ServiceOrderLogic::getError());
  273. }
  274. $order->fulfillment_status = ServiceWork::where('id', $order->work_id)->value('work_status');
  275. $order->save();
  276. }
  277. Db::commit();
  278. return $order['id'];
  279. } catch (\Exception $e) {
  280. Db::rollback();
  281. throw new \Exception($e->getMessage());
  282. }
  283. }
  284. public static function getOrderDetail($params)
  285. {
  286. //抖音订单信息/商品信息/预约信息(地址、时间、履约状态与信息)
  287. // $params['order_number'] user_id
  288. $order = DouyinOrder::with(['goods','serviceWork','douyinRefundOrder'])->where('order_number', $params['order_number'])->where('user_id', $params['user_id'])->findOrEmpty();
  289. if($order->isEmpty()){
  290. return [];
  291. }
  292. $orderInfo = $order->toArray();
  293. empty($orderInfo['goods']) && $orderInfo['goods'] = [];
  294. empty($orderInfo['serviceWork']) && $orderInfo['serviceWork'] = [];
  295. empty($orderInfo['douyinRefundOrder']) && $orderInfo['douyinRefundOrder'] = [];
  296. $work_status = $orderInfo['serviceWork']['work_status']??0;
  297. $performance = [];
  298. // tmp
  299. switch ($work_status){
  300. case 0:
  301. $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
  302. break;
  303. case 1:
  304. case 2:
  305. case 3:
  306. $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
  307. $performance[] = ['status' => '已派单','title' => '已派单','time' => date('Y-m-d H:i:s',time())];
  308. break;
  309. case 4:
  310. case 5:
  311. case 6:
  312. $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
  313. $performance[] = ['status' => '已派单','title' => '已派单','time' => date('Y-m-d H:i:s',time())];
  314. $performance[] = ['status' => '服务中','title' => '服务中','time' => date('Y-m-d H:i:s',time())];
  315. break;
  316. case 7:
  317. case 8:
  318. $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
  319. $performance[] = ['status' => '已派单','title' => '已派单','time' => date('Y-m-d H:i:s',time())];
  320. $performance[] = ['status' => '服务中','title' => '服务中','time' => date('Y-m-d H:i:s',time())];
  321. $performance[] = ['status' => '已完结','title' => '已完结','time' => date('Y-m-d H:i:s',time())];
  322. break;
  323. }
  324. $orderInfo['performance'] = $performance;
  325. return $orderInfo;
  326. }
  327. public static function refund($params)
  328. {
  329. Db::startTrans();
  330. try {
  331. // $params['order_number'] user_id
  332. $order = DouyinOrder::with(['goods','serviceWork'])->where('order_number', $params['order_number'])->where('user_id', $params['user_id'])->findOrEmpty();
  333. if($order->isEmpty()){
  334. throw new \Exception('订单不存在');
  335. }
  336. $orderInfo = $order->toArray();
  337. $work_status = $orderInfo['serviceWork']['work_status']??0;
  338. if(3 < $work_status){
  339. throw new \Exception('该订单禁止退款');
  340. }
  341. DouyinRefundOrder::create([
  342. 'refund_number' => generate_sn(DouyinRefundOrder::class, 'refund_number'),
  343. 'order_number' => $orderInfo['order_number'],
  344. 'transaction_id' => $orderInfo['transaction_id'],
  345. 'reason' => $params['reason']??'',
  346. 'refund_status' => 0,
  347. 'user_id' => $orderInfo['user_id'],
  348. 'refund_amount' => $orderInfo['paid_amount'],
  349. ]);
  350. Db::commit();
  351. return true;
  352. } catch (\Exception $e) {
  353. Db::rollback();
  354. throw new \Exception($e->getMessage());
  355. }
  356. }
  357. public static function refundExamine($params)
  358. {
  359. Db::startTrans();
  360. try {
  361. // $params['order_number']
  362. $order = DouyinOrder::with(['goods','serviceWork'])->where('order_number', $params['order_number'])->findOrEmpty();
  363. if($order->isEmpty()){
  364. throw new \Exception('订单不存在');
  365. }
  366. $orderInfo = $order->toArray();
  367. //$refund_number = $params['refund_number']??'';
  368. $douyinRefundOrder = DouyinRefundOrder::where('order_number', $params['order_number'])->order('id', 'desc')->findOrEmpty();
  369. if($params['is_examine_ok'] === 'pass'){
  370. $douyinRefundOrder->refund_status = 2;
  371. RechargeOrder::where('work_id', $orderInfo['work_id'])->update([
  372. 'pay_status' => 2,
  373. 'pay_time' => 0,
  374. 'paid_amount' => 0,
  375. ]);
  376. ServiceWork::where('id', $orderInfo['work_id'])->update([
  377. 'work_status' => 0,
  378. 'user_confirm_status' => 0,
  379. 'service_status' => 4,
  380. 'work_pay_status' => 0
  381. ]);
  382. }else{
  383. $douyinRefundOrder->refund_status = 1;
  384. }
  385. $douyinRefundOrder->save();
  386. Db::commit();
  387. if($params['is_examine_ok'] === 'pass'){
  388. //通过后向抖音申请退款
  389. self::sendRefundCreate($params['order_number']);
  390. }
  391. return true;
  392. } catch (\Exception $e) {
  393. Db::rollback();
  394. throw new \Exception($e->getMessage());
  395. }
  396. }
  397. public static function refundNotify($params)
  398. {
  399. Db::startTrans();
  400. try {
  401. $douyinRefundOrder = DouyinRefundOrder::where('refund_number', $params['out_refund_no'])->findOrEmpty();
  402. if($douyinRefundOrder->isEmpty()){
  403. throw new \Exception('退款订单不存在');
  404. }
  405. if($douyinRefundOrder->refund_status == 0){
  406. if($params['status'] === 'SUCCESS'){
  407. $douyinRefundOrder->refund_status = 3;
  408. DouyinOrder::where('order_number', $douyinRefundOrder->order_number)->update([
  409. 'order_status' => 4,
  410. 'pay_status' => 3,
  411. ]);
  412. }elseif($params['status'] === 'FAIL'){
  413. $douyinRefundOrder->refund_status = 4;
  414. }else{
  415. throw new \Exception('退款状态未知');
  416. }
  417. $douyinRefundOrder->save();
  418. }
  419. Db::commit();
  420. return true;
  421. } catch (\Exception $e) {
  422. Db::rollback();
  423. throw new \Exception($e->getMessage());
  424. }
  425. }
  426. public static function byteAuthorization($privateKeyStr, $data, $appId, $nonceStr, $timestamp, $keyVersion) {
  427. $byteAuthorization = '';
  428. // 读取私钥
  429. $privateKey = openssl_pkey_get_private($privateKeyStr);
  430. if (!$privateKey) {
  431. throw new \Exception("Invalid private key");
  432. }
  433. // 生成签名
  434. $signature = self::getSignature("POST", "/requestOrder", $timestamp, $nonceStr, $data, $privateKey);
  435. if ($signature === false) {
  436. return null;
  437. }
  438. // 构造 byteAuthorization
  439. $byteAuthorization = sprintf("SHA256-RSA2048 appid=%s,nonce_str=%s,timestamp=%s,key_version=%s,signature=%s", $appId, $nonceStr, $timestamp, $keyVersion, $signature);
  440. return $byteAuthorization;
  441. }
  442. public static function getSignature($method, $url, $timestamp, $nonce, $data, $privateKey) {
  443. Log::info("method:{$method}\n url:{$url}\n timestamp:{$timestamp}\n nonce:{$nonce}\n data:{$data}");
  444. $targetStr = $method. "\n" . $url. "\n" . $timestamp. "\n" . $nonce. "\n" . $data. "\n";
  445. openssl_sign($targetStr, $sign, $privateKey, OPENSSL_ALGO_SHA256);
  446. $sign = base64_encode($sign);
  447. return $sign;
  448. }
  449. public static function randStr($length = 8) {
  450. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  451. $str = '';
  452. for ($i = 0; $i < $length; $i++) {
  453. $str .= $chars[mt_rand(0, strlen($chars) - 1)];
  454. }
  455. return $str;
  456. }
  457. public static function getClientToken() {
  458. $url = config('douyin.host').'oauth/client_token/';
  459. $cache_name = 'dy_client_token';
  460. $cache_data = cache($cache_name);
  461. if(empty($cache_data) || $cache_data == null){
  462. $data = [
  463. 'client_key'=> config('douyin.appId'),
  464. 'client_secret'=> config('douyin.appSecret'),
  465. 'grant_type'=> "client_credential"
  466. ];
  467. $res = http_request($url,$data,['Content-Type' => 'application/json;charset=utf-8']);
  468. if($res['message'] === 'success'){
  469. cache($cache_name, json_encode($res['data']), (time()+$res['data']['expires_in']-1));
  470. $cache_data = $res['data'];
  471. }
  472. }
  473. return json_decode($cache_data, true)['access_token'];
  474. }
  475. public static function sendRefundCreate($order_number)
  476. {
  477. try {
  478. // $params['order_number']
  479. $order = DouyinOrder::with(['goods','serviceWork'])->where('order_number', $order_number)->findOrEmpty();
  480. if($order->isEmpty()){
  481. throw new \Exception('订单不存在');
  482. }
  483. $orderInfo = $order->toArray();
  484. $douyinRefundOrder = DouyinRefundOrder::where('order_number', $order_number)->order('id', 'desc')->findOrEmpty();
  485. //通过后向抖音申请退款
  486. //getClientToken()
  487. $url = config('douyin.host').'api/trade_basic/v1/developer/refund_create/';
  488. $data = [
  489. "order_id" => $orderInfo['transaction_id'],
  490. "out_refund_no" => $douyinRefundOrder->refund_number,
  491. "cp_extra" => $orderInfo['id'].'|'.$douyinRefundOrder->id,
  492. "order_entry_schema" => [
  493. "path" => "page/index/index",
  494. "params" => json_encode(['refund_number'=>$douyinRefundOrder->refund_number])
  495. ],
  496. "refund_total_amount " => $douyinRefundOrder->refund_amount * 100,
  497. "notify_url" => config('douyin.refundNotifyUrl'),
  498. "refund_reason" => [
  499. [
  500. "code" => 101,
  501. "text" => "不想要了"
  502. ]
  503. ]
  504. ];
  505. $res = http_request($url,$data,['Content-Type' => 'application/json;charset=utf-8','access_token' => self::getClientToken()]);
  506. if(isset($res['err_msg']) && $res['err_msg'] === 'success'){
  507. $douyinRefundOrder->transaction_id = $res['data']['refund_id'];
  508. $douyinRefundOrder->save();
  509. }
  510. return true;
  511. } catch (\Exception $e) {
  512. Log::info($e->getMessage());
  513. return false;
  514. }
  515. }
  516. }