DouYinService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 cancelOrder($params)
  114. {
  115. // $params['order_number']
  116. Db::startTrans();
  117. try {
  118. $order = DouyinOrder::where('order_number', $params['order_number'])->findOrEmpty();
  119. if(!$order->isEmpty()){
  120. if($order->order_status == 1 && $order->pay_status == 0){
  121. $order->order_status = 4;
  122. $order->save();
  123. }else{
  124. throw new \Exception('订单状态不可取消!');
  125. }
  126. }
  127. Db::commit();
  128. return $order['id'];
  129. } catch (\Exception $e) {
  130. Db::rollback();
  131. throw new \Exception($e->getMessage());
  132. }
  133. }
  134. public static function payNotify($params)
  135. {
  136. //Log::write(json_encode($params));
  137. // 查询抖音订单是否完成支付
  138. if ($params['trade_state'] === 'SUCCESS') {
  139. $transaction_id = $params['transaction_id']??'';
  140. $paid_amount = $params['paid_amount']??0;
  141. $out_trade_no = $params['out_trade_no'];
  142. $order = DouyinOrder::where('order_number', $out_trade_no)->findOrEmpty();
  143. if(!$order->isEmpty()){
  144. // 更新充值订单状态
  145. $order->transaction_id = $transaction_id;
  146. $order->order_status = 2;
  147. $order->pay_time = time();
  148. $order->paid_amount = $paid_amount;
  149. $user = User::where('id',$order->user_id)->findOrEmpty()->toArray();
  150. $form_detail = [
  151. 'user_name' => $user['real_name']??'',
  152. 'mobile' => $user['mobile'],
  153. 'transaction_id' => $transaction_id,
  154. 'out_trade_no' => $out_trade_no,
  155. 'paid_amount' => $paid_amount,
  156. ];
  157. $consultation = ExternalConsultation::create([
  158. 'external_platform_id' => self::$external_platform_id,
  159. 'form_detail' => json_encode($form_detail),
  160. 'user_name' => $user['real_name']??'',
  161. 'mobile' => $user['mobile'],
  162. 'goods_id' => $order->goods_id,
  163. 'amount' => $paid_amount
  164. ]);
  165. $order->consultation_id = $consultation->id;
  166. $order->save();
  167. return true;
  168. }
  169. }
  170. return false;
  171. }
  172. public static function reservation($params)
  173. {
  174. /*$lon_lat = get_address_lat_lng($params['user_address']);
  175. $params['lon'] = $lon_lat['lon'];
  176. $params['lat'] = $lon_lat['lat'];*/
  177. // $params['order_number']
  178. Db::startTrans();
  179. try {
  180. $order = DouyinOrder::where('order_number', $params['order_number'])->findOrEmpty();
  181. if(!$order->isEmpty()){
  182. $consultation = ExternalConsultation::where('id', $order->consultation_id)->findOrEmpty()->toArray();
  183. $consultation['user_name'] = $params['user_name']??$consultation['user_name'];
  184. $consultation['mobile'] = $params['mobile']??$consultation['mobile'];
  185. $consultation['user_address'] = $params['user_address'];
  186. $consultation['lon'] = $params['lon'];
  187. $consultation['lat'] = $params['lat'];
  188. $consultation['appointment_time'] = $params['appointment_time'];
  189. $result = ExternalConsultationLogic::order($consultation);
  190. if (false === $result) {
  191. throw new \Exception('预约失败');
  192. }
  193. $consultationOrder = ExternalConsultationOrder::where('consultation_id', $order->consultation_id)->where('goods_id', $order->goods_id)->where('amount', $order->paid_amount)
  194. ->findOrEmpty()->toArray();
  195. $work_status = ServiceWork::where('id', $consultationOrder['work_id'])->value('work_status');
  196. $order->work_id = $consultationOrder['work_id'];
  197. $order->fulfillment_status = $work_status;
  198. $order->save();
  199. }
  200. Db::commit();
  201. return $order['id'];
  202. } catch (\Exception $e) {
  203. Db::rollback();
  204. throw new \Exception($e->getMessage());
  205. }
  206. }
  207. public static function upReservation($params)
  208. {
  209. // $params['order_number']
  210. Db::startTrans();
  211. try {
  212. $order = DouyinOrder::where('order_number', $params['order_number'])->findOrEmpty();
  213. if(!$order->isEmpty()){
  214. // sn appointment_time
  215. $result = ServiceOrderLogic::approvalChangeAppointment(['sn'=>RechargeOrder::where('work_id', $order->work_id)->value('sn'),'appointment_time'=>$params['appointment_time']]);
  216. if (false === $result) {
  217. throw new \Exception(ServiceOrderLogic::getError());
  218. }
  219. $order->fulfillment_status = ServiceWork::where('id', $order->work_id)->value('work_status');
  220. $order->save();
  221. }
  222. Db::commit();
  223. return $order['id'];
  224. } catch (\Exception $e) {
  225. Db::rollback();
  226. throw new \Exception($e->getMessage());
  227. }
  228. }
  229. public static function getOrderDetail($params)
  230. {
  231. //抖音订单信息/商品信息/预约信息(地址、时间、履约状态与信息)
  232. // $params['order_number'] user_id
  233. $order = DouyinOrder::with(['goods','serviceWork'])->where('order_number', $params['order_number'])->where('user_id', $params['user_id'])->findOrEmpty();
  234. if($order->isEmpty()){
  235. return [];
  236. }
  237. $orderInfo = $order->toArray();
  238. empty($orderInfo['goods']) && $orderInfo['goods'] = [];
  239. empty($orderInfo['serviceWork']) && $orderInfo['serviceWork'] = [];
  240. $work_status = $orderInfo['serviceWork']['work_status']??0;
  241. $performance = [];
  242. // tmp
  243. switch ($work_status){
  244. case 0:
  245. $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
  246. break;
  247. case 1:
  248. case 2:
  249. case 3:
  250. $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
  251. $performance[] = ['status' => '已派单','title' => '已派单','time' => date('Y-m-d H:i:s',time())];
  252. break;
  253. case 4:
  254. case 5:
  255. case 6:
  256. $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
  257. $performance[] = ['status' => '已派单','title' => '已派单','time' => date('Y-m-d H:i:s',time())];
  258. $performance[] = ['status' => '服务中','title' => '服务中','time' => date('Y-m-d H:i:s',time())];
  259. break;
  260. case 7:
  261. case 8:
  262. $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
  263. $performance[] = ['status' => '已派单','title' => '已派单','time' => date('Y-m-d H:i:s',time())];
  264. $performance[] = ['status' => '服务中','title' => '服务中','time' => date('Y-m-d H:i:s',time())];
  265. $performance[] = ['status' => '已完结','title' => '已完结','time' => date('Y-m-d H:i:s',time())];
  266. break;
  267. }
  268. $orderInfo['performance'] = $performance;
  269. return $orderInfo;
  270. }
  271. public static function refund($params)
  272. {
  273. Db::startTrans();
  274. try {
  275. // $params['order_number'] user_id
  276. $order = DouyinOrder::with(['goods','serviceWork'])->where('order_number', $params['order_number'])->where('user_id', $params['user_id'])->findOrEmpty();
  277. if($order->isEmpty()){
  278. throw new \Exception('订单不存在');
  279. }
  280. $orderInfo = $order->toArray();
  281. $work_status = $orderInfo['serviceWork']['work_status']??0;
  282. if(3 < $work_status){
  283. throw new \Exception('该订单禁止退款');
  284. }
  285. DouyinRefundOrder::create([
  286. 'refund_number' => generate_sn(DouyinRefundOrder::class, 'refund_number'),
  287. 'order_number' => $orderInfo['order_number'],
  288. 'transaction_id' => $orderInfo['transaction_id'],
  289. 'reason' => $params['reason']??'',
  290. 'refund_status' => 0,
  291. 'user_id' => $orderInfo['user_id'],
  292. 'refund_amount' => $orderInfo['paid_amount'],
  293. ]);
  294. Db::commit();
  295. return true;
  296. } catch (\Exception $e) {
  297. Db::rollback();
  298. throw new \Exception($e->getMessage());
  299. }
  300. }
  301. public static function refundExamine($params)
  302. {
  303. Db::startTrans();
  304. try {
  305. // $params['order_number']
  306. $order = DouyinOrder::with(['goods','serviceWork'])->where('order_number', $params['order_number'])->findOrEmpty();
  307. if($order->isEmpty()){
  308. throw new \Exception('订单不存在');
  309. }
  310. $orderInfo = $order->toArray();
  311. //$refund_number = $params['refund_number']??'';
  312. $douyinRefundOrder = DouyinRefundOrder::where('order_number', $params['order_number'])->order('id', 'desc')->findOrEmpty();
  313. if($params['is_examine_ok'] === 'pass'){
  314. $douyinRefundOrder->refund_status = 2;
  315. RechargeOrder::where('work_id', $orderInfo['work_id'])->update([
  316. 'pay_status' => 2,
  317. 'pay_time' => 0,
  318. 'paid_amount' => 0,
  319. ]);
  320. ServiceWork::where('id', $orderInfo['work_id'])->update([
  321. 'work_status' => 0,
  322. 'user_confirm_status' => 0,
  323. 'service_status' => 4,
  324. 'work_pay_status' => 0
  325. ]);
  326. }else{
  327. $douyinRefundOrder->refund_status = 1;
  328. }
  329. $douyinRefundOrder->save();
  330. Db::commit();
  331. // TODO 需接抖音支付接口
  332. /*if($params['is_examine_ok'] === 'pass'){
  333. //通过后向抖音申请退款
  334. //https://open.douyin.com/api/trade_basic/v1/developer/refund_create/
  335. }*/
  336. return true;
  337. } catch (\Exception $e) {
  338. Db::rollback();
  339. throw new \Exception($e->getMessage());
  340. }
  341. }
  342. public static function refundNotify($params)
  343. {
  344. Db::startTrans();
  345. try {
  346. $douyinRefundOrder = DouyinRefundOrder::where('refund_number', $params['out_refund_no'])->findOrEmpty();
  347. if($douyinRefundOrder->isEmpty()){
  348. throw new \Exception('退款订单不存在');
  349. }
  350. if($douyinRefundOrder->refund_status == 0){
  351. if($params['status'] === 'SUCCESS'){
  352. $douyinRefundOrder->refund_status = 3;
  353. DouyinOrder::where('order_number', $douyinRefundOrder->order_number)->update([
  354. 'order_status' => 4,
  355. 'pay_status' => 3,
  356. ]);
  357. }elseif($params['status'] === 'FAIL'){
  358. $douyinRefundOrder->refund_status = 4;
  359. }else{
  360. throw new \Exception('退款状态未知');
  361. }
  362. $douyinRefundOrder->save();
  363. }
  364. Db::commit();
  365. return true;
  366. } catch (\Exception $e) {
  367. Db::rollback();
  368. throw new \Exception($e->getMessage());
  369. }
  370. }
  371. }