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; ///缓存用户的昵称,头像地址,性别 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; uni.showLoading({ title:'登陆中...', }) const result = await loginWeChat({ code : codestr, headimgurl : headUrl, nickname : nickName, sex : gender }); console.log('result',result); uni.hideLoading(); if(result.length <= 1){ uni.showToast({ title:'登录失败', }); 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; } export default{ ///格式化时间 formatTime, ///格式化金额 formatMoney, ///检查微信登录是否过期 wechatCheckSession, ///获取用户信息,需要用户授权 wechatGetUserProfile, ///微信登录 wechatLogin, ///如果本地有token,那么使用token进行登录 loginWithToken, }