公司小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.1 KiB

///格式化显示时间
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;
}
3 years ago
/**
* @param {Object} number 需要格式化的数字
* @param {Object} decimals 保留几位小鼠
* @param {Object} dec_point 小数点符号
* @param {Object} thousands_sep 千分位符号
*/
let formatMoney = function(number,decimals,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);
}
3 years ago
/**
* 微信检查登录状态是否过期如果过期需要要求用户重新授权
* 如果过期了返回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;
///缓存用户的昵称,头像地址,性别
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.showToast({
title:"获取用户信息失败,无法进行微信登录,请重试。"
});
return false;
}
var nickName = uni.getStorageSync(StoreKeys.WeChatNickName);
var gender = uni.getStorageSync(StoreKeys.WeChatGender);
var headUrl = uni.getStorageSync(StoreKeys.WeChatHeadUrl);
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;
var result = await uni.request({
url:urlstr,
data:{
code : codestr,
headimgurl : headUrl,
nickname : nickName,
sex : gender
},
method:'POST',
header:{
'content-type': 'application/json'
},
});
console.log('result',result);
return false;
}
export default{
3 years ago
///格式化时间
formatTime,
3 years ago
///格式化金额
3 years ago
formatMoney,
3 years ago
///检查微信登录是否过期
wechatCheckSession,
///获取用户信息,需要用户授权
wechatGetUserProfile,
///微信登录
wechatLogin,
}