wxmlUtil.wxs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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]?item[colName[i].name]:'')
  133. + (item[colName[i].name]?' | ':'');
  134. }
  135. if(value ){
  136. value = value.substring(0,value.length - 3);
  137. }
  138. } else {
  139. value = item[colName]?item[colName]:'';
  140. }
  141. return value;
  142. }
  143. /**
  144. * @desc : 设置底部值
  145. * @author : 周兴
  146. * @date : 2024/1/26 11:46
  147. */
  148. function setFooterItemValue(item, colName) {
  149. var value = ''
  150. if (typeof colName == 'object') {
  151. var count = colName.length;
  152. for (i = 0; i < count; i++) {
  153. // 如果前缀有值就显示
  154. if(colName[i].prefix){
  155. //控制参数为0 等于自定义参数
  156. if(colName[i].different && item[colName[i].name] == 0){
  157. value += colName[i].different
  158. }else{
  159. value += colName[i].prefix + (item[colName[i].name]?item[colName[i].name]:'') + colName[i].title;
  160. }
  161. }else{
  162. value += (item[colName[i].name]?item[colName[i].name]:'') + colName[i].title;
  163. }
  164. }
  165. } else {
  166. value = item[colName]?item[colName]:'';
  167. }
  168. return value;
  169. }
  170. /**
  171. * @desc : 设置查询提示值
  172. * @author : 周兴
  173. * @date : 2024/1/26 11:46
  174. */
  175. function setSearchPlaceholder(lang, cols) {
  176. var text = '';
  177. if (typeof cols == 'object') {
  178. for (i = 0; i < cols.length; i++) {
  179. text += lang[cols[i]] + '/';
  180. }
  181. text = text.substring(0,text.length - 1);
  182. } else {
  183. text = lang[cols];
  184. }
  185. return lang['search'] + ' ' + text;
  186. }
  187. /**
  188. * @desc : 设置提示信息
  189. * @author : 周兴
  190. * @date : 2024/1/26 11:46
  191. */
  192. function setPlaceholder(lang,name,searchFlag=false) {
  193. var text;
  194. if(lang[name]){
  195. text = lang[name];
  196. }else{
  197. text = name;
  198. }
  199. if(searchFlag){
  200. text = lang['searchWords'] + text;
  201. }else{
  202. text = lang['inputWords'] + text;
  203. }
  204. return text;
  205. }
  206. /**
  207. * @desc : 前台实现千分位
  208. * @date : 2024/2/18 16:49
  209. * @author : 周兴
  210. */
  211. function toThousandCents(num) {
  212. if (num == undefined) {
  213. return 0;
  214. }
  215. var num = num + '';
  216. var d = '';
  217. if (num.slice(0, 1) == '-') {
  218. d = num.slice(0, 1);
  219. num = num.slice(1);
  220. }
  221. var len = num.length;
  222. var index = num.indexOf('.');
  223. if (index == -1) {
  224. num = num + '.00';
  225. } else if ((index + 2) == len) {
  226. num = num + '0';
  227. }
  228. var index = num.indexOf('.'); // 字符出现的位置
  229. var num2 = num.slice(-3);
  230. num = num.slice(0, index)
  231. var result = '';
  232. while (num.length > 3) {
  233. result = ',' + num.slice(-3) + result;
  234. num = num.slice(0, num.length - 3);
  235. }
  236. if (num) {
  237. result = num + result;
  238. }
  239. return d + (result + num2)
  240. }
  241. /**
  242. * 暴露接口调用
  243. */
  244. module.exports = {
  245. toThousandCents:toThousandCents,
  246. setItemValue: setItemValue,
  247. setFooterItemValue: setFooterItemValue,
  248. setSearchPlaceholder: setSearchPlaceholder,
  249. setPlaceholder:setPlaceholder,
  250. addressToIndexOf: addressToIndexOf,
  251. addressFullIsUndefined: addressFullIsUndefined,
  252. setNumberOfDecimalPlacesAmount: setNumberOfDecimalPlacesAmount,
  253. toLocaleString: toLocaleString,
  254. subStr: subStr,
  255. format: format,
  256. parse: parse,
  257. dateFormat: dateFormat,
  258. getType: getType,
  259. jsCalcFix2: jsCalcFix2,
  260. defineIndexOf: defineIndexOf,
  261. formatDateTime: formatDateTime
  262. }