123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Elton
- * Date: 2019/8/7
- * Time: 16:15
- */
- namespace app\clientappapi\controller;
- use app\common\constants\ShortMsgConstants;
- use app\common\controller\ClientApi;
- use app\common\library\Redis;
- use app\common\service\ClientuserService;
- use app\common\service\ShortMessageService;
- use app\main\constants\ClientApiConstants;
- use app\main\constants\MqConstants;
- use app\main\constants\PayConstants;
- use app\main\model\object\DotObject;
- use app\main\service\ClientAppService;
- use app\main\service\LogService;
- use app\main\service\MqService;
- use app\main\service\UserService;
- use think\Config;
- use think\Cookie;
- use think\Exception;
- use think\Request;
- class Userapi extends ClientApi
- {
- /**
- * API: 发送短信验证码
- */
- public function sendshortmsg()
- {
- $phone = trim($this->params['phone']);
- $code = ShortMessageService::instance()->generateSMSCode();
- $redis = Redis::instance();
- $redis->set(ShortMsgConstants::REDISCODEPRE . $phone, $code, 10 * 60);
- $content = "您的验证码为{$code},十分钟内有效,切勿将验证码泄露与他人。有问题请致电客服4001234567";
- try {
- $data = ShortMessageService::instance()->singleShortMsg($content, $phone);
- if ($data['code']) {
- $this->success(ClientApiConstants::MSG_SUCCESS);
- } else {
- $this->error($data['msg']);
- }
- } catch (Exception $e) {
- LogService::error($e->getMessage());
- $this->error('发送验证码失败');
- }
- }
- /**
- * API: 通过手机号注册用户
- * @Method Post
- * @param phone 用户手机号
- * @param code 短信验证码
- * @throws Exception
- */
- public function phoneRegister()
- {
- if($this->request->isPost()){
- $phone = trim($this->params['phone']);
- $code = trim($this->params['code']);
- $redis_code = Redis::instance()->get(ShortMsgConstants::REDISCODEPRE . $phone);
- //后台配置的通用验证码
- if ($code && $code == Config::get('site.app_phone_code')) {
- $redis_code = $code;
- }
- if($code == $redis_code){
- #如果用户已经注册过,直接跳转,如果没有注册过,先进行注册操作再跳转
- $data = ClientuserService::instance()->getUserByPhone($phone);
- if($data){
- LogService::info('[ BindPhone ] 已注册用户,登录成功');
- $userobj = UserService::instance()->getUserModel()->getUserInfo($data->uid);
- $this->_setCookieUser($userobj['id'],$userobj['openid']);
- $userinfo = ClientuserService::instance()->packageUserInfo($userobj);
- $this->result($userinfo,ClientApiConstants::CLIENT_API_CODE_SUCCESS, '登录成功');
- }else{
- $userid = ClientuserService::instance()->createUserByPhone($phone);
- if($userid){
- LogService::info('[ BindPhone ] 新用户,注册并登录成功');
- $userobj = UserService::instance()->getUserModel()->getUserInfo($userid);
- # S: APP注册用户打点
- $dotData = new DotObject();
- $dotData->user_id = $userobj['id'];
- $dotData->channel_id = $userobj['channel_id'];
- $dotData->sex = $userobj['sex'];
- $dotData->business_line = PayConstants::BUSINESS_APP;
- $dotData->action_type = MqConstants::ROUTING_KEY_APP_REGISTER;
- $dotData->type = MqConstants::MSG_TYPE_APP_REGISTER;
- $dotData->event_time = Request::instance()->server('REQUEST_TIME');
- MqService::instance()->sendMessage($dotData);
- # End: APP注册用户打点
- $this->_setCookieUser($userobj['id'],$userobj['openid']);
- $userinfo = ClientuserService::instance()->packageUserInfo($userobj);
- $this->result($userinfo,ClientApiConstants::CLIENT_API_CODE_SUCCESS, '注册登录成功');
- }
- }
- }else{
- $this->error('验证码错误');
- }
- }
- }
- /**
- * API openinstall code 注册
- * @param opencode Code码
- */
- public function codeRegister()
- {
- if($this->request->isPost()){
- $code = trim($this->params['opencode']);
- $obj = ClientAppService::instance()->userRegister($code);
- if($obj->data){
- $data = $obj->toArray();
- # S: APP用户绑定手机号打点
- /*$dotData = new DotObject();
- $userobj = $data['data'];
- $dotData->user_id = $userobj['id'];
- $dotData->channel_id = $userobj['channel_id'];
- $dotData->sex = $userobj['sex'];
- $dotData->business_line = PayConstants::BUSINESS_APP;
- $dotData->action_type = MqConstants:: ROUTING_KEY_APP_BIND_CODE;
- $dotData->type = MqConstants::MSG_TYPE_APP_BIND_CODE;
- $dotData->event_time = Request::instance()->server('REQUEST_TIME');
- MqService::instance()->sendMessage($dotData);*/
- # End: APP用户绑定手机号打点
- $data = ClientuserService::instance()->packageUserInfo($data['data']);
- $this->info($data);
- }else{
- $this->error($obj->msg);
- }
- }
- }
- /**
- * API: 注册用户绑定手机号
- * @return string
- * @throws \think\Exception
- */
- public function bindphone()
- {
- if($this->request->isPost()){
- $uerId = trim($this->params['uid']);
- $phone = trim($this->params['phone']);
- $code = trim($this->params['code']);
- $redis_code = Redis::instance()->get(ShortMsgConstants::REDISCODEPRE . $phone);
- //后台配置的通用验证码
- if ($code && $code == Config::get('site.app_phone_code')) {
- $redis_code = $code;
- }
- if($code == $redis_code){
- // 检查手机号是否已绑定过用户
- $phoneData = ClientuserService::instance()->getUserByPhone($phone);
- if($phoneData){
- $this->error('该手机号已被使用');
- }
- // 绑定手机号
- $userobj = ClientuserService::instance()->bindUserByPhone($uerId, $phone);
- if($userobj){
- # S: APP用户绑定手机号打点
- $dotData = new DotObject();
- $dotData->user_id = $userobj['id'];
- $dotData->channel_id = $userobj['channel_id'];
- $dotData->sex = $userobj['sex'];
- $dotData->business_line = PayConstants::BUSINESS_APP;
- $dotData->action_type = MqConstants:: ROUTING_KEY_APP_BIND;
- $dotData->type = MqConstants::MSG_TYPE_APP_BIND;
- $dotData->event_time = Request::instance()->server('REQUEST_TIME');
- MqService::instance()->sendMessage($dotData);
- # End: APP用户绑定手机号打点
- $userinfo = ClientuserService::instance()->packageUserInfo($userobj);
- $bind_kandian = Config::get('site.phone_bind_credit');
- $userinfo['tips'] = "绑定成功,赠送{$bind_kandian}书币";
- $this->result($userinfo,ClientApiConstants::CLIENT_API_CODE_SUCCESS, '登录成功');
- }
- }else{
- $this->error('验证码错误');
- }
- }
- }
- /**
- * API: 获取用户信息
- */
- public function userInfo()
- {
- if ($this->request->isPost()) {
- $userId = trim($this->params['uid']);
- $data = ClientuserService::instance()->getUserInfoByUid($userId);
- $data = ClientuserService::instance()->packageUserInfo($data);
- $this->result($data, ClientApiConstants::CLIENT_API_CODE_SUCCESS, ClientApiConstants::MSG_SUCCESS);
- }
- }
- /**
- * API: 更改性别
- * @param uid 用户ID
- * @param gender 0=未知,1=男性,2=女性'
- */
- public function changeGender()
- {
- if($this->request->isPost()){
- $uerId = trim($this->params['uid']);
- $gender = trim($this->params['gender']);
- //0=未知,1=男性,2=女性
- if(!in_array($gender, ['0', '1', '2'])){
- $this->error("参数性别有误,仅'0', '1', '2'");
- }
- if(ClientuserService::instance()->modifyGender($uerId, $gender)){
- $this->success(ClientApiConstants::MSG_SUCCESS);
- }
- }
- }
- // 设置Cookies
- private function _setCookieUser($uid, $token)
- {
- Cookie::set('web_ust', ['uid' => $uid, 'token' => $token], 3600 * 24 * 30);
- }
- }
|