严谨的程序案例Api
文档
- 功能
同步推荐关系 - 接口方法
syncRelation - 参数描述
OriginalUsername 查询的用户用户名
RecommandUsername 推荐人用户名 - 返回值
status 1成功 9 失败 - 备注
Status=1 原用户不存在,关系未建立
Status=2 推荐用户不存在,关系未建立
Status=3 原用户存在,并且没有推荐人,推荐用户也存在,成功建立关系
Status=4 原用户存在,而且已经有了推荐人,推荐人是传递的推荐用户,关系不再重建
Status=5 原用户存在,而且已经有了推荐人,推荐人不是传递的推荐用户,关系重新建立,原推荐人是XXX
Status=9 查询失败,操作异常
protected function _initialize() {
parent::_initialize();
$this->outData = array('status'=>0,'msg'=>'','info'=>array());//输出参数
}
// 同步推荐关系
public function syncRelation() {
$memberModel = D('Member');
$memberInviteeModel = D('Member_invitee');
$OriginalUsername = $this->_request('OriginalUsername','trim'); // 被推荐人号码
$RecommandUsername= $this->_request('RecommandUsername','trim'); // 推荐人号码
if ($OriginalUsername == $RecommandUsername) {
$this->outData['status'] = '9';
$this->outData['msg'] = '参数有误';
$this->printOut();
}
$o_member = $memberModel->getMemberByTel($OriginalUsername);
if (empty($o_member)) {
$this->outData['status'] = '1';
$this->outData['msg'] = '原用户'.$OriginalUsername.'不存在,关系未建立';
$this->printOut();
}
$o_wxinfo = $memberModel->getWxinfo($o_member['id']);
$r_member = $memberModel->getMemberByTel($RecommandUsername);
if (empty($r_member)) {
$this->outData['status'] = '2';
$this->outData['msg'] = '推荐用户'.$RecommandUsername.'不存在,关系未建立';
$this->printOut();
}
$r_wxinfo = $memberModel->getWxinfo($r_member['id']);
// 查询原用户是否存在推荐关系
$o_member_invitee = $memberInviteeModel->getInviteeWxuserMember($o_wxinfo['id'],$o_member['id']);
if (empty($o_member_invitee)) { // 不存在推荐人
$r = $memberInviteeModel->setRelationship($r_member['id'],$o_member['id'],$o_wxinfo['id']);
if ($r !== FALSE) {
$this->outData['status'] = '3';
$this->outData['msg'] = '原用户'.$OriginalUsername.'存在,并且没有推荐人,推荐用户'.$RecommandUsername.'也存在,成功建立关系';
$this->printOut();
} else {
$this->outData['status'] = '9';
$this->outData['msg'] = '查询失败,操作异常';
$this->printOut();
}
}
if ($o_member_invitee['member_id'] == $r_member['id']) {
$this->outData['status'] = '4';
$this->outData['msg'] = '原用户'.$OriginalUsername.'存在,而且已经有了推荐人,推荐人'.$RecommandUsername.'是传递的推荐用户,关系不再重建';
$this->printOut();
}
// 获取原推荐人信息
$o_member_inviter = $memberModel->getMemberById($o_member_invitee['member_id']);
// 修改原有推荐关系
$o_member_invitee['member_id'] = $r_member['id'];
$r = $memberInviteeModel->where(array('id'=>$o_member_invitee['id']))->save($o_member_invitee);
if ($r !== FALSE) {
$this->outData['status'] = '5';
$this->outData['msg'] = '原用户'.$OriginalUsername.'存在,而且已经有了推荐人,推荐人不是传递的推荐用户,关系重新建立,新推荐人'.$RecommandUsername.',原推荐人是'.$o_member_inviter['tel'];
$this->printOut();
} else {
$this->outData['status'] = '9';
$this->outData['msg'] = '查询失败,操作异常';
$this->printOut();
}
}
protected function printOut() {
exit(json_encode($this->outData));
}