Exemplo n.º 1
0
 /**
  * 新增/更新收货地址
  */
 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 ', '数据库异常');
     }
 }
Exemplo n.º 2
0
 /**
  * 计算好友关系
  */
 public function actionUserRelship()
 {
     $util = Yii::$app->util;
     $params = Yii::$app->getRequest()->post();
     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参数非法');
     }
     if (!isset($params['mobiles'])) {
         return $util->responseArray(1102, 'mobiles_params_missing', '手机号码参数缺失');
     }
     $mobiles = strval($params['mobiles']);
     if (!$mobiles) {
         return $util->responseArray(2102, 'mobiles_params_notempty', '手机号码参数不能为空');
     }
     $mobiles = json_decode($mobiles, true);
     $mobiles = array_unique($mobiles);
     foreach ($mobiles as $k => $mobile) {
         if (!$util->regularMobile($mobile)) {
             unset($mobiles[$k]);
         }
     }
     if (!$mobiles) {
         return $util->responseArray(2103, 'illegal_mobiles_params', '手机号码参数不合法');
     }
     try {
         $ret = null;
         $userModel = new UserBaseInfoGroup();
         $user = $userModel->getById($id, UserBaseInfoGroup::USER_NORMAL_STATUS, true, ['mobile']);
         if (!$user) {
             return $util->responseArray(2100, 'user_forbidden_or_notexists', '用户不存在或者被禁用');
         }
         unset($mobiles[array_search($user['mobile'], $mobiles)]);
         if (!$mobiles) {
             return $util->responseArray(2104, 'mobiles_not_contain_self', '手机号码不允许只包含用户本身');
         }
         $registered = $userModel->registered($mobiles);
         if (!$registered) {
             return $util->responseArray(2105, 'all_mobiles_not_register', '所有手机号均未注册');
         }
         $friendModel = new FriendGroup();
         $friendList = $friendModel->getFriendlist($id);
         if (!$friendList) {
             return $util->responseArray(6101, 'user_no_friends', '当前用户没有添加任何好友');
         }
         $friendIds = json_decode($friendList['friend_list'], true);
         foreach ($registered as $user) {
             if (in_array($user['id'], $friendIds)) {
                 $ret[] = ['tmpPhoneIndex' => $user['mobile'], 'username' => $user['username'], 'is_friend' => 1];
             } else {
                 $ret[] = ['tmpPhoneIndex' => $user['mobile'], 'username' => $user['username'], 'is_friend' => 2];
             }
         }
         return $util->responseArray(0, 'success', '成功', $ret);
     } catch (Exception $ex) {
         return $util->responseArray(5101, 'database_exception', '数据库异常');
     }
 }