diff --git a/application/api/controller/User.php b/application/api/controller/User.php index 3ce98073648e855b7bc96fb86561f82108ea97bb..3b8ed8350067ee0b76bfcd915d00b173ac443f0e 100755 --- a/application/api/controller/User.php +++ b/application/api/controller/User.php @@ -248,6 +248,77 @@ class User extends Common return DataReturn($result['msg'], -10); } + /** + * 头条小程序用户授权 + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @date 2019-10-27 + * @desc description + */ + public function ToutiaoUserAuth() + { + $this->data_post['config'] = [ + 'appid' => MyC('common_app_mini_toutiao_appid', 'tt65341389fa1e87f3'), + 'secret' => MyC('common_app_mini_toutiao_appsecret', '9ea496422c189390d2d3ec8eec597fbcf3e1e5a7'), + ]; + $result = (new \base\ToutiaoAuth())->GetAuthCode($this->data_post); + if($result['status'] == 0) + { + return DataReturn('授权登录成功', 0, $result['data']['openid']); + } + return DataReturn($result['msg'], -10); + } + + /** + * 头条小程序获取用户信息 + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @date 2019-10-27 + * @desc description + */ + public function ToutiaoUserInfo() + { + // 参数校验 + $p = [ + [ + 'checked_type' => 'empty', + 'key_name' => 'openid', + 'error_msg' => 'openid为空', + ], + [ + 'checked_type' => 'empty', + 'key_name' => 'userinfo', + 'error_msg' => '用户信息为空', + ], + ]; + $ret = ParamsChecked($this->data_post, $p); + if($ret !== true) + { + return DataReturn($ret, -1); + } + + // 先从数据库获取用户信息 + $user = UserService::AppUserInfoHandle(null, 'toutiao_openid', $this->data_post['openid']); + if(empty($user)) + { + $result = json_decode(htmlspecialchars_decode($this->data_post['userinfo']), true); + if(is_array($result)) + { + $result['nick_name'] = isset($result['nickName']) ? $result['nickName'] : ''; + $result['avatar'] = isset($result['avatarUrl']) ? $result['avatarUrl'] : ''; + $result['gender'] = empty($result['gender']) ? 0 : ($result['gender'] == 2) ? 1 : 2; + $result['openid'] = $this->data_post['openid']; + $result['referrer']= isset($this->data_post['referrer']) ? $this->data_post['referrer'] : 0; + return UserService::AuthUserProgram($result, 'toutiao_openid'); + } + } else { + return DataReturn('授权成功', 0, $user); + } + return DataReturn(empty($result) ? '获取用户信息失败' : $result, -100); + } + /** * [ClientCenter 用户中心] * @author Devil diff --git a/application/service/UserService.php b/application/service/UserService.php index a043e0a1512210c8522b3a1c3b5884eacf4f9801..442325d581313e9ab29157fdb3c73cdb3ff858f0 100755 --- a/application/service/UserService.php +++ b/application/service/UserService.php @@ -315,6 +315,7 @@ class UserService 'status' => intval($params['status']), 'alipay_openid' => isset($params['alipay_openid']) ? $params['alipay_openid'] : '', 'baidu_openid' => isset($params['baidu_openid']) ? $params['baidu_openid'] : '', + 'toutiao_openid' => isset($params['toutiao_openid']) ? $params['toutiao_openid'] : '', 'weixin_openid' => isset($params['weixin_openid']) ? $params['weixin_openid'] : '', 'weixin_unionid' => isset($params['weixin_unionid']) ? $params['weixin_unionid'] : '', 'weixin_web_openid' => isset($params['weixin_web_openid']) ? $params['weixin_web_openid'] : '', @@ -1696,7 +1697,7 @@ class UserService public static function AppUserInfoHandle($user_id = null, $where_field = null, $where_value = null, $user = []) { // 获取用户信息 - $field = 'id,username,nickname,mobile,email,avatar,alipay_openid,weixin_openid,weixin_unionid,weixin_web_openid,baidu_openid,integral,locking_integral'; + $field = 'id,username,nickname,mobile,email,avatar,alipay_openid,weixin_openid,weixin_unionid,weixin_web_openid,baidu_openid,toutiao_openid,integral,locking_integral'; if(!empty($user_id)) { $user = self::UserInfo('id', $user_id, $field); diff --git a/extend/base/ToutiaoAuth.php b/extend/base/ToutiaoAuth.php new file mode 100644 index 0000000000000000000000000000000000000000..e9c1d0a4c0b296867ce0bdb094b0a7287f38c45d --- /dev/null +++ b/extend/base/ToutiaoAuth.php @@ -0,0 +1,55 @@ +-1, 'msg'=>'授权码有误']; + } + if(empty($params['config'])) + { + return ['status'=>-1, 'msg'=>'配置有误']; + } + + // 获取授权 + $url = 'https://developer.toutiao.com/api/apps/jscode2session?appid='.$params['config']['appid'].'&secret='.$params['config']['secret'].'&code='.$params['authcode']; + $result = json_decode(file_get_contents($url), true); + if(empty($result['openid'])) + { + return ['status'=>-1, 'msg'=>$result['errmsg']]; + } + return ['status'=>0, 'msg'=>'授权成功', 'data'=>$result]; + } +} +?> \ No newline at end of file diff --git a/public/appmini/old/toutiao/app.js b/public/appmini/old/toutiao/app.js index 2d788c4ef3d6b1e9ecb75dc08ba56e9cb98c1947..d2c60ac7c59a2938e872758b941f195e4c5fdcad 100755 --- a/public/appmini/old/toutiao/app.js +++ b/public/appmini/old/toutiao/app.js @@ -67,7 +67,7 @@ App({ // 请求地址 request_url: "{{request_url}}", request_url: 'http://tp5-dev.com/', - request_url: 'https://test.shopxo.net/', + // request_url: 'https://test.shopxo.net/', // 基础信息 application_title: "{{application_title}}", @@ -145,7 +145,7 @@ App({ return ( this.data.request_url + "index.php?s=/" + m + "/" + c + "/" + a + - "&application=app&application_client_type=weixin" + + "&application=app&application_client_type=toutiao" + "&token=" + token + "&ajax=ajax" + @@ -235,62 +235,55 @@ App({ * object 回调操作对象 * method 回调操作对象的函数 * openid 用户openid - * auth_data 授权数据 */ - get_user_login_info(object, method, openid, auth_data) { - console.log('user-info'); + get_user_login_info(object, method, openid) { + var self = this; tt.getUserInfo({ success (res) { - console.log(`getUserInfo调用成功${res.userInfo}`); + // 邀请人参数 + var params = tt.getStorageSync(self.data.cache_launch_info_key) || null; + var referrer = (params == null) ? 0 : (params.referrer || 0); + + // 远程处理用户数据 + tt.request({ + url: self.get_request_url('toutiaouserinfo', 'user'), + method: 'POST', + data: { + "userinfo": res.rawData, + "openid": openid, + "referrer": referrer + }, + dataType: 'json', + header: { 'content-type': 'application/x-www-form-urlencoded' }, + success: (res) => { + tt.hideLoading(); + if (res.data.code == 0) { + tt.setStorage({ + key: self.data.cache_user_info_key, + data: res.data.data, + success: (res) => { + if (typeof object === 'object' && (method || null) != null) { + object[method](); + } + }, + fail: () => { + self.showToast('用户信息缓存失败'); + } + }); + } else { + self.showToast(res.data.msg); + } + }, + fail: () => { + tt.hideLoading(); + self.showToast('服务器请求出错'); + }, + }); }, fail (res) { app.showToast("获取用户授权信息失败"); } }); - - - return false; - // 邀请人参数 - var params = tt.getStorageSync(this.data.cache_launch_info_key) || null; - var referrer = (params == null) ? 0 : (params.referrer || 0); - - // 远程解密数据 - var self = this; - tt.request({ - url: self.get_request_url('wechatuserinfo', 'user'), - method: 'POST', - data: { - "encrypted_data": auth_data.encryptedData, - "iv": auth_data.iv, - "openid": openid, - "referrer": referrer - }, - dataType: 'json', - header: { 'content-type': 'application/x-www-form-urlencoded' }, - success: (res) => { - tt.hideLoading(); - if (res.data.code == 0) { - tt.setStorage({ - key: self.data.cache_user_info_key, - data: res.data.data, - success: (res) => { - if (typeof object === 'object' && (method || null) != null) { - object[method](); - } - }, - fail: () => { - self.showToast('用户信息缓存失败'); - } - }); - } else { - self.showToast(res.data.msg); - } - }, - fail: () => { - tt.hideLoading(); - self.showToast('服务器请求出错'); - }, - }); }, /** diff --git a/public/appmini/old/toutiao/pages/login/login.js b/public/appmini/old/toutiao/pages/login/login.js index bfe2a43ed39318410fc545bc03e5500890bdd6ed..b175d72e5a2816452d615ddd765650aca26b0ea0 100755 --- a/public/appmini/old/toutiao/pages/login/login.js +++ b/public/appmini/old/toutiao/pages/login/login.js @@ -139,16 +139,15 @@ Page({ var validation = [ {fields: 'mobile', msg: '请填写手机号码'}, {fields: 'verify', msg: '请填写验证码'}, - {fields: 'weixin_openid', msg: '授权id不能为空'} + {fields: 'toutiao_openid', msg: '授权id不能为空'} ]; - e.detail.value['weixin_openid'] = this.data.user.weixin_openid; + e.detail.value['toutiao_openid'] = this.data.user.toutiao_openid; e.detail.value['nickname'] = this.data.user.nickname; e.detail.value['avatar'] = this.data.user.avatar; e.detail.value['province'] = this.data.user.province; e.detail.value['city'] = this.data.user.city; e.detail.value['gender'] = this.data.user.gender; - e.detail.value['weixin_unionid'] = this.data.user.weixin_unionid || ''; - e.detail.value['app_type'] = 'weixin'; + e.detail.value['app_type'] = 'toutiao'; e.detail.value['referrer'] = (params == null) ? (this.data.user.referrer || 0) : (params.referrer || 0); if(app.fields_check(e.detail.value, validation)) {