| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- <?php
- namespace app\api\service;
- use app\adminapi\logic\external\ExternalConsultationLogic;
- use app\api\logic\ServiceOrderLogic;
- use app\common\model\Config;
- use app\common\model\external\DouyinOrder;
- use app\common\model\external\DouyinRefundOrder;
- use app\common\model\external\ExternalConsultation;
- use app\common\model\external\ExternalConsultationOrder;
- use app\common\model\goods\Goods;
- use app\common\model\recharge\RechargeOrder;
- use app\common\model\user\User;
- use app\common\model\user\UserAuth;
- use app\common\model\works\ServiceWork;
- use app\common\service\ConfigService;
- use app\common\service\FileService;
- use think\facade\Db;
- use think\facade\Log;
- class DouYinService
- {
- protected static int $terminal = \app\common\enum\user\UserTerminalEnum::DOUYIN;
- protected static int $external_platform_id = 7;
- public static function register(array $params)
- {
- $userSn = User::createUserSn();
- $params['password'] = !empty($params['password'])?$params['password']:rand(100000,999999);
- $passwordSalt = \think\facade\Config::get('project.unique_identification');
- $password = create_password($params['password'], $passwordSalt);
- $avatar = ConfigService::get('default_image', 'user_avatar');
- $user = User::create([
- 'sn' => $userSn,
- 'avatar' => $avatar,
- 'nickname' => '用户' . $userSn,
- 'account' => $params['account'],
- 'mobile' => !empty($params['mobile'])?$params['mobile']:'',
- 'password' => $password,
- 'channel' => self::$terminal,
- 'user_type' => $params['user_type']??0,
- ]);
- return $user;
- }
- public static function phoneLogin(array $params)
- {
- try {
- $where = ['mobile' => $params['mobile']];
- $params['account'] = $params['mobile'];
- $user = User::where($where)->findOrEmpty();
- if ($user->isEmpty()) {
- //直接注册用户
- $params['channel'] = self::$terminal;
- $user = self::register($params);
- }
- //更新登录信息
- $user->login_time = time();
- $user->login_ip = request()->ip();
- $user->save();
- $userInfo = UserTokenService::setToken($user->id, self::$terminal);
- //返回登录信息
- $avatar = $user->avatar ?: Config::get('project.default_image.user_avatar');
- $avatar = FileService::getFileUrl($avatar);
- return [
- 'nickname' => $userInfo['nickname'],
- 'sn' => $userInfo['sn'],
- 'mobile' => $userInfo['mobile'],
- 'avatar' => $avatar,
- 'token' => $userInfo['token'],
- ];
- } catch (\Exception $e) {
- throw new \Exception($e->getMessage());
- }
- }
- /**
- * 提交订单
- * @param array $params
- * @return array|false
- */
- public static function submitOrder($params)
- {
- Db::startTrans();
- try {
- $goods = Goods::findOrEmpty($params['goods_id']);
- if($goods->isEmpty()){
- throw new \Exception('产品不存在!');
- }
- if(empty($params['user_info']['mobile'])){
- throw new \Exception('请先补充您的联系方式后在提交订单');
- }
- // TODO tmp防抖1m
- $isExist = DouyinOrder::where(['user_id'=>$params['user_id'],'goods_id'=>$goods['goods_id']])->where('create_time','>',(time() - 60))->findOrEmpty();
- if(!$isExist->isEmpty()){
- throw new \Exception('请勿重复下单!');
- }
- $quantity = $params['quantity']??1;
- //生成订单
- $create_data = [
- 'user_id' => $params['user_id'],
- 'mobile' => $params['user_info']['mobile'],
- 'title' => $goods['goods_name'],
- 'goods_id'=>$goods['goods_id'],
- 'unit_price' => $goods['service_fee'],
- 'quantity' => $quantity,
- 'total_amount' => $goods['service_fee'] * $quantity,
- 'order_number' => generate_sn(DouyinOrder::class, 'order_number'),
- ];
- $order = DouyinOrder::create($create_data);
- Db::commit();
- return $order['id'];
- } catch (\Exception $e) {
- Db::rollback();
- throw new \Exception($e->getMessage());
- }
- }
- public static function payNotify($params)
- {
- Log::write($params,JSON_UNESCAPED_UNICODE);
- // 查询抖音订单是否完成支付
- if ($params['trade_state'] === 'SUCCESS') {
- $transaction_id = '';
- $paid_amount = '';
- $out_trade_no = $params['out_trade_no'];
- $order = DouyinOrder::where('order_number', $out_trade_no)->findOrEmpty();
- if(!$order->isEmpty()){
- // 更新充值订单状态
- $order->transaction_id = $transaction_id;
- $order->order_status = 2;
- $order->pay_time = time();
- $order->paid_amount = $paid_amount;
- $user = User::where('id',$order->user_id)->findOrEmpty()->toArray();
- $form_detail = [
- 'user_name' => $user['real_name']??'',
- 'mobile' => $user['mobile'],
- 'transaction_id' => $transaction_id,
- 'out_trade_no' => $out_trade_no,
- 'paid_amount' => $paid_amount,
- ];
- $consultation = ExternalConsultation::create([
- 'external_platform_id' => self::$external_platform_id,
- 'form_detail' => $form_detail,
- 'user_name' => $user['real_name']??'',
- 'mobile' => $user['mobile'],
- 'goods_id' => $order->goods_id,
- 'amount' => $paid_amount
- ]);
- $order->consultation_id = $consultation['id'];
- $order->save();
- return true;
- }
- }
- return false;
- }
- public static function reservation($params)
- {
- // $params['order_number']
- Db::startTrans();
- try {
- $order = DouyinOrder::where('order_number', $params['order_number'])->findOrEmpty();
- if(!$order->isEmpty()){
- $consultation = ExternalConsultation::where('id', $order->consultation_id)->findOrEmpty()->toArray();
- $consultation['user_address'] = $params['user_address'];
- $consultation['lon'] = $params['lon'];
- $consultation['lat'] = $params['lat'];
- $consultation['appointment_time'] = $params['appointment_time'];
- $result = ExternalConsultationLogic::order($consultation);
- if (false === $result) {
- throw new \Exception('预约失败');
- }
- $consultationOrder = ExternalConsultationOrder::where('consultation_id', $order->consultation_id)->where('goods_id', $order->goods_id)->where('amount', $order->paid_amount)
- ->findOrEmpty()->toArray();
- $work_status = ServiceWork::where('id', $consultationOrder['work_id'])->value('work_status');
- $order->work_id = $consultationOrder['work_id'];
- $order->fulfillment_status = $work_status;
- $order->save();
- }
- Db::commit();
- return $order['id'];
- } catch (\Exception $e) {
- Db::rollback();
- throw new \Exception($e->getMessage());
- }
- }
- public static function upReservation($params)
- {
- // $params['order_number']
- Db::startTrans();
- try {
- $order = DouyinOrder::where('order_number', $params['order_number'])->findOrEmpty();
- if(!$order->isEmpty()){
- // sn appointment_time
- $result = ServiceOrderLogic::approvalChangeAppointment(['sn'=>RechargeOrder::where('work_id', $order->work_id)->value('sn'),'appointment_time'=>$params['appointment_time']]);
- if (false === $result) {
- throw new \Exception(ServiceOrderLogic::getError());
- }
- $order->fulfillment_status = ServiceWork::where('id', $order->work_id)->value('work_status');
- $order->save();
- }
- Db::commit();
- return $order['id'];
- } catch (\Exception $e) {
- Db::rollback();
- throw new \Exception($e->getMessage());
- }
- }
- public static function getOrderDetail($params)
- {
- //抖音订单信息/商品信息/预约信息(地址、时间、履约状态与信息)
- // $params['order_number'] user_id
- $order = DouyinOrder::with(['goods','serviceWork'])->where('order_number', $params['order_number'])->where('user_id', $params['user_id'])->findOrEmpty();
- if($order->isEmpty()){
- return [];
- }
- $orderInfo = $order->toArray();
- empty($orderInfo['goods']) && $orderInfo['goods'] = [];
- empty($orderInfo['serviceWork']) && $orderInfo['serviceWork'] = [];
- $work_status = $orderInfo['serviceWork']['work_status']??0;
- $performance = [];
- // tmp
- switch ($work_status){
- case 0:
- $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
- break;
- case 1:
- case 2:
- case 3:
- $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
- $performance[] = ['status' => '已派单','title' => '已派单','time' => date('Y-m-d H:i:s',time())];
- break;
- case 4:
- case 5:
- case 6:
- $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
- $performance[] = ['status' => '已派单','title' => '已派单','time' => date('Y-m-d H:i:s',time())];
- $performance[] = ['status' => '服务中','title' => '服务中','time' => date('Y-m-d H:i:s',time())];
- break;
- case 7:
- case 8:
- $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
- $performance[] = ['status' => '已派单','title' => '已派单','time' => date('Y-m-d H:i:s',time())];
- $performance[] = ['status' => '服务中','title' => '服务中','time' => date('Y-m-d H:i:s',time())];
- $performance[] = ['status' => '已完结','title' => '已完结','time' => date('Y-m-d H:i:s',time())];
- break;
- }
- $orderInfo['performance'] = $performance;
- return $orderInfo;
- }
- public static function refund($params)
- {
- Db::startTrans();
- try {
- // $params['order_number'] user_id
- $order = DouyinOrder::with(['goods','serviceWork'])->where('order_number', $params['order_number'])->where('user_id', $params['user_id'])->findOrEmpty();
- if($order->isEmpty()){
- throw new \Exception('订单不存在');
- }
- $orderInfo = $order->toArray();
- $work_status = $orderInfo['serviceWork']['work_status']??0;
- if(3 < $work_status){
- throw new \Exception('该订单禁止退款');
- }
- DouyinRefundOrder::create([
- 'refund_number' => generate_sn(DouyinRefundOrder::class, 'refund_number'),
- 'order_number' => $orderInfo['order_number'],
- 'transaction_id' => $orderInfo['transaction_id'],
- 'reason' => $params['reason']??'',
- 'refund_status' => 0,
- 'user_id' => $orderInfo['user_id'],
- 'refund_amount' => $orderInfo['paid_amount'],
- ]);
- Db::commit();
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- throw new \Exception($e->getMessage());
- }
- }
- public static function refundExamine($params)
- {
- Db::startTrans();
- try {
- // $params['order_number'] user_id
- $order = DouyinOrder::with(['goods','serviceWork'])->where('order_number', $params['order_number'])->where('user_id', $params['user_id'])->findOrEmpty();
- if($order->isEmpty()){
- throw new \Exception('订单不存在');
- }
- $orderInfo = $order->toArray();
- $douyinRefundOrder = DouyinRefundOrder::where('refund_number', $params['refund_number'])->findOrEmpty();
- if($params['is_examine_ok'] === 'pass'){
- $douyinRefundOrder->refund_status = 2;
- RechargeOrder::where('work_id', $orderInfo['work_id'])->update([
- 'pay_status' => 2,
- 'pay_time' => 0,
- 'paid_amount' => 0,
- ]);
- ServiceWork::where('id', $orderInfo['work_id'])->update([
- 'work_status' => 0,
- 'user_confirm_status' => 0,
- 'service_status' => 4,
- 'work_pay_status' => 0
- ]);
- }else{
- $douyinRefundOrder->refund_status = 1;
- }
- $douyinRefundOrder->save();
- Db::commit();
- // TODO 需接抖音支付接口
- /*if($params['is_examine_ok'] === 'pass'){
- //通过后向抖音申请退款
- //https://open.douyin.com/api/trade_basic/v1/developer/refund_create/
- }*/
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- throw new \Exception($e->getMessage());
- }
- }
- public static function refundNotify($params)
- {
- Db::startTrans();
- try {
- $douyinRefundOrder = DouyinRefundOrder::where('refund_number', $params['out_refund_no'])->findOrEmpty();
- if($douyinRefundOrder->isEmpty()){
- throw new \Exception('退款订单不存在');
- }
- if($douyinRefundOrder->refund_status == 0){
- if($params['status'] === 'SUCCESS'){
- $douyinRefundOrder->refund_status = 3;
- DouyinOrder::where('order_number', $douyinRefundOrder->order_number)->update([
- 'order_status' => 4,
- 'pay_status' => 3,
- ]);
- }elseif($params['status'] === 'FAIL'){
- $douyinRefundOrder->refund_status = 4;
- }else{
- throw new \Exception('退款状态未知');
- }
- $douyinRefundOrder->save();
- }
- Db::commit();
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- throw new \Exception($e->getMessage());
- }
- }
- }
|