|
|
|
|
|
|
|
import {loginWeChat,post} from './api.js'
|
|
|
|
///格式化显示时间
|
|
|
|
let formatTime = function(time,format = 'YY.MM.DD hh:mm'){
|
|
|
|
let date = new Date(time * 1000);
|
|
|
|
let year = date.getFullYear();
|
|
|
|
let mouth = date.getMonth() + 1;
|
|
|
|
let day = date.getDay();
|
|
|
|
let hour = date.getHours();
|
|
|
|
let min = date.getMinutes();
|
|
|
|
//sec = date.getSeconds(),
|
|
|
|
let preArr = Array.apply(null,Array(10)).map(function(elem,index){
|
|
|
|
return '0' + index;
|
|
|
|
});
|
|
|
|
let newTime = format.replace(/YY/g,year).replace(/MM/g,preArr[mouth] || mouth)
|
|
|
|
.replace(/DD/g,preArr[day] || day)
|
|
|
|
.replace(/hh/g,preArr[hour] || hour)
|
|
|
|
.replace(/mm/g,preArr[min] || min);
|
|
|
|
return newTime;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param {Object} number 需要格式化的数字
|
|
|
|
* @param {Object} decimals 保留几位小鼠
|
|
|
|
* @param {Object} dec_point 小数点符号
|
|
|
|
* @param {Object} thousands_sep 千分位符号
|
|
|
|
*/
|
|
|
|
let formatMoney = function(number,decimals = 2,dec_point = '.',thousands_sep = ','){
|
|
|
|
number = (number + '').replace(/[^0-9+-Ee.]/g, '');
|
|
|
|
var n = !isFinite(+number) ? 0 : +number,
|
|
|
|
|
|
|
|
prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
|
|
|
|
sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
|
|
|
|
dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
|
|
|
|
s = '',
|
|
|
|
toFixedFix = function (n, prec) {
|
|
|
|
var k = Math.pow(10, prec);
|
|
|
|
return '' + Math.floor(n * k) / k;
|
|
|
|
};
|
|
|
|
s = (prec ? toFixedFix(n, prec) : '' + Math.floor(n)).split('.');
|
|
|
|
var re = /(-?\d+)(\d{3})/;
|
|
|
|
//console.log(s)
|
|
|
|
while (re.test(s[0])) {
|
|
|
|
s[0] = s[0].replace(re, "$1" + sep + "$2");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((s[1] || '').length < prec) {
|
|
|
|
s[1] = s[1] || '';
|
|
|
|
s[1] += new Array(prec - s[1].length + 1).join('0');
|
|
|
|
}
|
|
|
|
return s.join(dec);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 微信检查登录状态是否过期,如果过期,需要要求用户重新授权
|
|
|
|
* 如果过期了返回true,反则返回false
|
|
|
|
*/
|
|
|
|
let wechatCheckSession = async function(){
|
|
|
|
var infoRes = await uni.checkSession({});
|
|
|
|
console.log(infoRes);
|
|
|
|
return infoRes.length <= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取使用者的微信信息,例如昵称,头像
|
|
|
|
*/
|
|
|
|
let wechatGetUserProfile = async function(){
|
|
|
|
var infoRes = await uni.getUserProfile({
|
|
|
|
desc:'用于显示您的头像和昵称。',
|
|
|
|
lang:'zh_CN',
|
|
|
|
});
|
|
|
|
var len = infoRes.length;
|
|
|
|
if(len == 2){
|
|
|
|
//成功了
|
|
|
|
var Define = getApp().globalData.Define;
|
|
|
|
var StoreKeys = Define.StoreKeys;
|
|
|
|
var nk = StoreKeys.WeChatNickName;
|
|
|
|
var hk = StoreKeys.WeChatHeadUrl;
|
|
|
|
var gk = StoreKeys.WeChatGender;
|
|
|
|
var info = infoRes[1];
|
|
|
|
var userInfo = info.userInfo;
|
|
|
|
console.log('获取到微信用户信息:',info);
|
|
|
|
///缓存用户的昵称,头像地址,性别
|
|
|
|
uni.setStorageSync(nk,userInfo.nickName);
|
|
|
|
uni.setStorageSync(hk,userInfo.avatarUrl);
|
|
|
|
uni.setStorageSync(gk,userInfo.gender);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 用户微信登录
|
|
|
|
*/
|
|
|
|
let wechatLogin = async function(){
|
|
|
|
//首先检查本地是否有Token
|
|
|
|
var Define = getApp().globalData.Define;
|
|
|
|
var StoreKeys = Define.StoreKeys;
|
|
|
|
var token = uni.getStorageSync(StoreKeys.WeChatLoginToken);
|
|
|
|
if(token){
|
|
|
|
//如果存在这个token,证明是登录了的,我就不需要在登录
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
var success = await wechatGetUserProfile();
|
|
|
|
if(!success){
|
|
|
|
//如果用户拒绝授权,给与提示信息
|
|
|
|
uni.$u.toast("获取用户信息失败,无法进行微信登录,请重试。");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var nickName = uni.getStorageSync(StoreKeys.WeChatNickName);
|
|
|
|
var gender = uni.getStorageSync(StoreKeys.WeChatGender);
|
|
|
|
var headUrl = uni.getStorageSync(StoreKeys.WeChatHeadUrl);
|
|
|
|
console.log("进行微信登录:",gender);
|
|
|
|
var info = await uni.login({
|
|
|
|
provider:'weixin',
|
|
|
|
});
|
|
|
|
console.log("微信登录结果:",info);
|
|
|
|
if(info.length <= 1){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var codeArr = info[1];
|
|
|
|
//这个code需要丢给服务器去进行一次验证
|
|
|
|
var codestr = codeArr.code;
|
|
|
|
var urlstr = Define.ServerInterfaces.WeChatLogin;
|
|
|
|
uni.showLoading({
|
|
|
|
title:'登陆中...',
|
|
|
|
})
|
|
|
|
var dInfo = {
|
|
|
|
code : codestr,
|
|
|
|
headimgurl : headUrl,
|
|
|
|
nickname : nickName,
|
|
|
|
sex : gender
|
|
|
|
};
|
|
|
|
console.log("登录:",dInfo);
|
|
|
|
const result = await loginWeChat(dInfo);
|
|
|
|
console.log('result',result);
|
|
|
|
uni.hideLoading();
|
|
|
|
if(result.length <= 1){
|
|
|
|
uni.$u.toast("登录失败");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
info = result[1];
|
|
|
|
var code = info.data.code;
|
|
|
|
if(code != 0){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var userdata = info.data.data.userdata;
|
|
|
|
var Player = getApp().globalData.Player;
|
|
|
|
Player.InitPlayerWithData(userdata);
|
|
|
|
var token = Player.user_token;
|
|
|
|
console.log("缓存当前的token",token);
|
|
|
|
//将这个token保存起来
|
|
|
|
uni.setStorageSync(StoreKeys.WeChatLoginToken,token);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 使用token进行登录
|
|
|
|
*/
|
|
|
|
let loginWithToken = async function(token,Player){
|
|
|
|
var res = await post('users/myselfinfo',{});
|
|
|
|
//console.log("token登录返回:",res);
|
|
|
|
if(res.length <= 1){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let infos = res[1];
|
|
|
|
console.log('使用token进行了登录:',infos);
|
|
|
|
var code = infos.data.code;
|
|
|
|
if(code != 0){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var userdata = infos.data.data.userdata;
|
|
|
|
Player.InitPlayerWithData(userdata);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 获取协议条款内容
|
|
|
|
*/
|
|
|
|
let getAgreementData = async function(){
|
|
|
|
let res = await post("pages/getcontractannex",{});
|
|
|
|
var Define = getApp().globalData.Define;
|
|
|
|
var data = Define.onNetMessage(res);
|
|
|
|
if(data == null){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var agreementData = getApp().globalData.agreementData;
|
|
|
|
var contract = data.contract;
|
|
|
|
for(var key in contract){
|
|
|
|
agreementData[key] = contract[key];
|
|
|
|
}
|
|
|
|
console.log('获取到合同信息:',agreementData);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
let random = function(lower,upper){
|
|
|
|
return Math.floor(Math.random() * (upper - lower)) + lower;
|
|
|
|
}
|
|
|
|
|
|
|
|
let plusXing = function(str,frontLen,endLen){
|
|
|
|
var len = str.length-frontLen-endLen;
|
|
|
|
var xing = '';
|
|
|
|
for (var i=0;i<len;i++) {
|
|
|
|
xing+='*';
|
|
|
|
}
|
|
|
|
return str.substring(0,frontLen)+xing+str.substring(str.length-endLen);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 微信获取手机号码的返回
|
|
|
|
* @param {Object} e
|
|
|
|
*/
|
|
|
|
let wechatGetPlayerPhoneNumberBack = async function(e){
|
|
|
|
console.log("获取手机号码的返回:",e);
|
|
|
|
var code = e.detail.code;
|
|
|
|
if(!code){
|
|
|
|
uni.$u.toast('获取手机号码失败');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
uni.showLoading();
|
|
|
|
var res = await post("users/wechatbindmobile",{code : code});
|
|
|
|
uni.hideLoading();
|
|
|
|
if(res.length <= 1){
|
|
|
|
//获取失败
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var info = res[1];
|
|
|
|
var code = info.data.code;
|
|
|
|
if(code != 0){
|
|
|
|
//有问题,返回。
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var data = info.data.data;
|
|
|
|
console.log(data);
|
|
|
|
var phonenumber = data.phonenumber;
|
|
|
|
return phonenumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default{
|
|
|
|
///格式化时间
|
|
|
|
formatTime,
|
|
|
|
///格式化金额
|
|
|
|
formatMoney,
|
|
|
|
///检查微信登录是否过期
|
|
|
|
wechatCheckSession,
|
|
|
|
///获取用户信息,需要用户授权
|
|
|
|
wechatGetUserProfile,
|
|
|
|
///微信登录
|
|
|
|
wechatLogin,
|
|
|
|
///如果本地有token,那么使用token进行登录
|
|
|
|
loginWithToken,
|
|
|
|
///从服务器获取合作协议内容
|
|
|
|
getAgreementData,
|
|
|
|
///把一个字符串中间的字符变成*
|
|
|
|
plusXing,
|
|
|
|
///产生随机整数,包含下限值,但不包括上限值
|
|
|
|
random,
|
|
|
|
///微信获取手机号的回调
|
|
|
|
wechatGetPlayerPhoneNumberBack,
|
|
|
|
}
|