// DOWX.js v1.0.6
// 2017-11-3
const com = require('common.js');
//================================================ 自写隐藏 错误弹出框 ====================================================
// 写在do-body标签外:
// {{do_yccontent}}
var zdo_errortimeout = null;
// 参数: page()对象 提示文字内容
function Showred( thC, contentC )
{ if (zdo_errortimeout != null) { clearTimeout(zdo_errortimeout); } thC.setData({ do_ycif: false }); thC.setData({ do_ycif: true, do_yccontent: contentC, do_toastcolor: 1 }); zdo_errortimeout = setTimeout(function () { thC.setData({ do_ycif: false, do_yccontent: "" }); }, 3000); }
module.exports.Showred = Showred;
//================================================ 自写隐藏 正确弹出框 ====================================================
// 参数: page()对象 提示文字内容
function Showgreen( thC, contentC )
{ if (zdo_errortimeout != null) { clearTimeout(zdo_errortimeout); } thC.setData({ do_ycif: false }); thC.setData({ do_ycif: true, do_yccontent: contentC, do_toastcolor: 2 }); zdo_errortimeout = setTimeout(function () { thC.setData({ do_ycif: false, do_yccontent: "" }); }, 3000); }
module.exports.Showgreen = Showgreen;
// ================================================== 自写刷新按钮 =======================================================
// 写在do-body外:
//
// js page中:
// refresh: function() { dowx.Startrefresh(this); }
// 开启刷新动效,参数: page()对象
var tf_refresh = false;
function Startrefresh (thC)
{if(tf_refresh==true){return false;}tf_refresh=true;thC.setData({ zdotf_refresh: true });}
module.exports.Startrefresh = Startrefresh;
// 关闭刷新动效,参数: page()对象 完成请求的bool组(可不填)
var timeout_Stoprefresh = null;
function Stoprefresh (thC,zuC)
{ if (zuC == null) { zuC = []; } var mo = zuC.indexOf(true); if (mo == -1) { if (timeout_Stoprefresh != null) { clearTimeout(timeout_Stoprefresh); } timeout_Stoprefresh = setTimeout(function(){ tf_refresh = false; thC.setData({ zdotf_refresh: false }); },800); } }
module.exports.Stoprefresh = Stoprefresh;
// ============================================== 语音播放的wifi图标动画 ==================================================
// 1. js data:
// zu_wificlass: ["wifiD", "wifiD", "wifiD"], //wifi播放图标的样式
// 2. html写法:
// {{time_luyin}}''删除
var zdo_interval=null;
// 开始wifi动画,参数: 当前page()对象 wifi的class样式
function Play_wifi (thatC, classC)
{ var zu_0 = [classC, "displayNone", "displayNone"]; var zu_1 = [classC, classC, "displayNone"]; var zu_2 = [classC, classC, classC]; var zu_3 = ["displayNone", "displayNone", "displayNone"]; var nummo = 0; thatC.setData({ zu_wificlass: zu_3 }); if (zdo_interval != null) { clearInterval(zdo_interval); zdo_interval = null; } zdo_interval = setInterval(function(){ if (nummo == 0) { thatC.setData({ zu_wificlass: zu_0 });} else if (nummo == 1) { thatC.setData({ zu_wificlass: zu_1 }); } else if (nummo == 2) { thatC.setData({ zu_wificlass: zu_2 }); } if (nummo >= 3) { nummo = 0; thatC.setData({ zu_wificlass: zu_3 }); } else { nummo++; } },600); }
module.exports.Play_wifi = Play_wifi;
// 结束wifi动画,参数: 当前page()对象 wifi的class样式
function Stop_wifi (thatC, classC)
{ clearInterval(zdo_interval); zdo_interval = null; thatC.setData({ zu_wificlass: [classC, classC, classC] });}
module.exports.Stop_wifi = Stop_wifi;
// ==================================================== 录音组件 ===========================================================
// 录音弹出框 do_ycly控制是否显示 do_yclytime控制一圈时长
// 写在do-body外:
var intval_record = null; //录音时长的计时器
var time_record = 0; //录音时长s
var time_oldrecord = 0; //旧的录音时间戳
var working_record = false; //录音是否正在工作
var tf_recordstop = false; //是否触发停止录音
// 开始录音,参数: page()对象 录音长度最高限制s 生成的录音文件回调函数 每秒的动态回调函数(可不填)
function Record_start(thC, timemaxC, callback, callback2)
{ if (callback2 == null) { callback2 = function () {}; } var time_newrecord = new Date().getTime(); if ( (working_record != true) || (time_newrecord - time_oldrecord > 2000) ) { working_record = true; time_oldrecord = time_newrecord; tf_recordstop = false; time_record = 0; thC.setData({ do_ycly: true, do_yclytime: (timemaxC / 2) }); intval_record = setInterval(function () { time_record++; console.log("interval:录音计时" + time_record); if (tf_recordstop == true) { Record_stop(thC); } if (time_record >= timemaxC) { Record_stop(thC); } callback2(time_record); }, 1000); wx.startRecord({ success: function (res) { var back = { time: time_record, tempFilePath: res.tempFilePath }; callback(back); }, complete: function () { if (intval_record != null) { clearInterval(intval_record); intval_record = null; } time_record = 0; working_record = false; } }); } }
module.exports.Record_start = Record_start;
// 结束录音,参数: page()对象
function Record_stop(thC)
{ tf_recordstop = true; thC.setData({ do_ycly: false }); if (time_record >= 1 && intval_record != null) { clearInterval(intval_record); intval_record = null; } wx.stopRecord(); }
module.exports.Record_stop = Record_stop;
// -=-=-=-=-=1
// -=-=-=-=-=
// -=-=-=-=-=
// -=-=-=-=-=2
// ============================================== 时间戳转换函数 =======================================================
// 参数: 1.时间戳
function FormatDate ( now )
{ now = parseFloat(now);var data = new Date(now);var year = data.getFullYear();var month = data.getMonth() + 1;var date = data.getDate();var hour = data.getHours();var minute = data.getMinutes();return year + "-" + month + "-" + date + " " + hour + ":" + minute; }
module.exports.FormatDate = FormatDate;
// ============================================= 获取随机字符串函数 ======================================================
// 参数: 要开头的字符串
function Get_randomstring (strC)
{ if(strC==null) { strC = ""; } var timestamp = new Date().getTime(); var sjs = Math.round(Math.random() * 899 + 100); var taskid = strC + timestamp + sjs; return taskid; }
module.exports.Get_randomstring = Get_randomstring;
// ================================================ 获取文件MD5格式 =====================================================
// 资源上传,参数: 1.资源路径 2.成功回调函数
function GetFileMD5 ( pathC, callback )
{ if (callback == null) { callback = function () { }; } wx.getFileInfo({ filePath: pathC, digestAlgorithm: 'md5', success: function (infodata) { callback(infodata.digest); }, fail: function (error) { wx.showLoading({ title: '资源MD5→fail' }); console.log(error); } }); }
module.exports.GetFileMD5 = GetFileMD5;
// ================================================= 获取设备信息 ======================================================
// 资源上传,参数: 1.成功回调函数
function GetSystemInfo ( callback )
{ if (callback == null) { callback = function () { }; } wx.getSystemInfo({ success: function (res) { var mydata = { brand: res.brand, model: res.model, screenWidth: res.screenWidth, screenHeight: res.screenHeight, windowWidth: res.windowWidth, windowHeight: res.windowHeight, version: res.version }; callback(mydata); }, fail: function () { wx.showLoading({ title: '设备info→fail' }); } }); }
module.exports.GetSystemInfo = GetSystemInfo;
// ================================================= 获取节点信息 ======================================================
// 资源上传,参数: 1.节点的id字符串 2.成功回调函数
function GetQueryInfo ( idC, callback )
{ if (callback == null) { callback = function () { }; } wx.createSelectorQuery().select('#'+idC).boundingClientRect(function (rect) { var mydata = { left: rect.left, right: rect.right, top: rect.top, bottom: rect.bottom, width: rect.width, height: rect.height }; callback(mydata); }).exec(); }
module.exports.GetQueryInfo = GetQueryInfo;
// -=-=-=-=-=2
// -=-=-=-=-=
// -=-=-=-=-=
// -=-=-=-=-=3
// ==================================================== POST请求 ===========================================================
// 数据请求,参数: 1.地址url 2.发送的数据 3.接收数据的回调函数
function AjaxPost ( can_url, can_data, can_callback )
{ wx.showNavigationBarLoading(); wx.request({ method: "POST", url: can_url, data: can_data, success: function (res) { can_callback(res.data); }, fail: function (res) { wx.showLoading({ title: res.errMsg }); } }); }
module.exports.AjaxPost = AjaxPost;
// ==================================================== GET请求 ===========================================================
// 数据请求,参数: 1.地址url 2.发送的数据 3.接收数据的回调函数
function AjaxGet ( can_url, can_data, can_callback )
{ wx.showNavigationBarLoading(); wx.request({ method: "GET", url: can_url, data: can_data, success: function (res) { can_callback(res.data); }, fail: function (res) { wx.showLoading({ title: 'GET→fail', }); } }); }
module.exports.AjaxGet = AjaxGet;
// ===================================================== 请求结束 =========================================================
// 关闭导航转圈动画, 参数: 完成请求的bool组(可不填)
function AjaxStop ( zuC )
{ if (zuC == null) { zuC = []; } var mo = zuC.indexOf(true); if (mo == -1) { wx.hideNavigationBarLoading(); } }
module.exports.AjaxStop = AjaxStop;
// -=-=-=-=-=3
// -=-=-=-=-=
// -=-=-=-=-=
// -=-=-=-=-=4
// ================================================== 获取用户信息 =======================================================
// 参数: 请求openid的url(可为空字符串"") 返回数据的回调函数
function Get_userInfo ( urlC, callback )
{ var calldata = {}; wx.login({ fail: function (res) { wx.showLoading({ title: 'login→fail' }); }, success: function (res) { wx.getUserInfo({ withCredentials: true, lang: "zh_CN", success: function (res2) { calldata.info1 = res2; if (urlC.length == 0) { callback(calldata); } else if (urlC.length > 0) { var mydata = { code: res.code }; var myurl = com.getWholeUrl(urlC, mydata); AjaxPost(myurl, {}, function (data) { if (data.code == 0) { calldata.info2 = data.data; callback(calldata); } else { console.log(data); wx.showLoading({ title: 'openid返回错误' }); } }); } } }); } }); }
module.exports.Get_userInfo = Get_userInfo;
// ================================================= 获取用户信息权限 ======================================================
function Authorize_userInfo ( callback )
{ if (callback == null) { callback = function () {}; } wx.authorize({ scope: 'scope.userInfo', fail: function () { wx.showModal({ content: '请授权获取用户信息,仅含头像、昵称等基本信息,不涉及隐私信息', showCancel: false, confirmText: '去授权', complete: function (res) { if (res.confirm == true) { wx.openSetting(); } else { Authorize_userInfo(callback); } } }); }, success: function () { callback(); } }); }
module.exports.Authorize_userInfo = Authorize_userInfo;
// ================================================== 获取摄像头权限 =======================================================
function Authorize_camera ( callback )
{ if (callback == null) { callback = function () { }; } wx.authorize({ scope: 'scope.camera', fail: function () { wx.showModal({ content: '后面有录像操作,请授权获取摄像头', showCancel: false, confirmText: '去授权', complete: function (res) { if (res.confirm == true) { wx.openSetting(); } else { Authorize_camera(callback); } } }); }, success: function () { callback(); } }); }
module.exports.Authorize_camera = Authorize_camera;
// =================================================== 获取录音权限 =======================================================
function Authorize_record ( callback )
{ if (callback == null) { callback = function () { }; } wx.authorize({ scope: 'scope.record', fail: function () { wx.showModal({ content: '后面有录音操作,请授权获取录音功能', showCancel: false, confirmText: '去授权', complete: function (res) { if (res.confirm == true) { wx.openSetting(); } else { Authorize_record(callback); } } }); }, success: function () { callback(); } }); }
module.exports.Authorize_record = Authorize_record;
// ================================================= 获取地理位置权限 ======================================================
function Authorize_userLocation ( callback )
{ if (callback == null) { callback = function () { }; } wx.authorize({ scope: 'scope.userLocation', fail: function () { wx.showModal({ content: '需要位置信息,请授权获取地理位置功能', showCancel: false, confirmText: '去授权', complete: function (res) { if (res.confirm == true) { wx.openSetting(); } else { Authorize_userLocation(callback); } } }); }, success: function () { callback(); } }); }
module.exports.Authorize_userLocation = Authorize_userLocation;