DouYinService.php 15 KB

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