百度小程序优化

This commit is contained in:
devil_gong 2019-11-29 20:53:08 +08:00
parent bf5a58a7f9
commit 5fb0af4166
30 changed files with 849 additions and 470 deletions

View File

@ -242,19 +242,76 @@ class User extends Common
*/
public function BaiduUserAuth()
{
$this->data_post['config'] = [
'id' => MyC('common_app_mini_baidu_appid'),
'key' => MyC('common_app_mini_baidu_appkey'),
'secret' => MyC('common_app_mini_baidu_appsecret'),
];
$result = (new \base\BaiduAuth())->GetAuthUserInfo($this->data_post);
$result = (new \base\BaiduAuth(MyC('common_app_mini_baidu_appid'), MyC('common_app_mini_baidu_appkey'), MyC('common_app_mini_baidu_appsecret')))->GetAuthSessionKey($this->data_post);
if($result['status'] == 0)
{
return UserService::AuthUserProgram($result['data'], 'baidu_openid');
// 先从数据库获取用户信息
$user = UserService::AppUserInfoHandle(null, 'baidu_openid', $result);
if(empty($user))
{
return DataReturn('授权登录成功', 0, ['is_alipay_user_exist'=>0, 'openid'=>$result['data']]);
}
$user['is_alipay_user_exist'] = 1;
return DataReturn('授权登录成功', 0, $user);
}
return DataReturn($result['msg'], -10);
}
/**
* 百度小程序获取用户信息
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-11-06
* @desc description
*/
public function BaiduUserInfo()
{
// 参数校验
$p = [
[
'checked_type' => 'empty',
'key_name' => 'openid',
'error_msg' => 'openid为空',
],
[
'checked_type' => 'empty',
'key_name' => 'encrypted_data',
'error_msg' => '解密数据为空',
],
[
'checked_type' => 'empty',
'key_name' => 'iv',
'error_msg' => 'iv为空,请重试',
]
];
$ret = ParamsChecked($this->data_post, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 先从数据库获取用户信息
$user = UserService::AppUserInfoHandle(null, 'baidu_openid', $this->data_post['openid']);
if(empty($user))
{
$result = (new \base\BaiduAuth(MyC('common_app_mini_baidu_appid'), MyC('common_app_mini_baidu_appkey'), MyC('common_app_mini_baidu_appsecret')))->DecryptData($this->data_post['encrypted_data'], $this->data_post['iv'], $this->data_post['openid']);
if($result['status'] == 0 && !empty($result['data']))
{
$result['nick_name'] = isset($result['data']['nickname']) ? $result['data']['nickname'] : '';
$result['avatar'] = isset($result['data']['headimgurl']) ? $result['data']['headimgurl'] : '';
$result['gender'] = empty($result['data']['sex']) ? 0 : ($result['data']['sex'] == 2) ? 1 : 2;
$result['openid'] = $result['data']['openid'];
$result['referrer']= isset($this->data_post['referrer']) ? $this->data_post['referrer'] : 0;
return UserService::AuthUserProgram($result, 'baidu_openid');
}
} else {
return DataReturn('授权成功', 0, $user);
}
return DataReturn(empty($result) ? '获取用户信息失败' : $result, -100);
}
/**
* 头条小程序用户授权
* @author Devil

View File

@ -17,10 +17,103 @@ namespace base;
*/
class BaiduAuth
{
// appid
private $_appid;
// appkey
private $_appkey;
// appsecret
private $_appsecret;
/**
* [__construct 构造方法]
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2017-12-30T18:04:05+0800
* @param [string] $app_id [应用appid]
* @param [string] $_appkey [应用key]
* @param [string] $app_secret [应用密钥]
*/
public function __construct(){}
public function __construct($app_id, $app_key, $app_secret)
{
$this->_appid = $app_id;
$this->_appkey = $app_key;
$this->_appsecret = $app_secret;
}
/**
* [DecryptData 检验数据的真实性,并且获取解密后的明文]
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2017-12-30T18:20:53+0800
* @param [string] $encrypted_data [加密的用户数据]
* @param [string] $iv [与用户数据一同返回的初始向量]
* @param [string] $openid [解密后的原文]
* @return [array|string] [成功返回用户信息数组, 失败返回错误信息]
*/
public function DecryptData($encrypted_data, $iv, $openid)
{
// 登录授权session
$login_key = 'baidu_user_login_'.$openid;
$session_data = GS($login_key);
if($session_data === false)
{
return ['status'=>-1, 'msg'=>'session key不存在'];
}
// iv长度
if(strlen($iv) != 24)
{
return ['status'=>-1, 'msg'=>'iv长度错误'];
}
// 数据解密
$session_key = base64_decode($session_data['session_key']);
$iv = base64_decode($iv);
$encrypted_data = base64_decode($encrypted_data);
$plaintext = false;
if (function_exists("openssl_decrypt")) {
$plaintext = openssl_decrypt($encrypted_data, "AES-192-CBC", $session_key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
} else {
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, null, MCRYPT_MODE_CBC, null);
mcrypt_generic_init($td, $session_key, $iv);
$plaintext = mdecrypt_generic($td, $encrypted_data);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
}
if ($plaintext == false) {
return ['status'=>-1, 'msg'=>'解密失败'];
}
// trim pkcs#7 padding
$pad = ord(substr($plaintext, -1));
$pad = ($pad < 1 || $pad > 32) ? 0 : $pad;
$plaintext = substr($plaintext, 0, strlen($plaintext) - $pad);
// trim header
$plaintext = substr($plaintext, 16);
// get content length
$unpack = unpack("Nlen/", substr($plaintext, 0, 4));
// get content
$data = json_decode(substr($plaintext, 4, $unpack['len']), true);
// get app_key
$app_key_decode = substr($plaintext, $unpack['len'] + 4);
if($app_key_decode != $this->_appkey)
{
return ['status'=>-1, 'msg'=>'appkey不匹配'];
}
// 缓存存储
$data_key = 'baidu_user_info_'.$openid;
SS($data_key, $data);
return ['status'=>0, 'data'=>$data];
}
/**
* 用户授权
@ -31,28 +124,29 @@ class BaiduAuth
* @desc description
* @param [array] $params [输入参数]
*/
public function GetAuthUserInfo($params = [])
public function GetAuthSessionKey($params = [])
{
if(empty($params['authcode']))
{
return ['status'=>-1, 'msg'=>'授权码有误'];
}
if(empty($params['config']))
{
return ['status'=>-1, 'msg'=>'配置有误'];
}
$data = [
'code' => $params['authcode'],
'client_id' => $params['config']['key'],
'sk' => $params['config']['secret'],
'client_id' => $this->_appkey,
'sk' => $this->_appsecret,
];
$result = $this->HttpRequest('https://spapi.baidu.com/oauth/jscode2sessionkey', $data);
if(empty($result['openid']))
if(!empty($result['openid']))
{
return ['status'=>-1, 'msg'=>$result['error_description']];
// 缓存SessionKey
$key = 'baidu_user_login_'.$result['openid'];
// 缓存存储
SS($key, $result);
return ['status'=>0, 'msg'=>'授权成功', 'data'=>$result['openid']];
}
return ['status'=>0, 'msg'=>'授权成功', 'data'=>$result];
return ['status'=>-1, 'msg'=>$result['error_description']];
}
/**

View File

@ -74,14 +74,13 @@ class Wechat
return 'openssl不支持';
}
$aes_cipher = base64_decode($encrypted_data);
$result = openssl_decrypt($aes_cipher, "AES-128-CBC", base64_decode($session_data['session_key']), 1, base64_decode($iv));
$result = openssl_decrypt(base64_decode($encrypted_data), "AES-128-CBC", base64_decode($session_data['session_key']), 1, base64_decode($iv));
$data = json_decode($result, true);
if($data == NULL)
{
return '请重试!';
}
if($data['watermark']['appid'] != $this->_appid )
if($data['watermark']['appid'] != $this->_appid)
{
return 'appid不匹配';
}
@ -109,7 +108,7 @@ class Wechat
$result = $this->HttpRequestGet($url);
if(!empty($result['openid']))
{
// 从缓存获取用户信息
// 缓存SessionKey
$key = 'wechat_user_login_'.$result['openid'];
// 缓存存储

View File

@ -63,7 +63,7 @@ App({
// 请求地址
request_url: "{{request_url}}",
request_url: 'http://tp5-dev.com/',
request_url: 'https://dev.shopxo.net/',
//request_url: 'https://dev.shopxo.net/',
// 基础信息
application_title: "{{application_title}}",
@ -170,6 +170,24 @@ App({
return this.data.request_url + "index.php?s=/" + m + "/" + c + "/" + a + "&application=app&application_client_type=baidu" + "&token=" + token + "&ajax=ajax" + params;
},
/**
* 获取用户信息,信息不存在则唤醒授权
* object 回调操作对象
* method 回调操作对象的函数
* return 有用户数据直接返回, 则回调调用者
*/
get_user_info(object, method) {
var user = this.get_user_cache_info();
if (user == false) {
// 唤醒用户授权
this.user_login(object, method);
return false;
} else {
return user;
}
},
/**
* 从缓存获取用户信息
*/
@ -185,27 +203,24 @@ App({
* 用户登录
* object 回调操作对象
* method 回调操作对象的函数
* auth_data 授权数据
*/
user_auth_login(object, method) {
user_auth_login(object, method, auth_data) {
var self = this;
// 请求授权接口
swan.authorize({
scope: "scope.userInfo",
success: res => {
swan.checkSession({
success: function (res) {
self.user_login(object, method);
},
fail: function (err) {
self.user_login(object, method);
}
});
swan.checkSession({
success: function () {
var openid = swan.getStorageSync(self.data.cache_user_login_key) || null;
if (openid == null)
{
self.user_login(object, method);
} else {
self.get_user_login_info(object, method, openid, auth_data);
}
},
fail: e => {
self.showToast("调用授权失败");
fail: function () {
self.user_login(object, method);
}
});
},
/**
@ -214,62 +229,136 @@ App({
* method 回调操作对象的函数
*/
user_login(object, method) {
var self = this;
// 加载loding
swan.showLoading({ title: "授权中..." });
var openid = swan.getStorageSync(this.data.cache_user_login_key) || null;
if (openid == null)
{
var self = this;
// 加载loding
swan.showLoading({ title: "授权中..." });
swan.login({
success: function (res) {
swan.request({
url: self.get_request_url("baiduuserauth", "user"),
method: "POST",
data: {
authcode: res.code
},
header: {'content-type': 'application/x-www-form-urlencoded'},
dataType: "json",
success: res => {
swan.hideLoading();
if (res.data.code == 0) {
var data = res.data.data;
if ((data.is_alipay_user_exist || 0) == 1) {
swan.setStorage({
key: self.data.cache_user_info_key,
data: data,
success: (res) => {
if (typeof object === 'object' && (method || null) != null) {
object[method]();
}
},
fail: () => {
self.showToast('用户信息缓存失败');
}
});
} else {
swan.setStorage({
key: self.data.cache_user_login_key,
data: data.openid
});
self.login_to_auth();
}
} else {
swan.hideLoading();
self.showToast(res.data.msg);
}
},
fail: () => {
swan.hideLoading();
self.showToast("服务器请求出错");
}
});
},
fail: function (err) {
swan.hideLoading();
self.showToast(err);
}
});
} else {
this.login_to_auth();
}
},
/**
* 跳转到登录页面授权
*/
login_to_auth() {
swan.showModal({
title: '温馨提示',
content: '授权用户信息',
confirmText: '确认',
cancelText: '暂不',
success: (result) => {
if (result.confirm) {
swan.navigateTo({
url: "/pages/login/login"
});
}
}
});
},
/**
* 获取用户授权信息
* object 回调操作对象
* method 回调操作对象的函数
* openid 用户openid
* auth_data 授权数据
*/
get_user_login_info(object, method, openid, auth_data) {
// 邀请人参数
var params = swan.getStorageSync(this.data.cache_launch_info_key) || null;
var referrer = (params == null) ? 0 : (params.referrer || 0);
// 请求登录
swan.login({
success: function (res) {
swan.request({
url: self.get_request_url("baiduuserauth", "user"),
method: "POST",
data: {
authcode: res.code,
referrer: referrer
},
header: {'content-type': 'application/x-www-form-urlencoded'},
dataType: "json",
success: res => {
swan.hideLoading();
if (res.data.code == 0) {
swan.getUserInfo({
success: function (user) {
res.data.data['nickname'] = user.userInfo.nickName;
res.data.data['avatar'] = user.userInfo.avatarUrl;
res.data.data['gender'] = parseInt(user.userInfo.gender)+1;
res.data.data['referrer'] = referrer;
swan.setStorage({
key: self.data.cache_user_info_key,
data: res.data.data
});
if (typeof object === "object" && (method || null) != null) {
object[method]();
}
},
fail: function(res) {
self.showToast("调用用户信息授权失败");
}
});
} else {
self.showToast(res.data.msg);
}
},
fail: () => {
swan.hideLoading();
self.showToast("服务器请求出错");
}
});
// 远程解密数据
swan.showLoading({ title: "授权中..." });
var self = this;
swan.request({
url: self.get_request_url('baiduuserinfo', 'user'),
method: 'POST',
data: {
"encrypted_data": auth_data.encryptedData,
"iv": auth_data.iv,
"openid": openid,
"referrer": referrer
},
fail: function (err) {
dataType: 'json',
header: { 'content-type': 'application/x-www-form-urlencoded' },
success: (res) => {
swan.hideLoading();
self.showToast(err);
}
if (res.data.code == 0) {
swan.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: () => {
swan.hideLoading();
self.showToast('服务器请求出错');
},
});
},
@ -505,19 +594,7 @@ App({
if(res.code == -400)
{
swan.clearStorage();
swan.showModal({
title: '温馨提示',
content: '授权用户信息',
confirmText: '确认',
cancelText: '暂不',
success: (result) => {
if (result.confirm) {
swan.navigateTo({
url: "/pages/login/login?event_callback=init"
});
}
},
});
this.get_user_info(object, method);
return false;
}
return true;

View File

@ -13,13 +13,15 @@ Page({
// 初始化
init() {
var user = app.get_user_cache_info(this, "init");
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
var user = app.get_user_info(this, "init");
if (user != false) {
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
}
}
},

View File

@ -75,7 +75,7 @@ Page({
this.setData({
data_list_loding_status: 0
});
if (app.is_login_check(res.data)) {
if (app.is_login_check(res.data, this, 'get_data_list')) {
app.showToast(res.data.msg);
}
}

View File

@ -124,7 +124,7 @@ Page({
data_list_loding_status: 2,
data_list_loding_msg: res.data.msg
});
if (app.is_login_check(res.data)) {
if (app.is_login_check(res.data, this, 'init')) {
app.showToast(res.data.msg);
}
}

View File

@ -22,31 +22,39 @@ Page({
},
init(e) {
var user = app.get_user_cache_info(this, "init");
// 用户未绑定用户则转到登录页面
var msg = user == false ? '授权用户信息' : '绑定手机号码';
if (app.user_is_need_login(user)) {
swan.showModal({
title: '温馨提示',
content: msg,
confirmText: '确认',
cancelText: '暂不',
success: result => {
if (result.confirm) {
swan.navigateTo({
url: "/pages/login/login?event_callback=init"
});
} else {
this.setData({
data_list_loding_status: 0,
data_bottom_line_status: false,
data_list_loding_msg: '请先' + msg
});
}
}
});
var user = app.get_user_info(this, "init");
if (user != false) {
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.showModal({
title: '温馨提示',
content: msg,
confirmText: '确认',
cancelText: '暂不',
success: (result) => {
if (result.confirm) {
swan.navigateTo({
url: "/pages/login/login?event_callback=init"
});
} else {
this.setData({
data_list_loding_status: 0,
data_bottom_line_status: false,
data_list_loding_msg: '请先' + msg,
});
}
},
});
} else {
this.get_data();
}
} else {
this.get_data();
swan.stopPullDownRefresh();
this.setData({
data_list_loding_status: 0,
data_bottom_line_status: false,
data_list_loding_msg: '请先授权用户信息',
});
}
},
@ -87,7 +95,7 @@ Page({
data_bottom_line_status: false,
data_list_loding_msg: res.data.msg
});
if (app.is_login_check(res.data)) {
if (app.is_login_check(res.data, this, 'get_data')) {
app.showToast(res.data.msg);
}
}

View File

@ -5,7 +5,11 @@ Page({
data_list_loding_status: 1,
data_list_loding_msg: '',
data_list: [],
data_base: null
data_base: null,
// 优惠劵领取
temp_coupon_receive_index: null,
temp_coupon_receive_value: null,
},
onLoad(params) {
@ -61,9 +65,7 @@ Page({
data_list_loding_status: 2,
data_list_loding_msg: res.data.msg
});
if (app.is_login_check(res.data)) {
app.showToast(res.data.msg);
}
app.showToast(res.data.msg);
}
},
fail: () => {
@ -81,44 +83,58 @@ Page({
// 优惠劵领取事件
coupon_receive_event(e) {
var user = app.get_user_cache_info(this, "coupon_receive_event");
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=coupon_receive_event"
});
return false;
// 参数处理
if((e || null) == null)
{
var index = this.data.temp_coupon_receive_index;
var value = this.data.temp_coupon_receive_value;
} else {
var self = this;
var index = e.currentTarget.dataset.index;
var value = e.currentTarget.dataset.value;
var temp_list = this.data.data_list;
if (temp_list[index]['is_operable'] != 0) {
swan.showLoading({ title: "处理中..." });
swan.request({
url: app.get_request_url("receive", "coupon"),
method: "POST",
data: { "coupon_id": value },
dataType: "json",
header: { 'content-type': 'application/x-www-form-urlencoded' },
success: res => {
swan.hideLoading();
if (res.data.code == 0) {
app.showToast(res.data.msg, "success");
if (self.data.data_base != null && self.data.data_base.is_repeat_receive != 1) {
temp_list[index]['is_operable'] = 0;
temp_list[index]['is_operable_name'] = '已领取';
self.setData({ data_list: temp_list });
}
} else {
app.showToast(res.data.msg);
}
},
fail: () => {
swan.hideLoading();
app.showToast("服务器请求出错");
}
this.setData({temp_coupon_receive_index: index, temp_coupon_receive_value: value});
}
// 登录校验
var user = app.get_user_info(this, 'coupon_receive_event');
if (user != false) {
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
wx.navigateTo({
url: "/pages/login/login?event_callback=coupon_receive_event"
});
return false;
} else {
var self = this;
var temp_list = this.data.data_list;
if (temp_list[index]['is_operable'] != 0) {
swan.showLoading({ title: "处理中..." });
swan.request({
url: app.get_request_url("receive", "coupon"),
method: "POST",
data: { "coupon_id": value },
dataType: "json",
header: { 'content-type': 'application/x-www-form-urlencoded' },
success: res => {
swan.hideLoading();
if (res.data.code == 0) {
app.showToast(res.data.msg, "success");
if (self.data.data_base != null && self.data.data_base.is_repeat_receive != 1) {
temp_list[index]['is_operable'] = 0;
temp_list[index]['is_operable_name'] = '已领取';
self.setData({ data_list: temp_list });
}
} else {
if (app.is_login_check(res.data, self, 'coupon_receive_event')) {
app.showToast(res.data.msg);
}
}
},
fail: () => {
swan.hideLoading();
app.showToast("服务器请求出错");
}
});
}
}
}
},

View File

@ -19,16 +19,23 @@ Page({
// 初始化
init() {
var user = app.get_user_cache_info(this, "init");
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
var user = app.get_user_info(this, "init");
if (user != false) {
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
} else {
// 获取数据
this.get_data_list();
}
} else {
// 获取数据
this.get_data_list();
this.setData({
data_list_loding_status: 0,
data_bottom_line_status: false,
});
}
},
@ -75,7 +82,7 @@ Page({
this.setData({
data_list_loding_status: 0
});
if (app.is_login_check(res.data)) {
if (app.is_login_check(res.data, this, 'get_data_list')) {
app.showToast(res.data.msg);
}
}

View File

@ -125,8 +125,8 @@ Page({
this.setData({
data_list_loding_status: 0
});
if (app.is_login_check(res.data)) {
app.showToast(res.data.msg);
if (app.is_login_check(res.data, this, 'get_data_list')) {
app.showToast(res.data.msg);
}
}
},

View File

@ -53,6 +53,10 @@ Page({
// 站点类型
common_site_type: 0,
customer_service_tel: null,
// 优惠劵领取
temp_coupon_receive_index: null,
temp_coupon_receive_value: null,
},
onLoad(params) {
@ -251,79 +255,83 @@ Page({
// 收藏事件
goods_favor_event(e) {
var user = app.get_user_cache_info(this, 'goods_favor_event');
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.navigateTo({
url: "/pages/login/login?event_callback=init"
});
return false;
} else {
swan.showLoading({ title: '处理中...' });
var user = app.get_user_info(this, 'goods_favor_event');
if (user != false) {
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
wx.navigateTo({
url: "/pages/login/login?event_callback=goods_favor_event"
});
return false;
} else {
swan.showLoading({ title: '处理中...' });
swan.request({
url: app.get_request_url('favor', 'goods'),
method: 'POST',
data: { "id": this.data.goods.id },
dataType: 'json',
success: res => {
swan.hideLoading();
if (res.data.code == 0) {
this.setData({
'goods.is_favor': res.data.data.status,
goods_favor_text: res.data.data.text,
goods_favor_icon: '/images/goods-detail-favor-icon-'+res.data.data.status+'.png'
});
app.showToast(res.data.msg, "success");
} else {
if (app.is_login_check(res.data)) {
swan.request({
url: app.get_request_url('favor', 'goods'),
method: 'POST',
data: { "id": this.data.goods.id },
dataType: 'json',
success: res => {
swan.hideLoading();
if (res.data.code == 0) {
this.setData({
'goods.is_favor': res.data.data.status,
goods_favor_text: res.data.data.text,
goods_favor_icon: '/images/goods-detail-favor-icon-'+res.data.data.status+'.png'
});
app.showToast(res.data.msg, "success");
} else {
if (app.is_login_check(res.data, this, 'goods_favor_event')) {
app.showToast(res.data.msg);
}
}
}
},
fail: () => {
swan.hideLoading();
},
fail: () => {
swan.hideLoading();
app.showToast('服务器请求出错');
}
});
app.showToast('服务器请求出错');
}
});
}
}
},
// 加入购物车事件
goods_cart_event(e, spec) {
var user = app.get_user_cache_info(this, 'goods_buy_confirm_event');
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.navigateTo({
url: "/pages/login/login?event_callback=init"
});
return false;
} else {
swan.showLoading({ title: '处理中...' });
swan.request({
url: app.get_request_url('save', 'cart'),
method: 'POST',
data: { "goods_id": this.data.goods.id, "stock": this.data.temp_buy_number, "spec": JSON.stringify(spec) },
dataType: 'json',
success: res => {
swan.hideLoading();
if (res.data.code == 0) {
this.setData({ quick_nav_cart_count: res.data.data});
this.popup_close_event();
app.showToast(res.data.msg, "success");
} else {
if (app.is_login_check(res.data)) {
var user = app.get_user_info(this, 'goods_buy_confirm_event');
if (user != false) {
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
wx.navigateTo({
url: "/pages/login/login?event_callback=goods_buy_confirm_event"
});
return false;
} else {
swan.showLoading({ title: '处理中...' });
swan.request({
url: app.get_request_url('save', 'cart'),
method: 'POST',
data: { "goods_id": this.data.goods.id, "stock": this.data.temp_buy_number, "spec": JSON.stringify(spec) },
dataType: 'json',
success: res => {
swan.hideLoading();
if (res.data.code == 0) {
this.setData({ quick_nav_cart_count: res.data.data});
this.popup_close_event();
app.showToast(res.data.msg, "success");
} else {
if (app.is_login_check(res.data, this, 'goods_buy_confirm_event')) {
app.showToast(res.data.msg);
}
}
}
},
fail: () => {
swan.hideLoading();
},
fail: () => {
swan.hideLoading();
app.showToast('服务器请求出错');
}
});
app.showToast('服务器请求出错');
}
});
}
}
},
@ -424,9 +432,7 @@ Page({
this.setData({ goods_specifications_choose: temp_data });
}
} else {
if (app.is_login_check(res.data)) {
app.showToast(res.data.msg);
}
app.showToast(res.data.msg);
}
},
fail: () => {
@ -476,9 +482,7 @@ Page({
goods_spec_base_inventory: res.data.data.inventory
});
} else {
if (app.is_login_check(res.data)) {
app.showToast(res.data.msg);
}
app.showToast(res.data.msg);
}
},
fail: () => {
@ -531,57 +535,59 @@ Page({
// 确认
goods_buy_confirm_event(e) {
var user = app.get_user_cache_info(this, 'goods_buy_confirm_event');
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.navigateTo({
url: "/pages/login/login?event_callback=init"
});
return false;
} else {
// 属性
var temp_data = this.data.goods_specifications_choose;
var sku_count = temp_data.length;
var active_count = 0;
var spec = [];
if (sku_count > 0) {
for (var i in temp_data) {
for (var k in temp_data[i]['value']) {
if ((temp_data[i]['value'][k]['is_active'] || null) != null) {
active_count++;
spec.push({ "type": temp_data[i]['name'], "value": temp_data[i]['value'][k]['name'] });
var user = app.get_user_info(this, 'goods_buy_confirm_event');
if (user != false) {
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
wx.navigateTo({
url: "/pages/login/login?event_callback=goods_buy_confirm_event"
});
return false;
} else {
// 属性
var temp_data = this.data.goods_specifications_choose;
var sku_count = temp_data.length;
var active_count = 0;
var spec = [];
if (sku_count > 0) {
for (var i in temp_data) {
for (var k in temp_data[i]['value']) {
if ((temp_data[i]['value'][k]['is_active'] || null) != null) {
active_count++;
spec.push({ "type": temp_data[i]['name'], "value": temp_data[i]['value'][k]['name'] });
}
}
}
if (active_count < sku_count) {
app.showToast('请选择属性');
return false;
}
}
if (active_count < sku_count) {
app.showToast('请选择属性');
return false;
// 操作类型
switch (this.data.buy_event_type) {
case 'buy':
// 进入订单确认页面
var data = {
"buy_type": "goods",
"goods_id": this.data.goods.id,
"stock": this.data.temp_buy_number,
"spec": JSON.stringify(spec)
};
swan.navigateTo({
url: '/pages/buy/buy?data=' + JSON.stringify(data)
});
this.popup_close_event();
break;
case 'cart':
this.goods_cart_event(e, spec);
break;
default:
app.showToast("操作事件类型有误");
}
}
// 操作类型
switch (this.data.buy_event_type) {
case 'buy':
// 进入订单确认页面
var data = {
"buy_type": "goods",
"goods_id": this.data.goods.id,
"stock": this.data.temp_buy_number,
"spec": JSON.stringify(spec)
};
swan.navigateTo({
url: '/pages/buy/buy?data=' + JSON.stringify(data)
});
this.popup_close_event();
break;
case 'cart':
this.goods_cart_event(e, spec);
break;
default:
app.showToast("操作事件类型有误");
}
}
},
@ -698,44 +704,58 @@ Page({
// 优惠劵领取事件
coupon_receive_event(e) {
var user = app.get_user_cache_info(this, "coupon_receive_event");
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=coupon_receive_event"
});
return false;
// 参数处理
if((e || null) == null)
{
var index = this.data.temp_coupon_receive_index;
var value = this.data.temp_coupon_receive_value;
} else {
var self = this;
var index = e.currentTarget.dataset.index;
var value = e.currentTarget.dataset.value;
var temp_list = this.data.plugins_coupon_data.data;
if (temp_list[index]['is_operable'] != 0) {
swan.showLoading({ title: "处理中..." });
swan.request({
url: app.get_request_url("receive", "coupon"),
method: "POST",
data: { "coupon_id": value },
dataType: "json",
header: { 'content-type': 'application/x-www-form-urlencoded' },
success: res => {
swan.hideLoading();
if (res.data.code == 0) {
app.showToast(res.data.msg, "success");
if (self.data.plugins_coupon_data.base != null && self.data.plugins_coupon_data.base.is_repeat_receive != 1) {
temp_list[index]['is_operable'] = 0;
temp_list[index]['is_operable_name'] = '已领取';
self.setData({ 'plugins_coupon_data.data': temp_list });
}
} else {
app.showToast(res.data.msg);
}
},
fail: () => {
swan.hideLoading();
app.showToast("服务器请求出错");
}
this.setData({temp_coupon_receive_index: index, temp_coupon_receive_value: value});
}
// 登录校验
var user = app.get_user_info(this, 'coupon_receive_event');
if (user != false) {
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
wx.navigateTo({
url: "/pages/login/login?event_callback=coupon_receive_event"
});
return false;
} else {
var self = this;
var temp_list = this.data.plugins_coupon_data.data;
if (temp_list[index]['is_operable'] != 0) {
swan.showLoading({ title: "处理中..." });
swan.request({
url: app.get_request_url("receive", "coupon"),
method: "POST",
data: { "coupon_id": value },
dataType: "json",
header: { 'content-type': 'application/x-www-form-urlencoded' },
success: res => {
swan.hideLoading();
if (res.data.code == 0) {
app.showToast(res.data.msg, "success");
if (self.data.plugins_coupon_data.base != null && self.data.plugins_coupon_data.base.is_repeat_receive != 1) {
temp_list[index]['is_operable'] = 0;
temp_list[index]['is_operable_name'] = '已领取';
self.setData({ 'plugins_coupon_data.data': temp_list });
}
} else {
if (app.is_login_check(res.data, self, 'coupon_receive_event')) {
app.showToast(res.data.msg);
}
}
},
fail: () => {
swan.hideLoading();
app.showToast("服务器请求出错");
}
});
}
}
}
},
@ -747,7 +767,7 @@ Page({
// 自定义分享
onShareAppMessage() {
var user = app.get_user_cache_info(this, 'onShareAppMessage') || null;
var user = app.get_user_cache_info() || null;
var user_id = (user != null && (user.id || null) != null) ? user.id : 0;
return {
title: app.data.application_title + '-' + this.data.goods.title,

View File

@ -29,8 +29,21 @@ Page({
/**
* 登录授权事件
*/
get_user_info_event() {
app.user_auth_login(this, 'user_auth_back_event');
get_user_info_event(e) {
this.user_auth_code(e.detail);
},
/**
* 用户授权
* auth_data 授权数据
*/
user_auth_code(auth_data) {
console.log(auth_data)
if ((auth_data.encryptedData || null) != null && (auth_data.iv || null) != null) {
app.user_auth_login(this, 'user_auth_back_event', auth_data);
} else {
app.showToast("授权失败");
}
},
/**

View File

@ -11,5 +11,5 @@
<view s-if="user == null" class="user-login tc">
<view class="cr-888 fs-12">确认登录授权,为您提供更优质的服务</view>
<button type="primary" size="mini" bindtap="get_user_info_event">授权登录</button>
<button type="primary" size="mini" open-type="getUserInfo" bindgetuserinfo="get_user_info_event">授权登录</button>
</view>

View File

@ -14,16 +14,23 @@ Page({
},
init() {
var user = app.get_user_cache_info(this, "init");
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
var user = app.get_user_info(this, "init");
if (user != false) {
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
} else {
// 获取数据
this.get_data_list();
}
} else {
// 获取数据
this.get_data_list();
this.setData({
data_list_loding_status: 0,
data_bottom_line_status: false,
});
}
},
@ -88,7 +95,7 @@ Page({
this.setData({
data_list_loding_status: 0
});
if (app.is_login_check(res.data)) {
if (app.is_login_check(res.data, this, 'get_data_list')) {
app.showToast(res.data.msg);
}
}

View File

@ -36,21 +36,33 @@ Page({
},
init() {
var user = app.get_user_cache_info(this, "init");
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
} else {
// 获取地址数据
if ((this.data.params.id || null) != null) {
this.get_user_address();
}
var user = app.get_user_info(this, "init");
if (user != false) {
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
this.setData({
data_list_loding_status: 2,
data_list_loding_msg: '请先绑定手机号码',
});
return false;
} else {
// 获取地址数据
if((this.data.params.id || null) != null)
{
this.get_user_address();
}
// 获取省
this.get_province_list();
// 获取省
this.get_province_list();
}
} else {
this.setData({
data_list_loding_status: 2,
data_list_loding_msg: '请先授权用户信息',
});
}
},
@ -282,7 +294,11 @@ Page({
swan.navigateBack();
}, 1000);
} else {
app.showToast(res.data.msg);
if (app.is_login_check(res.data)) {
app.showToast(res.data.msg);
} else {
app.showToast('提交失败,请重试!');
}
}
},
fail: () => {

View File

@ -19,16 +19,23 @@ Page({
// 初始化
init() {
var user = app.get_user_cache_info(this, "init");
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
var user = app.get_user_info(this, "init");
if (user != false) {
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
} else {
// 获取数据
this.get_data_list();
}
} else {
// 获取数据
this.get_data_list();
this.setData({
data_list_loding_status: 0,
data_bottom_line_status: false,
});
}
},
@ -75,7 +82,7 @@ Page({
this.setData({
data_list_loding_status: 0
});
if (app.is_login_check(res.data)) {
if (app.is_login_check(res.data, this, 'get_data_list')) {
app.showToast(res.data.msg);
}
}

View File

@ -16,16 +16,23 @@ Page({
},
init() {
var user = app.get_user_cache_info(this, "init");
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
var user = app.get_user_info(this, "init");
if (user != false) {
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
} else {
// 获取数据
this.get_data_list();
}
} else {
// 获取数据
this.get_data_list();
this.setData({
data_list_loding_status: 0,
data_bottom_line_status: false,
});
}
},
@ -90,7 +97,7 @@ Page({
this.setData({
data_list_loding_status: 0
});
if (app.is_login_check(res.data)) {
if (app.is_login_check(res.data, this, 'get_data_list')) {
app.showToast(res.data.msg);
}
}

View File

@ -19,16 +19,23 @@ Page({
},
init() {
var user = app.get_user_cache_info(this, "init");
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
var user = app.get_user_info(this, "init");
if (user != false) {
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
} else {
// 获取数据
this.get_data_list();
}
} else {
// 获取数据
this.get_data_list();
this.setData({
data_list_loding_status: 0,
data_bottom_line_status: false,
});
}
},
@ -62,7 +69,7 @@ Page({
data_list_loding_status: 2,
data_list_loding_msg: res.data.msg
});
if (app.is_login_check(res.data)) {
if (app.is_login_check(res.data, self, 'get_data_list')) {
app.showToast(res.data.msg);
}
}

View File

@ -14,16 +14,23 @@ Page({
},
init() {
var user = app.get_user_cache_info(this, "init");
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
var user = app.get_user_info(this, "init");
if (user != false) {
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
} else {
// 获取数据
this.get_data_list();
}
} else {
// 获取数据
this.get_data_list();
this.setData({
data_list_loding_status: 0,
data_bottom_line_status: false,
});
}
},
@ -88,7 +95,7 @@ Page({
this.setData({
data_list_loding_status: 0
});
if (app.is_login_check(res.data)) {
if (app.is_login_check(res.data, this, 'get_data_list')) {
app.showToast(res.data.msg);
}
}

View File

@ -14,16 +14,23 @@ Page({
},
init() {
var user = app.get_user_cache_info(this, "init");
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
var user = app.get_user_info(this, "init");
if (user != false) {
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
} else {
// 获取数据
this.get_data_list();
}
} else {
// 获取数据
this.get_data_list();
this.setData({
data_list_loding_status: 0,
data_bottom_line_status: false,
});
}
},
@ -88,7 +95,7 @@ Page({
this.setData({
data_list_loding_status: 0
});
if (app.is_login_check(res.data)) {
if (app.is_login_check(res.data, this, 'get_data_list')) {
app.showToast(res.data.msg);
}
}

View File

@ -14,16 +14,23 @@ Page({
},
init() {
var user = app.get_user_cache_info(this, "init");
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
var user = app.get_user_info(this, "init");
if (user != false) {
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
} else {
// 获取数据
this.get_data_list();
}
} else {
// 获取数据
this.get_data_list();
this.setData({
data_list_loding_status: 0,
data_bottom_line_status: false,
});
}
},
@ -88,7 +95,7 @@ Page({
this.setData({
data_list_loding_status: 0
});
if (app.is_login_check(res.data)) {
if (app.is_login_check(res.data, this, 'get_data_list')) {
app.showToast(res.data.msg);
}
}

View File

@ -56,7 +56,7 @@ Page({
data_list_loding_status: 2,
data_list_loding_msg: res.data.msg
});
if (app.is_login_check(res.data)) {
if (app.is_login_check(res.data, self, 'init')) {
app.showToast(res.data.msg);
}
}

View File

@ -76,7 +76,7 @@ Page({
data_bottom_line_status: false,
data_list_loding_msg: res.data.msg
});
if (app.is_login_check(res.data)) {
if (app.is_login_check(res.data, self, 'init')) {
app.showToast(res.data.msg);
}
}

View File

@ -42,16 +42,23 @@ Page({
},
init() {
var user = app.get_user_cache_info(this, "init");
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
var user = app.get_user_info(this, 'init');
if (user != false) {
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
} else {
// 获取数据
this.get_data_list();
}
} else {
// 获取数据
this.get_data_list();
this.setData({
data_list_loding_status: 0,
data_bottom_line_status: false,
});
}
},
@ -144,7 +151,7 @@ Page({
data_list_loding_status: 0,
load_status: 1
});
if (app.is_login_check(res.data)) {
if (app.is_login_check(res.data, this, 'get_data_list')) {
app.showToast(res.data.msg);
}
}

View File

@ -146,7 +146,7 @@ Page({
data_bottom_line_status: false,
data_list_loding_msg: res.data.msg
});
if (app.is_login_check(res.data)) {
if (app.is_login_check(res.data, self, 'init')) {
app.showToast(res.data.msg);
}
}

View File

@ -43,16 +43,23 @@ Page({
},
init() {
var user = app.get_user_cache_info(this, "init");
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
var user = app.get_user_info(this, "init");
if (user != false) {
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.redirectTo({
url: "/pages/login/login?event_callback=init"
});
return false;
} else {
// 获取数据
this.get_data_list();
}
} else {
// 获取数据
this.get_data_list();
this.setData({
data_list_loding_status: 0,
data_bottom_line_status: false,
});
}
},
@ -126,7 +133,7 @@ Page({
data_list_loding_status: 0,
data_list_loding_msg: res.data.msg
});
if (app.is_login_check(res.data)) {
if (app.is_login_check(res.data, this, 'get_data_list')) {
app.showToast(res.data.msg);
}
}

View File

@ -38,31 +38,32 @@ Page({
},
init(e) {
var user = app.get_user_cache_info(this, "init"),
self = this;
// 用户未绑定用户则转到登录页面
var msg = user == false ? '授权用户信息' : '绑定手机号码';
if (app.user_is_need_login(user)) {
swan.showModal({
title: '温馨提示',
content: msg,
confirmText: '确认',
cancelText: '暂不',
success: result => {
swan.stopPullDownRefresh();
if (result.confirm) {
swan.navigateTo({
url: "/pages/login/login?event_callback=init"
var user = app.get_user_info(this, "init"),
self = this;
if (user != false) {
// 用户未绑定用户则转到登录页面
if (app.user_is_need_login(user)) {
swan.showModal({
title: '温馨提示',
content: '绑定手机号码',
confirmText: '确认',
cancelText: '暂不',
success: (result) => {
swan.stopPullDownRefresh();
if(result.confirm) {
swan.navigateTo({
url: "/pages/login/login?event_callback=init"
});
}
self.setData({
avatar: user.avatar || app.data.default_user_head_src,
nickname: user.user_name_view || '用户名',
});
}
self.setData({
avatar: user.avatar || app.data.default_user_head_src,
nickname: user.user_name_view || '用户名'
});
}
});
} else {
self.get_data();
},
});
} else {
self.get_data();
}
}
},
@ -112,7 +113,8 @@ Page({
common_app_is_head_vice_nav: data.common_app_is_head_vice_nav || 0,
});
} else {
if (app.is_login_check(res.data)) {
if(app.is_login_check(res.data, this, 'get_data'))
{
app.showToast(res.data.msg);
}
}

View File

@ -187,6 +187,7 @@ App({
* 用户登录
* object 回调操作对象
* method 回调操作对象的函数
* auth_data 授权数据
*/
user_auth_login(object, method, auth_data) {
var self = this;

View File

@ -302,7 +302,11 @@ Page({
wx.navigateBack();
}, 1000);
} else {
app.showToast(res.data.msg);
if (app.is_login_check(res.data)) {
app.showToast(res.data.msg);
} else {
app.showToast('提交失败,请重试!');
}
}
},
fail: () => {