/**
  * 新增/更新收货地址
  */
 public function actionAddUpdate()
 {
     $util = Yii::$app->util;
     $params = Yii::$app->getRequest()->post();
     $addrId = null;
     // 传递id参数表示更新操作
     if (isset($params['id'])) {
         $addrId = intval($params['id']);
         if ($addrId <= 0) {
             return $util->responseArray(2111, 'illegal_addrid_params', '收货地址ID参数非法');
         }
     }
     $service = new AddressService();
     $ret = $service->check($params);
     if ($ret['code']) {
         return $ret;
     }
     $data = $ret['data'];
     if ($addrId) {
         $data['id'] = $addrId;
     }
     $userId = $data['user_id'];
     try {
         $userModel = new UserBaseInfoGroup();
         $user = $userModel->getById($userId);
         if (!$user) {
             return $util->responseArray(2100, 'user_forbidden_or_notexists', '用户不存在或者被禁用');
         }
         $addressModel = new Address();
         // 新增操作时判断地址的数量
         if ($addrId === null) {
             $num = $addressModel->calculateAddrNum($userId);
             if ($num == Address::ADDR_MAX_NUM) {
                 return $util->responseArray(6101, 'addr_max_num_limit', '用户最大收货地址数量不允许超过' . Address::ADDR_MAX_NUM);
             }
         }
         $county = District::getById($data['county'], false);
         if (!$county) {
             return $util->responseArray(6102, 'county_info_notexists', '所选区/县信息不存在');
         }
         if ($county['level'] != District::AREA_LEVEL_COUNTY) {
             return $util->responseArray(6103, 'not_belong_county', '所选区/县ID不属于区/县');
         }
         $parentCounty = District::getById($county['upid'], true);
         $data['address'] = $parentCounty['parent']['name'] . $parentCounty['name'] . $county['name'] . '  ' . $data['address'];
         $ret = $addrId ? $this->update($data, $addressModel, $util) : $this->add($data, $addressModel, $util);
         return $ret;
     } catch (Exception $ex) {
         return $util->responseArray(5101, 'database_exception ', '数据库异常');
     }
 }
示例#2
0
 /**
  * 获取用户信息
  */
 public function actionGetUser()
 {
     $params = Yii::$app->getRequest()->post();
     $util = Yii::$app->util;
     if (!isset($params['id'])) {
         return $util->responseArray(1101, 'userid_params_missing', '用户ID参数缺失');
     }
     $id = intval($params['id']);
     if ($id <= 0) {
         return $util->responseArray(2101, 'illegal_userid_params', '用户ID参数非法');
     }
     try {
         $userModel = new UserBaseInfoGroup();
         $user = $userModel->getById($id, UserBaseInfoGroup::USER_NORMAL_STATUS, true, ['id', 'avatar', 'username', 'realname', 'city', 'contact_phone', 'email', 'gender', 'personality_sig']);
         if (!$user) {
             return $util->responseArray(2100, 'user_forbidden_or_notexists', '用户不存在或者被禁用');
         }
         $addressModel = new Address();
         $address = $addressModel->getAddrByUserId($id, Address::STATUS_ENABLE, true, ['id', 'mobile', 'address']);
         $county = District::getById($user['city'], false);
         if ($county && $county['level'] == District::AREA_LEVEL_COUNTY) {
             $parentCounty = District::getById($county['upid'], true);
             $user['district'] = $parentCounty['parent']['name'] . ' ' . $parentCounty['name'] . ' ' . $county['name'];
         } else {
             $user['district'] = '';
         }
         $user['address'] = $address;
         unset($user['city']);
         return $util->responseArray(0, 'success', '成功', $user);
     } catch (Exception $ex) {
         return $util->responseArray(5101, 'database_exception', '数据库异常');
     }
 }