DouYinService.php 15 KB

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