123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkAdmin
- // +----------------------------------------------------------------------
- // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
- // +----------------------------------------------------------------------
- // | 官方网站: http://demo.thinkadmin.top
- // +----------------------------------------------------------------------
- // | 开源协议 ( https://mit-license.org )
- // +----------------------------------------------------------------------
- // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
- // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
- // +----------------------------------------------------------------------
- namespace app\api\controller;
- use AlibabaCloud\Client\AlibabaCloud;
- use AlibabaCloud\Client\Exception\ClientException;
- use AlibabaCloud\Client\Exception\ServerException;
- use app\api\controller\Base;
- use think\Db;
- use Firebase\JWT\JWT;
- use EasyWeChat\Factory;
- /**
- * @title 登录
- * @controller Login
- * @group worker
- */
- class Login extends Base
- {
- /**
- * @title 用户协议
- * @desc 用户协议
- * @author QGF
- * @url /api/Login/agreement
- * @method GET
- * @tag 用户协议
- * @return name:-- type:string default:-- desc:用户协议内容(富文本)
- */
- public function agreement(){
- $this->success('获取成功',htmlspecialchars_decode(sysconf('agreement')));
- }
- /**
- * @title 隐私政策
- * @desc 隐私政策
- * @author QGF
- * @url /api/Login/privacy_policy
- * @method GET
- * @tag 隐私政策
- * @return name:-- type:string default:-- desc:隐私政策
- */
- public function privacy_policy(){
- $this->success('获取成功',htmlspecialchars_decode(sysconf('privacy_policy')));
- }
- /**
- * @title 注册
- * @desc 注册
- * @author QGF
- * @url /api/Login/register
- * @method POST
- * @tag 注册
- * @param name:phone type:int require:1 default:-- desc:手机号
- * @param name:code type:int require:1 default:-- desc:短信验证码
- * @param name:password type:string require:1 default:-- desc:密码
- */
- public function register(){
- $phone = input('phone');
- $code = input('code');
- $password = input('password');
- if(empty($phone) || empty($code) || empty($password)){
- $this ->error('参数错误');
- }
- $sms_id = $this->verify_sms($phone,$code);
- if(empty($sms_id)){
- $this->error('验证码不正确');
- }
- $member_id = Db::name('store_member')->where('phone',$phone)->value('id');
- if(!empty($member_id)){
- $this->error('该手机号已被注册');
- }
- $data = array(
- 'phone'=>$phone,
- 'password' => md5($password),
- 'decode_password' => $password,
- 'name' => substr($phone,-4),
- 'headimg' => 'https://zjth2021.oss-cn-beijing.aliyuncs.com/0909a49add201291/a20687c6493d3eab.jpg',
- 'background' => Db::name('store_background')->where('id',1)->value('logo'),
- );
- Db::name('store_member')->insert($data);
- $member_id = Db::name('store_member')->where('phone',$phone)->value('id');
- Db::name('store_member_sms')->where('id',$sms_id)->update(array('used'=>1));
- $token = self::create_jwt($member_id);
- $this->success('注册成功',$token);
- }
- /**
- * @title 微信登录(小程序)
- * @desc 微信登录(小程序)
- * @author QGF
- * @url /api/Login/we_chat_login
- * @method POST
- * @tag 登录 授权
- * @param name:code type:int require:1 default:-- desc:code值
- * @param name:headimg type:string require:1 default:-- desc:头像地址
- * @param name:name type:string require:1 default:-- desc:昵称
- * @return name:openid type:string default:-- desc:用户openid(未绑定手机号)
- * @return name:headimg type:string default:-- desc:用户头像地址(未绑定手机号)
- * @return name:name type:string default:屈耀光 desc:用户昵称(未绑定手机号)
- * @return name:token type:string default:-- desc:用户登录成功后的token值(已绑定手机号)
- */
- public function we_chat_login(){
- $code = input('code');
- $headimg = input('headimg');
- $name = input('name');
- if(empty($code) || empty($headimg) || empty($name)){
- $this->error('参数错误');
- }
- $app = Factory::miniProgram(config('app.mini_program'));
- $data = $app->auth->session($code);
- if(empty($data['openid'])){
- $this->error('微信登录失败');
- }
- $member = Db::name('store_member')->field('id,phone')->where('openid',$data['openid'])->find();
- if(empty($member['phone'])){
- $member_data = array(
- 'openid' => $data['openid'],
- 'headimg' => $headimg,
- 'name' => $name
- );
- $this->success('授权成功',$member_data);
- }
- $uid = $member['id'];
- if(empty($uid)){
- $this->error('数据有误');
- }
- $token = self::create_jwt($uid);
- $this->success('登录成功',$token);
- }
- /**
- * @title 微信登录(app)
- * @desc 微信登录
- * @author QGF
- * @url /api/Login/we_chat_app
- * @method POST
- * @tag 登录 授权
- * @param name:code type:int require:1 default:oRZeJ55dhq8y6gHI9PVwCrv0gvSM desc:code值
- * @return name:-- type:json default:-- desc:微信注册返回授权后的信息(openid:微信openid,name:微信昵称,headimgurl:微信头像地址,token:token值(这时为空)注:仅微信注册有)
- * @return name:token type:string default:-- desc:用户微信登录成功后的token值(仅用户登录有)
- */
- public function we_chat_app(){
- $code = input('code');
- if(empty($code)){
- $this->error('参数错误');
- }
- $res = requestGet('https://api.weixin.qq.com/sns/oauth2/access_token?appid='.config('app_program')['app_id'].'&secret='.config('app_program')['secret'].'&code='.$code.'&grant_type=authorization_code');
- $res = json_decode($res,true);
- $user_info = requestGet('https://api.weixin.qq.com/sns/userinfo?access_token='.$res['access_token'].'&openid='.$res['openid']);
- $user_info = json_decode($user_info,true);
- $data['openid'] = $user_info['openid'];
- $data['name'] = $user_info['nickname'];
- $data['headimg'] = $user_info['headimgurl'];
- $data['token'] = '';
- $member_id = Db::name('store_member')->where('app_openid',$data['openid'])->value('id');
- if(empty($member_id)){
- $this->success('授权成功',$data);
- }else{
- $token = self::create_jwt($member_id);
- $data['token'] = $token;
- $this->success('登录成功',$data);
- }
- }
- /**
- * @title 密码登录
- * @desc 密码登录
- * @author QGF
- * @url /api/Login/password_login
- * @method POST
- * @tag 密码登录
- * @param name:phone type:int require:1 default:-- desc:手机号
- * @param name:password type:string require:1 default:-- desc:密码
- * @return name:token type:string default:-- desc:用户登录成功后的token值
- */
- public function password_login(){
- $phone = input('phone');
- $password = input('password');
- if(empty($password) || empty($phone)){
- $this->error('参数错误');
- }
- $member_id = Db::name('store_member')->where('phone',$phone)->where('password',md5($password))->value('id');
- if(empty($member_id)){
- $this->error('手机号或密码错误');
- }
- $token = self::create_jwt($member_id);
- $this->success('登录成功',$token);
- }
- /**
- * @title 绑定手机号(点击微信登录时还没注册,绑定完手机号提交信息去注册)
- * @desc 绑定手机号
- * @author QGF
- * @url /api/Login/binding_phone
- * @method POST
- * @tag 绑定手机号
- * @param name:platform type:1 require:0 default:1 desc:平台(注:1:小程序2:app)
- * @param name:phone type:int require:1 default:-- desc:要绑定的手机号
- * @param name:code type:int require:1 default:-- desc:短信验证码
- * @param name:openid type:string require:1 default:-- desc:微信授权返回的openid
- * @param name:name type:string require:1 default:-- desc:微信授权返回的用户昵称
- * @param name:headimg type:string require:1 default:-- desc:微信授权返回的用户头像地址
- * @return name:token type:string default:-- desc:绑定成功后的token值
- */
- public function binding_phone(){
- $phone = input('phone');
- $code = input('code');
- $openid = input('openid');
- $name = input('name');
- $headimg = input('headimg');
- $platform = input('platform',1); //1:小程序,2:app
- if(empty($phone) || empty($code) || empty($openid) || empty($name) || empty($headimg)){
- $this ->error('参数错误');
- }
- $sms_id = $this->verify_sms($phone,$code);
- if(empty($sms_id)){
- $this->error('验证码不正确');
- }
- $member_id = Db::name('store_member')->where('phone',$phone)->value('id');
- if(!$member_id){
- $data = array(
- 'phone'=>$phone,
- 'name' => $name,
- 'headimg' => $headimg,
- );
- if($platform == 1){
- $data['openid'] = $openid;
- }else{
- $data['app_openid'] = $openid;
- }
- Db::name('store_member')->insert($data);
- $member_id = Db::name('store_member')->where('phone',$phone)->value('id');
- }else{
- if($platform == 1){
- $update_data['openid'] = $openid;
- }else{
- $update_data['app_openid'] = $openid;
- }
- Db::name('store_member')->where('id',$member_id)->update($update_data);
- }
- Db::name('store_member_sms')->where('id',$sms_id)->update(array('used'=>1));
- $token = self::create_jwt($member_id);
- $this->success('注册成功',$token);
- }
- /**
- * @title 忘记密码
- * @desc 忘记密码
- * @author QGF
- * @url /api/Login/reset_password
- * @method POST
- * @tag 忘记密码
- * @param name:phone type:int require:1 default:-- desc:手机号
- * @param name:code type:int require:1 default:-- desc:短信验证码
- * @param name:password type:string require:1 default:-- desc:密码
- */
- public function reset_password(){
- $phone = input('phone');
- $code = input('code');
- $password = input('password');
- if(empty($phone) || empty($code) || empty($password)){
- $this ->error('参数错误');
- }
- $sms_id = $this->verify_sms($phone,$code);
- if(empty($sms_id)){
- $this->error('验证码不正确');
- }
- $member = Db::name('store_member')->field('id,decode_password')->where('phone',$phone)->find();
- if(empty($member['id'])){
- $this ->error('该手机号未注册');
- }
- if($password == $member['decode_password']){
- $this ->error('新密码与原密码一致,直接登录');
- }
- $update_data = array(
- 'password'=>md5($password),
- 'decode_password'=>$password
- );
- Db::name('store_member')->where('phone',$phone)->update($update_data);
- Db::name('store_member_sms')->where('id',$sms_id)->update(array('used'=>1));
- $this->success('修改密码成功');
- }
- /**
- * @title 发送短信验证码
- * @desc 发送短信验证码
- * @author QGF
- * @url /api/Login/send_sms
- * @method POST
- * @tag 短信验证码
- * @param name:phone type:int require:1 default:-- desc:要获取验证码的手机号
- * @return name:code type:string default:-- desc:验证码
- */
- public function send_sms(){
- $phone = input('phone');
- if(empty($phone)){
- $this ->error('参数错误');
- }
- $code = rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);
- AlibabaCloud::accessKeyClient('LTAI5tDNASTjwM9mDSGckY3N', 'nAKY40GjGz4hf5A9RpoffDmaYWNL3y')->regionId('cn-hangzhou')->asDefaultClient();
- try {
- $result = AlibabaCloud::rpc()
- ->product('Dysmsapi')
- ->version('2017-05-25')
- ->action('SendSms')
- ->method('POST')
- ->host('dysmsapi.aliyuncs.com')
- ->options([
- 'query' => [
- 'RegionId' => "cn-hangzhou",
- 'PhoneNumbers' => $phone,
- 'SignName' => "碳汇资产",
- 'TemplateCode' => "SMS_222240090",
- 'TemplateParam' => json_encode(array("code"=>$code)),
- ],
- ])->request();
- $result = $result->toArray();
- $sms_data = array(
- 'phone'=>$phone,
- 'code'=>$code,
- 'result'=>$result['Message']
- );
- Db::name('store_member_sms')->insert($sms_data);
- } catch (ClientException $e) {
- echo $e->getErrorMessage() . PHP_EOL;
- } catch (ServerException $e) {
- echo $e->getErrorMessage() . PHP_EOL;
- }
- $this->success('发送成功',$code);
- }
- //校验短信验证码
- public function verify_sms($phone = '',$code = ''){
- $store_member_sms = Db::name('store_member_sms')->field('id,code')->where('phone',$phone)->where('used',0)->order('id desc')->find();
- if($store_member_sms['code'] == $code){
- return $store_member_sms['id'];
- }else{
- return 0;
- }
- }
- //token加密
- public function create_jwt($uid)
- {
- $key = md5(config('app.jwt')); //jwt的签发密钥,验证token的时候需要用到
- $time = time(); //签发时间
- $expire = $time + config('app.jwt_time'); //过期时间
- $token = array(
- "uid" => $uid,
- "iss" => "https://zain.com",//签发组织
- "aud" => "https://zain.com", //签发作者
- "iat" => $time,
- "nbf" => $time,
- "exp" => $expire
- );
- $jwt = JWT::encode($token, $key);
- return $jwt;
- }
- public function privacy_policy(){
- $this->success('获取成功',htmlspecialchars_decode(sysconf('privacy_policy')));
- }
- public function get_token(){
- $uid = input('uid',500);
- $token = $this->create_jwt($uid);
- $this->success('',$token);
- }
- }
|