UserCenter.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\UserLevelRank;
  4. use app\common\model\UserMessage;
  5. use app\common\model\UserVitality;
  6. use app\common\validate\UserVali;
  7. use think\Db;
  8. use app\common\model\User;
  9. /**
  10. * @title 会员个人中心
  11. * @controller UserCenter
  12. * @group base
  13. */
  14. class UserCenter extends Base
  15. {
  16. public function initialize()
  17. {
  18. parent::initialize();
  19. parent::checkLogin();
  20. }
  21. /**
  22. * @title 获取个人信息
  23. * @desc 个人信息
  24. * @author qc
  25. * @url /api/User_center/getUserInfo
  26. * @method GET
  27. * @tag 个人信息
  28. * @header name:Authorization require:1 desc:Token
  29. * @return name:phone type:int default:-- desc:手机号
  30. * @return name:name type:string default:-- desc:昵称
  31. * @return name:headimg type:string default:-- desc:头像地址
  32. * @return name:sex type:int default:1 desc:性别(1男2女)
  33. * @return name:true_name type:string default:-- desc:真实姓名
  34. * @return name:invite_code type:string default:-- desc:邀请码
  35. * @return name:account_type type:int default:-- desc:账号类型1公司,2个人
  36. * @return name:hx_username type:string default:-- desc:环信账号用户名
  37. * @return name:hx_uuid type:string default:-- desc:环信账号uuid
  38. * @return name:bind_phone type:string default:-- desc:绑定的手机号
  39. * @return name:bind_email type:string default:-- desc:绑定的邮箱
  40. * @return name:hx_password type:string default:-- desc:环信密码
  41. * @return name:is_hx type:string default:-- desc:是否是活动门票核销员【1是0否】
  42. * @return name:bind_id type:string default:-- desc:被绑定会员id
  43. * @return name:is_first type:int default:-- desc:企业组是否是第一个注册(1是0否【第一个注册有会员会员重置权限,公司其他人会员权限读取公司第一个注册的会员权限】)
  44. * @return name:user_level type:array default:-- desc:开通会员信息[为空就不是vip]
  45. * @return name:user_level.level_id type:int default:-- desc:等级id
  46. * @return name:user_level.end_date type:string default:-- desc:过期时间
  47. * @return name:user_level.name type:string default:-- desc:会员等级名称
  48. * @return name:user_level.logo type:string default:-- desc:会员等级logo
  49. * @return name:user_level.icons type:string default:-- desc:等级标签
  50. *
  51. */
  52. public function getUserInfo()
  53. {
  54. $field = 'id,phone,name,level_id,email,headimg,openid,true_name,sex,invite_code,account_type,group_id,is_first,group_first,hx_username,hx_password,hx_uuid,bind_id,is_hx';
  55. $user_info = User::field($field)->where('id',$this->user_id)->find()->toArray();
  56. $user_level = UserLevelRank::getUserVipInfo($this->user_id);
  57. $user_info['bind_phone']= $user_info['phone'];
  58. $user_info['bind_email']= $user_info['email'];
  59. if($user_info['bind_id']) $user_info['bind_phone'] = User::where('id',$user_info['bind_id'])->value('phone');
  60. $bind_email = User::where('bind_id',$user_info['id'])->value('email');
  61. if($bind_email) $user_info['bind_email'] = $bind_email;
  62. $user_info['level_id'] = !empty($user_level) ? $user_level['level_id'] : 0;
  63. $this->success('获取成功',['detail'=>$user_info,'user_level'=>$user_level]);
  64. }
  65. /**
  66. * @title 编辑个人信息
  67. * @desc 编辑个人信息
  68. * @author qc
  69. * @url /api/User_center/updateUserInfo
  70. * @method POST
  71. * @tag 编辑信息
  72. * @header name:Authorization require:1 desc:Token
  73. * @param name:name type:string default:-- desc:昵称
  74. * @param name:headimg type:string default:-- desc:头像地址
  75. */
  76. public function updateUserInfo()
  77. {
  78. $headimg = input('post.headimg');
  79. $name = trim(input('post.name',''));
  80. $update_data= [];
  81. if($name)$update_data['name'] = $name;
  82. if($headimg)$update_data['headimg'] = $headimg;
  83. if(empty($update_data)) $this->error('修改会员信息有误');
  84. Db::name('store_member')->where('id',$this->user_id)->update($update_data);
  85. $field = 'phone,name,headimg,email';
  86. $user_info = Db::name('store_member')->field($field)->where('id',$this->user_id)->find();
  87. $user_level = UserLevelRank::getUserVipInfo($this->user_id);
  88. $user_info['level_id'] = !empty($user_level) ? $user_level['level_id'] : 0;
  89. $this->success('编辑成功',['detail'=>$user_info]);
  90. }
  91. /**
  92. * @title 更换或设置登录密码
  93. * @desc 更换或设置登录密码
  94. * @author qc
  95. * @url /api/User_center/modifyPassword
  96. * @method POST
  97. * @header name:Authorization require:1 desc:Token
  98. * @param name:phone type:int require:1 default:-- desc:手机号
  99. * @param name:code type:int require:1 default:-- desc:手机号验证码
  100. * @param name:password type:string default:-- desc:密码
  101. */
  102. public function modifyPassword()
  103. {
  104. $uid = $this->user_id;
  105. $phone = input('post.phone');
  106. $code = input('post.code');
  107. $password = input('post.password');
  108. if(empty($phone) || empty($code) || empty($password) ) $this->error('参数错误');
  109. $field = 'id,phone';
  110. $user_info = User::field($field)->where('id',$this->user_id)->find()->toArray();
  111. if(!$user_info['phone']) $this->error('请先绑定手机号');
  112. if($user_info['phone'] != $phone) $this->error('与绑定手机号不一致');
  113. $check_code = $this->checkPhoneCode($phone,$code);
  114. if(!$check_code) $this->error('验证码错误');
  115. $validate = new UserVali();
  116. if (!$validate->scene('reset_password')->check(['phone'=>$phone,'password'=>$password])) {
  117. $this->error($validate->getError());
  118. }
  119. $encryption_password = encrypt_password($password);
  120. User::where('id',$uid)->update(['password'=>$password,'encryption_password'=>$encryption_password]);
  121. $this->updatePhoneCode($check_code);
  122. $this->success('更换成功');
  123. }
  124. /**
  125. * @title 完善用户openid
  126. * @desc 完善用户openid
  127. * @author qc
  128. * @method POST
  129. * @tag 编辑信息
  130. * @url /api/User_center/saveOpenid
  131. * @header name:Authorization require:1 desc:Token
  132. * @param name:code type:string require:1 default:-- desc:code
  133. */
  134. public function saveOpenid()
  135. {
  136. $code = input('post.code');
  137. if (empty($code)) $this->error('参数错误');
  138. $res = http_get('https://api.weixin.qq.com/sns/oauth2/access_token?appid='.config('app.official_account')['appid'].'&secret='.config('app.official_account')['secret'].'&code='.$code.'&grant_type=authorization_code');
  139. $res = json_decode($res,true);
  140. if (empty($res['openid'])) $this->error('未获取到openid');
  141. $member_data = ['openid' => $res['openid']];
  142. Db::name('store_member')->where('id',$this->user_id)->update($member_data);
  143. $this->success('已绑定openid');
  144. }
  145. /**
  146. * @title 获取消息未读量
  147. * @desc 获取消息未读量
  148. * @author qc
  149. * @method GET
  150. * @url /api/User_center/getUnreadNum
  151. * @header name:Authorization require:1 desc:Token
  152. * @param name:num type:int default:0 desc:未读数量
  153. */
  154. public function getUnreadNum()
  155. {
  156. $num = UserMessage::where(['user_id'=>$this->user_id,'is_deleted'=>0])->where('is_read',0)->count();
  157. $this->success('ok',['num'=>$num]);
  158. }
  159. /**
  160. * @title 消息列表【会员消息】
  161. * @desc 我的点赞
  162. * @author qc
  163. * @method GET
  164. * @url /api/User_center/getMessageList
  165. * @header name:Authorization require:1 desc:Token
  166. * @param name:title type:string default:-- desc:标题
  167. * @param name:page type:int default:-- desc:页数
  168. * @param name:page_num type:int default:-- desc:每页数
  169. *
  170. * @return name:id type:int default:-- desc:记录id
  171. * @return name:title type:string default:-- desc:标题
  172. * @return name:content type:string default:-- desc:内容
  173. * @return name:create_at type:string default:-- desc:时间
  174. * @return name:is_read type:int default:-- desc:是否已读【0否1是】
  175. */
  176. public function getMessageList()
  177. {
  178. $list = UserMessage::where(['user_id'=>$this->user_id,'is_deleted'=>0])
  179. ->limit($this->off_set,$this->page_num)
  180. ->order(['id'=>'desc'])
  181. ->select()->toArray();
  182. //if(!empty($list)) UserMessage::where('id','in',array_column($list,'id'))->update(['is_read'=>1]);
  183. $this->success('ok',['list'=>$list]);
  184. }
  185. /**
  186. * @title 消息详情【会员消息】
  187. * @desc 消息详情
  188. * @author qc
  189. * @method GET
  190. * @url /api/User_center/getMessageInfo
  191. * @header name:Authorization require:1 desc:Token
  192. * @param name:id type:int default:-- desc:消息记录id
  193. * @return name:title type:string default:-- desc:标题
  194. * @return name:content type:string default:-- desc:内容
  195. * @return name:create_at type:string default:-- desc:时间
  196. * @return name:is_read type:int default:-- desc:是否已读【0否1是】
  197. */
  198. public function getMessageInfo()
  199. {
  200. $detail= UserMessage::where(['user_id'=>$this->user_id,'id'=>input('get.id')])->find()->toArray();
  201. UserMessage::where('id',input('get.id'))->update(['is_read'=>1]);
  202. $this->success('ok',['detail'=>$detail]);
  203. }
  204. /**
  205. * @title 用户注销
  206. * @desc 用户注销
  207. * @author qc
  208. * @method GET
  209. * @url /api/User_center/delUser
  210. * @header name:Authorization require:1 desc:Token
  211. */
  212. public function delUser()
  213. {
  214. User::where('id',$this->user_id)->update(['is_deleted' => '1','phone'=>'','email'=>'']);
  215. User::where('bind_id',$this->user_id)->update(['bind_id' => '0']);
  216. $this->success('注销成功');
  217. }
  218. /**
  219. * @title 以下接口为二期需求
  220. * @desc 以下接口为二期需求
  221. * @author qc
  222. * @url /api/User_center/secondPhase
  223. * @method
  224. * @return name:1 type:string default:-- desc:以下接口为二期需求
  225. */
  226. public function secondPhase(){}
  227. /**
  228. * @title 获取会员活跃度
  229. * @desc 获取会员活跃度
  230. * @author qc
  231. * @url /api/User_center/getUserVitality
  232. * @method GET
  233. * @param name:date_str type:int default:-- desc:时间查询【按年:2023,按月:2023-02,按天:2023-02-22】
  234. * @return name:type type:int default:-- desc:1:安卓手机,2:ios手机设备,3:安卓ipad,4:iosipad,5:h5,pc电脑
  235. * @return name:num type:int default:-- desc:1:活跃度(登录天数)
  236. */
  237. public function getUserVitality()
  238. {
  239. $date_str = input('get.date_str');
  240. $date_arr = explode('-',$date_str);
  241. $where = [];
  242. $where['user_id'] = $this->user_id;
  243. if(isset($date_arr[0])) $where['year'] =$date_arr[0];
  244. if(isset($date_arr[1])) $where['month'] =$date_arr[1];
  245. if(isset($date_arr[2])) $where['day'] =$date_arr[2];
  246. $list = UserVitality::field('type,count(id) num')->where($where)->group('type')->select()->toArray();
  247. $this->success('ok',['list'=>$list]);
  248. }
  249. /**
  250. * @title 获取会员活跃度日志
  251. * @desc 获取会员活跃度日志
  252. * @author qc
  253. * @url /api/User_center/getUserVitalityLog
  254. * @method GET
  255. * @param name:page type:int default:0 desc:页数
  256. * @param name:page_num type:int default:20 desc:每页数
  257. * @param name:date_str type:int default:-- desc:时间查询【按年:2023,按月:2023-02,按天:2023-02-22】
  258. * @param name:type type:int default:-- desc:1:安卓手机,2:ios手机设备,3:安卓ipad,4:iosipad,5:h5,pc电脑
  259. * @return name:type type:int default:-- desc:1:安卓手机,2:ios手机设备,3:安卓ipad,4:iosipad,5:h5,pc电脑
  260. * @return name:year type:string default:-- desc:年
  261. * @return name:month type:string default:-- desc:月
  262. * @return name:day type:string default:-- desc:日
  263. * @return name:day_time type:string default:-- desc:日期
  264. * @return name:create_at type:string default:-- desc:具体时间
  265. */
  266. public function getUserVitalityLog()
  267. {
  268. $date_str = input('get.date_str');
  269. $date_arr = explode('-',$date_str);
  270. $where = [];
  271. $where['user_id'] = $this->user_id;
  272. $where['type'] = input('get.type');
  273. if(isset($date_arr[0])) $where['year'] =$date_arr[0];
  274. if(isset($date_arr[1])) $where['month'] =$date_arr[1];
  275. if(isset($date_arr[2])) $where['day'] =$date_arr[2];
  276. $list = UserVitality::where($where)->order('id desc')->select()->toArray();
  277. $this->success('ok',['list'=>$list]);
  278. }
  279. /**
  280. * @title 绑定手机号
  281. * @desc 绑定手机号
  282. * @author qc
  283. * @url /api/User_center/modifyPhone
  284. * @method POST
  285. * @header name:Authorization require:1 desc:Token
  286. * @param name:phone type:int require:1 default:-- desc:手机号
  287. * @param name:code type:int require:1 default:-- desc:手机号验证码
  288. * @param name:phone_pre type:int require:0 default:-- desc:手机号前缀
  289. * @return name:phone type:int default:-- desc:手机号
  290. * @return name:name type:string default:-- desc:昵称
  291. * @return name:headimg type:string default:-- desc:头像地址
  292. */
  293. public function modifyPhone()
  294. {
  295. $phone = input('post.phone');
  296. $code = input('post.code');
  297. if(empty($phone) || empty($code) ) $this->error('参数错误');
  298. $field = 'id,phone,name,headimg,bind_id,account_type';
  299. $user_info = Db::name('store_member')->field($field)->where('id',$this->user_id)->find();
  300. if($user_info['bind_id']) $this->error('已绑定其他会员');
  301. if($user_info['phone']) $this->error('已绑定手机号');
  302. if($user_info['account_type'] == 2) $this->error('只有企业用户可以更换手机号');
  303. //if($user_info['phone'] == $phone)$this->error('更换手机号与绑定手机号一致!');
  304. $check_code = $this->checkPhoneCode($phone,$code,input('post.phone_pre'));
  305. //if(!$check_code) $this->error('验证码错误');
  306. //判断手机号是否已注册
  307. $sel_where = [];
  308. $sel_where[] = ['phone','=',$phone];
  309. $sel_where[] = ['is_deleted','=',0];
  310. if(input('post.phone_pre')) $sel_where[] = ['phone_pre','=',input('post.phone_pre')];
  311. $member_id = Db::name('store_member')->where($sel_where)->value('id');
  312. if($member_id) {
  313. $check_bind = User::where(['bind_id'=>$member_id])->value('id');
  314. // var_dump($check_bind,$this->user_id,$this->account_id);
  315. if($check_bind) $this->error('该用户已绑定');
  316. }else{
  317. $reg_data = [];
  318. $reg_data['phone'] = $phone;
  319. $reg_data['account_type'] = 2;
  320. $reg_data['name'] = 'G'. substr_replace($phone,'****',3,4);
  321. $user_info = User::create($reg_data);
  322. $member_id = $user_info->id;
  323. }
  324. Db::name('store_member')->where('id',$this->account_id)->update(['bind_id'=>$member_id]);
  325. $this->updatePhoneCode($check_code);
  326. $this->success('绑定成功');
  327. }
  328. /**
  329. * @title 取消绑定
  330. * @desc 取消绑定
  331. * @author qc
  332. * @url /api/User_center/cancelBindPhone
  333. * @method POST
  334. * @header name:Authorization require:1 desc:Token
  335. */
  336. public function cancelBindPhone()
  337. {
  338. Db::name('store_member')->where('id',$this->account_id)->update(['bind_id'=>0]);
  339. $this->success('解绑成功');
  340. }
  341. /**
  342. * @title 添加用户学习记录
  343. * @desc 添加用户学习记录
  344. * @author qc
  345. * @url /api/User_center/study
  346. * @method POST
  347. * @param name:study_duration type:int require:1 default:-- desc:学习时长(单位:分钟)
  348. * @param name:video_intro_id type:int require:1 default:-- desc:视频系列id
  349. * @header name:Authorization require:1 desc:Token
  350. */
  351. public function study(){
  352. $uid = $this->user_id;
  353. $data = input();
  354. if(!isset($data['video_intro_id'])){
  355. $this->success('ok1');
  356. }
  357. $user = Db::name('store_member')->field('id,phone,email')->where('id',$uid)->find();
  358. // 设置时区为UTC,可以更改为其他时区
  359. date_default_timezone_set('UTC');
  360. // 获取今天开始的时间戳
  361. $todayStart = strtotime("today");
  362. // 获取今天结束的时间戳
  363. $todayEnd = strtotime("tomorrow") - 1;
  364. // 如果需要将时间戳转换为日期格式,可以使用date函数
  365. $todayStartDate = date("Y-m-d H:i:s", $todayStart);
  366. $todayEndDate = date("Y-m-d H:i:s", $todayEnd);
  367. if($user['phone']){
  368. $user_phone = $user['phone'];
  369. }else{
  370. $user_phone = $user['email'];
  371. }
  372. $arr = [
  373. 'user_id'=>$uid,
  374. 'user_phone' => $user_phone,
  375. 'study_duration' => $data['study_duration'],
  376. 'video_intro_id' => $data['video_intro_id'],
  377. ];
  378. $study = Db::name('user_study_log')->where('user_id',$uid)
  379. ->where('video_intro_id',$data['video_intro_id'])
  380. ->where('create_at','> time',$todayStartDate)
  381. ->where('create_at','< time',$todayEndDate)
  382. ->find();
  383. if($study){
  384. $arr['study_duration'] = $arr['study_duration'] + $study['study_duration'];
  385. Db::name('user_study_log')->where('id',$study['id'])->update($arr);
  386. }else{
  387. $arr['create_at'] = date('Y-m-d H:i:s');
  388. Db::name('user_study_log')->insert($arr);
  389. }
  390. $this->success('ok');
  391. }
  392. }