<?php
/**
  * @class 用户操作类
  * @author soul
  * @copyright 2017/4/13
  */
class userAction extends baseAction
{
	/**
	  * @method 会员注册
	  * @http  post
	  * @param  token              string [选填] 调用接口凭证 (post|get)
	  * @param  u_name             string [选填] 用户名  4-20个(字母、数字、下划线)的组合，必须含有字母
	  * @param  u_mobile           string [必填] 手机号码
	  * @param  mobile_code        string [必填] 手机验证码
	  * @param  u_password         string [必填] 长度为6~20个字符的组合
	  * @param  u_email            string [选填] 邮箱
	  * @param  u_from             string [选填] 来源  YP(宜配网)|YPAPP|YPGZG|YPIM|YPYPC|PJD(跑街的)|PJDAPP|PJDGZH|PJDSGZH(商城店铺公众号)|PJL(跑街令)|CRM|BMTSR|CLIENT(客户网)
	  * @param  lang             string [选填] cn|en
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
			{"status":true,"data": [u_id, u_name,],"code":""}
			{"status":false,"data":"失败原因","code":"错误代码"}
			错误代码:
			name_error:用户名格式不正确
			pass_error:密码长度不正确
			email_error:邮箱格式不正确
			mobile_error:手机不正确
			name_exists:用户名已被注册
			email_exists:email已被注册
	  */
	public function register()
	{
		$filedArr = ['u_name', 'u_mobile', 'mobile_code', 'u_password', 'u_email', 'u_from'];
		$returnFiledArr = ['u_id', 'u_name', 'u_mobile', 'u_email'];
		$dataArr = [];
		foreach($filedArr as $filed)
		{
			if(isset($_POST[$filed]))
				$dataArr[$filed] = $_POST[$filed];
		}
		$YpwUser = new YpwUser();
		$res = $YpwUser->Register($dataArr, $_POST['lang']);
		$res = $YpwUser->Get('yp_user', $returnFiledArr, sprintf(" and u_id=%d", $res['data']), true);
		LibFc::apiReturn($res);
	}


	/**
	  * @method 发送短信验证码
	  * @http  get
	  * @param  token            string [选填] 调用接口凭证 (post|get)
	  * @param  mobile           string [必填] 手机号码
	  * @param  style            string [必填] 类型  
												手机绑定（含注册）:bind
												手机解绑：unbind
												找回密码：password
												手机登录：login
												设置支付密码：paypassword
												绑定提现账号：baccount
												解绑提现账号：ubaccount
	  * @param  from             string [必填] 来源  YP(宜配网)|YPAPP|YPGZG|YPIM|YPYPC|PJD(跑街的)|PJDAPP|PJDGZH|PJDSGZH(商城店铺公众号)|PJL(跑街令)|CRM|BMTSR|CLIENT(客户网)
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
			{"status":true,"data": 发送短信的ID ,"code":""}
			{"status":false,"data":"失败原因","code":"错误代码"}
	  */
	public function sendMobileCode()
	{
		$Params = mvc::$URL_PARAMS;
		$filedArr = ['mobile', 'style', 'from'];
		$dataArr = [];
		foreach($filedArr as $filed)
		{
			if(isset($Params[$filed]))
				$dataArr[$filed] = $Params[$filed];
		}
		$SendSms = new SendSms();
		$res = $SendSms->sendMobileCode($dataArr);
		$res['data'] = $res['data']['smc_id'];
		LibFc::apiReturn($res);
	}

	

	/**
	  * @method 发送短信验证码
	  * @http  get
	  * @param  token            string [选填] 调用接口凭证 (post|get)
	  * @param  mobile           string [必填] 手机号码
	  * @param  style            string [必填] 类型  
												手机绑定（含注册）:bind
												手机解绑：unbind
												找回密码：password
												手机登录：login
												设置支付密码：paypassword
												绑定提现账号：baccount
												解绑提现账号：ubaccount
	  * @param  code             string [必填] 验证码
	  * @param  from             string [必填] 来源  YP(宜配网)|YPAPP|YPGZG|YPIM|YPYPC|PJD(跑街的)|PJDAPP|PJDGZH|PJDSGZH(商城店铺公众号)|PJL(跑街令)|CRM|BMTSR|CLIENT(客户网)
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
			{"status":true,"data": 发送短信的ID ,"code":""}
			{"status":false,"data":"手机验证码不正确","code":"code_error"}
	  */
	public function checkMobileCode()
	{
		$Params = mvc::$URL_PARAMS;
		$filedArr = ['mobile', 'style', 'from', 'code'];
		$dataArr = [];
		foreach($filedArr as $filed)
		{
			if(isset($Params[$filed]))
				$dataArr[$filed] = $Params[$filed];
		}
		$SendSms = new SendSms();
		$res = $SendSms->checkMobileCode($dataArr);
		$res['data'] = $res['data']['smc_id'];
		LibFc::apiReturn($res);
	}

	

	/**
	  * @method 发送短信验证码
	  * @http  post
	  * @param  token            string [选填] 调用接口凭证 (post|get)
	  * @param  type             string [必填] 手机登录：mobile    密码登录：password    微信登录：weixin  token：token
	  * @param  mobile           string [type=mobile必填] 手机号码  
	  * @param  mobile_code      string [type=mobile必填] 手机短信验证码 
	  * @param  username         string [type=password必填] 用户名/邮箱/已验证手机
	  * @param  password         string [type=password必填] 登录密码   长度为6~20个字符的组合
	  * @param  unionid          string [type=weixin必填] 微信唯一标识码
	  * @param  utoken           string [type=token必填] utoken
	  * @param  from             string [必填] 来源  YP(宜配网)|YPAPP|YPGZG|YPIM|YPYPC|PJD(跑街的)|PJDAPP|PJDGZH|PJDSGZH(商城店铺公众号)|PJL(跑街令)|CRM|BMTSR|CLIENT(客户网)
	  * @param  lang             string [选填] cn|en
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
			{"status":true,"data":{"u_id":"149894","u_name":"soul12","u_pay_password":null,"u_nickname":"soul12","u_real_name":"欧昌宇","u_yiparts":"N","u_b2c_worker":"N","u_b2c_shopid":"149894","u_type":"2","u_ul_id":"4","u_email":"472211059@qq.com","u_emil_check":"1","u_mobile":"13434580358","u_mobile_check":"Y","u_weixin_unionid":"ok4Ppv4IBlRwm5pzM45YU6TamVNA","u_weixin_info":{"openid":"obxtFwBqEAQFD8xyJ1QroR7mqk0I","nickname":"蓝宇战车","sex":0,"language":"zh_CN","city":"","province":"","country":"","headimgurl":"http:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/I3mx3eDkoPhsaib0dM62bLRhcHOibGS2LQEbxOJ7E6FN3HgySf3pu8gbIIoicIexn8LaNOmb2bOxslkssYsia5ltqA\/132","privilege":[],"unionid":"ok4Ppv4IBlRwm5pzM45YU6TamVNA"},"u_weixin_bd":"Y","u_gzh_bd":"Y","u_integral":"37781","u_register_time":"1392825600","u_login_count":"3155","u_logo":"Logo\/1009\/100\/ccad6358aba98691.jpg","u_growth":"1160","u_from":null,"ui_period":"2022-04-22 23:59:59","utag":"c291bDEyQDFjNzU3ZjIyODMxYjk4ZmM1M2QzY2E1N2FmNWNjYzZj"},"code":"c291bDEyQDFjNzU3ZjIyODMxYjk4ZmM1M2QzY2E1N2FmNWNjYzZj"}

			{"status":false,"data":"错误代码","code":"code_error"}
			错误代码 
			error:账户名与密码不匹配
			no_email_check:邮箱未验证，请先验证
			token_error: token失效或者不存在
			weixin_no:微信未绑定
	  */
	public function login()
	{
		$dataArr = [];
		switch($_POST['type'])
		{
			case 'mobile': $dataArr = ['mobile'=>$_POST['mobile'], 'code'=>$_POST['mobile_code']]; break;
			case 'password': $dataArr = ['name'=>$_POST['username'], 'password'=>$_POST['password']]; break;
			case 'weixin': $dataArr = ['unionid'=>$_POST['unionid']]; break;
			case 'token': $dataArr = ['token'=>$_POST['utoken']]; break;
		}
		$YpwUser = new YpwUser();
		$res = $YpwUser->Login($_POST['type'], $dataArr, $_POST['lang']);
		unset($res['data']['u_error_ltime']);
		unset($res['data']['u_error_count']);
		$this->buildToken($res['data']['u_id']);
		LibFc::apiReturn($res);
	}

	/**
	  * @method 用户信息
	  * @http  get
	  * @param  token            string [必填] 调用接口凭证 (post|get)
	  * @author soul
	  * @copyright 2017/4/13
	  * @return 
			{"status":true,"data":{"u_id":"149894","u_name":"soul12","u_pay_password":null,"u_nickname":"soul12","u_real_name":"欧昌宇","u_type":"2","u_ul_id":"4","u_email":"472211059@qq.com","u_emil_check":"1","u_mobile":"13434580358","u_mobile_check":"Y","u_weixin_unionid":"ok4Ppv4IBlRwm5pzM45YU6TamVNA","u_weixin_info":{"openid":"obxtFwBqEAQFD8xyJ1QroR7mqk0I","nickname":"蓝宇战车","sex":0,"language":"zh_CN","city":"","province":"","country":"","headimgurl":"http:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/I3mx3eDkoPhsaib0dM62bLRhcHOibGS2LQEbxOJ7E6FN3HgySf3pu8gbIIoicIexn8LaNOmb2bOxslkssYsia5ltqA\/132","privilege":[],"unionid":"ok4Ppv4IBlRwm5pzM45YU6TamVNA"},"u_weixin_bd":"Y","u_gzh_bd":"Y","u_qq_bd":"N","u_integral":"37781","u_register_time":"1392825600","u_login_time":"1538043350","u_logo":"http:\/\/192.168.1.42\/file_center\/center\/read.php?site=yp&c=yp_logo&f=S100\/S.jpg&i=42","u_growth":"1160"},"code":0}

			{"status":false,"data":"错误代码","code":"code_error"}
	  */
	public function myInfo()
	{
		$this->needUser();
		$YpwUser = new YpwUser();
		$res = $YpwUser->Get('yp_user', ['*'], sprintf(" and u_id=%d", $this->UID), true);
		$res = $YpwUser->initUserLogo($res['data'], true);
		unset($res['data']['u_b2c_worker']);
		unset($res['data']['u_password']);
		unset($res['data']['u_yiparts']);
		unset($res['data']['u_b2c_shopid']);
		unset($res['data']['u_login_count']);
		unset($res['data']['u_qq_unionid']);
		unset($res['data']['u_qq_info']);
		unset($res['data']['u_error_count']);
		unset($res['data']['u_error_ltime']);
		unset($res['data']['u_from']);
		$res['data']['u_weixin_info'] = !empty($res['data']['u_weixin_info']) ? json_decode($res['data']['u_weixin_info'], true): [];
		LibFc::apiReturn($res);
	}
}