util.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import Config from './config.js'
  2. import Store from '@/store/index.js'
  3. export default {
  4. /**
  5. * 页面跳转
  6. * @param {string} to 跳转链接 /pages/idnex/index
  7. * @param {Object} param 参数 {key : value, ...}
  8. * @param {string} mode 模式
  9. */
  10. redirectTo(to, param, mode) {
  11. let tabbar = [
  12. "/pages/billing/index", // 开单
  13. "/pages/reserve/index", // 预约
  14. "/pages/buycard/index", // 售卡
  15. "/pages/recharge/index", // 充值
  16. "/pages/verify/index" // 核销
  17. ];
  18. if (tabbar.indexOf(to) != -1) mode = 'tabbar';
  19. Store.commit('setCurrRoute', to);
  20. let url = to;
  21. if (param != undefined) {
  22. Object.keys(param).forEach(function(key) {
  23. if (url.indexOf('?') != -1) {
  24. url += "&" + key + "=" + param[key];
  25. } else {
  26. url += "?" + key + "=" + param[key];
  27. }
  28. });
  29. }
  30. switch (mode) {
  31. case 'tabbar':
  32. // 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面。
  33. uni.switchTab({
  34. url
  35. })
  36. break;
  37. case 'redirectTo':
  38. // 关闭当前页面,跳转到应用内的某个页面。
  39. uni.redirectTo({
  40. url
  41. });
  42. break;
  43. case 'reLaunch':
  44. // 关闭所有页面,打开到应用内的某个页面。
  45. uni.reLaunch({
  46. url
  47. });
  48. break;
  49. default:
  50. // 保留当前页面,跳转到应用内的某个页面
  51. uni.navigateTo({
  52. url
  53. });
  54. }
  55. },
  56. /**
  57. * 图片路径转换
  58. * @param {String} img_path 图片地址
  59. * @param {Object} params 参数,针对商品、相册里面的图片区分大中小,size: big、mid、small
  60. */
  61. img(img_path, params) {
  62. var path = "";
  63. if (img_path != undefined && img_path != "") {
  64. if (img_path.split(',').length > 1) {
  65. img_path = img_path.split(',')[0];
  66. }
  67. if (params && img_path) {
  68. // 过滤默认图
  69. let arr = img_path.split(".");
  70. let suffix = arr[arr.length - 1];
  71. arr.pop();
  72. arr[arr.length - 1] = arr[arr.length - 1] + "_" + params.size.toUpperCase();
  73. arr.push(suffix);
  74. img_path = arr.join(".");
  75. }
  76. if (img_path.indexOf("http://") == -1 && img_path.indexOf("https://") == -1) {
  77. path = Config.imgDomain + "/" + img_path;
  78. } else {
  79. path = img_path;
  80. }
  81. // 处理商品助手的图片路径
  82. path = path.replace("addons/NsGoodsAssist/", "").replace("shop/goods/", "");
  83. }
  84. // path += '?t=' + parseInt(new Date().getTime() / 1000);
  85. return path;
  86. },
  87. /**
  88. * 验证当前数组或对象是否为空
  89. * @param param 校验对象
  90. */
  91. checkIsNotNull(param) {
  92. if (param) {
  93. if (typeof(param) == 'object') {
  94. if (Array.isArray(param)) {
  95. if (param.length > 0) return true
  96. } else {
  97. if (JSON.stringify(param) != '{}') return true
  98. }
  99. } else {
  100. return true;
  101. }
  102. }
  103. return false;
  104. },
  105. /**
  106. * 金额格式化
  107. * @param {Object} money
  108. */
  109. moneyFormat(money) {
  110. if (isNaN(parseFloat(money))) return money;
  111. return parseFloat(money).toFixed(2);
  112. },
  113. /**
  114. * 时间格式化
  115. * @param {Object} time 时间戳
  116. * @param {Object} format 输出格式
  117. */
  118. timeFormat(time, format = 'y-m-d h:i:s') {
  119. var date = new Date();
  120. date.setTime(time * 1000);
  121. var y = date.getFullYear();
  122. var m = date.getMonth() + 1;
  123. var d = date.getDate();
  124. var h = date.getHours();
  125. var i = date.getMinutes();
  126. var s = date.getSeconds();
  127. format = format.replace('y', y);
  128. format = format.replace('m', (m < 10 ? '0' + m : m));
  129. format = format.replace('d', (d < 10 ? '0' + d : d));
  130. format = format.replace('h', (h < 10 ? '0' + h : h));
  131. format = format.replace('i', (i < 10 ? '0' + i : i));
  132. format = format.replace('s', (s < 10 ? '0' + s : s));
  133. return format;
  134. },
  135. /**
  136. * 日期格式转时间戳
  137. * @param {Object} timeStamp
  138. */
  139. timeTurnTimeStamp(string) {
  140. var f = string.split(' ', 2);
  141. var d = (f[0] ? f[0] : '').split('-', 3);
  142. var t = (f[1] ? f[1] : '').split(':', 3);
  143. return (new Date(
  144. parseInt(d[0], 10) || null,
  145. (parseInt(d[1], 10) || 1) - 1,
  146. parseInt(d[2], 10) || null,
  147. parseInt(t[0], 10) || null,
  148. parseInt(t[1], 10) || null,
  149. parseInt(t[2], 10) || null
  150. )).getTime() / 1000;
  151. },
  152. /**
  153. * 获取当前页面路由
  154. */
  155. getCurrRoute() {
  156. let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
  157. return routes.length ? routes[routes.length - 1].route : '';
  158. },
  159. /**
  160. * 显示消息提示框
  161. * @param {Object} params 参数
  162. */
  163. showToast(params = {}) {
  164. params.title = params.title || "";
  165. params.icon = params.icon || "none";
  166. // params.position = params.position || 'bottom';
  167. params.duration = params.duration || 1500;
  168. uni.showToast(params);
  169. if (params.success) params.success();
  170. },
  171. /*
  172. * 深度拷贝对象
  173. * @param {Object} obj
  174. */
  175. deepClone(obj) {
  176. const isObject = function(obj) {
  177. return typeof obj == 'object';
  178. }
  179. if (!isObject(obj)) {
  180. throw new Error('obj 不是一个对象!')
  181. }
  182. //判断传进来的是对象还是数组
  183. let isArray = Array.isArray(obj)
  184. let cloneObj = isArray ? [] : {}
  185. //通过for...in来拷贝
  186. for (let key in obj) {
  187. cloneObj[key] = isObject(obj[key]) ? this.deepClone(obj[key]) : obj[key]
  188. }
  189. return cloneObj
  190. },
  191. /**
  192. * 图片选择加上传
  193. * @param number num
  194. * @param {Object} params
  195. * @param {Object} callback
  196. * @param string url
  197. * return array
  198. */
  199. upload: function(num, params, callback, url) {
  200. const app_type = 'pc';
  201. const app_type_name = 'PC';
  202. var data = {
  203. token: uni.getStorageSync('cashier_token'),
  204. app_type: app_type,
  205. app_type_name: app_type_name
  206. }
  207. data = Object.assign(data, params);
  208. var imgs_num = num;
  209. var _self = this;
  210. uni.chooseImage({
  211. count: imgs_num,
  212. sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
  213. sourceType: ['album', 'camera'], //从相册或者拍照
  214. success: async function(res) {
  215. const tempFilePaths = res.tempFilePaths;
  216. var _data = data;
  217. var imgs = [];
  218. for (var i = 0; i < tempFilePaths.length; i++) {
  219. var path = await _self.upload_file_server(tempFilePaths[i], _data, params.path,
  220. url);
  221. imgs.push(path);
  222. }
  223. typeof callback == 'function' && callback(imgs);
  224. },
  225. fail: err => {
  226. console.log('图片上传错误', err)
  227. }
  228. });
  229. },
  230. //上传
  231. upload_file_server(tempFilePath, data, path, url = "") {
  232. if (url) {
  233. var uploadUrl = Config.baseUrl + url
  234. } else {
  235. var uploadUrl = Config.baseUrl + '/cashier/storeapi/upload/' + path
  236. }
  237. data.site_id = Config.siteId
  238. return new Promise((resolve, reject) => {
  239. uni.uploadFile({
  240. url: uploadUrl,
  241. filePath: tempFilePath,
  242. name: 'file',
  243. formData: data,
  244. success: function(res) {
  245. var path_str = JSON.parse(res.data);
  246. if (path_str.code >= 0) {
  247. resolve(path_str.data.pic_path);
  248. } else {
  249. reject("error");
  250. }
  251. },
  252. });
  253. });
  254. }
  255. }