Passport.php 29.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 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 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 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 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 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 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
<?php

/**
 * Created by PhpStorm.
 * User: ziy
 * Date: 14-7-14
 * Time: 下午4:08
 */
class YHMApi_App_V1_Passport extends YHMApi_App_V1_Base {

    /**
     * 注册发送验证码
     * @param string $mobile
     */
    private static function sendMobileCode($mobile) {
        $mc = YHMUtils_Mc::getMc();
        $strValidData = $mc->get('valid_code_' . $mobile);
        if (!empty($strValidData)) {
            $validData = json_decode($strValidData, true);
            if (time() - $validData['send_time'] < 120) {
                return array('status' => false, 'message' => '请稍后重试');
            }
        }
        $code = substr(mt_rand(), 0, 4);
        $strVaildData = json_encode(array('send_time' => time(), 'valid_code' => $code));
        $mc->set('valid_code_' . $mobile, $strVaildData, 600);
        return YHMUtils_Sms_Client::Sms('yhmreg', $mobile, array('code' => $code));
    }

    /**
     * 找回密码发送验证码
     * @param string $mobile
     */
    private static function sendMobileCodeFindPassword($mobile) {
        $mc = YHMUtils_Mc::getMc();
        $strValidData = $mc->get('valid_code_' . $mobile);
        if (!empty($strValidData)) {
            $validData = json_decode($strValidData, true);
            if (time() - $validData['send_time'] < 120) {
                return array('status' => false, 'message' => '请稍后重试');
            }
        }
        $code = substr(mt_rand(), 0, 4);
        $strVaildData = json_encode(array('send_time' => time(), 'valid_code' => $code));
        $mc->set('valid_code_' . $mobile, $strVaildData, 600);
        return YHMUtils_Sms_Client::Sms('yhmback', $mobile, array('code' => $code));
    }

    /**
     * 发送邮箱
     * @param type $email
     * @param type $nick_name
     * @param type $type
     * @param type $uid
     * @param type $password
     * @return boolean
     */
    private static function sendEmail($email, $nick_name, $type, $uid = 0, $password = '') {
        $params = array(
            'email' => $email,
            'type' => $type,
            'create_time' => time()
        );
        if (!empty($uid)) {
            $params['uid'] = $uid;
        }
        if (!empty($password)) {
            $params['password'] = $password;
        }
        $str = json_encode($params);
        $code = Q_Utils_AuthCode::encode($str, YHMConfig_Email::secret_key);
        $link = YHMConfig_Passport::$emailBackUrl[Q_APPLICATION_ENV][$type] . Q_Utils_Function::base64_url_encode($code);
        $tplParams = array(
            '{nick_name}' => $nick_name,
            '{link}' => $link,
            '{send_time}' => date('Y/m/d H:i')
        );
        try {
            $emailClient = new YHMUtils_Email_Client();
            $ret = $emailClient->sendByTpl($email, $type, $tplParams);
        } catch (Exception $e) {
            self::setLog($e->getMessage());
            return false;
        }
    }

    /**
     * 设置用户登录
     * @param integer $uid
     */
    private static function setLogin($uid) {
        $token = self::getToken($uid);
        $last_login_ip = ip2long(Q_Utils_Request::getClientIp());
        YHMPassport_Models_Logininfo_Client::setLogin($uid, $token, $last_login_ip);
        return $token;
    }

    /**
     * 重新发送手机验证码
     * @param array $params
     * @param string $fields
     */
    public static function resendByMobile(array $params, $fields = '*') {
        $mobile = $params['mobile'];
        if (empty($mobile)) {
            return self::result(412, '手机号不能为空');
        }
        $ret = self::sendMobileCode($mobile);
        if ($ret['status']) {
            return self::result(200, '发送成功');
        } else {
            return self::result(500, $ret['message']);
        }
    }

    public static function resendByEmail(array $params, $fields = '*') {
        $email = $params['email'];
        if (empty($email)) {
            return self::result(412, 'Email不能为空');
        }
        $ret = self::sendEmail($email, '', 'emailReg');
        return self::result(200, '发送成功');
    }

    /**
     * 用户登录
     * @param array $params
     * @param type $fields
     * @return type
     */
    static function signin(array $params, $fields = '*') {
        if (empty($params['profile'])) {
            return self::result(421, '账号不能为空.');
        }
        if (empty($params['password'])) {
            return self::result(422, '密码不能为空.');
        }
        try {
            $authObj = YHMAuth_Factory::profile(self::$channel);
            $uid = $authObj->signin($params['profile'], $params['password']);
            $token = self::setLogin($uid);
            $userInfo = array(
                'uid' => $uid,
                'profile' => $params['profile'],
                'token' => $token
            );
            return self::result(200, '登录成功', $userInfo);
        } catch (Exception $e) {
           //  return self::result(400, '登陆失败');
           return self::result($e->getCode(), $e->getMessage());
        }
    }

    /**
     *
     * @param array $params
     * @param string $fields
     */
    public static function bindMobile(array $params, $fields = '*') {
        $uid = intval($params['uid']);
        $mobile = $params['mobile'];
        if ($uid < 1) {
            return self::result(401, '用户ID不能为空');
        }
        $isMobile = Q_Utils_Validate_Mobile::isValid($mobile);
        if (empty($isMobile)) {
            return self::result(402, '手机格式不正确');
        }
        $exist = YHMPassport_Models_Auth_Client::getProfileByMobile($mobile);
        if (!empty($exist)) {
            return self::result(403, '该手机号已经被用户绑定');
        }
        self::sendMobileCode($mobile);
        return self::result(200, '绑定手机验证码', array('uid' => $uid, 'mobile' => $mobile));
    }

    /**
     * 检查手机绑定输入的验证码
     * @param array $params
     * @param string $fields
     */
    public static function checkBindMobileValidCode(array $params, $fields = '*') {
        $mobile = $params['mobile'];
        $valid_code = $params['valid_code'];
        $uid = intval($params['uid']);
        $password = empty($params['password']) ? '' : $params['password'];
        if ($uid < 1 || empty($valid_code)) {
            return self::result(401, '参数输入不正确');
        }
        $mc = YHMUtils_Mc::getMc();
        $strValidData = $mc->get('valid_code_' . $mobile);
        if (!empty($strValidData)) {
            $validData = json_decode($strValidData, true);
            if ($validData['valid_code'] == $valid_code) {
                //验证码验证通过,添加mobile关联
                $authObj = YHMAuth_Factory::profile(self::$channel);
                try {
                    $ret = $authObj->addAuth($uid, $mobile, YHMConfig_Passport::PROFILE_TYPE_MOBILE, YHMConfig_Passport::$profileSource[self::$channel], $password);
                    if (empty($ret)) {
                        return self::result(402, '添加关联失败');
                    }
                } catch (Exception $e) {
                    return self::result($e->getCode(), $e->getMessage());
                }
                return self::result(200, '绑定成功');
            } else {
                return self::result(406, "验证码错误");
            }
        }
        return self::result(402, '绑定失败');
    }

    public static function bindEmail(array $params, $fields = '*') {
        $uid = $params['uid'];
        $email = $params['email'];
        $profile = YHMPassport_Models_Profile_Client::getByUid($uid);
        if (empty($profile)) {
            return self::result(412, '没有找到该用户');
        }
        if (strlen($profile['password']) < 32 && empty($params['password'])) {
            return self::result(413, '需要输入密码才能绑定');
        }
        $password = empty($params['password']) ? '' : $params['password'];

        $authObj = YHMAuth_Factory::profile(self::$channel);
        try {
            $ret = $authObj->addAuth($uid, $email, YHMConfig_Passport::PROFILE_TYPE_MAIL, self::$channel, $password);
            if (empty($ret)) {
                //发生错误
                return self::result(501, '绑定失败');
            }
        } catch (Exception $e) {
            return self::result($e->getCode(), $e->getMessage());
        }

        self::sendEmail($email, $profile['nick_name'], 'emailBind', $profile['uid'], $password);
        return self::result(200, '绑定成功');
    }

    /**
     * 注册
     * @param array $params
     * @param type $fields
     * @return array
     */
    static function register(array $params, $fields = '*') {
        if (empty($params['profile']))
            return self::result(422, '账号不能为空.');
        if (empty($params['password']))
            return self::result(426, '密码不能为空.');
        $nick_name = $params['nick_name'];
        $isEmail = Q_Utils_Validate_EmailAddress::isValid($params['profile']);
        $uid = 0;
        $token = '';
        $authObj = YHMAuth_Factory::profile(self::$channel);
        if ($isEmail) {
            //邮箱注册
            try {
                $uid = $authObj->register($params['profile'], $params['password'], array('nick_name' => $nick_name)); //创建店铺再hook中
                $token = self::setLogin($uid); //设置登陆
            } catch (Exception $e) {
                return self::result($e->getCode(), $e->getMessage());
            }
            //发送邮件
            self::sendEmail($params['profile'], $nick_name, 'emailReg', $uid);
        } else {
            //手机号
            $isMobile = Q_Utils_Validate_Mobile::isValid($params['profile']);
            if (!$isMobile) {
                return self::result(423, '手机号格式不正确');
            }
            $mobile = $params['profile'];
            $exist = $authObj->isExistAuth($params['profile']);
            if (!empty($exist)) {
                return self::result(421, '该手机号已经注册');
            }
            $existNick = YHMPassport_Models_Profile_Client::getByNickname($nick_name);
            if (!empty($existNick)) {
                return self::result(425, '昵称已存在');
            }
            //记录下该手机号对应的用户信息
            $userInfo = array(
                'mobile' => $params['profile'],
                'nick_name' => $nick_name,
                'password' => $params['password']
            );
            $token = md5('reg_mobile_user_' . $params['profile'] . time());
            $mc = YHMUtils_Mc::getMc();
            $mc->set($token, json_encode($userInfo), 3600); //缓存一小时
            //发送手机验证码
            self::sendMobileCode($mobile);
        }

        return self::result(200, '注册成功', array(
                    'uid' => $uid,
                    'profile' => $params['profile'],
                    'token' => $token
        ));
    }

    /**
     * 验证token
     * @param array $params
     * @return type
     */
    static function validateToken(array $params) {
        if (empty($params['uid']))
            return self::result(400, 'uid不能为空.');
        if (empty($params['token']))
            return self::result(400, 'token不能为空.');
        $loginInfo = YHMPassport_Models_Logininfo_Client::getByUid($params['uid']);
        if (!empty($loginInfo) && $loginInfo['token'] == $params['token']) {
            return true;
        }
        return false;
    }

    /**
     * 获取用户信息
     * @param array $params
     * @fields all | baseinfo
     * @return type
     */
    static function getUserInfo(array $params, $fields = "baseinfo") {
        if (empty($params['uid'])) {
            return self::result(400, 'uid不能为空.');
        }
        if (empty($params['token'])) {
            return self::result(400, 'token不能为空.');
        }
        $result = self::_getUserInfo($params['uid']);
        if (empty($result)) {
            return self::result(400, '暂无用户信息');
        }
        //根据用户类型获取用户手机号,weibo,email等
        $atuhMobile = YHMPassport_Models_Auth_Client::getAuthByUid($params['uid'], YHMConfig_Passport::PROFILE_TYPE_MOBILE);
        $authEmail = YHMPassport_Models_Auth_Client::getAuthByUid($params['uid'], YHMConfig_Passport::PROFILE_TYPE_MAIL);
        $authWeibo = YHMPassport_Models_Auth_Client::getAuthByUid($params['uid'], YHMConfig_Passport::PROFILE_TYPE_PLATFORM_WEIBO);
        //省市区
        $province = YHMPassport_Models_Area_Client::getAreaByCode(substr($result['area_code'], 0, 2));
        $city = YHMPassport_Models_Area_Client::getAreaByCode(substr($result['area_code'], 0, 4));
        $district = YHMPassport_Models_Area_Client::getAreaByCode($result['area_code']);

        $is_password = YHMPassport_Models_Profile_Client::checkPasswordByUid($params['uid']);
        $password = 'Y';
        if (empty($is_password)) {
            $password = 'N';
        }
        if ($fields == 'all') {
            $result['mobile'] = empty($atuhMobile) ? '' : $atuhMobile['profile'];
            $result['email'] = empty($authEmail) ? '' : $authEmail['profile'];
            $result['weibo'] = empty($authWeibo['account_name']) ? '' : $authWeibo['account_name'];
            $result['facebook'] = '';
            $result['has_password'] = $password;
            $result['province'] = empty($province) ? '' : $province;
            $result['city'] = empty($city) ? '' : $city;
            $result['district'] = empty($district) ? '' : $district;
        }
        if (!$result) {
            return self::result(400, "您还没有登录,或登录超时,请重新登录");
        }
        return self::result(200, "用户信息", $result);
    }

    /**
     * 退出,注销
     * @param array $params
     * @param type $fields
     * @return type
     */
    static function logout(array $params, $fields = "*") {
        if (empty($params['uid']) && empty($params['token'])) {
            return self::result(400, '操作失败');
        }
        return self::result(200, '注销成功');
    }

    /**
     * 修改个人信息
     * @param array $params
     * @param type $fields
     * @return type
     */
    public static function setUserInfo(array $params, $fields = '*') {
        if (empty($params['uid'])) {
            return self::result(400, 'uid不能为空');
        }
        $authObj = YHMAuth_Factory::profile(self::$channel);
        $ret = $authObj->updateInfo($params['uid'], $params);
        if (empty($ret)) {
            return self::result(501, '修改失败');
        }
        try {
            $syncStore = new YHMSearch_Syncstore();
            $store_id = YHMStore_Models_Store_Client::getStoreIdByUid($params['uid']);
            $syncParams = array(
                'nick_name' => $params['nickname'],
                'store_id' => $store_id
            );
            $syncStore->sync($syncParams);
        } catch (Exception $e) {
            
        }
        return self::result(200, '修改成功');
    }

    /**
     * 修改用户头像
     * @param array $params
     * @param type $fields
     * @return type
     */
    static function setHeadico(array $params, $fields = '*') {
        if (empty($params['uid'])) {
            return self::result(400, 'uid不能为空');
        }
        if (empty($params['image_path'])) {
            return self::result(400, '头像不能为空');
        }
        $user_info = YHMPassport_Models_Profile_Client::getByUid($uid);
        if($user_info['head_ico']!=$params['image_path']){
            YHMPassport_Models_Profile_Client::updateStatus($uid, 2);
        }
        YHMPassport_Models_Profile_Client::setHeadicoByUid($params['uid'], $params['image_path']);
        return self::result(200, "修改成功");
    }

    /**
     * 修改用户背景
     * @param array $params
     * @param type $fields
     * @return type
     */
    static function setUserBackground(array $params, $fields = '*') {
        if (empty($params['uid'])) {
            return self::result(400, 'uid不能为空');
        }
        if (empty($params['image_path'])) {
            return self::result(400, '背景不能为空');
        }
        YHMPassport_Models_Profile_Client::setBackgroundByUid($params['uid'], $params['image_path']);
        return self::result(200, "修改成功");
    }

    /**
     *
     * @param array $params
     * @param string $fields
     */
    static function findPassword(array $params, $fields = '*') {
        if (empty($params['profile']))
            return self::result(400, '账号不能为空.');
        $find_method = '';
        $authObj = YHMAuth_Factory::profile(self::$channel);
        $isExist = $authObj->isExistAuth($params['profile']);

        if (Q_Utils_Validate_Mobile::isValid($params['profile'])) {
            //手机号
            if (empty($isExist)) {
                $profile = YHMPassport_Models_Auth_Client::getProfileByMobile($params['profile']);
                if (empty($profile)) {
                    return self::result(421, '没有找到该手机号对应的账户');
                }
            }
            self::sendMobileCodeFindPassword($params['profile']);
            $find_method = 'mobile';
        } else {
            if (empty($isExist)) {
                $profile = YHMPassport_Models_Auth_Client::getProfileByEmail($params['profile']);
                if (empty($profile)) {
                    return self::result(421, '没有找到该邮箱号对应的账户');
                }
            }
            //发送邮件
            self::sendEmail($params['profile'], '', 'emailFindpwd');
            $find_method = 'email';
        }
        $message = $find_method == 'mobile' ? '验证码发送成功' : '邮件发送成功';
        return self::result(200, $message, array('find_method' => $find_method));
    }

    /**
     * 验证注册验证码
     * @param array $params
     * @param string $fields
     */
    public static function checkRegisterValidCode(array $params, $fields = '*') {
        if (empty($params['mobile'])) {
            return self::result(400, '手机号不能为空.');
        }
        if (empty($params['valid_code'])) {
            return self::result(400, '验证码不能为空.');
        }
        $mobile = $params['mobile'];
        $valid_code = $params['valid_code'];
        $token = $params['token'];

        $mc = YHMUtils_Mc::getMc();
        $strValidData = $mc->get('valid_code_' . $mobile);
        if (!empty($strValidData)) {
            $validData = json_decode($strValidData, true);
            if ($validData['valid_code'] == $valid_code) {
                //根据token获取用户信息,并完成注册
                $strRegInfo = $mc->get($token);
                if (empty($strRegInfo)) {
                    return self::result(413, '未找到对应的注册信息,请重新输入手机号,昵称,密码');
                }
                $regData = json_decode($strRegInfo, true);
                $authObj = YHMAuth_Factory::profile(self::$channel);
                $uid = $authObj->register($regData['mobile'], $regData['password'], array('nick_name' => $regData['nick_name'])); //创建店铺再hook中
                $token = self::setLogin($uid); //设置登陆

                return self::result(200, '验证成功', array('uid' => $uid, 'token' => $token, 'profile' => $regData['mobile']));
            }
        }
        return self::result(412, '验证失败');
    }

    /**
     * 验证验证码
     * @param array $params
     * @param string $fields
     */
    public static function checkValidCode(array $params, $fields = '*') {
        if (empty($params['mobile'])) {
            return self::result(400, '手机号不能为空.');
        }
        if (empty($params['valid_code'])) {
            return self::result(400, '验证码不能为空.');
        }
        $mobile = $params['mobile'];
        $valid_code = $params['valid_code'];

        $mc = YHMUtils_Mc::getMc();
        $strValidData = $mc->get('valid_code_' . $mobile);
        if (!empty($strValidData)) {
            $validData = json_decode($strValidData, true);
            if ($validData['valid_code'] == $valid_code) {
                //生成一个token,用于修改密码的标识
                $token = md5($mobile . time() . '_findpwd');
                $mc->set($token, $mobile, 600);
                return self::result(200, '验证成功', array('token' => $token));
            }
        }
        return self::result(412, '验证失败');
    }

    /**
     * 重置密码
     * @param array $params
     * @param string $fields
     */
    public static function resetPassword(array $params, $fields = '*') {
        $password = $params['password'];
        $token = $params['token'];
        $mc = YHMUtils_Mc::getMc();
        $mobile = $mc->get($token);
        if (empty($mobile)) {
            return self::result(401, '未找到该手机号的重置信息');
        }
        $authObj = YHMAuth_Factory::profile(self::$channel);
        try {
            $authObj->updatePassword(0, $password, $mobile, YHMConfig_Passport::PROFILE_TYPE_MOBILE);
        } catch (Exception $e) {
            return self::result(415, $e->getMessage());
        }

        return self::result(200, '密码重置成功');
    }

    /**
     *
     * @param array $params
     * @param string $fields
     * @return multitype:Integer String mixed string
     */
    static function modifyPassword(array $params, $fields = '*') {

        if (empty($params['password']))
            return self::result(400, '密码不能为空.');
        if (empty($params['token']))
            return self::result(401, 'token不能为空.');
        if (empty($params['uid']))
            return self::result(402, 'uid不能为空.');
        if (!self::validateToken($params)) {
            return self::result(412, 'token验证失败');
        }
        $authObj = YHMAuth_Factory::profile(self::$channel);
        $authObj->updatePassword($params['uid'], $params['password']);

        return 1 ? self::result(200, '修改密码成功.') : self::result(500, '修改密码失败.');
    }

    static function checkBinding(array $params, $fields = '*') {
        if (empty($params['open_id'])) {
            return self::result(401, 'open_id不能为空.');
        }
        if (empty($params['open_platform'])) {
            return self::result(401, 'open_platform 不能为空.');
        }
        $openID = $params['open_id'];
        $openPlatform = $params['open_platform'];
        if (empty(YHMConfig_Passport::$profileType[$openPlatform])) {
            return self::result(401, '不支持此注册方式.');
        }
        $openPlatform = YHMConfig_Passport::$profileType[$openPlatform];
        $auth = YHMPassport_Models_Auth_Client::getAuthByOpenID($openID, $openPlatform);
        if (empty($auth)) {
            return self::result(400, '没有绑定这个信息.');
        }
        $auth['token'] = self::getToken($auth['uid']);
        return self::result(200, '第三方信息.', $auth);
    }

    static function bind(array $params, $fields = '*') {
        if (empty($params['open_platform'])) {
            return self::result(401, 'open_platform 不能为空.');
        }
        if (empty($params['open_id'])) {
            return self::result(401, 'open_id 不能为空.');
        }
        $openPlatform = $params['open_platform'];
        $clientType = $params['client_type'];
        if (empty(YHMConfig_Passport::$profileSource[$clientType])) {
            return self::result(401, '不支持当前客户端.');
        }
        if (empty(YHMConfig_Passport::$profileType[$openPlatform])) {
            return self::result(401, '不支持此注册方式.');
        }
        $profileType = YHMConfig_Passport::$profileType[$openPlatform];
        $openID = $params['open_id'];

        $profilePackage = array();
        $profilePackage['profile_type'] = $profileType;
        $profilePackage['channel'] = YHMConfig_Passport::$profileSource[$clientType];
        $profilePackage['nick_name'] = empty($params['nick_name']) ? '' : $params['nick_name'];
        $profilePackage['gender'] = empty($params['gender']) ? '' : $params['gender'];
        $profilePackage['head_ico'] = empty($params['head_ico']) ? '' : $params['head_ico'];
        $profilePackage['profile'] = $openID;
        $profilePackage['gender'] = empty($params['gender']) ? 3 : $params['gender'];
        $profilePackage['area_code'] = empty($params['area']) ? 3 : $params['area'];

        $authObj = YHMAuth_Factory::profile(self::$channel);
        $uid = $authObj->associate($openID, $profilePackage);
        $profile = YHMPassport_Models_Profile_Client::getByUid($uid);

        $retData = array(
            'uid' => $uid,
            'nick_name' => empty($profile) ? '' : $profile['nick_name'],
            'token' => self::getToken($uid),
            'profile' => '',
            'is_active' => 'Y'
        );
        return self::result(200, '创建用户成功.', $retData);
    }

    /**
     * 绑定微博
     * @param array $params
     * @param type $fields
     * @return type
     */
    public static function bindWeibo(array $params, $fields = '*') {
        if ($params['uid'] < 1) {
            return self::result(400, 'uid不能为空');
        }
        if ($params['open_id'] < 1) {
            return self::result(400, 'uid不能为空');
        }
        if (empty($params['account_name'])) {
            return self::result(400, '账户名不能为空');
        }
        if (empty($params['open_platform'])) {
            return self::result(400, '绑定类型不能为空');
        }
        if (empty(YHMConfig_Passport::$profileType[$params['open_platform']])) {
            return self::result(401, '不支持此注册方式.');
        }
        $bind_info = YHMPassport_Models_Auth_Client::getAuthByUid($params['uid'], YHMConfig_Passport::PROFILE_TYPE_PLATFORM_WEIBO);
        if (!empty($bind_info)) {
            //暂时不支持解绑,修改绑定的微博账号
            return self::result(403, '您已经绑定过了');
        }
        $bind_check = YHMPassport_Models_Auth_Client::checkBindByOpenID($params['open_id'], YHMConfig_Passport::PROFILE_TYPE_PLATFORM_WEIBO);
        if (!empty($bind_check)) {
            return self::result(410, '对不起,此账号已被绑定');
        }
        $id = YHMPassport_Models_Auth_Client::addAuth($params['uid'], $params['open_id'], $params['account_name'], YHMConfig_Passport::PROFILE_TYPE_PLATFORM_WEIBO, YHMConfig_Passport::$profileSource[self::$channel]);
        if ($id < 1) {
            return self::result(500, '绑定失败');
        }
        return self::result(200, '绑定成功', $params['account_name']);
    }

    public function autoAddUser() {
        exit;
        for ($i = 1; $i <= 100; $i++) {
            $uid = YHMPassport_Models_Profile_Client::addUserProfile(123456, 'masttest' . $i, 'yhfair-user-head.5178d1abf67f897da23fb8f4d2ff3d63.jpg');
            if (empty($uid)) {
                throw new Exception('添加用户失败');
            }
            $auth = YHMPassport_Models_Auth_Client::addAuth($uid, '', YHMConfig_Passport::PROFILE_TYPE_MAIL, '', '');
            //同步
            $store_id = YHMStore_Models_Store_Client::addStore($uid, 3, 1);
            if (empty($store_id)) {
                return self::result(503, '店铺添加失败');
            }
            //同步店铺记录
            try {
                $syncParams = array(
                    'store_id' => $store_id,
                    'nick_name' => 'shoptest' . $i,
                    'store_type' => 3 //店铺类型
                );
                $syncStore = new YHMSearch_Syncstore();
                $syncStore->sync($syncParams);
            } catch (Exception $e) {
                
            }
            //添加商品
            $product_skc = YHMProduct_Models_Goods_Client::setGoods($uid, $store_id, '1', 'X' . $i, '1', '2', '3', '1', '0', $i);
            if (empty($product_skc)) {
                return self::result(501, '添加商品失败');
            }
            //添加SKU
            $sku = YHMProduct_Models_Stock_Client::setStock($product_skc, '-', 1);
            if (empty($sku)) {
                return self::result(508, '添加sku失败');
            }
            //添加图片
            $ret = YHMProduct_Models_Goodsimages_Client::setGoodsImage($product_skc, 'yhfair-product.9e6ea4e249f5d2fe263b56752af951de.jpg', 'Y');
            if (empty($ret)) {
                return self::result(504, '添加商品图片失败');
            }
            // 同步索引
            try {
                $syncParams = array(
                    'uid' => $uid,
                    'product_skc' => $product_skc,
                    'max_category_id' => '1',
                    'store_id' => $store_id,
                    'goods_name' => 'X' . $i,
                    'sale_price' => $i,
                    'brand_id' => '1',
                    'gender' => '1',
                    'quality_level' => '0',
                    'create_time' => time(),
                    'store_type' => 3,
                    'hot_number' => 1,
                    'last_update_time' => time(),
                    'stock_number' => 1,
                    'status' => 1
                );
                $syncProduct = new YHMSearch_Syncgoods ();
                $syncProduct->sync($syncParams);
            } catch (Exception $e) {
                
            }
        }
        return self::result(200, '更新成功');
    }

}