提交 ac9b2812 编写于 作者: D Devil

头条小程序用户绑定

上级 13e79871
......@@ -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
......
......@@ -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);
......
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace base;
/**
* 头条用户授权驱动
* @author Devil
* @version V_1.0.0
*/
class ToutiaoAuth
{
/**
* [__construct 构造方法]
*/
public function __construct(){}
/**
* 用户授权
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-10-27
* @desc description
* @param [array] $params [输入参数]
*/
public function GetAuthCode($params = [])
{
if(empty($params['authcode']))
{
return ['status'=>-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
......@@ -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('服务器请求出错');
},
});
},
/**
......
......@@ -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))
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册