123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 |
- var api = require('./api');
- function formatTime(date) {
- var year = date.getFullYear()
- var month = date.getMonth() + 1
- var day = date.getDate()
- var hour = date.getHours()
- var minute = date.getMinutes()
- var second = date.getSeconds()
- return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
- }
- function formatTime_1(fmt, date) {
- date = new Date(date)
- let ret;
- const opt = {
- "Y+": date.getFullYear().toString(), // 年
- "m+": (date.getMonth() + 1).toString(), // 月
- "d+": date.getDate().toString(), // 日
- "H+": date.getHours().toString(), // 时
- "M+": date.getMinutes().toString(), // 分
- "S+": date.getSeconds().toString(), // 秒
- // 有其他格式化字符需求可以继续添加,必须转化成字符串
- };
- for (let k in opt) {
- ret = new RegExp("(" + k + ")").exec(fmt);
- if (ret) {
- fmt = fmt.replace(
- ret[1],
- ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, "0")
- );
- }
- }
- return fmt;
- }
- const formatNumber = n => {
- n = n.toString()
- return n[1] ? n : '0' + n
- }
- function formatTimeNum(number, format) {
- var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
- var returnArr = [];
- var date = new Date(number * 1000);
- returnArr.push(date.getFullYear());
- returnArr.push(formatNumber(date.getMonth() + 1));
- returnArr.push(formatNumber(date.getDate()));
- returnArr.push(formatNumber(date.getHours()));
- returnArr.push(formatNumber(date.getMinutes()));
- returnArr.push(formatNumber(date.getSeconds()));
- for (var i in returnArr) {
- format = format.replace(formateArr[i], returnArr[i]);
- }
- return format;
- }
- function testMobile(num) {
- console.log
- var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(16[0-9]{1})|(19[0-9]{1}))+\d{8})$/;
- if (num.length == 0) {
- wx.showToast({
- title: '手机号为空',
- image: '/images/icon/icon_error.png',
- })
- return false;
- } else if (num.length < 11) {
- wx.showToast({
- title: '手机号长度有误!',
- image: '/images/icon/icon_error.png',
- })
- return false;
- } else if (!myreg.test(num)) {
- wx.showToast({
- title: '手机号有误!',
- image: '/images/icon/icon_error.png',
- })
- return false;
- } else {
- return true;
- }
- }
- /**
- * 封封微信的的request
- */
- function request(url, data = {}, method = "GET") {
- return new Promise(function (resolve, reject) {
- wx.request({
- url: url,
- data: data,
- method: method,
- header: {
- 'Content-Type': 'application/json',
- 'X-Nideshop-Token': wx.getStorageSync('token')
- },
- success: function (res) {
- if (res.statusCode == 200) {
- if (res.data.errno == 401) {
- //需要登录后才可以操作
- let code = null;
- return login().then((res) => {
- code = res.code;
- return getUserInfo();
- }).then((userInfo) => {
- //登录远程服务器
- request(api.AuthLoginByWeixin, {
- code: code,
- userInfo: userInfo
- }, 'POST').then(res => {
- if (res.errno === 0) {
- //存储用户信息
- wx.setStorageSync('userInfo', res.data.userInfo);
- wx.setStorageSync('token', res.data.token);
- resolve(res);
- } else {
- reject(res);
- }
- }).catch((err) => {
- reject(err);
- });
- }).catch((err) => {
- reject(err);
- })
- } else {
- resolve(res.data);
- }
- } else {
- reject(res.errMsg);
- }
- },
- fail: function (err) {
- reject(err)
- }
- })
- });
- }
- /**
- * 检查微信会话是否过期
- */
- function checkSession() {
- return new Promise(function (resolve, reject) {
- wx.checkSession({
- success: function () {
- resolve(true);
- },
- fail: function () {
- reject(false);
- }
- })
- });
- }
- /**
- * 调用微信登录
- */
- function login() {
- return new Promise(function (resolve, reject) {
- wx.login({
- success: function (res) {
- if (res.code) {
- //登录远程服务器
- resolve(res);
- } else {
- reject(res);
- }
- },
- fail: function (err) {
- reject(err);
- }
- });
- });
- }
- function getUserInfo() {
- return new Promise(function (resolve, reject) {
- wx.getUserInfo({
- withCredentials: true,
- success: function (res) {
- resolve(res);
- },
- fail: function (err) {
- reject(err);
- }
- })
- });
- }
- function redirect(url) {
- //判断页面是否需要登录
- if (false) {
- } else {
- wx.redirectTo({
- url: url
- });
- }
- }
- function showErrorToast(msg) {
- wx.showToast({
- title: msg,
- icon: 'none',
- })
- }
- function showSuccessToast(msg) {
- wx.showToast({
- title: msg,
- icon: 'success',
- })
- }
- function sentRes(url, data, method, fn) {
- data = data || null;
- if (data == null) {
- var content = require('querystring').stringify(data);
- } else {
- var content = JSON.stringify(data); //json format
- }
- var parse_u = require('url').parse(url, true);
- var isHttp = parse_u.protocol == 'http:';
- var options = {
- host: parse_u.hostname,
- port: parse_u.port || (isHttp ? 80 : 443),
- path: parse_u.path,
- method: method,
- headers: {
- 'Content-Type': 'application/json',
- 'Content-Length': Buffer.byteLength(content, "utf8"),
- 'Trackingmore-Api-Key': '1b70c67e-d191-4301-9c05-a50436a2526d'
- }
- };
- var req = require(isHttp ? 'http' : 'https').request(options, function (res) {
- var _data = '';
- res.on('data', function (chunk) {
- _data += chunk;
- });
- res.on('end', function () {
- fn != undefined && fn(_data);
- });
- });
- req.write(content);
- req.end();
- }
- function loginNow() {
- var company = getApp().globalData.company
- let userInfo = wx.getStorageSync('userLogin');
- if (userInfo == '') {
- switch (company) {
- // 未来出行港
- case "nTFkj1GpWQ":
- wx.navigateTo({
- url: '/nova-travel/app-auth/index',
- });
- break;
- // 推理社
- case "mwgEpKKsGg":
- wx.navigateTo({
- url: '/nova-drama/app-auth/index',
- });
- break;
- //VR旅游
- case "5G6p8gMdOH":
- wx.navigateTo({
- url: "/nova-vrtrip/app-auth/index",
- })
- break;
- case "668rM7MPii":
- wx.navigateTo({
- url: "/nova-workers/app-auth/index",
- })
- break;
- case "i3cwsEHS4U":
- wx.navigateTo({
- url: "/nova-page/diypage/app-auth/index"
- })
- break;
- case "Aoh1ZFpBSa":
- wx.navigateTo({
- url: "/nova-qingServiceCenter/app-auth/index"
- })
- break;
- // 学位通
- case "tBm19Q58Zm":
- wx.navigateTo({
- url: "/nova-degree/app-auth/index"
- })
- break;
- // 成长口袋
- case "WpgR8iSgGM":
- wx.navigateTo({
- url: "/nova-pocket/app-auth/index"
- })
- break;
- }
- return false;
- } else {
- return true;
- }
- }
- function getTextLength(str, full) {
- let len = 0;
- for (let i = 0; i < str.length; i++) {
- let c = str.charCodeAt(i);
- //单字节加1
- if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
- len++;
- } else {
- len += (full ? 2 : 1);
- }
- }
- return len;
- }
- /**
- * rgba(255, 255, 255, 1) => #ffffff
- * @param {String} color
- */
- function transferColor(color = '') {
- let res = '#';
- color = color.replace(/^rgba?\(/, '').replace(/\)$/, '');
- color = color.split(', ');
- color.length > 3 ? color.length = 3 : '';
- for (let item of color) {
- item = parseInt(item || 0);
- if (item < 10) {
- res += ('0' + item)
- } else {
- res += (item.toString(16))
- }
- }
- return res;
- }
- function transferBorder(border = '') {
- let res = border.match(/(\w+)px\s(\w+)\s(.*)/);
- let obj = {};
- if (res) {
- obj = {
- width: +res[1],
- style: res[2],
- color: res[3]
- }
- }
- return res ? obj : null;
- }
- /**
- * 内边距,依次为上右下左
- * @param {*} padding
- */
- function transferPadding(padding = '0 0 0 0') {
- padding = padding.split(' ');
- for (let i = 0, len = padding.length; i < len; i++) {
- padding[i] = +padding[i].replace('px', '');
- }
- return padding;
- }
- /**
- * type1: 0, 25, 17, rgba(0, 0, 0, 0.3)
- * type2: rgba(0, 0, 0, 0.3) 0px 25px 17px 0px => (0, 25, 17, rgba(0, 0, 0, 0.3))
- * @param {*} shadow
- */
- function transferBoxShadow(shadow = '', type) {
- if (!shadow || shadow === 'none') return;
- let color;
- let split;
- split = shadow.match(/(\w+)\s(\w+)\s(\w+)\s(rgb.*)/);
- if (split) {
- split.shift();
- shadow = split;
- color = split[3] || '#ffffff';
- } else {
- split = shadow.split(') ');
- color = split[0] + ')'
- shadow = split[1].split('px ');
- }
- return {
- offsetX: +shadow[0] || 0,
- offsetY: +shadow[1] || 0,
- blur: +shadow[2] || 0,
- color
- }
- }
- function getUid(prefix) {
- prefix = prefix || '';
- return (
- prefix +
- 'xxyxxyxx'.replace(/[xy]/g, c => {
- let r = (Math.random() * 16) | 0;
- let v = c === 'x' ? r : (r & 0x3) | 0x8;
- return v.toString(16);
- })
- );
- }
- function initProfile() {
- var company = getApp().globalData.company
- let userVetify = wx.getStorageSync('userVetify');
- if (userVetify == '') {
- switch (company) {
- case "668rM7MPii":
- wx.redirectTo({
- url: "/nova-workers/app-auth/Vetify/Vetify",
- })
- break;
- }
- return false;
- } else {
- return true;
- }
- }
- module.exports = {
- formatTime_1: formatTime_1,
- formatTime: formatTime,
- formatTimeNum: formatTimeNum,
- request,
- redirect,
- showErrorToast,
- showSuccessToast,
- checkSession,
- login,
- getUserInfo,
- testMobile,
- sentRes,
- loginNow,
- getTextLength,
- transferBorder,
- transferColor,
- transferPadding,
- transferBoxShadow,
- getUid,
- initProfile
- }
|