Login.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace app\data\controller\api;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. use app\common\model\Config;
  7. use app\common\model\Sms;
  8. use app\data\model\DataUser;
  9. use app\data\service\MessageService;
  10. use app\data\service\UserAdminService;
  11. use Carbon\Carbon;
  12. use hg\apidoc\annotation\Method;
  13. use hg\apidoc\annotation\Param;
  14. use hg\apidoc\annotation\Returned;
  15. use hg\apidoc\annotation\Title;
  16. use think\Request;
  17. /**
  18. * @Title("用户登录注册接口")
  19. */
  20. class Login extends Auth
  21. {
  22. protected $noNeedLogin=['in','hx','register','sendsms','findpwd','sms','getOpenid','getloginset','status','platform_phone','isUserMoney'];
  23. /**
  24. * @Title("手机号或用户名+密码登陆")
  25. * @Method("post")
  26. * @Param("phone",desc="手机号或用户名")
  27. * @Param("password",desc="密码")
  28. * @Param("openid",desc="openid,小程序登录时传")
  29. * @Returned("token.token",desc="token")
  30. */
  31. public function in()
  32. {
  33. $data = $this->_vali([
  34. //'phone.mobile' => '手机号码格式错误!',
  35. 'phone.require' => '登录名不能为空!',
  36. 'password.require' => '登录密码不能为空!',
  37. 'openid.default' => '',
  38. ],'post');
  39. $map = ['deleted' => 0, 'phone|username' => $data['phone']];
  40. $user = DataUser::mk()->where($map)->order('id','desc')->findOrEmpty();
  41. if ($user->isEmpty()) $this->error('该手机号还没有注册哦!');
  42. if (empty($user['status'])) $this->error('账号不存在!');
  43. if (md5($data['password']) === $user['password']) {
  44. if(isset($data['openid'])){
  45. $user->openid1 = $data['openid'];
  46. }else{
  47. $user->openid1 = '';
  48. }
  49. $user->update_at = date('Y-m-d H:i:s');
  50. $user->huanxinID = 'cbz_'.$user->id;
  51. $user->jgalias = 'user_'.$user->id;
  52. $user->save();
  53. huanxin_zhuce('cbz_'.$user->id,$user->nickname);
  54. $this->success('手机登录成功!', UserAdminService::set($map, [], $this->type, true));
  55. } else {
  56. $this->error('密码错误');
  57. }
  58. }
  59. public function hx(){
  60. huanxin_zhuce('cbz-1','张三');
  61. }
  62. /**
  63. * @Title("用户统一注册入口")
  64. * @Method("post")
  65. * @Param("nickname",desc="昵称")
  66. * @Param("phone",desc="手机号")
  67. * @Param("verify",desc="验证码")
  68. * @Param("password",desc="密码")
  69. * @Param("openid1",desc="openid")
  70. */
  71. public function register()
  72. {
  73. $data = $this->_vali([
  74. 'region_province.default' => '',
  75. 'region_city.default' => '',
  76. 'region_area.default' => '',
  77. 'username.default' => '',
  78. 'openid1.default' => '',
  79. 'phone.mobile' => '手机格式错误!',
  80. 'phone.require' => '手机不能为空!',
  81. 'nickname.require' => '昵称必须!',
  82. 'verify.require' => '验证码不能为空!',
  83. 'password.require' => '登录密码不能为空!',
  84. ]);
  85. $map = ['phone' => $data['phone'], 'deleted' => 0,'status' => 1];
  86. if (DataUser::mk()->where($map)->count() > 0) {
  87. $this->error('手机号已注册,请使用其它手机号!');
  88. }
  89. if (!MessageService::instance()->checkCode($data['verify'], $data['phone'],2)) {
  90. $this->error('手机短信验证失败!');
  91. }
  92. $data['password'] = md5($data['password']);
  93. $user = UserAdminService::set($map, $data, $this->type, true);
  94. $huanxinID = 'cbz_'.$user['id'];
  95. $jgalias = 'user_'.$user['id'];
  96. DataUser::mk()->where('id',$user['id'])->update(['huanxinID'=>$huanxinID,'jgalias'=>$jgalias,'openid1'=>$data['openid1']]);
  97. huanxin_zhuce('cbz_'.$user['id'],$user['nickname']);
  98. empty($user) ? $this->error('手机注册失败!') : $this->success('用户注册成功!', $user);
  99. }
  100. /**
  101. * @Title("发送短信验证码")
  102. * @Param("phone",desc="手机号")
  103. * @Param("type",desc="1登陆2注册3找回密码4发布应聘5商家登录6商家找回密码7用户注销8商家注销")
  104. */
  105. public function sendsms()
  106. {
  107. $data = $this->_vali([
  108. 'phone.mobile' => '手机号格式错误!',
  109. 'phone.require' => '手机号不能为空!',
  110. 'type.require' => '类型不能为空!',
  111. 'type.in:1,2,3,4,5,6,7,8' => '类型有误!',
  112. ]);
  113. $needLogin=[];
  114. if(in_array($data['type'],$needLogin) && !$this->uuid){
  115. $this->error('请登录');
  116. }
  117. MessageService::instance()->sendCode($data['phone'],$data['type']);
  118. $this->success('发送成功');
  119. }
  120. /**
  121. * @Title("找回密码")
  122. * @Method("post")
  123. * @Param("phone",desc="手机号")
  124. * @Param("verify",desc="验证码")
  125. * @Param("password",desc="密码")
  126. */
  127. public function findpwd(){
  128. $data=$this->_vali([
  129. 'phone.mobile'=>'手机号必须',
  130. 'phone.require'=>'手机号必须',
  131. 'verify.require'=>'验证码必须',
  132. 'password.require'=>'密码必须',
  133. ],'post');
  134. if (!MessageService::instance()->checkCode($data['verify'], $data['phone'],3)) {
  135. $this->error('手机短信验证失败!');
  136. }
  137. $user=DataUser::where('phone',$data['phone'])->find();
  138. if(!$user){
  139. $this->error('用户不存在');
  140. }
  141. $user['password']=md5($data['password']);
  142. $user->save();
  143. $this->success('修改成功');
  144. }
  145. /**
  146. * @Title ("验证码登陆")
  147. * @Method ("post")
  148. * @Param("phone",desc="手机号")
  149. * @Param("verify",desc="验证码")
  150. * @Param("openid",desc="openid,小程序登录时传")
  151. * @Returned("registered",type="boolean",desc="是否已注册")
  152. */
  153. public function sms(){
  154. $data=$this->_vali([
  155. 'phone.mobile'=>'手机号必须',
  156. 'phone.require'=>'手机号必须',
  157. 'verify.require'=>'验证码必须',
  158. 'openid.default' => '',
  159. ],'post');
  160. if (!MessageService::instance()->checkCode($data['verify'], $data['phone'],1)) {
  161. $this->error('手机短信验证失败!');
  162. }
  163. $user=DataUser::where('phone',$data['phone'])->find();
  164. if(!$user){
  165. $this->error('用户未注册',[
  166. 'registered'=>false
  167. ]);
  168. }
  169. if(!$user['status']){
  170. $this->error('用户被禁用');
  171. }
  172. $user->openid1 = $data['openid'];
  173. $user->update_at = date('Y-m-d H:i:s');
  174. $user->huanxinID = 'cbz_'.$user['id'];
  175. $user->jgalias = 'user_'.$user->id;
  176. $user->save();
  177. huanxin_zhuce('cbz_'.$user['id'],$user['nickname']);
  178. $user = UserAdminService::set(['id'=>$user['id']], [], $this->type, true);
  179. $this->success('登录成功',$user);
  180. }
  181. /**
  182. * @Title ("小程序根据code获取openid")
  183. * @Method ("post")
  184. * @Param("code",desc="code")
  185. */
  186. public function getOpenid(){
  187. $data=$this->_vali([
  188. 'code.require'=>'code必须',
  189. ],'post');
  190. $code = $data['code'];
  191. $appid = 'wxa5033cf91977e574';
  192. $secret = '2a36bfbced93a2c4256e7b6b15922990';
  193. $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $secret . "&js_code=" . $code . "&grant_type=authorization_code";
  194. $session_key = curlRequest($url);
  195. if (!empty($session_key['session_key'])) {
  196. $re['openid'] = $session_key['openid'];
  197. $this->success('成功',$re);
  198. }else{
  199. $this->error('获取session_key失败!');
  200. }
  201. }
  202. /**
  203. * @Title("获取用户服务协议和个人保护政策")
  204. * @Method("post")
  205. * @Returned("service_agreement",desc="用户服务协议")
  206. * @Returned("protection_policy",desc="个人信息保护政策")
  207. */
  208. public function getloginset(){
  209. $info= [
  210. 'service_agreement'=>htmlspecialchars_decode(sysconf('user_login.service_agreement')),
  211. 'protection_policy'=>htmlspecialchars_decode(sysconf('user.privacy_policy')),
  212. ];
  213. $this->success('成功',$info);
  214. }
  215. //华为上架
  216. public function status(){
  217. $data = SystemConfig('status.huawei');
  218. $this->success('成功',$data);
  219. }
  220. //苹果商家控制会员充值接口 0隐藏1显示
  221. public function isUserMoney(){
  222. $data = SystemConfig('status.is_user_money');
  223. $this->success('成功',$data);
  224. }
  225. /**
  226. * @Title("客服电话")
  227. * @Method("post")
  228. * @Returned("protection_policy",desc="个人信息保护政策")
  229. */
  230. public function platform_phone(){
  231. $data = SystemConfig('user.service_phone')?:[];
  232. $this->success('成功',$data);
  233. }
  234. /**
  235. * @Title("注销提示")
  236. * @Method("get")
  237. * @Returned("protection_policy",desc="个人信息保护政策")
  238. */
  239. public function logout_prompt(){
  240. $data = SystemConfig('user.logout_prompt')?:[];
  241. $this->success('成功',$data);
  242. }
  243. }