From aa0e11e6783770419ec0a3ac4d097f2265445f88 Mon Sep 17 00:00:00 2001 From: devil_gong Date: Tue, 20 Nov 2018 17:02:19 +0800 Subject: [PATCH] user --- .../pages/goods-category/goods-category.acss | 3 +- .../Default/AppMiniAlipayConfig/Index.html | 29 +- .../Api/Controller/UserController.class.php | 357 ++++++++++-------- service/Application/Common/Conf/config.php | 3 - service/Application/Library/Alipay.class.php | 81 ++-- .../Application/Library/BaiduAuth.class.php | 95 +++++ 6 files changed, 354 insertions(+), 214 deletions(-) create mode 100644 service/Application/Library/BaiduAuth.class.php diff --git a/alipay/pages/goods-category/goods-category.acss b/alipay/pages/goods-category/goods-category.acss index 1733f6efe..7d0a85424 100644 --- a/alipay/pages/goods-category/goods-category.acss +++ b/alipay/pages/goods-category/goods-category.acss @@ -3,6 +3,7 @@ box-sizing: border-box; overflow: hidden; padding-bottom: 120rpx; + min-height: 100vh; } .content-items { float: left; @@ -15,6 +16,6 @@ line-height: 46rpx; } .content-items .icon { - width: 120rpx; + width: 100%; height: 120rpx; } \ No newline at end of file diff --git a/service/Application/Admin/View/Default/AppMiniAlipayConfig/Index.html b/service/Application/Admin/View/Default/AppMiniAlipayConfig/Index.html index aef61f293..d2fc6a2ab 100755 --- a/service/Application/Admin/View/Default/AppMiniAlipayConfig/Index.html +++ b/service/Application/Admin/View/Default/AppMiniAlipayConfig/Index.html @@ -6,16 +6,33 @@
- - value="{{$data.home_app_mini_alipay_title.value}}" required /> + + value="{{$data.common_app_mini_alipay_appid.value}}" required />
- - value="{{$data.home_app_mini_alipay_describe.value}}" required /> + +
- - value="{{$data.home_app_mini_alipay_customer_service_tel.value}}" /> + + +
+
+ + +
+ +
+ + value="{{$data.common_app_mini_alipay_title.value}}" required /> +
+
+ + value="{{$data.common_app_mini_alipay_describe.value}}" required /> +
+
+ + value="{{$data.common_app_mini_alipay_customer_service_tel.value}}" />
diff --git a/service/Application/Api/Controller/UserController.class.php b/service/Application/Api/Controller/UserController.class.php index 225b5d5e5..efd5970df 100755 --- a/service/Application/Api/Controller/UserController.class.php +++ b/service/Application/Api/Controller/UserController.class.php @@ -11,70 +11,70 @@ namespace Api\Controller; */ class UserController extends CommonController { - /** - * [_initialize 前置操作-继承公共前置方法] - * @author Devil - * @blog http://gong.gg/ - * @version 0.0.1 - * @datetime 2017-03-02T22:48:35+0800 - */ - public function _initialize() - { - // 调用父类前置方法 - parent::_initialize(); + /** + * [_initialize 前置操作-继承公共前置方法] + * @author Devil + * @blog http://gong.gg/ + * @version 0.0.1 + * @datetime 2017-03-02T22:48:35+0800 + */ + public function _initialize() + { + // 调用父类前置方法 + parent::_initialize(); // 是否ajax请求 if(!IS_AJAX) { $this->error(L('common_unauthorized_access')); } - } - - /** - * [Reg 用户注册-数据添加] - * @author Devil - * @blog http://gong.gg/ - * @version 0.0.1 - * @datetime 2017-03-07T00:08:36+0800 - */ - public function Reg() - { - // 账户校验 - $this->UserRegAccountsCheck(); - - // 验证码校验 - $verify_param = array( - 'key_prefix' => 'reg', - 'expire_time' => MyC('common_verify_expire_time') - ); - $obj = new \Library\Sms($verify_param); - - // 是否已过期 - if(!$obj->CheckExpire()) - { - $this->ajaxReturn(L('common_verify_expire'), -10); - } - // 是否正确 - if(!$obj->CheckCorrect(I('verify'))) - { - $this->ajaxReturn(L('common_verify_error'), -11); - } - - // 绑定 - $m = M('User'); + } + + /** + * [Reg 用户注册-数据添加] + * @author Devil + * @blog http://gong.gg/ + * @version 0.0.1 + * @datetime 2017-03-07T00:08:36+0800 + */ + public function Reg() + { + // 账户校验 + $this->UserRegAccountsCheck(); + + // 验证码校验 + $verify_param = array( + 'key_prefix' => 'reg', + 'expire_time' => MyC('common_verify_expire_time') + ); + $obj = new \Library\Sms($verify_param); + + // 是否已过期 + if(!$obj->CheckExpire()) + { + $this->ajaxReturn(L('common_verify_expire'), -10); + } + // 是否正确 + if(!$obj->CheckCorrect(I('verify'))) + { + $this->ajaxReturn(L('common_verify_error'), -11); + } + + // 绑定 + $m = M('User'); $type_field = I('app_type', 'alipay').'_openid'; - $data = array( - $type_field => I('alipay_openid'), - 'mobile' => I('mobile'), + $data = array( + $type_field => I($type_field), + 'mobile' => I('mobile'), 'referrer' => intval(I('referrer', 0)), 'nickname' => I('nickname'), 'avatar' => I('avatar'), 'province' => I('province'), 'city' => I('city'), 'gender' => intval(I('gender', 0)), - ); + ); - $where = [$type_field=>I('alipay_openid'), 'is_delete_time'=>0]; + $where = ['mobile'=>$data['mobile'], 'is_delete_time'=>0]; $temp = $m->where($where)->find(); if(empty($temp)) { @@ -87,97 +87,91 @@ class UserController extends CommonController $user_id = $temp['id']; } } - - if(isset($user_id) && $user_id > 0) - { - // 清除验证码 - $obj->Remove(); - - $this->ajaxReturn(L('common_bind_success'), 0, $m->find($user_id)); - } else { - $this->ajaxReturn(L('common_bind_error'), -100); - } - } - - /** - * [RegVerifySend 用户注册-验证码发送] - * @author Devil - * @blog http://gong.gg/ - * @version 0.0.1 - * @datetime 2017-03-05T19:17:10+0800 - */ - public function RegVerifySend() - { - // 账户校验 - $this->UserRegAccountsCheck(); - - // 验证码公共基础参数 - $verify_param = array( - 'key_prefix' => 'reg', - 'expire_time' => MyC('common_verify_expire_time'), - 'time_interval' => MyC('common_verify_time_interval'), - ); - - // 发送验证码 - $obj = new \Library\Sms($verify_param); - $code = GetNumberCode(6); - $status = $obj->SendCode(I('mobile'), $code, MyC('home_sms_user_reg')); - - // 状态 - if($status) - { - $this->ajaxReturn(L('common_send_success'), 0); - } else { - $this->ajaxReturn(L('common_send_error').'['.$obj->error.']', -100); - } - } - - /** - * [UserRegAccountsCheck 用户注册账户校验] - * @author Devil - * @blog http://gong.gg/ - * @version 0.0.1 - * @datetime 2017-03-10T10:06:29+0800 - */ - private function UserRegAccountsCheck() - { - // 参数 - $accounts = I('mobile'); - if(empty($accounts)) - { - $this->ajaxReturn(L('common_param_error'), -1); - } - - // 手机号码格式 - if(!CheckMobile($accounts)) - { - $this->ajaxReturn(L('common_mobile_format_error'), -2); - } - - // 手机号码是否已存在 - if($this->IsExistAccounts($accounts, 'mobile')) - { - $this->ajaxReturn(L('common_mobile_exist_error'), -3); - } - } - - /** - * [IsExistAccounts 账户是否存在] - * @author Devil - * @blog http://gong.gg/ - * @version 0.0.1 - * @datetime 2017-03-08T10:27:14+0800 - * @param [string] $accounts [账户名称] - * @param [string] $field [字段名称] - * @return [boolean] [存在true, 不存在false] - */ - private function IsExistAccounts($accounts, $field = 'mobile') - { - $id = M('User')->where(array('is_delete_time'=>0, $field=>$accounts))->getField('id'); - return !empty($id); - } - - /** + + if(isset($user_id) && $user_id > 0) + { + // 清除验证码 + $obj->Remove(); + + $this->ajaxReturn(L('common_bind_success'), 0, $m->find($user_id)); + } else { + $this->ajaxReturn(L('common_bind_error'), -100); + } + } + + /** + * [RegVerifySend 用户注册-验证码发送] + * @author Devil + * @blog http://gong.gg/ + * @version 0.0.1 + * @datetime 2017-03-05T19:17:10+0800 + */ + public function RegVerifySend() + { + // 账户校验 + $this->UserRegAccountsCheck(); + + // 验证码公共基础参数 + $verify_param = array( + 'key_prefix' => 'reg', + 'expire_time' => MyC('common_verify_expire_time'), + 'time_interval' => MyC('common_verify_time_interval'), + ); + + // 发送验证码 + $obj = new \Library\Sms($verify_param); + $code = GetNumberCode(6); + $status = $obj->SendCode(I('mobile'), $code, MyC('home_sms_user_reg')); + + // 状态 + if($status) + { + $this->ajaxReturn(L('common_send_success'), 0); + } else { + $this->ajaxReturn(L('common_send_error').'['.$obj->error.']', -100); + } + } + + /** + * [UserRegAccountsCheck 用户注册账户校验] + * @author Devil + * @blog http://gong.gg/ + * @version 0.0.1 + * @datetime 2017-03-10T10:06:29+0800 + */ + private function UserRegAccountsCheck() + { + // 参数 + $accounts = I('mobile'); + if(empty($accounts)) + { + $this->ajaxReturn(L('common_param_error'), -1); + } + + // 手机号码格式 + if(!CheckMobile($accounts)) + { + $this->ajaxReturn(L('common_mobile_format_error'), -2); + } + } + + /** + * [IsExistAccounts 账户是否存在] + * @author Devil + * @blog http://gong.gg/ + * @version 0.0.1 + * @datetime 2017-03-08T10:27:14+0800 + * @param [string] $accounts [账户名称] + * @param [string] $field [字段名称] + * @return [boolean] [存在true, 不存在false] + */ + private function IsExistAccounts($accounts, $field = 'mobile') + { + $id = M('User')->where(array('is_delete_time'=>0, $field=>$accounts))->getField('id'); + return !empty($id); + } + + /** * [GetAlipayUserInfo 获取支付宝用户信息] * @author Devil * @blog http://gong.gg/ @@ -186,38 +180,49 @@ class UserController extends CommonController */ public function GetAlipayUserInfo() { - $result = (new \Library\Alipay())->GetAlipayUserInfo(I('authcode'), C('alipay_mini_appid')); + $result = (new \Library\Alipay())->GetAlipayUserInfo(I('authcode'), MyC('common_app_mini_alipay_appid')); if($result === false) { $this->ajaxReturn('获取授权信息失败'); } else { - $data = [ - 'alipay_openid' => $result['user_id'], - 'nickname' => empty($result['nick_name']) ? '' : $result['nick_name'], - 'avatar' => empty($result['avatar']) ? '' : $result['avatar'], - 'gender' => empty($result['gender']) ? 0 : ($result['gender'] == 'm') ? 2 : 1, - 'province' => empty($result['province']) ? '' : $result['province'], - 'city' => empty($result['city']) ? '' : $result['city'], - 'referrer' => intval(I('referrer', 0)), - ]; - $m = M('User'); - $where = ['alipay_openid'=>$result['user_id'], 'is_delete_time'=>0]; - $user = $m->where($where)->find(); - if(!empty($user)) - { - $data['upd_time'] = time(); - if($m->where($where)->save($data) === false) - { - $this->ajaxReturn('用户更新失败'); - } - $data = $user; - } - - // 返回成功 - $this->ajaxReturn('授权成功', 0, $data); + $result['openid'] = $result['user_id']; + $this->AuthUserProgram($result, 'alipay_openid'); } } + /** + * 用户授权保存 + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @date 2018-11-06 + * @desc description + * @param [array] $params [用户数据] + * @param [string] $field [平台字段名称] + */ + private function AuthUserProgram($params, $field) + { + $data = [ + $field => $params['openid'], + 'nickname' => empty($params['nick_name']) ? '' : $params['nick_name'], + 'avatar' => empty($params['avatar']) ? '' : $params['avatar'], + 'gender' => empty($params['gender']) ? 0 : ($params['gender'] == 'm') ? 2 : 1, + 'province' => empty($params['province']) ? '' : $params['province'], + 'city' => empty($params['city']) ? '' : $params['city'], + 'referrer' => intval(I('referrer', 0)), + ]; + $m = M('User'); + $where = [$field=>$params['openid'], 'is_delete_time'=>0]; + $user = $m->where($where)->find(); + if(!empty($user)) + { + $data = $user; + } + + // 返回成功 + $this->ajaxReturn('授权成功', 0, $data); + } + /** * [ClientCenter 用户中心] * @author Devil @@ -243,5 +248,25 @@ class UserController extends CommonController // 返回数据 $this->ajaxReturn(L('common_operation_success'), 0, $result); } + + /** + * 百度小程序获取用户信息 + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @date 2018-11-06 + * @desc description + */ + public function GetBaiduUserInfo() + { + $_POST['config'] = C('baidu_mini_program_config'); + $result = (new \Library\BaiduAuth())->GetAuthUserInfo($_POST); + if($result['status'] == 0) + { + $this->AuthUserProgram($result['data'], 'baidu_openid'); + } else { + $this->ajaxReturn($result['msg']); + } + } } ?> \ No newline at end of file diff --git a/service/Application/Common/Conf/config.php b/service/Application/Common/Conf/config.php index 460e79ee5..a75f07ec6 100755 --- a/service/Application/Common/Conf/config.php +++ b/service/Application/Common/Conf/config.php @@ -124,9 +124,6 @@ return array( // 开启U带域名 'url_domain_deploy' => true, - - // 支付宝appid - 'alipay_mini_appid' => '2018071160553916', // 支付业务类型,支付插件根据业务类型自动生成支付入口文件 'payment_business_type_all' => [ diff --git a/service/Application/Library/Alipay.class.php b/service/Application/Library/Alipay.class.php index fdcd1dfac..cb1859779 100755 --- a/service/Application/Library/Alipay.class.php +++ b/service/Application/Library/Alipay.class.php @@ -47,7 +47,7 @@ class Alipay private function SoonPayApp($data, $config) { $parameter = array( - 'app_id' => C('alipay_mini_appid'), + 'app_id' => MyC('common_app_mini_alipay_appid'), 'method' => 'alipay.trade.app.pay', 'format' => 'JSON', 'charset' => 'utf-8', @@ -446,38 +446,51 @@ class Alipay } /** - * [SyncRsaVerify 同步返回签名验证] + * [MyRsaSign 签名字符串] * @author Devil * @blog http://gong.gg/ * @version 1.0.0 - * @datetime 2017-09-25T13:13:39+0800 - * @param [array] $data [返回数据] - * @param [boolean] $key [数据key] + * @datetime 2017-09-24T08:38:28+0800 + * @param [string] $prestr [需要签名的字符串] + * @return [string] [签名结果] */ - private function SyncRsaVerify($data, $key) + private function MyRsaSign($prestr) { - $string = json_encode($data[$key], JSON_UNESCAPED_UNICODE); - return $this->AlipayRsaVerify($string, $data['sign']); + $res = "-----BEGIN RSA PRIVATE KEY-----\n"; + $res .= wordwrap(MyC('common_app_mini_alipay_rsa_private'), 64, "\n", true); + $res .= "\n-----END RSA PRIVATE KEY-----"; + return openssl_sign($prestr, $sign, $res, OPENSSL_ALGO_SHA256) ? base64_encode($sign) : null; } /** - * [MyRsaSign 签名字符串] + * [MyRsaDecrypt RSA解密] * @author Devil * @blog http://gong.gg/ * @version 1.0.0 - * @datetime 2017-09-24T08:38:28+0800 - * @param [string] $prestr [需要签名的字符串] - * @return [string] [签名结果] + * @datetime 2017-09-24T09:12:06+0800 + * @param [string] $content [需要解密的内容,密文] + * @return [string] [解密后内容,明文] */ - private function MyRsaSign($prestr) + private function MyRsaDecrypt($content) { - $public_key = file_get_contents(ROOT_PATH.'Rsakeys/rsa_mini_private_key_pkcs8.pem'); - $pkeyid = openssl_pkey_get_private($public_key); - return openssl_sign($prestr, $sign, $pkeyid, OPENSSL_ALGO_SHA256) ? base64_encode($sign) : null; + $res = "-----BEGIN PUBLIC KEY-----\n"; + $res .= wordwrap(MyC('common_app_mini_alipay_rsa_public'), 64, "\n", true); + $res .= "\n-----END PUBLIC KEY-----"; + $res = openssl_get_privatekey($res); + $content = base64_decode($content); + $result = ''; + for($i=0; $iOutRsaVerify($string, $data['sign']); } /** @@ -582,7 +587,7 @@ class Alipay { // 请求参数 $params = [ - 'app_id' => C('alipay_mini_appid'), + 'app_id' => MyC('common_app_mini_alipay_appid'), 'method' => 'alipay.open.app.qrcode.create', 'charset' => 'utf-8', 'format' => 'JSON', diff --git a/service/Application/Library/BaiduAuth.class.php b/service/Application/Library/BaiduAuth.class.php new file mode 100644 index 000000000..7d0742f78 --- /dev/null +++ b/service/Application/Library/BaiduAuth.class.php @@ -0,0 +1,95 @@ +-1, 'msg'=>'授权码有误']; + } + if(empty($params['config'])) + { + return ['status'=>-1, 'msg'=>'配置有误']; + } + + $data = [ + 'code' => $params['authcode'], + 'client_id' => $params['config']['key'], + 'sk' => $params['config']['secret'], + ]; + $result = $this->HttpRequest('https://openapi.baidu.com/nalogin/getSessionKeyByCode', $data); + if(empty($result['openid'])) + { + return ['status'=>-1, 'msg'=>$result['error_description']]; + } + return ['status'=>0, 'msg'=>'授权成功', 'data'=>$result]; + } + + /** + * [HttpRequest 网络请求] + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @datetime 2017-09-25T09:10:46+0800 + * @param [string] $url [请求url] + * @param [array] $data [发送数据] + * @return [mixed] [请求返回数据] + */ + private function HttpRequest($url, $data) + { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_FAILONERROR, false); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + + $body_string = ''; + if(is_array($data) && 0 < count($data)) + { + foreach($data as $k => $v) + { + $body_string .= $k.'='.urlencode($v).'&'; + } + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $body_string); + } + $headers = array('content-type: application/x-www-form-urlencoded;charset=UTF-8'); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + $reponse = curl_exec($ch); + if(curl_errno($ch)) + { + return false; + } else { + $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + if(200 !== $httpStatusCode) + { + return false; + } + } + curl_close($ch); + return json_decode($reponse, true); + } + +} \ No newline at end of file -- GitLab