public function ihm() { $now = time(); $query = Member::find()->from(self::tableName() . ' member')->where(['and', 'regtime>1439879084', "regtime<{$now}"])->asArray()->select(['member.*', 'mdetail.sex', 'mdetail.company'])->leftJoin(MemberDetail::tableName() . ' mdetail', 'member.userid=mdetail.userid'); // ->groupBy(['member.mobile']); // $query = $query->from(Member::tableName()); $user = new UserBaseInfo(); // $user_openid = new UserOpenid(); $fileLogger = FileLogger::getInstance('iheima.log'); foreach ($query->batch(1000, Yii::$app->iheima) as $rows) { $params = []; $openId = []; if (!arr_null($rows)) { foreach ($rows as $row) { $openIds = openId('iHeiMa'); $params[] = ['username' => $row['username'], 'password' => $row['password'], 'password_salt' => $row['salt'], 'mobile' => $row['mobile'], 'email' => $row['email'], 'gender' => $row['sex'], 'reg_ip' => $row['regip'], 'last_login_time' => $row['lastlogintime'], 'last_login_ip' => $row['lastloginip'], 'create_time' => $row['regtime'], 'update_time' => time(), 'status' => $row['status'], 'open_id' => $openIds]; $openId[] = ['openid' => $openIds, 'userid' => $row['userid'], 'flag' => 'iheima']; } $result = $user->batchInsertUser($params); if (!$result) { $fileLogger->writeOne('同步失败' . json_encode($row['email']), Logger::LEVEL_INFO, 'iHeiMa用户同步失败'); return false; } // $rt = $user_openid->batchInsert($openId); // if (!$rt) { // return false; // } //$fileLogger->writeOne('同步成功' . json_encode($params), Logger::LEVEL_INFO, 'iHeiMa用户同步成功'); } } $fileLogger->writeOne(json_encode($openId), Logger::LEVEL_INFO, 'iHeiMa用户openId与用户id对应关系数据'); return $openId; }
/** * 注册功能 * * @param Array $user * ~ * $user = [ * 'mobile' => '', * 'password' => '', * 'reg_channel' => '', * 'reg_ip' => '' * ] * ~ * * @return array */ public function register($user) { if (isset($user['_format'])) { unset($user['_format']); } $mobile = isset($user['mobile']) ? $user['mobile'] : ''; if (!$mobile) { return responseArray(1101, 'mobile_params_missing', '手机号不能为空'); } if (!\Yii::$app->util->regularMobile($mobile)) { return responseArray(2101, 'mobile_format_error', '手机号码格式有误'); } $userInfo = new UserBaseInfo(); $count = $userInfo->uniqueUser($mobile); if ($count > 0) { return responseArray(1103, 'mobile_been_registered', '手机已被注册'); } $password = isset($user['password']) ? $user['password'] : ''; if (!$password) { return responseArray(1102, 'password_params_missing', '密码不能为空'); } // 正则匹配 密码格式 $preg = "/((?=.*\\d)(?=.*\\D)|(?=.*[a-zA-Z])(?=.*[^a-zA-Z]))^.{8,16}\$/"; $space = "/\\s/"; $numSpace = preg_match($space, $password); $num = preg_match($preg, $password); if (!$num) { return responseArray(2102, 'password_format_wrong', '密码格式有误'); } if ($numSpace) { return responseArray(2103, 'password_not_contain_space', '密码不允许包含空格'); } $reg_channel = isset($user['reg_channel']) ? $user['reg_channel'] : ''; if (!$reg_channel) { return responseArray(1103, 'channel_params_missing', '注册渠道不能为空'); } $reg_ip = isset($user['reg_ip']) ? $user['reg_ip'] : ''; if (!$reg_ip) { return responseArray(1104, 'ip_params_missing', '注册IP不能为空'); } $user['open_id'] = openId(); $userInfo = new UserBaseInfo(); $userId = $userInfo->register($user); if ($userId) { $registerUser = UserBaseInfo::findOne($userId); // TODO:: 通知其他应用系统 $ret = $this->generateToken(); $ret['username'] = $registerUser->username; $ret['open_id'] = $registerUser->open_id; $ret['mobile'] = $registerUser->mobile; $ret['password'] = $registerUser->password; $ret['password_salt'] = $registerUser->password_salt; return responseArray(0, 'success', '注册成功', $ret); } else { return responseArray(1, 'network_anomaly', '网络异常,请稍后重试'); } }
public function actionCreateUser() { // TODO: 内测通过后增加请求的限制 $util = Yii::$app->util; $request = Yii::$app->getRequest(); // AJAX POST请求参数 $params = $request->post(); $params = array_map('htmlEntityString', $params); if (isset($params['busi']) && $params['busi']) { $busi = $params['busi']; } elseif (isset($params['ca']) && $params['ca']) { $busi = $params['ca']; } else { $busi = ''; } $password = isset($params['password']) && $params['password'] ? $params['password'] : ''; // $rePassword = (isset($params['repassword']) && $params['repassword']) ? $params['repassword'] : '';; if (empty($busi)) { $util->formatResData(1104, 'param_busi_miss', ['msg' => '业务标识参数缺失']); } // if (empty($password) || empty($rePassword)) { if (empty($password)) { $util->formatResData(1101, 'param_missing_password', ['msg' => '密码不能为空']); } $regular = "/((?=.*\\d)(?=.*\\D)|(?=.*[a-zA-Z])(?=.*[^a-zA-Z]))^.{8,16}\$/"; $space = '/\\s/'; if (preg_match($space, $password)) { $util->formatResData(1110, 'error_password_space', ['msg' => '密码不能包含空格']); } /* if (empty($rePassword)) { $util->formatResData(1102, 'param_missing_repassword', ['msg' => '密码确认不能为空']); } */ if (!preg_match($regular, $password)) { $util->formatResData(1107, 'error_password_format', ['msg' => '密码格式不正确']); } /* if ($password !== $rePassword) { $util->formatResData(1100, 'error_password_nequal', ['msg' => '两次密码输入不符']); } */ $config = $util->loadConfig('flow'); if (empty($config)) { $util->formatResData(1106, 'error_config_miss', ['msg' => '流程配置缺失']); } $ca = trim($util->routedecrypt($busi)); $result = false; try { $session = Yii::$app->getSession(); $curBusiName = trim($ca); $curBusiStep = $session->get($curBusiName, ''); if (!isset($config[$curBusiName]) && !isset($config[$curBusiName][$curBusiStep])) { $util->formatResData(1001, 'missing_busi_param', ['msg' => '系统繁忙,请稍后重试']); } $stepConfig = $config[$curBusiName][$curBusiStep]; $nextStep = isset($stepConfig['next_step']) ? $stepConfig['next_step'] : null; $relStep = 'step' . ($nextStep - 1); if (!$relStep || $curBusiStep !== $relStep) { $util->formatResData(1002, 'error_curstep_illegal', ['msg' => '系统繁忙,请稍后重试']); } $mobile = $session->get($curBusiName . '_tool', ''); if (empty($mobile) || !$mobile) { // TODO::怎么处理 $util->formatResData(1003, 'missing_mobile_param', ['msg' => '系统繁忙,请稍后重试']); } $register = $session['register_returnurl']; if (empty($register)) { $register = 'http://' . $_SERVER['HTTP_HOST'] . '/user/login'; } $data = ['mobile' => $mobile, 'password' => $password, 'open_id' => openId(), 'reg_channel' => Yii::$app->util->judgTransUrl($register)['app_name']]; $mUser = new UserBaseInfo(); $unique = $mUser->uniqueUser($mobile); if ($unique) { $util->formatResData(1109, 'error_mobile_register', ['msg' => '该手机已经被注册']); } $data['id'] = $result = $mUser->register($data); $session['tpb_user'] = $data; } catch (\Exception $e) { // TODO: 增加日志记录 } if ($result) { $content = false; if (isset($stepConfig['next_step_tpl']) && $stepConfig['next_step_tpl']) { try { $content = $this->renderAjax($stepConfig['next_step_tpl'], ['config' => $stepConfig, 'route' => $ca, 'tool' => $mobile]); } catch (\Exception $e) { // TODO: 对存在模板情况进行处理 } } $param = ['msg' => '注册成功', 'appCookieUrl' => Url::toRoute(['sso/app-cookie'], true)]; if ($content) { $param['content'] = $content; } // 用户信息存入SESSION $session->set(UserBaseInfo::SESSION_KEY_USER, ['id' => $result, 'name' => '', 'mobile' => $mobile, 'avatar' => '', 'email' => '', 'gender' => UserBaseInfo::GENDER_UNKNOWN, 'open_id' => $data['open_id']]); $cookies = Yii::$app->response->cookies; $times = time() + 60 * 60 * 24; $id = ['name' => UserBaseInfo::COOKIE_KEY_ID, 'value' => $result, 'expire' => $times]; $userId = ['name' => UserBaseInfo::COOKIE_KEY_NAME, 'value' => $mobile, 'expire' => $times]; $avatar = ['name' => 'avatar', 'value' => '', 'expire' => $times]; $openId = ['name' => 'open_id', 'value' => $data['open_id'], 'expire' => $times]; $cookie = ['id' => $id, 'userId' => $userId, 'avatar' => $avatar, 'open_id' => $openId]; $util->cookie($cookie, $times); $formatResData = ['code' => 0, 'msg' => 'success', 'data' => $param]; // $util->formatResData(0,'success', ['url' => $returnUrl]); echo json_encode($formatResData); } else { $util->formatResData(1302, 'error_register', ['msg' => '注册失败']); } }
/** * 分页同步数据 * select * from i_member where length(mobile)>11 手机长度超过11位 * @param int $page 数据库分页当前页数 * @param int $totalNum 数据库总记录 */ public function sync($page) { $params = null; $prefixAvatar = 'http://www.idaiyan.cn'; $sexMap = [1 => 1, 2 => 2, 3 => 0, 0 => 0]; $statusMap = [0 => 1, 1 => 0]; if ($page <= 0) { $page = 1; } $page = ($page - 1) * self::$pageSize; $multiMobile = $this->multi(); $multiMobile = implode(',', $multiMobile); $query = self::find()->where(['AND', "mobile not in ({$multiMobile})", "mobile<>''", "is_admin=0", "is_del<>2"])->orderBy('uid')->offset($page)->limit(self::$pageSize)->asArray(); $fileLogger = FileLogger::getInstance('idaiyan.log'); $fileLog = FileLogger::getInstance('idaiyan_dc.log'); $user = new UserBaseInfo(); foreach ($query->batch(self::$batchSize) as $members) { $now = time(); foreach ($members as $member) { // $exists = $user->uniqueUser($member['mobile']); // if ($exists) { // $fileLog->writeOne($member['mobile'] , Logger::LEVEL_INFO, 'i代言语句数据中心用户同步手机重复检测'); // continue; // } if ($member['avatar']) { $avatar = $prefixAvatar . $member['avatar']; } else { $avatar = ''; } $openId = openId('idaiyan'); $params[] = ['username' => $member['nickname'], 'realname' => $member['truename'], 'password' => $member['password'], 'password_salt' => $member['random'], 'mobile' => $member['mobile'], 'email' => $member['email'], 'gender' => $sexMap[$member['sex']], 'avatar' => $avatar, 'birthday' => date('Y-m-d', $member['birthday']), 'reg_ip' => $member['create_ip'], 'reg_channel' => 'idaiyan', 'last_login_time' => $member['update_at'], 'last_login_ip' => '', 'create_time' => $now, 'update_time' => $member['update_at'], 'status' => $statusMap[$member['is_del']], 'open_id' => $openId]; } $result = $user->batchInsertUser($params); if (!$result) { $mobiles = i_array_column($members, 'mobile'); $fileLogger->writeOne('同步失败' . json_encode($mobiles), Logger::LEVEL_INFO, 'iDaiyan用户同步失败'); } unset($params); } }