瀏覽代碼

feat(user): 添加小程序企业用户关联功能

- 新增 merId 和 source 属性用于企业用户识别
- 从请求头获取 source 和 merId 参数
- 添加数据库日志记录手机解密数据
- 实现企业用户关联逻辑,与 Auth::applet_phone 保持一致
- 支持通过 spread 或 share_id 进行推广关联
- 验证邀请人存在性并创建企业用户关系
- 在企业环境中自动注册用户到企业用户表
shichen 2 月之前
父節點
當前提交
65347e9325
共有 1 個文件被更改,包括 34 次插入0 次删除
  1. 34 0
      app/controller/api/user/UserMp.php

+ 34 - 0
app/controller/api/user/UserMp.php

@@ -29,11 +29,15 @@ class UserMp extends BaseController
29 29
     public $repository;
30 30
     private $appid;
31 31
     private $secret;
32
+    protected $merId;
33
+    protected $source;
32 34
 
33 35
     public function __construct(App $app, UserRepository $repository)
34 36
     {
35 37
         parent::__construct($app);
36 38
         $this->repository = $repository;
39
+        $this->source = $this->request->header('source');
40
+        $this->merId = $this->request->header('merId');
37 41
         //点餐小程序
38 42
         // $this->appid = 'wx10669a2eca032216';
39 43
         // $this->secret = '0e71d5c636fdb9bf1c79c403db70f739';
@@ -110,6 +114,7 @@ class UserMp extends BaseController
110 114
             if ($errCode != 0) {
111 115
                 return app('json')->fail('手机号解密失败');
112 116
             }
117
+            Db::name('log')->insert(['data' => 'mpLogin一键登录获取的数据:' . $decryptData]);
113 118
             $decryptData = json_decode($decryptData, true);
114 119
             $phone = $decryptData['purePhoneNumber'] ?? '';
115 120
             if ($phone == '') {
@@ -135,6 +140,35 @@ class UserMp extends BaseController
135 140
             }
136 141
 
137 142
             $user = $repository->accountByUser($phone);
143
+
144
+            // 企业用户关联逻辑(与 Auth::applet_phone 保持一致)
145
+            if ($this->source == 'yhc' && $this->merId) {
146
+                $where = ['mer_id' => $this->merId, 'uid' => $user['uid']];
147
+                $count = Db::name('enterprise_user')->where($where)->count();
148
+                if ($count < 1) {
149
+                    $insert = ['mer_id' => $this->merId, 'uid' => $user['uid']];
150
+                    $epData = $this->request->params(['spread', 'share_id']);
151
+
152
+                    if (isset($epData['spread']) && $epData['spread']) {
153
+                        $insert['puid'] = $epData['spread'];
154
+                        $insert['node_uid'] = $epData['spread'];
155
+                    } else if (isset($epData['share_id']) && $epData['share_id']) {
156
+                        $spreadUid = Db::name('product_share')->where('id', $epData['share_id'])->value('uid');
157
+                        $insert['puid'] = $spreadUid;
158
+                        $insert['node_uid'] = $spreadUid;
159
+                    }
160
+                    if (!isset($insert['node_uid']) || !$insert['node_uid']) {
161
+                        return app('json')->fail('必须要有邀请人才能注册', ['is' => 1]);
162
+                    }
163
+
164
+                    if (isset($epData['spread']) && isset($zhuce)) {
165
+                        $insert['puid'] = $epData['spread'];
166
+                        $insert['node_uid'] = $epData['spread'];
167
+                    }
168
+                    Db::name('enterprise_user')->insert($insert);
169
+                }
170
+            }
171
+
138 172
             $user = $repository->mainUser($user);
139 173
             $tokenInfo = $repository->createToken($user);
140 174
             $repository->loginAfter($user);