|
|
import { LoginService } from './loginService.js'
|
|
|
|
|
|
const login = new LoginService().yohoApi();
|
|
|
|
|
|
export const loginAction = (callBack) => {
|
|
|
return wx.login({
|
|
|
success(res) {
|
|
|
if (res.code) {
|
|
|
login.checkLogin(res.code).then((data) => {
|
|
|
// 获取 srd_session
|
|
|
const loginData = data;
|
|
|
handleLoginData(loginData);
|
|
|
callBack(null, data);
|
|
|
}).catch((error) => {
|
|
|
callBack(error);
|
|
|
});
|
|
|
}
|
|
|
},
|
|
|
fail(error) {
|
|
|
callBack(error);
|
|
|
}
|
|
|
})
|
|
|
};
|
|
|
|
|
|
const handleLoginData = (loginData) => {
|
|
|
// 上报接口返回的结果,线上debug用
|
|
|
// 缓存 srd_session
|
|
|
setStorageWithValueForKey('srd_session', loginData.srd_session);
|
|
|
// 缓存 openid
|
|
|
setStorageWithValueForKey('openid', loginData.openid);
|
|
|
// 如果 unionid 不存在(未使用过任何有货微信产品的全新用户),调用getUnionID函数,再次获取unionID
|
|
|
if (loginData.unionid) {
|
|
|
// 对于已经授权过的用户,拿到unionid之后获取一次userinfo更新个人信息
|
|
|
setStorageWithValueForKey('unionid', loginData.unionid);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 判断微信用户是否绑定
|
|
|
* @param srd_session
|
|
|
* @param result
|
|
|
* @returns {Promise.}
|
|
|
*/
|
|
|
export const decodeUnionId = (srd_session, result) => {
|
|
|
let res = result.detail;
|
|
|
// 存储 userInfo
|
|
|
if (res && res.userInfo) {
|
|
|
setStorageWithValueForKey('userInfo', res.userInfo);
|
|
|
} else {
|
|
|
return;
|
|
|
}
|
|
|
const nickName = res.userInfo.nickName;
|
|
|
const avatarUrl = res.userInfo.avatarUrl;
|
|
|
if (!srd_session) return;
|
|
|
return login.getUnionID(srd_session, res.encryptedData, res.iv).then((data) => {
|
|
|
let union_id;
|
|
|
// 本地保存 union_id
|
|
|
if (data.union_id) {
|
|
|
setStorageWithValueForKey('union_id', data.union_id);
|
|
|
union_id = data.union_id;
|
|
|
} else {
|
|
|
union_id = wx.getStorageSync('union_id');
|
|
|
}
|
|
|
if (!union_id) {
|
|
|
throw new Error('union_id is null');
|
|
|
}
|
|
|
// 网络上传头像,union_id,昵称
|
|
|
login.sendWeChatUserDataWithUnionId(data.union_id, nickName, avatarUrl).catch(error => {});
|
|
|
return {
|
|
|
union_id: union_id,
|
|
|
userInfo: res.userInfo
|
|
|
};
|
|
|
});
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 判断微信用户是否绑定
|
|
|
* @param unionID
|
|
|
* @param userInfo
|
|
|
* @returns {Promise.}
|
|
|
*/
|
|
|
export const wechatUserIsBind = (union_id, userInfo) => {
|
|
|
return login.wechatUserIsBind(union_id, userInfo.nickName).then(data => {
|
|
|
if (!isStringEmpty(data.is_bind) && data.is_bind === "Y") {
|
|
|
const newUserInfo = {
|
|
|
is_bind: data.is_bind,
|
|
|
mobile: data.mobile,
|
|
|
session_key: data.session_key,
|
|
|
uid: data.uid,
|
|
|
ssouid: data.ssouid
|
|
|
};
|
|
|
const assignUserInfo = Object.assign(newUserInfo, userInfo);
|
|
|
setStorageWithValueForKey('userInfo', assignUserInfo);
|
|
|
|
|
|
return Promise.resolve({
|
|
|
code: 10003,
|
|
|
data: userInfo,
|
|
|
message: '微信用户已绑定手机号'
|
|
|
});
|
|
|
} else {
|
|
|
return Promise.resolve({
|
|
|
code: 10004,
|
|
|
message: '微信用户未绑定手机号'
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
const setStorageWithValueForKey = (key, value) => {
|
|
|
const globalData = getGlobalData();
|
|
|
wx.setStorage({
|
|
|
key: key,
|
|
|
data: value,
|
|
|
})
|
|
|
globalData[key] = value;
|
|
|
}
|
|
|
|
|
|
const getStorageWithValueForKey = (key) => {
|
|
|
wx.getStorage({
|
|
|
key: key,
|
|
|
success: function(res) {
|
|
|
|
|
|
},
|
|
|
})
|
|
|
}
|
|
|
|
|
|
const getGlobalData = () => {
|
|
|
const app = getApp();
|
|
|
let globalData;
|
|
|
if (app && app.globalData) {
|
|
|
// 原生小程序的 globalData 获取方式
|
|
|
globalData = app && app.globalData;
|
|
|
} else {
|
|
|
// Taro 下 globalData 获取方式
|
|
|
globalData = app && app.props && app.props.globalData;
|
|
|
}
|
|
|
return globalData;
|
|
|
}
|
|
|
|
|
|
const isStringEmpty = (str) => {
|
|
|
if (str === undefined || str === null || str === '') {
|
|
|
return true
|
|
|
} else {
|
|
|
return false
|
|
|
}
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|