User.php 28.5 KB
Newer Older
D
v1.2.0  
devil_gong 已提交
1 2 3 4
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
D
2.0  
Devil 已提交
5
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
D
v1.2.0  
devil_gong 已提交
6
// +----------------------------------------------------------------------
D
2.0  
Devil 已提交
7
// | Licensed ( https://opensource.org/licenses/mit-license.php )
D
v1.2.0  
devil_gong 已提交
8 9 10 11 12
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\api\controller;

D
Devil 已提交
13
use app\service\ApiService;
D
Devil 已提交
14
use app\service\SystemBaseService;
15 16 17 18
use app\service\UserService;
use app\service\OrderService;
use app\service\GoodsService;
use app\service\MessageService;
D
devil_gong 已提交
19
use app\service\AppCenterNavService;
D
devil 已提交
20
use app\service\BuyService;
D
devil 已提交
21 22 23
use app\service\GoodsFavorService;
use app\service\GoodsBrowseService;
use app\service\IntegralService;
D
v1.2.0  
devil_gong 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

/**
 * 用户
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class User extends Common
{
    /**
     * [__construct 构造方法]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-03T12:39:08+0800
     */
    public function __construct()
    {
        // 调用父类前置方法
        parent::__construct();
    }

    /**
48 49 50 51 52 53
     * 用户登录
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2021-03-04
     * @desc    description
D
v1.2.0  
devil_gong 已提交
54
     */
55
    public function Login()
D
v1.2.0  
devil_gong 已提交
56
    {
D
Devil 已提交
57
        return ApiService::ApiDataReturn(UserService::Login($this->data_post));
58
    }
D
v1.2.0  
devil_gong 已提交
59

60 61 62 63 64 65 66 67 68 69
    /**
     * 用户登录-验证码发送
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2021-03-04
     * @desc    description
     */
    public function LoginVerifySend()
    {
D
Devil 已提交
70
        return ApiService::ApiDataReturn(UserService::LoginVerifySend($this->data_post));
D
v1.2.0  
devil_gong 已提交
71 72 73
    }

    /**
74 75 76 77 78 79 80 81 82
     * 用户注册
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2021-03-04
     * @desc    description
     */
    public function Reg()
    {
D
Devil 已提交
83
        return ApiService::ApiDataReturn(UserService::Reg($this->data_post));
84 85 86 87 88 89 90 91 92
    }

    /**
     * 用户注册-验证码发送
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2021-03-04
     * @desc    description
D
v1.2.0  
devil_gong 已提交
93 94 95
     */
    public function RegVerifySend()
    {
D
Devil 已提交
96
        return ApiService::ApiDataReturn(UserService::RegVerifySend($this->data_post));
97
    }
D
v1.2.0  
devil_gong 已提交
98

99 100 101 102 103 104 105 106 107 108
    /**
     * 密码找回
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2021-03-04
     * @desc    description
     */
    public function ForgetPwd()
    {
D
Devil 已提交
109
        return ApiService::ApiDataReturn(UserService::ForgetPwd($this->data_post));
D
v1.2.0  
devil_gong 已提交
110 111 112
    }

    /**
113 114 115 116 117 118 119 120 121
     * 密码找回-验证码发送
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2021-03-04
     * @desc    description
     */
    public function ForgetPwdVerifySend()
    {
D
Devil 已提交
122
        return ApiService::ApiDataReturn(UserService::ForgetPwdVerifySend($this->data_post));
123 124 125 126 127 128 129 130 131 132 133 134 135 136
    }

    /**
     * 用户-验证码显示
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2021-03-04
     * @desc    description
     */
    public function UserVerifyEntry()
    {
        $params = [
                'width'         => 100,
D
Devil 已提交
137
                'height'        => 28,
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
                'key_prefix'    => input('type', 'user_reg'),
            ];
        $verify = new \base\Verify($params);
        $verify->Entry();
    }

    /**
     * app用户手机绑定
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2021-03-04
     * @desc    description
     */
    public function AppMobileBind()
    {
D
Devil 已提交
154
        return ApiService::ApiDataReturn(UserService::AppMobileBind($this->data_post));
155 156 157 158 159 160 161 162 163 164 165 166
    }

    /**
     * app用户手机绑定-验证码发送
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2021-03-04
     * @desc    description
     */
    public function AppMobileBindVerifySend()
    {
D
Devil 已提交
167
        return ApiService::ApiDataReturn(UserService::AppMobileBindVerifySend($this->data_post));
168 169 170 171 172 173 174 175 176
    }

    /**
     * 支付宝用户授权
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2021-03-04
     * @desc    description
D
v1.2.0  
devil_gong 已提交
177 178 179 180
     */
    public function AlipayUserAuth()
    {
        // 参数
D
Devil 已提交
181
        if(!empty($this->data_post['authcode']))
D
v1.2.0  
devil_gong 已提交
182
        {
D
Devil 已提交
183 184 185
            // 授权
            $result = (new \base\Alipay())->GetAuthSessionKey(MyC('common_app_mini_alipay_appid'), $this->data_post['authcode']);
            if($result['status'] == 0)
D
Devil 已提交
186
            {
D
Devil 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
                // 先从数据库获取用户信息
                $user = UserService::AppUserInfoHandle(null, 'alipay_openid', $result['data']['user_id']);
                if(empty($user))
                {
                    $ret = DataReturn('授权登录成功', 0, ['is_user_exist'=>0, 'openid'=>$result['data']['user_id']]);
                } else {
                    // 用户状态
                    $ret = UserService::UserStatusCheck('id', $user['id']);
                    if($ret['code'] == 0)
                    {
                        // 标记用户存在
                        $user['is_user_exist'] = 1;
                        $ret = DataReturn('授权登录成功', 0, $user);
                    }
                }
            } else {
                $ret = DataReturn($result['msg'], -100);
D
Devil 已提交
204
            }
D
Devil 已提交
205 206
        } else {
            $ret = DataReturn('授权码为空', -1);
D
1.6  
devil_gong 已提交
207
        }
D
Devil 已提交
208
        return ApiService::ApiDataReturn($ret);
D
1.6  
devil_gong 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
    }

    /**
     * 支付宝小程序获取用户信息
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-11-06
     * @desc    description
     */
    public function AlipayUserInfo()
    {
        // 参数校验
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'openid',
                'error_msg'         => 'openid为空',
            ],
        ];
        $ret = ParamsChecked($this->data_post, $p);
D
Devil 已提交
230
        if($ret === true)
D
v1.2.0  
devil_gong 已提交
231
        {
D
Devil 已提交
232 233 234
            // 先从数据库获取用户信息
            $user = UserService::AppUserInfoHandle(null, 'alipay_openid', $this->data_post['openid']);
            if(empty($user))
D
Devil 已提交
235
            {
D
Devil 已提交
236 237 238 239 240 241 242 243 244 245
                $this->data_post['nickname'] = isset($this->data_post['nickName']) ? $this->data_post['nickName'] : '';
                $this->data_post['gender'] = empty($this->data_post['gender']) ? 0 : (($this->data_post['gender'] == 'f') ? 1 : 2);
                $ret = UserService::AuthUserProgram($this->data_post, 'alipay_openid');
            } else {
                // 用户状态
                $ret = UserService::UserStatusCheck('id', $user['id']);
                if($ret['code'] == 0)
                {
                    $ret = DataReturn('授权成功', 0, $user);
                }
D
Devil 已提交
246
            }
D
Devil 已提交
247 248
        } else {
            $ret = DataReturn($ret, -1);
D
v1.2.0  
devil_gong 已提交
249
        }
D
Devil 已提交
250
        return ApiService::ApiDataReturn($ret);
D
v1.2.0  
devil_gong 已提交
251 252
    }

G
微信  
gongfuxiang 已提交
253 254 255 256 257 258 259 260 261 262
    /**
     * 微信小程序获取用户授权
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-11-06
     * @desc    description
     */
    public function WechatUserAuth()
    {
G
gongfuxiang 已提交
263
        // 授权
D
devil 已提交
264 265
        $result = (new \base\Wechat(MyC('common_app_mini_weixin_appid'), MyC('common_app_mini_weixin_appsecret')))->GetAuthSessionKey($this->data_post);
        if($result['status'] == 0)
G
微信  
gongfuxiang 已提交
266
        {
267 268 269
            // unionid
            $unionid = empty($result['data']['unionid']) ? '' : $result['data']['unionid'];

D
devil_gong 已提交
270
            // 先从数据库获取用户信息
271 272 273 274 275 276
            $user = UserService::AppUserInfoHandle(null, 'weixin_openid', $result['data']['openid']);
            if(empty($user) && !empty($unionid))
            {
                // 根据unionid获取数据
                $user = UserService::AppUserInfoHandle(null, 'weixin_unionid', $unionid);
            }
D
devil_gong 已提交
277 278
            if(empty($user))
            {
D
Devil 已提交
279
                $ret = DataReturn('授权登录成功', 0, ['is_user_exist'=>0, 'openid'=>$result['data']['openid'], 'unionid'=>$unionid]);
D
Devil 已提交
280 281 282 283
            } else {
                // 如果用户openid为空则绑定到用户下面
                if(empty($user['weixin_openid']))
                {
D
Devil 已提交
284
                    if(UserService::UserOpenidBind($user['id'], $result['data']['openid'], 'weixin_openid'))
D
Devil 已提交
285 286
                    {
                        // 登录数据更新
D
Devil 已提交
287
                        $user = UserService::AppUserInfoHandle($user['id']);
D
Devil 已提交
288 289
                    }
                }
D
devil_gong 已提交
290
            }
D
devil 已提交
291

D
Devil 已提交
292
            // 用户状态
D
Devil 已提交
293
            if(!empty($user))
D
Devil 已提交
294
            {
D
Devil 已提交
295 296 297 298 299 300 301
                $ret = UserService::UserStatusCheck('id', $user['id']);
                if($ret['code'] == 0)
                {
                    // 标记用户存在
                    $user['is_user_exist'] = 1;
                    $ret = DataReturn('授权登录成功', 0, $user);
                }
D
Devil 已提交
302
            }
D
Devil 已提交
303 304
        } else {
            $ret = DataReturn($result['msg'], -10);
G
微信  
gongfuxiang 已提交
305
        }
D
Devil 已提交
306
        return ApiService::ApiDataReturn($ret);
G
微信  
gongfuxiang 已提交
307 308 309 310 311 312 313 314 315 316 317 318
    }

    /**
     * 微信小程序获取用户信息
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-11-06
     * @desc    description
     */
    public function WechatUserInfo()
    {
G
gongfuxiang 已提交
319 320 321 322 323 324 325 326 327
        // 参数校验
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'openid',
                'error_msg'         => 'openid为空',
            ],
            [
                'checked_type'      => 'empty',
328 329
                'key_name'          => 'auth_data',
                'error_msg'         => '授权数据为空',
G
gongfuxiang 已提交
330 331 332
            ],
        ];
        $ret = ParamsChecked($this->data_post, $p);
D
Devil 已提交
333
        if($ret === true)
G
微信  
gongfuxiang 已提交
334
        {
D
Devil 已提交
335 336 337
            // 先从数据库获取用户信息
            $user = UserService::AppUserInfoHandle(null, 'weixin_openid', $this->data_post['openid']);
            if(empty($user))
D
Devil 已提交
338
            {
D
Devil 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
                // 字段名称不一样参数处理
                $auth_data = is_array($this->data_post['auth_data']) ? $this->data_post['auth_data'] : json_decode(htmlspecialchars_decode($this->data_post['auth_data']), true);
                $auth_data['nickname'] = isset($auth_data['nickName']) ? $auth_data['nickName'] : '';
                $auth_data['avatar'] = isset($auth_data['avatarUrl']) ? $auth_data['avatarUrl'] : '';
                $auth_data['gender'] = empty($auth_data['gender']) ? 0 : (($auth_data['gender'] == 2) ? 1 : 2);

                // 公共参数处理
                $auth_data['weixin_unionid'] = isset($this->data_post['unionid']) ? $this->data_post['unionid'] : '';
                $auth_data['openid'] = $this->data_post['openid'];
                $auth_data['referrer']= isset($this->data_post['referrer']) ? $this->data_post['referrer'] : 0;
                $ret = UserService::AuthUserProgram($auth_data, 'weixin_openid');
            } else {
                // 用户状态
                $ret = UserService::UserStatusCheck('id', $user['id']);
                if($ret['code'] == 0)
                {
                    $ret = DataReturn('授权成功', 0, $user);
                }
            }   
        } else {
            $ret = DataReturn($ret, -1);
G
微信  
gongfuxiang 已提交
360
        }
D
Devil 已提交
361
        return ApiService::ApiDataReturn($ret);
G
微信  
gongfuxiang 已提交
362 363
    }

D
v1.2.0  
devil_gong 已提交
364 365 366 367 368 369 370 371 372 373
    /**
     * 百度小程序获取用户信息
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-11-06
     * @desc    description
     */
    public function BaiduUserAuth()
    {
D
devil 已提交
374 375 376 377 378 379
        $config = [
            'appid'     => MyC('common_app_mini_baidu_appid'),
            'key'       => MyC('common_app_mini_baidu_appkey'),
            'secret'    => MyC('common_app_mini_baidu_appsecret'),
        ];
        $result = (new \base\Baidu($config))->GetAuthSessionKey($this->data_post);
D
v1.2.0  
devil_gong 已提交
380 381
        if($result['status'] == 0)
        {
D
devil_gong 已提交
382 383
            // 先从数据库获取用户信息
            $user = UserService::AppUserInfoHandle(null, 'baidu_openid', $result);
D
Devil 已提交
384
            if(!empty($user))
D
Devil 已提交
385
            {
D
Devil 已提交
386 387 388 389 390 391 392 393 394 395
                // 用户状态
                $ret = UserService::UserStatusCheck('id', $user['id']);
                if($ret['code'] == 0)
                {
                    // 标记用户存在
                    $user['is_user_exist'] = 1;
                    $ret = DataReturn('授权登录成功', 0, $user);
                }
            } else {
                $ret = DataReturn('授权登录成功', 0, ['is_user_exist'=>0, 'openid'=>$result['data']]);
D
Devil 已提交
396
            }
D
Devil 已提交
397 398
        } else {
            $ret = DataReturn($result['msg'], -10);
D
v1.2.0  
devil_gong 已提交
399
        }
D
Devil 已提交
400
        return ApiService::ApiDataReturn($ret);
D
v1.2.0  
devil_gong 已提交
401 402
    }

D
devil_gong 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
    /**
     * 百度小程序获取用户信息
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-11-06
     * @desc    description
     */
    public function BaiduUserInfo()
    {
        // 参数校验
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'openid',
                'error_msg'         => 'openid为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'encrypted_data',
                'error_msg'         => '解密数据为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'iv',
                'error_msg'         => 'iv为空,请重试',
            ]
        ];
        $ret = ParamsChecked($this->data_post, $p);
D
Devil 已提交
432
        if($ret === true)
D
devil_gong 已提交
433
        {
D
Devil 已提交
434 435 436
            // 先从数据库获取用户信息
            $user = UserService::AppUserInfoHandle(null, 'baidu_openid', $this->data_post['openid']);
            if(empty($user))
D
devil_gong 已提交
437
            {
D
Devil 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
                $config = [
                    'appid'     => MyC('common_app_mini_baidu_appid'),
                    'key'       => MyC('common_app_mini_baidu_appkey'),
                    'secret'    => MyC('common_app_mini_baidu_appsecret'),
                ];
                $result = (new \base\Baidu($config))->DecryptData($this->data_post['encrypted_data'], $this->data_post['iv'], $this->data_post['openid']);

                if($result['status'] == 0 && !empty($result['data']))
                {
                    $result['nickname'] = isset($result['data']['nickname']) ? $result['data']['nickname'] : '';
                    $result['avatar'] = isset($result['data']['headimgurl']) ? $result['data']['headimgurl'] : '';
                    $result['gender'] = empty($result['data']['sex']) ? 0 : (($result['data']['sex'] == 2) ? 1 : 2);
                    $result['openid'] = $result['data']['openid'];
                    $result['referrer']= isset($this->data_post['referrer']) ? $this->data_post['referrer'] : 0;
                    $ret = UserService::AuthUserProgram($result, 'baidu_openid');
                } else {
                    $ret = DataReturn($result['msg'], -1);
                }
D
devil 已提交
456
            } else {
D
Devil 已提交
457 458 459 460 461 462
                // 用户状态
                $ret = UserService::UserStatusCheck('id', $user['id']);
                if($ret['code'] == 0)
                {
                    $ret = DataReturn('授权成功', 0, $user);
                }
D
devil_gong 已提交
463 464
            }
        } else {
D
Devil 已提交
465
            $ret = DataReturn($ret, -1);
D
devil_gong 已提交
466
        }
D
Devil 已提交
467
        return ApiService::ApiDataReturn($ret);
D
devil_gong 已提交
468 469
    }

D
Devil 已提交
470 471 472 473 474 475 476 477 478 479
    /**
     * 头条小程序用户授权
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-10-27
     * @desc    description
     */
    public function ToutiaoUserAuth()
    {
D
devil 已提交
480
        $config = [
D
devil_gong 已提交
481 482
            'appid'     => MyC('common_app_mini_toutiao_appid'),
            'secret'    => MyC('common_app_mini_toutiao_appsecret'),
D
Devil 已提交
483
        ];
D
devil 已提交
484
        $result = (new \base\Toutiao($config))->GetAuthSessionKey($this->data_post);
D
Devil 已提交
485 486
        if($result['status'] == 0)
        {
D
devil_gong 已提交
487 488 489 490
            // 先从数据库获取用户信息
            $user = UserService::AppUserInfoHandle(null, 'toutiao_openid', $result);
            if(empty($user))
            {
D
Devil 已提交
491 492 493 494 495 496 497 498 499 500
                $ret = DataReturn('授权登录成功', 0, ['is_user_exist'=>0, 'openid'=>$result['data']]);
            } else {
                // 用户状态
                $ret = UserService::UserStatusCheck('id', $user['id']);
                if($ret['code'] == 0)
                {
                    // 标记用户存在
                    $user['is_user_exist'] = 1;
                    $ret = DataReturn('授权登录成功', 0, $user);
                }
D
Devil 已提交
501
            }
D
Devil 已提交
502 503
        } else {
            $ret = DataReturn($result['msg'], -10);
D
Devil 已提交
504
        }
D
Devil 已提交
505
        return ApiService::ApiDataReturn($ret);
D
Devil 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
    }

    /**
     * 头条小程序获取用户信息
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-10-27
     * @desc    description
     */
    public function ToutiaoUserInfo()
    {
        // 参数校验
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'openid',
                'error_msg'         => 'openid为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'userinfo',
                'error_msg'         => '用户信息为空',
            ],
        ];
        $ret = ParamsChecked($this->data_post, $p);
D
Devil 已提交
532
        if($ret === true)
D
Devil 已提交
533
        {
D
Devil 已提交
534 535 536
            // 先从数据库获取用户信息
            $user = UserService::AppUserInfoHandle(null, 'toutiao_openid', $this->data_post['openid']);
            if(empty($user))
D
Devil 已提交
537
            {
D
Devil 已提交
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
                $result = json_decode(htmlspecialchars_decode($this->data_post['userinfo']), true);
                if(is_array($result))
                {
                    $result['nickname'] = isset($result['nickName']) ? $result['nickName'] : '';
                    $result['avatar'] = isset($result['avatarUrl']) ? $result['avatarUrl'] : '';
                    $result['gender'] = empty($result['gender']) ? 0 : (($result['gender'] == 2) ? 1 : 2);
                    $result['openid'] = $this->data_post['openid'];
                    $result['referrer']= isset($this->data_post['referrer']) ? $this->data_post['referrer'] : 0;
                    $ret = UserService::AuthUserProgram($result, 'toutiao_openid');
                } else {
                    $ret = DataReturn(empty($result) ? '获取用户信息失败' : $result, -1);
                }
            } else {
                // 用户状态
                $ret = UserService::UserStatusCheck('id', $user['id']);
                if($ret['code'] == 0)
                {
                    $ret = DataReturn('授权成功', 0, $user);
                }
D
Devil 已提交
557 558
            }
        } else {
D
Devil 已提交
559
            $ret = DataReturn($ret, -1);
D
Devil 已提交
560
        }
D
Devil 已提交
561
        return ApiService::ApiDataReturn($ret);
D
Devil 已提交
562 563
    }

D
devil_gong 已提交
564 565 566 567 568 569 570 571 572 573 574
    /**
     * QQ小程序获取用户授权
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-10-31
     * @desc    description
     */
    public function QQUserAuth()
    {
        // 参数
D
Devil 已提交
575
        if(!empty($this->data_post['authcode']))
D
devil_gong 已提交
576
        {
D
Devil 已提交
577 578 579
            // 授权
            $result = (new \base\QQ(MyC('common_app_mini_qq_appid'), MyC('common_app_mini_qq_appsecret')))->GetAuthSessionKey($this->data_post['authcode']);
            if($result !== false)
D
Devil 已提交
580
            {
D
Devil 已提交
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
                // 先从数据库获取用户信息
                $user = UserService::AppUserInfoHandle(null, 'qq_openid', $result);
                if(empty($user))
                {
                    $ret = DataReturn('授权登录成功', 0, ['is_user_exist'=>0, 'openid'=>$result]);
                } else {
                    // 用户状态
                    $ret = UserService::UserStatusCheck('id', $user['id']);
                    if($ret['code'] == 0)
                    {
                        // 标记用户存在
                        $user['is_user_exist'] = 1;
                        $ret = DataReturn('授权登录成功', 0, $user);
                    }
                }
            } else {
                $ret = DataReturn('授权登录失败', -100);
D
Devil 已提交
598
            }
D
Devil 已提交
599 600
        } else {
            $ret = DataReturn('授权码为空', -1);
D
devil_gong 已提交
601
        }
D
Devil 已提交
602
        return ApiService::ApiDataReturn($ret);
D
devil_gong 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
    }

    /**
     * QQ小程序获取用户信息
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-10-31
     * @desc    description
     */
    public function QQUserInfo()
    {
        // 参数校验
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'openid',
                'error_msg'         => 'openid为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'encrypted_data',
                'error_msg'         => '解密数据为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'iv',
                'error_msg'         => 'iv为空,请重试',
            ]
        ];
        $ret = ParamsChecked($this->data_post, $p);
D
Devil 已提交
634
        if($ret === true)
D
devil_gong 已提交
635
        {
D
Devil 已提交
636 637 638
            // 先从数据库获取用户信息
            $user = UserService::AppUserInfoHandle(null, 'qq_openid', $this->data_post['openid']);
            if(empty($user))
D
devil_gong 已提交
639
            {
D
Devil 已提交
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
                $result = (new \base\QQ(MyC('common_app_mini_qq_appid'), MyC('common_app_mini_qq_appsecret')))->DecryptData($this->data_post['encrypted_data'], $this->data_post['iv'], $this->data_post['openid']);
                if(is_array($result))
                {
                    $result['nickname'] = isset($result['nickName']) ? $result['nickName'] : '';
                    $result['avatar'] = isset($result['avatarUrl']) ? $result['avatarUrl'] : '';
                    $result['gender'] = empty($result['gender']) ? 0 : (($result['gender'] == 2) ? 1 : 2);
                    $result['qq_unionid'] = isset($result['unionId']) ? $result['unionId'] : '';
                    $result['openid'] = $result['openId'];
                    $result['referrer']= isset($this->data_post['referrer']) ? $this->data_post['referrer'] : 0;
                    $ret = UserService::AuthUserProgram($result, 'qq_openid');
                } else {
                    $ret = DataReturn(empty($result) ? '获取用户信息失败' : $result, -1);
                }
            } else {
                // 用户状态
                $ret = UserService::UserStatusCheck('id', $user['id']);
                if($ret['code'] == 0)
                {
                    $ret = DataReturn('授权成功', 0, $user);
                }
            }   
D
devil_gong 已提交
661
        } else {
D
Devil 已提交
662
            $ret = DataReturn($ret, -1);
D
devil_gong 已提交
663
        }
D
Devil 已提交
664
        return ApiService::ApiDataReturn($ret);
D
devil_gong 已提交
665 666
    }

D
v1.2.0  
devil_gong 已提交
667 668 669 670 671 672 673 674 675 676
    /**
     * [ClientCenter 用户中心]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-05-21T15:21:52+0800
     */
    public function Center()
    {
        // 登录校验
G
gongfuxiang 已提交
677
        $this->IsLogin();
D
v1.2.0  
devil_gong 已提交
678 679

        // 订单总数
D
devil_gong 已提交
680
        $where = ['user_id'=>$this->user['id'], 'is_delete_time'=>0, 'user_is_delete_time'=>0];
D
v1.2.0  
devil_gong 已提交
681 682 683 684
        $user_order_count = OrderService::OrderTotal($where);

        // 商品收藏总数
        $where = ['user_id'=>$this->user['id']];
D
devil 已提交
685
        $user_goods_favor_count = GoodsFavorService::GoodsFavorTotal($where);
D
v1.2.0  
devil_gong 已提交
686 687 688

        // 商品浏览总数
        $where = ['user_id'=>$this->user['id']];
D
devil 已提交
689
        $user_goods_browse_count = GoodsBrowseService::GoodsBrowseTotal($where);
D
v1.2.0  
devil_gong 已提交
690 691

        // 未读消息总数
D
devil_gong 已提交
692
        $params = ['user'=>$this->user, 'is_more'=>1, 'is_read'=>0];
D
v1.2.0  
devil_gong 已提交
693 694 695
        $common_message_total = MessageService::UserMessageTotal($params);

        // 用户订单状态
D
Devil 已提交
696
        $user_order_status = OrderService::OrderStatusStepTotal(['user_type'=>'user', 'user'=>$this->user, 'is_comments'=>1, 'is_aftersale'=>1]);
D
v1.2.0  
devil_gong 已提交
697

D
devil 已提交
698
        // 用户积分
D
Devil 已提交
699 700
        $integral = IntegralService::UserIntegral($params['user']['id']);
        $user_integral = (!empty($integral) && !empty($integral['integral'])) ? $integral['integral'] : 0;
D
devil 已提交
701

D
v1.2.0  
devil_gong 已提交
702 703
        // 初始化数据
        $result = array(
D
devil 已提交
704
            'integral'                          => $user_integral,
D
v1.2.0  
devil_gong 已提交
705 706 707
            'avatar'                            => $this->user['avatar'],
            'nickname'                          => $this->user['nickname'],
            'username'                          => $this->user['username'],
D
Devil 已提交
708
            'user_name_view'                    => $this->user['user_name_view'],
D
v1.2.0  
devil_gong 已提交
709 710 711 712 713
            'user_order_status'                 => $user_order_status['data'],
            'user_order_count'                  => $user_order_count,
            'user_goods_favor_count'            => $user_goods_favor_count,
            'user_goods_browse_count'           => $user_goods_browse_count,
            'common_message_total'              => $common_message_total,
D
devil_gong 已提交
714
            'navigation'                        => AppCenterNavService::AppCenterNav(),
D
devil 已提交
715
            'common_cart_total'                 => BuyService::UserCartTotal(['user'=>$this->user]),
D
v1.2.0  
devil_gong 已提交
716 717 718
        );

        // 返回数据
D
Devil 已提交
719
        return ApiService::ApiDataReturn(SystemBaseService::DataReturn($result));
D
v1.2.0  
devil_gong 已提交
720
    }
721 722

    /**
D
devil 已提交
723
     * 小程序用户手机一键绑定
724 725 726 727 728 729
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2020-09-20
     * @desc    description
     */
D
devil 已提交
730
    public function OnekeyUserMobileBind()
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
    {
        // 参数校验
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'openid',
                'error_msg'         => 'openid为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'encrypted_data',
                'error_msg'         => '解密数据为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'iv',
                'error_msg'         => 'iv为空,请重试',
            ]
        ];
        $ret = ParamsChecked($this->data_post, $p);
D
Devil 已提交
751
        if($ret === true)
752
        {
D
Devil 已提交
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
            // 根据不同平台处理数据解密逻辑
            $mobile = '';
            $error_msg = '';
            switch(APPLICATION_CLIENT_TYPE)
            {
                // 微信
                case 'weixin' :
                    $result = (new \base\Wechat(MyC('common_app_mini_weixin_appid'), MyC('common_app_mini_weixin_appsecret')))->DecryptData($this->data_post['encrypted_data'], $this->data_post['iv'], $this->data_post['openid']);
                    if($result['status'] == 0 && !empty($result['data']) && !empty($result['data']['purePhoneNumber']))
                    {
                        $mobile = $result['data']['purePhoneNumber'];
                    } else {
                        $error_msg = $result['msg'];
                    }
                    break;

                // 百度
                case 'baidu' :
                    $config = [
                        'appid'     => MyC('common_app_mini_baidu_appid'),
                        'key'       => MyC('common_app_mini_baidu_appkey'),
                        'secret'    => MyC('common_app_mini_baidu_appsecret'),
                    ];
                    $result = (new \base\Baidu($config))->DecryptData($this->data_post['encrypted_data'], $this->data_post['iv'], $this->data_post['openid'], 'mobile_bind');
                    if($result['status'] == 0 && !empty($result['data']) && !empty($result['data']['mobile']))
                    {
                        $mobile = $result['data']['mobile'];
                    } else {
                        $error_msg = $result['msg'];
                    }
                    break;
D
devil 已提交
784

D
Devil 已提交
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
                // 默认
                default :
                    $error_msg = APPLICATION_CLIENT_TYPE.'平台还未开发手机一键登录';
            }
            if(empty($mobile) || !empty($error_msg))
            {
                $ret = DataReturn(empty($error_msg) ? '数据解密失败' : $error_msg, -1);
            } else {
                // 用户信息处理
                $this->data_post['mobile'] = $mobile;
                $this->data_post['is_onekey_mobile_bind'] = 1;
                $ret = UserService::AuthUserProgram($this->data_post, APPLICATION_CLIENT_TYPE.'_openid');
            }
        } else {
            $ret = DataReturn($ret, -1);
800
        }
D
Devil 已提交
801
        return ApiService::ApiDataReturn($ret);
802
    }
D
v1.2.0  
devil_gong 已提交
803 804
}
?>