UserCenter.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace app\api\controller;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. use think\Db;
  7. /**
  8. * @title 会员个人中心
  9. * @controller UserCenter
  10. * @group base
  11. */
  12. class UserCenter extends Base
  13. {
  14. public function initialize()
  15. {
  16. parent::check_login();
  17. }
  18. /**
  19. * @title 获取个人信息
  20. * @desc 个人信息
  21. * @author qc
  22. * @url /api/User_center/getUserInfo
  23. * @method GET
  24. * @tag 个人信息
  25. * @header name:Authorization require:1 desc:Token
  26. * @return name:phone type:int default:-- desc:手机号
  27. * @return name:name type:string default:-- desc:昵称
  28. * @return name:headimg type:string default:-- desc:头像地址
  29. * @return name:crystal type:int default:-- desc:元石
  30. * @return name:true_name type:string default:-- desc:真实姓名
  31. * @return name:id_card type:string default:-- desc:身份证号
  32. * @return name:is_auth type:int default:-- desc:是否认证(0否1是)
  33. * @return name:auth_at type:string default:-- desc:认证时间
  34. */
  35. public function getUserInfo()
  36. {
  37. $uid = $this->uid;
  38. $field = 'id,phone,name,headimg,status,crystal,openid,true_name,id_card,is_auth,auth_at';
  39. $user_info = Db::name('store_member')->field($field)->where('id',$uid)->find();
  40. if(empty($user_info)) $this->error('用户信息不正确');
  41. if($user_info['status'] == 0) $this->error('该用户已被禁用');
  42. $crystal_switch = intval(sysconf('crystal_switch'));
  43. $user_info['crystal_switch'] = $crystal_switch;
  44. $this->success('获取成功',$user_info);
  45. }
  46. /**
  47. * @title 绑定手机号
  48. * @desc 绑定手机号
  49. * @author qc
  50. * @url /api/User_center/modifyPhone
  51. * @method POST
  52. * @header name:Authorization require:1 desc:Token
  53. * @param name:phone type:int require:1 default:-- desc:手机号
  54. * @param name:code type:int require:1 default:-- desc:手机号验证码
  55. * @return name:phone type:int default:-- desc:手机号
  56. * @return name:name type:string default:-- desc:昵称
  57. * @return name:headimg type:string default:-- desc:头像地址
  58. * @return name:crystal type:int default:-- desc:元石
  59. * @return name:true_name type:string default:-- desc:真实姓名
  60. * @return name:id_card type:string default:-- desc:身份证号
  61. * @return name:is_auth type:int default:-- desc:是否认证(0否1是)
  62. */
  63. public function modifyPhone()
  64. {
  65. $uid = $this->uid;
  66. $phone = input('post.phone');
  67. $code = input('post.code');
  68. if(empty($phone) || empty($code) ) $this->error('参数错误');
  69. $field = 'id,phone,name,headimg,status,crystal,openid,true_name,id_card,is_auth';
  70. $user_info = Db::name('store_member')->field($field)->where('id',$this->uid)->find();
  71. if($user_info['phone'] == $phone)$this->error('更换手机号与绑定手机号一致!');
  72. $sel_time =date('Y-m-d H:i:s',time()-600);
  73. $store_member_sms = Db::name('store_member_sms')
  74. ->field('id,code')->where('phone',$phone)
  75. ->where('create_at','> time',$sel_time)
  76. ->where('used',0)->order('id desc')->find();
  77. if(empty($store_member_sms) || $store_member_sms['code'] != $code) $this->error('验证码错误');
  78. //判断手机号是否已注册
  79. $member_id = Db::name('store_member')
  80. ->where('phone','=',$phone)->where('id','<>',$this->uid)->value('id');
  81. if($member_id) $this->error('手机号已注册过');
  82. Db::name('store_member')->where('id',$uid)->update(['phone'=>$phone]);
  83. $user_info['phone'] = $phone;
  84. Db::name('store_member_sms')->where('id',$store_member_sms['id'])->update(['used'=>1]);
  85. $this->success('绑定成功',$user_info);
  86. }
  87. /**
  88. * @title 验证已绑定手机号是否正确
  89. * @desc 验证已绑定手机号是否正确
  90. * @author qc
  91. * @url /api/User_center/checkBindPhone
  92. * @method POST
  93. * @header name:Authorization require:1 desc:Token
  94. * @param name:phone type:int require:1 default:-- desc:手机号
  95. * @param name:code type:int require:1 default:-- desc:手机号验证码
  96. */
  97. public function checkBindPhone()
  98. {
  99. $phone = input('post.phone');
  100. $code = input('post.code');
  101. if(empty($phone) || empty($code) ) $this->error('参数错误');
  102. $field = 'id,phone,name,headimg,status,crystal,openid,true_name,id_card,is_auth';
  103. $user_info = Db::name('store_member')->field($field)->where('id',$this->uid)->find();
  104. if($user_info['phone'] != $phone) $this->error('该手机号与当前账户不一致!');
  105. $sel_time =date('Y-m-d H:i:s',time()-600);
  106. $store_member_sms = Db::name('store_member_sms')
  107. ->field('id,code')->where('phone',$phone)
  108. ->where('create_at','> time',$sel_time)
  109. ->where('used',0)->order('id desc')->find();
  110. if(empty($store_member_sms) || $store_member_sms['code'] != $code) $this->error('验证码错误');
  111. Db::name('store_member_sms')->where('id',$store_member_sms['id'])->update(['used'=>1]);
  112. $this->success('验证通过');
  113. }
  114. /**
  115. * @title 编辑个人信息
  116. * @desc 编辑个人信息
  117. * @author qc
  118. * @url /api/User_center/updateUserInfo
  119. * @method POST
  120. * @tag 编辑信息
  121. * @header name:Authorization require:1 desc:Token
  122. * @return name:name type:string default:-- desc:姓名
  123. * @return name:headimg type:string default:-- desc:头像地址
  124. */
  125. public function updateUserInfo()
  126. {
  127. $headimg = input('post.headimg');
  128. $name = trim(input('post.name',''));
  129. if(!$headimg && !$name) $this->error('参数错误');
  130. $update_data= [];
  131. $update_data['name'] = $name;
  132. $update_data['headimg'] = $headimg;
  133. if($name){
  134. $check_member = Db::name('store_member')->where('name',$name)->where('id','<>',$this->uid)->count();
  135. if($check_member) $this->error('该用户名已被占用');
  136. }
  137. Db::name('store_member')->where('id',$this->uid)->update($update_data);
  138. $field = 'phone,name,headimg';
  139. $user_info = Db::name('store_member')->field($field)->where('id',$this->uid)->find();
  140. $this->success('编辑成功',$user_info);
  141. }
  142. /**
  143. * @title 会员认证
  144. * @desc 会员认证
  145. * @author qc
  146. * @method POST
  147. * @tag 编辑信息
  148. * @url /api/User_center/userCertification
  149. * @header name:Authorization require:1 desc:Token
  150. * @param name:true_name type:string require:1 default:-- desc:真实姓名
  151. * @param name:id_card type:string require:1 default:-- desc:身份证号
  152. */
  153. public function userCertification()
  154. {
  155. $true_name = input('post.true_name');
  156. $id_card = input('post.id_card');
  157. $is_auth = Db::table('store_member')->where('id',$this->uid)->value('is_auth');
  158. if($is_auth) $this->error('已认证');
  159. $get_age = $this->getAge($id_card);
  160. if($get_age < 18)$this->error('未满18周岁!');
  161. $res = user_certification($id_card,$true_name);
  162. if(!$res) $this->error('认证失败');
  163. Db::table('store_member')->where('id',$this->uid)->update(['is_auth'=>1,'true_name'=>$true_name,'id_card'=>$id_card,'auth_at'=>date('Y-m-d H:i:s')]);
  164. $this->success('认证成功');
  165. }
  166. /**
  167. * @param $id
  168. * @return false|string
  169. */
  170. function getAge($id){
  171. # 1.从身份证中获取出生日期
  172. $birth_Date = strtotime(substr($id, 6, 8));//截取日期并转为时间戳
  173. # 2.格式化[出生日期]
  174. $Year = date('Y', $birth_Date);//yyyy
  175. $Month = date('m', $birth_Date);//mm
  176. $Day = date('d', $birth_Date);//dd
  177. # 3.格式化[当前日期]
  178. $current_Y = date('Y');//yyyy
  179. $current_M = date('m');//mm
  180. $current_D = date('d');//dd
  181. # 4.计算年龄()
  182. $age = $current_Y - $Year;//今年减去生日年
  183. if($Month > $current_M || $Month == $current_M && $Day > $current_D){//深层判断(日)
  184. $age--;//如果出生月大于当前月或出生月等于当前月但出生日大于当前日则减一岁
  185. }
  186. # 返回
  187. return $age;
  188. }
  189. }