public function actionSafety() { $userBaseInfo = new UserBaseInfo(); $address = new Address(); $userTB = new UserThirdPartyLogin(); $this->layout = 'uc'; if (!$userBaseInfo->islogin()) { return $this->redirect(array('user/login')); } else { // 获取用户密码信息 // $cookie = Yii::$app->getRequest()->cookies; // $cookieId = $cookie[UserBaseInfo::COOKIE_KEY_ID]->value; $session = Yii::$app->session[UserBaseInfo::SESSION_KEY_USER]; $userId = isset($session['id']) && $session['id'] ? $session['id'] : 0; $inforMation = $userBaseInfo->getUserById($userId); $addr = $address->getAddress($userId); $thirds = $userTB->getBindByUser($userId, UserThirdPartyLogin::BIND_WEB_CHANNEL) ? $userTB->getBindByUser($userId, UserThirdPartyLogin::BIND_WEB_CHANNEL) : []; foreach ($thirds as $key => $value) { $thirds[$key]['type'] = strtoupper($thirds[$key]['type']); } $level = 0; $item = 5; if ($thirds) { $level += count($thirds) * 10; $item -= count($thirds); } if ($inforMation['mobile']) { $level += 40; $item -= 1; } if ($inforMation['email']) { $level += 30; $item -= 1; } $inforMation['level'] = $level; $inforMation['item'] = $item; $inforMation['addrNum'] = count($addr); $inforMation['thirds'] = arrayColumn($thirds, 'type'); return $this->render('safety', $inforMation); } }
/** * 添加用户地址信息 */ public function actionAdd() { $util = Yii::$app->util; $request = Yii::$app->getRequest(); $params = Yii::$app->request->post(); //判断用户是否登录 if (!UserBaseInfo::isLogin()) { $util->formatResData(1202, 'please_login', ['redirect' => Url::toRoute('user/login')]); } //判断是否是Ajax请求 if (!$request->getIsAjax($params)) { $util->formatResData(1301, 'illegal_request', ['msg' => '非法请求']); } $params['addressee'] = strip_tags(trim($params['addressee'])); if (!$params['addressee']) { $util->formatResData(1101, "name_not_empty", ['msg' => '姓名不能为空']); } $length = mb_strlen($params['addressee'], 'utf8'); if ($length > 10) { $util->formatResData(1102, 'name_over_specified_length', ['msg' => '名字长度不能超过10']); } $data['addressee'] = $params['addressee']; if (!$params['mobile']) { $util->formatResData(1103, 'mobile_not_empty', ['msg' => '手机号码不能为空']); } $mobile = Yii::$app->util->regularMobile($params['mobile']); if (!$mobile) { $util->formatResData(1104, 'mobile_format_illegal', ['msg' => '手机号码格式非法']); } $data['mobile'] = $params['mobile']; $params['province'] = intval($params['province']); $params['city'] = intval($params['city']); $params['county'] = intval($params['county']); if (!$params['province']) { $util->formatResData(1105, 'province_message_not_exists', ['msg' => '请选择省']); } if (!$params['city']) { $util->formatResData(1106, 'city_message_not_exists', ['msg' => '请选择市']); } if (!$params['county']) { $util->formatResData(1107, 'county_message_not_exists', ['msg' => '请选择区/县']); } $region = District::getById($params['county'], true); if (!$region) { $util->formatResData(1108, 'region_message_not_exists', ['msg' => '区域信息不存在']); } if ($region['level'] != 3) { $util->formatResData(1109, 'county_is_not_exist', ['msg' => '不属于区/县']); } $subRegion = District::getById($region['upid'], true); $regionData = ['provinceName' => $subRegion['parent']['name'], 'cityName' => $subRegion['name'], 'countyName' => $region['name']]; $params = array_merge($params, $regionData); $data['county'] = intval($params['county']); $params['address'] = strip_tags(trim($params['address'])); if (!$params['address']) { $util->formatResData(1110, 'address_not_empty', ['msg' => '地址不能为空']); } $length = mb_strlen($params['address'], 'utf8'); if ($length > 30) { $util->formatResData(1111, 'address_over_specified_length', ['msg' => '地址长度不能超过30']); } $data['address'] = $subRegion['parent']['name'] . $subRegion['name'] . $region['name'] . ' ' . $params['address']; $params['address'] = $subRegion['parent']['name'] . $subRegion['name'] . $region['name'] . ' ' . $params['address']; if (isset($params['post_code'])) { $postCode = YII::$app->util->regularPostCode($params['post_code']); if (!$postCode) { $util->formatResData(1112, 'postcode_illegal', ['msg' => '邮政编码非法']); } $data['post_code'] = $params['post_code']; } //从session或cookie中获取用户id $userId = UserBaseInfo::getUserId(); $data['user_id'] = $userId; $address = new Address(); //获取地址信息 $result = $address->getAddress($userId, Address::STATUS_ENABLE); $maxNum = 20; if (count($result) >= $maxNum) { $util->formatResData(1113, 'number_of_addresses', ['msg' => '最多能添加' . $maxNum . '条地址']); } $result = $address->add($data); $params['id'] = $result; $addressLog = new UserOperateLog(); if ($result) { $data = json_encode($params); $result = $addressLog->store('用户地址添加', '', $data, $result); if (!$result) { FileLogger::getInstance(date('Ymd') . '_address_operate_fail.log')->writeOne($data, Logger::LEVEL_ERROR, 'add_log_fail'); } $util->formatResData(0, 'success', $params); } else { $result = $addressLog->store('用户地址添加', '', $data, $result); if (!$result) { FileLogger::getInstance(date('Ymd') . '_address_operate_fail.log')->writeOne($data, Logger::LEVEL_ERROR, 'add_log_fail'); } $util->formatResData(1201, 'add_fail', ['msg' => '添加失败']); } }