|
@@ -0,0 +1,193 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+namespace app\api\service;
|
|
|
|
|
+
|
|
|
|
|
+use app\adminapi\logic\external\ExternalConsultationLogic;
|
|
|
|
|
+use app\common\model\Config;
|
|
|
|
|
+use app\common\model\external\DouyinOrder;
|
|
|
|
|
+use app\common\model\external\ExternalConsultation;
|
|
|
|
|
+use app\common\model\external\ExternalConsultationOrder;
|
|
|
|
|
+use app\common\model\goods\Goods;
|
|
|
|
|
+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());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|