1.检测软件是否安装
2.授权获取code
3.获取token,openid等
4.获取头像昵称
var wx,qq,weibo;
var loginParam={};
apiready = function () {
fix_status_bar();
var user = $api.getStorage('user');
var customerId = user.customer_id;
var memberId = user.member_id;
// 获取第三方绑定信息
api.ajax({
url: BASE_REQUEST_URL+'/Customer/GetExternal',
method: 'post',
data: {
values: {
customerId: customerId,
memberId: memberId
}
}
}, function(json, err) {
if (json.result) {
var interText = doT.template($("#info_tmpl").text());
$("#info_area").html(interText(json.info));
} else {
var toast = new auiToast();
toast.fail({
title: json.msg,
duration: 2000
});
}
});
}
function initWxBind() {
wx = api.require('wx');
wx.isInstalled(function(ret, err) {
if (!ret.installed) {
alert('当前设备未安装微信客户端');
} else {
wx.auth(function(ret, err) {
if (ret.status) {
wx.getToken({
code: ret.code
}, function(ret, err) {
if (ret.status) {
api.showProgress({
style: 'default',
animationType: 'fade',
title: '授权成功',
text: '绑定处理中...',
modal: true
});
var provideName="wx_" + BASE_APP_TYPE;
//临时存储openId,与accessToken后面绑定账号时用到(绑定用完需要清理掉)
loginParam.provideName=provideName;
loginParam.openId=ret.openId;
loginParam.accessToken=ret.accessToken;
loginParam.refreshToken=ret.dynamicToken;
$api.setStorage('loginParam', loginParam);
//获取wx头像,昵称
wx.getUserInfo({
accessToken: ret.accessToken,
openId: ret.openId
}, function(ret, err) {
if (ret.status) {
// 绑定成功
var loginParam=$api.getStorage('loginParam');
loginParam.nickName=ret.nickname;
loginParam.avatar=ret.headimgurl;
$api.setStorage('loginParam', loginParam);
// 实现绑定动作
bindAccount(function (res) {
if (res == true) {
// 发送页面刷新事件
var toast = new auiToast();
toast.success({
title: "绑定成功",
duration: 2000
});
setTimeout("location.reload()", 2000);
}
});
api.hideProgress();
}
});
} else {
alert(JSON.stringify(err))
}
});
} else {
var toast = new auiToast();
toast.fail({
title: "微信授权绑定失败",
duration: 1500
});
}
});
}
});
}
function initQQBind() {
qq = api.require('qq');
qq.login(function(ret, err) {
if (ret.status) {
api.showProgress({
style: 'default',
animationType: 'fade',
title: '授权成功',
text: '绑定处理中...',
modal: true
});
var provideName="qq_" + BASE_APP_TYPE;
loginParam.provideName=provideName;
loginParam.openId=ret.openId;
loginParam.accessToken=ret.accessToken;
$api.setStorage('loginParam', loginParam);
//获取qq头像,昵称
qq.getUserInfo(function(ret, err) {
if (ret.status) {
// 绑定成功
var loginParam=$api.getStorage('loginParam');
loginParam.nickName=ret.info.nickname;
loginParam.avatar=ret.info.figureurl_qq_2;
$api.setStorage('loginParam', loginParam);
//实现绑定动作
bindAccount(function (res) {
if (res == true) {
// 发送页面刷新事件
var toast = new auiToast();
toast.success({
title: "绑定成功",
duration: 2000
});
setTimeout("location.reload()", 2000);
}
});
api.hideProgress();
}
});
} else {
var toast = new auiToast();
toast.fail({
title: "授权失败",
duration: 1500
});
}
});
}
function initWbBind() {
weibo = api.require('weibo');
weibo.auth(function(ret, err) {
//alert(JSON.stringify(ret));
if (ret.status) {
api.showProgress({
style: 'default',
animationType: 'fade',
title: '授权成功',
text: '绑定处理中...',
modal: true
});
var provideName="wb_" + BASE_APP_TYPE;
//临时存储openId,与accessToken后面绑定账号时用到(绑定用完需要清理掉)
loginParam.provideName=provideName;
loginParam.openId=ret.userId;
loginParam.accessToken=ret.token;
$api.setStorage('loginParam', loginParam);
//获取微博头像,昵称
weibo.getUserInfo(function(ret,err){
if (ret.status) {
// 绑定成功
var loginParam=$api.getStorage('loginParam');
loginParam.nickName=ret.userInfo.screen_name;
loginParam.avatar=ret.userInfo.avatar_large;
$api.setStorage('loginParam', loginParam);
// 实现绑定动作
bindAccount(function (res) {
if (res == true) {
// 发送页面刷新事件
var toast = new auiToast();
toast.success({
title: "绑定成功",
duration: 2000
});
setTimeout("location.reload()", 2000);
}
});
api.hideProgress();
}
});
}else {
var toast = new auiToast();
toast.fail({
title: "授权失败",
duration: 1500
});
}
});
}
// 解除绑定
function removeAuth(externalId) {
var user = $api.getStorage('user');
var customerId = user.customer_id;
// 获取第三方绑定信息
api.ajax({
url: BASE_REQUEST_URL+'/Customer/RemoveAuthentication',
method: 'post',
data: {
values: {
customerId: customerId,
externalId:externalId
}
}
}, function(json, err) {
if (json.result) {
var toast = new auiToast();
toast.success({
title: '解绑成功',
duration: 2000
});
setTimeout("location.reload();", 2000);
} else {
var toast = new auiToast();
toast.fail({
title: json.msg,
duration: 2000
});
}
});
}
function bindAccount(callback) {
var loginParam = $api.getStorage('loginParam');
var user = $api.getStorage('user');
api.ajax({
url: BASE_REQUEST_URL + '/Customer/AssociateAccountWithExistCustomer',
method: 'post',
data: {
values: {
provideName: loginParam.provideName,
openId: loginParam.openId,
accessToken: loginParam.accessToken,
refreshToken: loginParam.refreshToken,
userName: user.login_name,
nickName: loginParam.nickName,
avatar: loginParam.avatar,
}
}
}, function(ret, err) {
if (ret.status == 1) {
callback(true);
}
callback(false);
});
}
function showAction() {
var dialogBox = api.require('dialogBox');
dialogBox.actionMenu ({
tapClose: true, // 点击关闭
rect:{
h: 150
},
texts:{
cancel: '取消'
},
items:[
{
text: '绑定微信',
icon: 'widget://image/share_wx.png'
},
{
text: '绑定QQ',
icon: 'widget://image/share_qq.png'
},
{
text: '绑定微博',
icon: 'widget://image/share_wb.png'
},
],
styles:{
bg:'#FFF',
column: 3,
itemText: {
color: '#000',
size: 12,
marginT:8
},
itemIcon:{
size:50
},
cancel:{
bg: 'fs://icon.png',
color:'#000',
h: 44 ,
size: 14
}
}
}, function(ret){
if (ret.index == '0') { // 微信
initWxBind();
dialogBox.close();
}
if (ret.index == '1') { // QQ
initQQBind();
dialogBox.close();
}
if (ret.index == '2') { // 微博
initWbBind();
dialogBox.close();
}
if (ret.eventType == 'cancel') {
dialogBox.close();
}
});
}
确保已经有这些数据申请,申请要些时日。
<feature name="wx">
<param name="urlScheme" value="wx65fbcf8b5a4765cc"/>
<param name="apiKey" value="wx65fbcf8b5a4765cc"/>
<param name="apiSecret" value="6f9e7a1cb328745de623263c18773368"/>
</feature>
<feature name="qq">
<param name="urlScheme" value="tencent1105688283"/>
<param name="apiKey" value="1105688283"/>
</feature>
<feature name="weibo">
<param name="urlScheme" value="wb3537030359"/>
<param name="apiKey" value="3537030359"/>
<param name="registUrl" value="http://www.diandodo.com"/>
</feature>