User.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. namespace app\controller\api;
  3. use app\service\api\UserServiceFacade;
  4. use app\validate\api\user\Login;
  5. use app\model\api\Log;
  6. use app\service\ConfServiceFacade;
  7. use plugin\ali_sms\service\AliSmsServiceFacade;
  8. use laytp\controller\Api;
  9. use laytp\library\Random;
  10. use laytp\library\Token;
  11. use laytp\library\Str;
  12. use think\facade\Db;
  13. /**
  14. * 会员相关
  15. * @ApiWeigh (90)
  16. */
  17. class User extends Api
  18. {
  19. protected function _initialize()
  20. {
  21. $this->model = new \app\model\User();
  22. }
  23. public $noNeedLogin = [
  24. 'login',
  25. 'login_code',
  26. 'register'
  27. ];
  28. // 用户名密码登录
  29. public function login(){
  30. global $_GPC;
  31. //获取表单提交数据
  32. $post = $this->request->post();
  33. //验证表单提交
  34. if(empty($post['account'])){
  35. return $this->error('登录失败,请填写账号');
  36. }
  37. if(empty($post['password'])){
  38. return $this->error('登录失败,请填写密码');
  39. }
  40. $loginUserInfo = $this->model->where(['account' => $post['account'],'uniacid'=>$_GPC['uniacid'] ])->find();
  41. if(!$loginUserInfo){
  42. return $this->error('登录失败,此账号无注册信息,请注册后再登录');
  43. }
  44. $passwordHash = $loginUserInfo->password;
  45. if (!Str::checkPassword($post['password'], $passwordHash)) {
  46. return $this->error('用户名或密码错误');
  47. }
  48. $status = $loginUserInfo->status;
  49. if ($status == 2) {
  50. return $this->error('用户已被禁用,请联系管理员');
  51. }
  52. $token = Random::uuid();
  53. $loginUserInfo['token'] = $token;
  54. Token::set($token, $loginUserInfo['id'], 24 * 60 * 60 * 365);
  55. //
  56. // $post['password'] = '******';//登录成功不记录用户密码
  57. // Log::create([
  58. // 'login_status' => 1,
  59. // 'admin_id' => $loginUserInfo['id'],
  60. // 'request_body' => json_encode($post),
  61. // 'request_header' => json_encode($this->request->header()),
  62. // 'ip' => $this->request->ip(),
  63. // 'create_time' => date('Y-m-d H:i:s'),
  64. // 'uniacid' => $_GPC['uniacid'],
  65. // ]);
  66. return $this->success('登录成功', [
  67. 'user'=>$loginUserInfo
  68. ]);
  69. }
  70. // 验证码登录
  71. public function login_code(){
  72. //获取表单提交数据
  73. $post = $this->request->post();
  74. if(!$post['phone'] || !$post['code']){
  75. return $this->error('注册失败,请上传手机号或验证码!');
  76. }
  77. $check = AliSmsServiceFacade::checkCode($post['phone'],'register',$post['code']);
  78. if(!$check){
  79. return $this->error('验证失败,'.AliSmsServiceFacade::getError());
  80. }
  81. $loginUserInfo = $this->model->where('phone', '=', $post['phone'])->find();
  82. if(!$loginUserInfo){
  83. return $this->error('登录失败,此手机号无注册信息,请注册后再登录');
  84. }
  85. $token = Random::uuid();
  86. $res['token'] = $token;
  87. Token::set($token, $loginUserInfo['id'], 24 * 60 * 60 * 3);
  88. return $this->success('获取结果', $res);
  89. }
  90. // 绑定手机号
  91. public function phone_binding(){
  92. //获取表单提交数据
  93. $post = $this->request->post();
  94. if(!$post['phone'] || !$post['code']){
  95. return $this->error('注册失败,请上传手机号或验证码!');
  96. }
  97. $check = AliSmsServiceFacade::checkCode($post['phone'],'register',$post['code']);
  98. if(!$check){
  99. return $this->error('验证失败,'.AliSmsServiceFacade::getError());
  100. }
  101. $loginUserInfo = $this->model->where('phone', '=', $post['phone'])->find();
  102. // print_r($loginUserInfo);
  103. if($loginUserInfo){
  104. return $this->error('绑定失败,此手机号已有注册信息');
  105. }
  106. // $token = Random::uuid();
  107. // $res['token'] = $token;
  108. // Token::set($token, $loginUserInfo['id'], 24 * 60 * 60 * 3);
  109. // return $this->success('获取结果', $res);
  110. }
  111. // 通过手机验证码修改密码
  112. public function reset()
  113. {
  114. $post = $this->request->post();
  115. if(!$post['phone'] || !$post['code'] || !$post['password']){
  116. return $this->error('注册失败,请上传正确的手机号格式或验证码密码等信息!');
  117. }
  118. $check = AliSmsServiceFacade::checkCode($post['phone'],'register',$post['code']);
  119. if(!$check){
  120. return $this->error('验证失败,'.AliSmsServiceFacade::getError());
  121. }
  122. $loginUserInfo = $this->model->where('phone', '=', $post['phone'])->find();
  123. if(!$loginUserInfo){
  124. return $this->error('修改失败,此手机号无注册信息');
  125. }
  126. $post['password'] = Str::createPassword($post['password']);
  127. $data = array('password' => $post['password']);
  128. $updateRes = $this->model->where('id', '=', $loginUserInfo['id'])->update($data);
  129. if (!$updateRes) throw new \Exception('保存基本信息失败');
  130. return $this->success('获取结果', $loginUserInfo);
  131. }
  132. // 通过手机验证码修改密码
  133. public function profile()
  134. {
  135. $post = $this->request->post();
  136. if(!($post['nickname'] || $post['avatar'])){
  137. return $this->error('修改失败,请上传要修改的头像或昵称等信息');
  138. }
  139. if (!empty($post['password'])){
  140. $post['password'] = Str::createPassword($post['password']);
  141. }
  142. $loginUserInfo = UserServiceFacade::getUserInfo();
  143. $updateRes = $this->model->where('id', '=', $loginUserInfo['id'])->update($post);
  144. if (!$updateRes) throw new \Exception('保存基本信息失败');
  145. if($post['nickname']){
  146. $loginUserInfo['nickname'] = $post['nickname'];
  147. }
  148. if($post['avatar']){
  149. $loginUserInfo['avatar'] = $post['avatar'];
  150. }
  151. return $this->success('获取结果', $loginUserInfo);
  152. }
  153. // 注册即登录
  154. public function register()
  155. {
  156. global $_GPC;
  157. $conf = ConfServiceFacade::groupGet('system.plan', true);
  158. // $validate = new Login();
  159. Db::startTrans();
  160. try {
  161. $post = $this->request->post();
  162. $post['uniacid'] = $_GPC['uniacid'];
  163. if(!$post['account']){
  164. return $this->error('注册失败,请填写账号!');
  165. }
  166. if(!$post['password']){
  167. return $this->error('注册失败,请填写密码!');
  168. }
  169. $post['password'] = Str::createPassword($post['password']);
  170. $loginUserInfo = $this->model->where('account', '=', $post['account'])->value('id');
  171. if($loginUserInfo){
  172. return $this->error('注册失败,此账号已被绑定!');
  173. }
  174. $post['login_time'] = date('Y-m-d H:i:s');
  175. $post['id_number'] = Random::numeric(10);
  176. $post['coin'] = !empty($conf['register'])?$conf['register']:0;
  177. $post['nickname'] = '默认用户';
  178. $saveRes = $this->model->save($post);
  179. if (!$saveRes) throw new \Exception('保存基础信息失败');
  180. Db::commit();
  181. $token = Random::uuid();
  182. $post['token'] = $token;
  183. Token::set($token, $this->model->id, 24 * 60 * 60 * 3);
  184. return $this->success('操作成功',$post);
  185. } catch (\Exception $e) {
  186. Db::rollback();
  187. return $this->error('数据库异常,操作失败');
  188. }
  189. }
  190. /*@formatter:off*/
  191. /**
  192. * @ApiTitle (根据token获取用户信息)
  193. * @ApiSummary (根据token获取用户信息)
  194. * @ApiMethod (GET)
  195. * @ApiHeaders (name="token", type="string", required="true", description="用户登录后得到的Token")
  196. * @ApiReturnParams (name="code", type="integer", description="接口返回码.0=常规正确码,表示常规操作成功;1=常规错误码,客户端仅需提示msg;其他返回码与具体业务相关。框架实现了的唯一其他返回码:10401,前端需要跳转至登录界面。在一个复杂的交互过程中,你可能需要自行定义其他返回码")
  197. * @ApiReturnParams (name="msg", type="string", description="返回描述")
  198. * @ApiReturnParams (name="time", type="integer", description="请求时间,Unix时间戳,单位秒")
  199. * @ApiReturnParams (name="data.id", type="integer", description="用户主键ID")
  200. * @ApiReturnParams (name="data.phone", type="string", description="手机号")
  201. * @ApiReturnParams (name="data.username", type="string", description="用户名")
  202. * @ApiReturnParams (name="data.nickname", type="string", description="昵称")
  203. * @ApiReturnParams (name="data.avatar", type="string", description="头像")
  204. * @ApiReturnParams (name="data.token", type="string", description="用户登录凭证,Token")
  205. * @ApiReturn
  206. ({
  207. "code": 0,
  208. "msg": "获取成功",
  209. "time": 1591149171,
  210. "data": {
  211. "id": 4,
  212. "mobile": "17603005414",
  213. "username": "",
  214. "nickname": "",
  215. "avatar": "http://local.laytp.com/static/index/image/default.png",
  216. "token": "d32e5210-050d-4902-b4b2-0173da12e191"
  217. }
  218. })
  219. */
  220. /*@formatter:on*/
  221. public function info()
  222. {
  223. global $_GPC;
  224. $loginUserInfo = UserServiceFacade::getUserInfo();
  225. $modelVipinfo =new \app\model\Vipinfo();
  226. if($loginUserInfo['vip_time'] > time() || $loginUserInfo['vip_time']==9999){
  227. $vipinfo = $modelVipinfo->where('uid',$loginUserInfo['id'])->find();
  228. if(empty($vipinfo)){
  229. $save =[
  230. 'uid'=>$loginUserInfo['id'],
  231. 'gpt35_times'=>-1,
  232. 'uniacid'=>$_GPC['uniacid']
  233. ];
  234. $res = $modelVipinfo->save($save);
  235. }
  236. }
  237. return $this->success('获取成功', $loginUserInfo);
  238. }
  239. // 用户数据
  240. public function userData()
  241. {
  242. global $_GPC;
  243. $modelVipinfo =new \app\model\Vipinfo();
  244. $today = strtotime(date("Y-m-d"),time());
  245. $loginUserInfo = UserServiceFacade::getUserInfo();
  246. $data['coin'] = $loginUserInfo['coin'];
  247. $data['vip_time'] = 0;
  248. $data['countdown'] = 0; //倒计时多少天
  249. $data['is_validity'] = 0;//有效期
  250. $data['vip_info'] = '';
  251. if(!empty($loginUserInfo['vip_time'])){
  252. if($loginUserInfo['vip_time'] === 9999){
  253. $data['is_validity'] = 1;
  254. $data['countdown'] = 9999;
  255. }else{
  256. if($loginUserInfo['vip_time'] < time()){
  257. $data['is_validity'] = 0;
  258. }else{
  259. $data['is_validity'] = 1;
  260. $data['countdown'] = round(($loginUserInfo['vip_time']-time())/86400);
  261. }
  262. }
  263. $vipinfo = $modelVipinfo->where('uid',$loginUserInfo['id'])->find();
  264. if(!empty($vipinfo)){
  265. $data['vip_info'] = $vipinfo;
  266. if($vipinfo['gpt35_type'] == 'time'){
  267. // 每天
  268. $data['vip_info']['gpt35_used'] = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['type','=','gpt35'],['is_time','=','1'],['create_time','>',date('Y-m-d 0:0:0')],['uniacid','=',$_GPC['uniacid']] ])->count();
  269. }else{
  270. // 总数
  271. $data['vip_info']['gpt35_used'] = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['type','=','gpt35'],['is_time','=','1'],['create_time','>',$vipinfo['create_time']],['uniacid','=',$_GPC['uniacid']] ])->count();
  272. }
  273. if($vipinfo['gpt4_type'] == 'time'){
  274. // 每天
  275. $data['vip_info']['gpt4_used'] = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['type','=','gpt4'],['is_time','=','1'],['create_time','>',date('Y-m-d 0:0:0')],['uniacid','=',$_GPC['uniacid']] ])->count();
  276. }else{
  277. // 总数
  278. $data['vip_info']['gpt4_used'] = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['type','=','gpt4'],['is_time','=','1'],['create_time','>',$vipinfo['create_time']],['uniacid','=',$_GPC['uniacid']] ])->count();
  279. }
  280. if($vipinfo['sd_type'] == 'time'){
  281. // 每天
  282. $data['vip_info']['sd_used'] = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['is_time','=','1'],['type','=','sd'],['create_time','>',date('Y-m-d 0:0:0')],['uniacid','=',$_GPC['uniacid']] ])->count();
  283. }else{
  284. // 总数
  285. $data['vip_info']['sd_used'] = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['type','=','sd'],['is_time','=','1'],['create_time','>',$vipinfo['create_time']],['uniacid','=',$_GPC['uniacid']] ])->count();
  286. }
  287. if($vipinfo['mj_type'] == 'time'){
  288. // 每天
  289. $data['vip_info']['mj_used'] = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['is_time','=','1'],['type','=','mj'],['create_time','>',date('Y-m-d 0:0:0')],['uniacid','=',$_GPC['uniacid']] ])->count();
  290. }else{
  291. // 总数
  292. $data['vip_info']['mj_used'] = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['type','=','mj'],['is_time','=','1'],['create_time','>',$vipinfo['create_time']],['uniacid','=',$_GPC['uniacid']] ])->count();
  293. }
  294. }
  295. }
  296. $data['vip_time'] = !empty($loginUserInfo['vip_time'])?$loginUserInfo['vip_time']:0;
  297. $data['share_count_today'] = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['type','=','share'],['create_time','>',date('Y-m-d 0:0:0')],['uniacid','=',$_GPC['uniacid']] ])->count();
  298. $data['video_count_today'] = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['type','=','video'],['create_time','>',date('Y-m-d 0:0:0')],['uniacid','=',$_GPC['uniacid']] ])->count();
  299. $data['sign_count_today'] = \app\model\Bill::where([['uid','=',$loginUserInfo['id']],['type','=','sign'],['create_time','>',date('Y-m-d 0:0:0')],['uniacid','=',$_GPC['uniacid']] ])->count();
  300. return $this->success('获取成功', $data);
  301. }
  302. /*@formatter:off*/
  303. /**
  304. * @ApiTitle (注销登录)
  305. * @ApiSummary (注销登录信息)
  306. * @ApiMethod (GET)
  307. * @ApiRoute (/api.user/logout)
  308. * @ApiHeaders (name="token", type="string", required="true", description="用户登录后得到的Token")
  309. * @ApiReturnParams (name="code", type="integer", description="接口返回码.0=常规正确码,表示常规操作成功;1=常规错误码,客户端仅需提示msg;其他返回码与具体业务相关。框架实现了的唯一其他返回码:10401,前端需要跳转至登录界面。在一个复杂的交互过程中,你可能需要自行定义其他返回码")
  310. * @ApiReturnParams (name="msg", type="string", description="返回描述")
  311. * @ApiReturnParams (name="time", type="integer", description="请求时间,Unix时间戳,单位秒")
  312. * @ApiReturnParams (name="data", type="null", description="只会返回null")
  313. * @ApiReturn
  314. ({
  315. "code": 0,
  316. "msg": "注销成功",
  317. "time": 1584513627,
  318. "data": null
  319. })
  320. */
  321. /*@formatter:on*/
  322. public function logout()
  323. {
  324. if (UserServiceFacade::logout()) {
  325. return $this->success('注销成功');
  326. } else {
  327. return $this->error(UserServiceFacade::getError());
  328. }
  329. }
  330. }