wxmlUtil.wxs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /**
  2. * 小数位数
  3. */
  4. var numberOfDecimalPlacesAmount = 0;
  5. /**
  6. * 小数位数
  7. * @param num
  8. */
  9. var setNumberOfDecimalPlacesAmount = function (num) {
  10. numberOfDecimalPlacesAmount = num;
  11. return "ok";
  12. }
  13. /**
  14. * 截取字符串
  15. * @param str
  16. * @param start
  17. * @param end
  18. */
  19. var subStr = function (str, start, end) {
  20. return str.substring(start, end)
  21. }
  22. /**
  23. * 转成字符串
  24. * @param number
  25. */
  26. var toLocaleString = function (number) {
  27. number = number.toFixed(numberOfDecimalPlacesAmount);
  28. var arr = number.split('.')
  29. return parseInt(arr[0]).toLocaleString() + '.' + arr[1]
  30. }
  31. /**
  32. * 格式化日期
  33. * @param time
  34. */
  35. var format = function (time) {
  36. if (!time || time == '') {
  37. return ''
  38. }
  39. var date = getDate(time);
  40. var year = date.getFullYear()
  41. var month = date.getMonth() + 1
  42. var day = date.getDate()
  43. return year + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day);
  44. }
  45. var formatDateTime = function (time) {
  46. if (!time || time == '') {
  47. return ''
  48. }
  49. var date = getDate(time);
  50. var year = date.getFullYear()
  51. var month = date.getMonth() + 1
  52. var day = date.getDate()
  53. var hour = date.getHours()
  54. var minute = date.getMinutes()
  55. // var second = date.getSeconds()
  56. return year + '-' + month + '-' + day + ' ' + hour + ':' + minute
  57. }
  58. /**
  59. * json
  60. * @param str
  61. */
  62. var parse = function (str) {
  63. if (str == '') return str;
  64. return JSON.parse(str);
  65. }
  66. /**
  67. * 截取日期
  68. * @param str
  69. * @param start
  70. * @param end
  71. */
  72. var dateFormat = function (str, start, end) {
  73. if (!str) return ''
  74. return str.substring(start, end);
  75. }
  76. /**
  77. * 获取类型
  78. * @param item
  79. */
  80. var getType = function (item) {
  81. if (item.BOMID && item.BOMID != 0) {
  82. return "blue"
  83. } else if (item.CustomFlag == 1) {
  84. return ""
  85. } else {
  86. return ""
  87. }
  88. }
  89. /**
  90. * 价格和金额
  91. * @param num
  92. */
  93. var jsCalcFix2 = function (num) {
  94. return parseFloat(num + "").toFixed(numberOfDecimalPlacesAmount)
  95. }
  96. /**
  97. * 判断str中是否含有val
  98. * @param str
  99. * @param val
  100. */
  101. var defineIndexOf = function (str, val) {
  102. if (str != null) {
  103. return str.indexOf(val);
  104. }
  105. }
  106. var addressFullIsUndefined = function (addressFull) {
  107. if (addressFull && (addressFull.indexOf('undefined') != -1)) {
  108. return addressFull.replace("undefined", "") ? addressFull.replace("undefined", "") : ''
  109. } else {
  110. return addressFull ? addressFull : ''
  111. }
  112. }
  113. //地址出现undefined null NaN(目前只能替换一个 每种)
  114. function addressToIndexOf(str) {
  115. // console.log('地址出现undefined',typeof str)
  116. if (typeof str == 'string') {
  117. if (str != null && str != '') {
  118. return str = str.replace('undefined', '').replace('null', '').replace('NaN', '') ? str.replace('undefined', '').replace('null', '').replace('NaN', '') : ''
  119. }
  120. }
  121. }
  122. /**
  123. * @desc : 设置值
  124. * @author : 周兴
  125. * @date : 2024/1/26 11:46
  126. */
  127. function setItemValue(item, colName) {
  128. var value = ''
  129. if (typeof colName == 'object') {
  130. var count = colName.length;
  131. for (i = 0; i < count; i++) {
  132. value += colName[i].title + item[colName[i].name] + (count > 1 && i < count - 1 ? ' | ' : '');
  133. }
  134. } else {
  135. value = item[colName]?item[colName]:'';
  136. }
  137. return value;
  138. }
  139. /**
  140. * @desc : 设置底部值
  141. * @author : 周兴
  142. * @date : 2024/1/26 11:46
  143. */
  144. function setFooterItemValue(item, colName) {
  145. var value = ''
  146. if (typeof colName == 'object') {
  147. var count = colName.length;
  148. for (i = 0; i < count; i++) {
  149. // 如果前缀有值就显示
  150. if(colName[i].prefix){
  151. value += colName[i].prefix + item[colName[i].name] + colName[i].title;
  152. }else{
  153. value += item[colName[i].name] + colName[i].title;
  154. }
  155. }
  156. } else {
  157. value = item[colName];
  158. }
  159. return value;
  160. }
  161. /**
  162. * @desc : 设置查询提示值
  163. * @author : 周兴
  164. * @date : 2024/1/26 11:46
  165. */
  166. function setSearchPlaceholder(lang, cols) {
  167. var text = '';
  168. if (typeof cols == 'object') {
  169. for (i = 0; i < cols.length; i++) {
  170. text += lang[cols[i]] + '/';
  171. }
  172. text = text.substring(0,text.length - 1);
  173. } else {
  174. text = lang[cols];
  175. }
  176. return lang['search'] + ' ' + text;
  177. }
  178. /**
  179. * @desc : 设置提示信息
  180. * @author : 周兴
  181. * @date : 2024/1/26 11:46
  182. */
  183. function setPlaceholder(lang,name,searchFlag=false) {
  184. var text;
  185. if(lang[name]){
  186. text = lang[name];
  187. }else{
  188. text = name;
  189. }
  190. if(searchFlag){
  191. text = lang['searchWords'] + text;
  192. }else{
  193. text = lang['inputWords'] + text;
  194. }
  195. return text;
  196. }
  197. /**
  198. * @desc : 前台实现千分位
  199. * @date : 2024/2/18 16:49
  200. * @author : 周兴
  201. */
  202. function toThousandCents(num) {
  203. if (num == undefined) {
  204. return 0;
  205. }
  206. var num = num + '';
  207. var d = '';
  208. if (num.slice(0, 1) == '-') {
  209. d = num.slice(0, 1);
  210. num = num.slice(1);
  211. }
  212. var len = num.length;
  213. var index = num.indexOf('.');
  214. if (index == -1) {
  215. num = num + '.00';
  216. } else if ((index + 2) == len) {
  217. num = num + '0';
  218. }
  219. var index = num.indexOf('.'); // 字符出现的位置
  220. var num2 = num.slice(-3);
  221. num = num.slice(0, index)
  222. var result = '';
  223. while (num.length > 3) {
  224. result = ',' + num.slice(-3) + result;
  225. num = num.slice(0, num.length - 3);
  226. }
  227. if (num) {
  228. result = num + result;
  229. }
  230. return d + (result + num2)
  231. }
  232. /**
  233. * 暴露接口调用
  234. */
  235. module.exports = {
  236. toThousandCents:toThousandCents,
  237. setItemValue: setItemValue,
  238. setFooterItemValue: setFooterItemValue,
  239. setSearchPlaceholder: setSearchPlaceholder,
  240. setPlaceholder:setPlaceholder,
  241. addressToIndexOf: addressToIndexOf,
  242. addressFullIsUndefined: addressFullIsUndefined,
  243. setNumberOfDecimalPlacesAmount: setNumberOfDecimalPlacesAmount,
  244. toLocaleString: toLocaleString,
  245. subStr: subStr,
  246. format: format,
  247. parse: parse,
  248. dateFormat: dateFormat,
  249. getType: getType,
  250. jsCalcFix2: jsCalcFix2,
  251. defineIndexOf: defineIndexOf,
  252. formatDateTime: formatDateTime
  253. }