UserCenter.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\UserLevelRank;
  4. use app\common\model\UserMessage;
  5. use app\common\validate\UserVali;
  6. use think\Db;
  7. use app\common\model\User;
  8. /**
  9. * @title 会员个人中心
  10. * @controller UserCenter
  11. * @group base
  12. */
  13. class UserCenter extends Base
  14. {
  15. public function initialize()
  16. {
  17. parent::initialize();
  18. parent::checkLogin();
  19. }
  20. /**
  21. * @title 获取个人信息
  22. * @desc 个人信息
  23. * @author qc
  24. * @url /api/User_center/getUserInfo
  25. * @method GET
  26. * @tag 个人信息
  27. * @header name:Authorization require:1 desc:Token
  28. * @return name:phone type:int default:-- desc:手机号
  29. * @return name:name type:string default:-- desc:昵称
  30. * @return name:headimg type:string default:-- desc:头像地址
  31. * @return name:sex type:int default:1 desc:性别(1男2女)
  32. * @return name:true_name type:string default:-- desc:真实姓名
  33. * @return name:invite_code type:string default:-- desc:邀请码
  34. * @return name:account_type type:int default:-- desc:账号类型1公司,2个人
  35. * @return name:is_first type:int default:-- desc:企业组是否是第一个注册(1是0否【第一个注册有会员会员重置权限,公司其他人会员权限读取公司第一个注册的会员权限】)
  36. * @return name:user_level type:array default:-- desc:开通会员信息[为空就不是vip]
  37. * @return name:user_level.level_id type:int default:-- desc:等级id
  38. * @return name:user_level.end_date type:string default:-- desc:过期时间
  39. * @return name:user_level.name type:string default:-- desc:会员等级名称
  40. * @return name:user_level.logo type:string default:-- desc:会员等级logo
  41. *
  42. */
  43. public function getUserInfo()
  44. {
  45. $field = 'id,phone,name,level_id,headimg,openid,true_name,sex,invite_code,account_type,group_id,is_first,group_first';
  46. $user_info = User::field($field)->where('id',$this->user_id)->find()->toArray();
  47. if($user_info['account_type'] == 2 || ($user_info['account_type']== 1 && $user_info['is_first'])){
  48. $user_level = UserLevelRank::field('r.id,r.level_id,r.end_time,r.end_date,l.name,l.logo')
  49. ->alias('r')
  50. ->leftJoin('UserLevel l','l.id = r.level_id')
  51. ->where([['r.user_id','=',$this->user_id],['r.end_time','>',time()]])->order('r.level_id desc')->find();
  52. }else{
  53. $user_level = UserLevelRank::field('r.id,r.level_id,r.end_time,r.end_date,l.name,l.logo')
  54. ->alias('r')
  55. ->leftJoin('UserLevel l','l.id = r.level_id')
  56. ->where([['r.user_id','=',$user_info['group_first']],['r.end_time','>',time()]])->order('r.level_id desc')->find();
  57. }
  58. $this->success('获取成功',['detail'=>$user_info,'user_level'=>$user_level ? $user_level->toArray() :null]);
  59. }
  60. /**
  61. * @title 编辑个人信息
  62. * @desc 编辑个人信息
  63. * @author qc
  64. * @url /api/User_center/updateUserInfo
  65. * @method POST
  66. * @tag 编辑信息
  67. * @header name:Authorization require:1 desc:Token
  68. * @param name:name type:string default:-- desc:昵称
  69. * @param name:headimg type:string default:-- desc:头像地址
  70. */
  71. public function updateUserInfo()
  72. {
  73. $headimg = input('post.headimg');
  74. $name = trim(input('post.name',''));
  75. $update_data= [];
  76. $update_data['name'] = $name;
  77. $update_data['headimg'] = $headimg;
  78. Db::name('store_member')->where('id',$this->user_id)->update($update_data);
  79. $field = 'phone,name,headimg';
  80. $user_info = Db::name('store_member')->field($field)->where('id',$this->user_id)->find();
  81. $this->success('编辑成功',['detail'=>$user_info]);
  82. }
  83. /**
  84. * @title 绑定手机号
  85. * @desc 绑定手机号
  86. * @author qc
  87. * @url /api/User_center/modifyPhone
  88. * @method POST
  89. * @header name:Authorization require:1 desc:Token
  90. * @param name:phone type:int require:1 default:-- desc:手机号
  91. * @param name:code type:int require:1 default:-- desc:手机号验证码
  92. * @return name:phone type:int default:-- desc:手机号
  93. * @return name:name type:string default:-- desc:昵称
  94. * @return name:headimg type:string default:-- desc:头像地址
  95. */
  96. public function modifyPhone()
  97. {
  98. $uid = $this->user_id;
  99. $phone = input('post.phone');
  100. $code = input('post.code');
  101. if(empty($phone) || empty($code) ) $this->error('参数错误');
  102. $field = 'id,phone,name,headimg';
  103. $user_info = Db::name('store_member')->field($field)->where('id',$this->user_id)->find();
  104. if($user_info['phone'] == $phone)$this->error('更换手机号与绑定手机号一致!');
  105. $check_code = $this->checkPhoneCode($phone,$code);
  106. if(!$check_code) $this->error('验证码错误');
  107. //判断手机号是否已注册
  108. $member_id = Db::name('store_member')
  109. ->where('phone','=',$phone)->where('is_deleted','=',0)->where('id','<>',$this->user_id)->value('id');
  110. if($member_id) $this->error('手机号已注册过');
  111. Db::name('store_member')->where('id',$uid)->update(['phone'=>$phone]);
  112. $user_info['phone'] = $phone;
  113. $this->updatePhoneCode($check_code);
  114. $this->success('绑定成功',$user_info);
  115. }
  116. /**
  117. * @title 更换或设置登录密码
  118. * @desc 更换或设置登录密码
  119. * @author qc
  120. * @url /api/User_center/modifyPassword
  121. * @method POST
  122. * @header name:Authorization require:1 desc:Token
  123. * @param name:phone type:int require:1 default:-- desc:手机号
  124. * @param name:code type:int require:1 default:-- desc:手机号验证码
  125. * @param name:password type:string default:-- desc:密码
  126. */
  127. public function modifyPassword()
  128. {
  129. $uid = $this->user_id;
  130. $phone = input('post.phone');
  131. $code = input('post.code');
  132. $password = input('post.password');
  133. if(empty($phone) || empty($code) || empty($password) ) $this->error('参数错误');
  134. $field = 'id,phone';
  135. $user_info = User::field($field)->where('id',$this->user_id)->find()->toArray();
  136. if(!$user_info['phone']) $this->error('请先绑定手机号');
  137. if($user_info['phone'] != $phone) $this->error('与绑定手机号不一致');
  138. $check_code = $this->checkPhoneCode($phone,$code);
  139. if(!$check_code) $this->error('验证码错误');
  140. $validate = new UserVali();
  141. if (!$validate->scene('reset_password')->check(['phone'=>$phone,'password'=>$password])) {
  142. $this->error($validate->getError());
  143. }
  144. $encryption_password = encrypt_password($password);
  145. User::where('id',$uid)->update(['password'=>$password,'encryption_password'=>$encryption_password]);
  146. $this->updatePhoneCode($check_code);
  147. $this->success('更换成功');
  148. }
  149. /**
  150. * @title 完善用户openid
  151. * @desc 完善用户openid
  152. * @author qc
  153. * @method POST
  154. * @tag 编辑信息
  155. * @url /api/User_center/saveOpenid
  156. * @header name:Authorization require:1 desc:Token
  157. * @param name:code type:string require:1 default:-- desc:code
  158. */
  159. public function saveOpenid()
  160. {
  161. $code = input('post.code');
  162. if (empty($code)) $this->error('参数错误');
  163. $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');
  164. $res = json_decode($res,true);
  165. if (empty($res['openid'])) $this->error('未获取到openid');
  166. $member_data = ['openid' => $res['openid']];
  167. Db::name('store_member')->where('id',$this->user_id)->update($member_data);
  168. $this->success('已绑定openid');
  169. }
  170. /**
  171. * @title 消息列表【会员消息】
  172. * @desc 我的点赞
  173. * @author qc
  174. * @method GET
  175. * @url /api/User_center/getMessageList
  176. * @header name:Authorization require:1 desc:Token
  177. * @param name:title type:string default:-- desc:标题
  178. * @param name:page type:int default:-- desc:页数
  179. * @param name:page_num type:int default:-- desc:每页数
  180. *
  181. * @return name:id type:int default:-- desc:记录id
  182. * @return name:title type:string default:-- desc:标题
  183. * @return name:content type:string default:-- desc:内容
  184. * @return name:create_at type:string default:-- desc:时间
  185. * @return name:is_read type:int default:-- desc:是否已读【0否1是】
  186. */
  187. public function getMessageList()
  188. {
  189. $list = UserMessage::where(['user_id'=>$this->user_id])
  190. ->limit($this->off_set,$this->page_num)
  191. ->order(['id'=>'desc'])
  192. ->select()->toArray();
  193. $this->success('ok',['list'=>$list]);
  194. }
  195. /**
  196. * @title 消息详情【会员消息】
  197. * @desc 消息详情
  198. * @author qc
  199. * @method GET
  200. * @url /api/User_center/getMessageInfo
  201. * @header name:Authorization require:1 desc:Token
  202. * @param name:id type:int default:-- desc:消息记录id
  203. * @return name:title type:string default:-- desc:标题
  204. * @return name:content type:string default:-- desc:内容
  205. * @return name:create_at type:string default:-- desc:时间
  206. * @return name:is_read type:int default:-- desc:是否已读【0否1是】
  207. */
  208. public function getMessageInfo()
  209. {
  210. $detail= UserMessage::where(['user_id'=>$this->user_id,'id'=>input('get.id')])->find()->toArray();
  211. $this->success('ok',['detail'=>$detail]);
  212. }
  213. }