Userapi.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Elton
  5. * Date: 2019/8/7
  6. * Time: 16:15
  7. */
  8. namespace app\clientappapi\controller;
  9. use app\common\constants\ShortMsgConstants;
  10. use app\common\controller\ClientApi;
  11. use app\common\library\Redis;
  12. use app\common\service\ClientuserService;
  13. use app\common\service\ShortMessageService;
  14. use app\main\constants\ClientApiConstants;
  15. use app\main\constants\MqConstants;
  16. use app\main\constants\PayConstants;
  17. use app\main\model\object\DotObject;
  18. use app\main\service\ClientAppService;
  19. use app\main\service\LogService;
  20. use app\main\service\MqService;
  21. use app\main\service\UserService;
  22. use think\Config;
  23. use think\Cookie;
  24. use think\Exception;
  25. use think\Request;
  26. class Userapi extends ClientApi
  27. {
  28. /**
  29. * API: 发送短信验证码
  30. */
  31. public function sendshortmsg()
  32. {
  33. $phone = trim($this->params['phone']);
  34. $code = ShortMessageService::instance()->generateSMSCode();
  35. $redis = Redis::instance();
  36. $redis->set(ShortMsgConstants::REDISCODEPRE . $phone, $code, 10 * 60);
  37. $content = "您的验证码为{$code},十分钟内有效,切勿将验证码泄露与他人。有问题请致电客服4001234567";
  38. try {
  39. $data = ShortMessageService::instance()->singleShortMsg($content, $phone);
  40. if ($data['code']) {
  41. $this->success(ClientApiConstants::MSG_SUCCESS);
  42. } else {
  43. $this->error($data['msg']);
  44. }
  45. } catch (Exception $e) {
  46. LogService::error($e->getMessage());
  47. $this->error('发送验证码失败');
  48. }
  49. }
  50. /**
  51. * API: 通过手机号注册用户
  52. * @Method Post
  53. * @param phone 用户手机号
  54. * @param code 短信验证码
  55. * @throws Exception
  56. */
  57. public function phoneRegister()
  58. {
  59. if($this->request->isPost()){
  60. $phone = trim($this->params['phone']);
  61. $code = trim($this->params['code']);
  62. $redis_code = Redis::instance()->get(ShortMsgConstants::REDISCODEPRE . $phone);
  63. //后台配置的通用验证码
  64. if ($code && $code == Config::get('site.app_phone_code')) {
  65. $redis_code = $code;
  66. }
  67. if($code == $redis_code){
  68. #如果用户已经注册过,直接跳转,如果没有注册过,先进行注册操作再跳转
  69. $data = ClientuserService::instance()->getUserByPhone($phone);
  70. if($data){
  71. LogService::info('[ BindPhone ] 已注册用户,登录成功');
  72. $userobj = UserService::instance()->getUserModel()->getUserInfo($data->uid);
  73. $this->_setCookieUser($userobj['id'],$userobj['openid']);
  74. $userinfo = ClientuserService::instance()->packageUserInfo($userobj);
  75. $this->result($userinfo,ClientApiConstants::CLIENT_API_CODE_SUCCESS, '登录成功');
  76. }else{
  77. $userid = ClientuserService::instance()->createUserByPhone($phone);
  78. if($userid){
  79. LogService::info('[ BindPhone ] 新用户,注册并登录成功');
  80. $userobj = UserService::instance()->getUserModel()->getUserInfo($userid);
  81. # S: APP注册用户打点
  82. $dotData = new DotObject();
  83. $dotData->user_id = $userobj['id'];
  84. $dotData->channel_id = $userobj['channel_id'];
  85. $dotData->sex = $userobj['sex'];
  86. $dotData->business_line = PayConstants::BUSINESS_APP;
  87. $dotData->action_type = MqConstants::ROUTING_KEY_APP_REGISTER;
  88. $dotData->type = MqConstants::MSG_TYPE_APP_REGISTER;
  89. $dotData->event_time = Request::instance()->server('REQUEST_TIME');
  90. MqService::instance()->sendMessage($dotData);
  91. # End: APP注册用户打点
  92. $this->_setCookieUser($userobj['id'],$userobj['openid']);
  93. $userinfo = ClientuserService::instance()->packageUserInfo($userobj);
  94. $this->result($userinfo,ClientApiConstants::CLIENT_API_CODE_SUCCESS, '注册登录成功');
  95. }
  96. }
  97. }else{
  98. $this->error('验证码错误');
  99. }
  100. }
  101. }
  102. /**
  103. * API openinstall code 注册
  104. * @param opencode Code码
  105. */
  106. public function codeRegister()
  107. {
  108. if($this->request->isPost()){
  109. $code = trim($this->params['opencode']);
  110. $obj = ClientAppService::instance()->userRegister($code);
  111. if($obj->data){
  112. $data = $obj->toArray();
  113. # S: APP用户绑定手机号打点
  114. /*$dotData = new DotObject();
  115. $userobj = $data['data'];
  116. $dotData->user_id = $userobj['id'];
  117. $dotData->channel_id = $userobj['channel_id'];
  118. $dotData->sex = $userobj['sex'];
  119. $dotData->business_line = PayConstants::BUSINESS_APP;
  120. $dotData->action_type = MqConstants:: ROUTING_KEY_APP_BIND_CODE;
  121. $dotData->type = MqConstants::MSG_TYPE_APP_BIND_CODE;
  122. $dotData->event_time = Request::instance()->server('REQUEST_TIME');
  123. MqService::instance()->sendMessage($dotData);*/
  124. # End: APP用户绑定手机号打点
  125. $data = ClientuserService::instance()->packageUserInfo($data['data']);
  126. $this->info($data);
  127. }else{
  128. $this->error($obj->msg);
  129. }
  130. }
  131. }
  132. /**
  133. * API: 注册用户绑定手机号
  134. * @return string
  135. * @throws \think\Exception
  136. */
  137. public function bindphone()
  138. {
  139. if($this->request->isPost()){
  140. $uerId = trim($this->params['uid']);
  141. $phone = trim($this->params['phone']);
  142. $code = trim($this->params['code']);
  143. $redis_code = Redis::instance()->get(ShortMsgConstants::REDISCODEPRE . $phone);
  144. //后台配置的通用验证码
  145. if ($code && $code == Config::get('site.app_phone_code')) {
  146. $redis_code = $code;
  147. }
  148. if($code == $redis_code){
  149. // 检查手机号是否已绑定过用户
  150. $phoneData = ClientuserService::instance()->getUserByPhone($phone);
  151. if($phoneData){
  152. $this->error('该手机号已被使用');
  153. }
  154. // 绑定手机号
  155. $userobj = ClientuserService::instance()->bindUserByPhone($uerId, $phone);
  156. if($userobj){
  157. # S: APP用户绑定手机号打点
  158. $dotData = new DotObject();
  159. $dotData->user_id = $userobj['id'];
  160. $dotData->channel_id = $userobj['channel_id'];
  161. $dotData->sex = $userobj['sex'];
  162. $dotData->business_line = PayConstants::BUSINESS_APP;
  163. $dotData->action_type = MqConstants:: ROUTING_KEY_APP_BIND;
  164. $dotData->type = MqConstants::MSG_TYPE_APP_BIND;
  165. $dotData->event_time = Request::instance()->server('REQUEST_TIME');
  166. MqService::instance()->sendMessage($dotData);
  167. # End: APP用户绑定手机号打点
  168. $userinfo = ClientuserService::instance()->packageUserInfo($userobj);
  169. $bind_kandian = Config::get('site.phone_bind_credit');
  170. $userinfo['tips'] = "绑定成功,赠送{$bind_kandian}书币";
  171. $this->result($userinfo,ClientApiConstants::CLIENT_API_CODE_SUCCESS, '登录成功');
  172. }
  173. }else{
  174. $this->error('验证码错误');
  175. }
  176. }
  177. }
  178. /**
  179. * API: 获取用户信息
  180. */
  181. public function userInfo()
  182. {
  183. if ($this->request->isPost()) {
  184. $userId = trim($this->params['uid']);
  185. $data = ClientuserService::instance()->getUserInfoByUid($userId);
  186. $data = ClientuserService::instance()->packageUserInfo($data);
  187. $this->result($data, ClientApiConstants::CLIENT_API_CODE_SUCCESS, ClientApiConstants::MSG_SUCCESS);
  188. }
  189. }
  190. /**
  191. * API: 更改性别
  192. * @param uid 用户ID
  193. * @param gender 0=未知,1=男性,2=女性'
  194. */
  195. public function changeGender()
  196. {
  197. if($this->request->isPost()){
  198. $uerId = trim($this->params['uid']);
  199. $gender = trim($this->params['gender']);
  200. //0=未知,1=男性,2=女性
  201. if(!in_array($gender, ['0', '1', '2'])){
  202. $this->error("参数性别有误,仅'0', '1', '2'");
  203. }
  204. if(ClientuserService::instance()->modifyGender($uerId, $gender)){
  205. $this->success(ClientApiConstants::MSG_SUCCESS);
  206. }
  207. }
  208. }
  209. // 设置Cookies
  210. private function _setCookieUser($uid, $token)
  211. {
  212. Cookie::set('web_ust', ['uid' => $uid, 'token' => $token], 3600 * 24 * 30);
  213. }
  214. }