caimeiApi.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /**
  2. * @Time 2019-12-12
  3. * @Author Zhengjingyi
  4. * @Action 全局公共方法
  5. */
  6. // import requestUrl from './config.js'
  7. const Tool = {
  8. /**
  9. * @封装公共get数据请求方法无加载动画
  10. * @方法参数:请求地址,请求后台需要的参数字段,回调函数
  11. * @自定义请求头信息
  12. */
  13. get:function(url,data,callback){
  14. uni.request({
  15. url: requestUrl + url,
  16. data:data,
  17. header: {
  18. 'Accept': 'application/json',
  19. 'Content-Type': 'application/x-www-form-urlencoded',
  20. 'X-Token': uni.getStorageSync('token') ? uni.getStorageSync('token') : 'token',
  21. 'cookie': uni.getStorageSync('sessionid')
  22. },
  23. method: 'GET',
  24. success: (response) => {
  25. if(response.statusCode !== 200){
  26. uni.showToast({icon: 'none',title:'服务器连接错误',duration: 2000})
  27. callback(response.statusCode)
  28. }else{
  29. callback(response.data)
  30. }
  31. },
  32. fail: (error) => {
  33. if (error) {
  34. uni.showToast({icon: 'none',title: '网络错误,请稍后重试',duration: 2000})
  35. }
  36. }
  37. })
  38. },
  39. /**
  40. * @封装公共get数据请求方法有加载动画
  41. * @方法参数:请求地址,请求后台需要的参数字段,回调函数
  42. * @自定义请求头信息
  43. */
  44. lodingGet:function(url,data,callback){
  45. uni.showLoading({mask: true,title:'加载中~',})
  46. uni.request({
  47. url: requestUrl + url,
  48. data:data,
  49. header: {
  50. 'Accept': 'application/json',
  51. 'Content-Type': 'application/x-www-form-urlencoded',
  52. 'X-Token': uni.getStorageSync('token') ? uni.getStorageSync('token') : 'token',
  53. 'cookie': uni.getStorageSync('sessionid')
  54. },
  55. method: 'GET',
  56. success: (response) => {
  57. if(response.statusCode !== 200){
  58. uni.showToast({icon: 'none',title: '服务器连接错误',duration: 2000})
  59. callback(response.statusCode)
  60. }else{
  61. callback(response.data)
  62. }
  63. },
  64. fail: (error) => {
  65. if (error) {
  66. uni.showToast({icon: 'none',title: '网络错误,请稍后重试',duration: 2000})
  67. }
  68. },
  69. complete: () => {
  70. setTimeout(function () {
  71. uni.hideLoading()
  72. }, 250)
  73. }
  74. })
  75. },
  76. /**
  77. * @封装公共post数据请求方法
  78. * @方法参数:请求地址,请求后台需要的参数字段,回调函数
  79. */
  80. post:function(url,data,loadingStatus,callback){
  81. if(loadingStatus){uni.showLoading({mask: true,title:'加载中~'})}
  82. uni.request({
  83. url: requestUrl+url,
  84. data:data,
  85. header: {
  86. 'Accept': 'application/json',
  87. 'Content-Type': 'application/x-www-form-urlencoded',
  88. 'X-Token': uni.getStorageSync('token') ? uni.getStorageSync('token') : 'token',
  89. 'cookie': uni.getStorageSync('sessionid')
  90. },
  91. method: 'POST',
  92. success: (response) => {
  93. if(loadingStatus){uni.hideLoading()}
  94. const result = response.data
  95. callback(result)
  96. },
  97. fail: (error) => {
  98. uni.hideLoading()
  99. if (error) {
  100. uni.showToast({icon: 'none',title: '网络错误,请稍后重试',duration: 2000})
  101. }
  102. }
  103. })
  104. },
  105. getComStorage:function(key){// 获取本地Storage
  106. return new Promise(function(resolve,reject) {
  107. uni.getStorage({
  108. key: key,
  109. success: function (res){
  110. resolve(res.data)
  111. }
  112. })
  113. })
  114. },
  115. setStorage:function(key,data){// 存储本地Storage
  116. return new Promise(function(resolve,reject) {
  117. uni.setStorage({
  118. key: key,
  119. data:data,
  120. success: function (res){
  121. }
  122. })
  123. })
  124. },
  125. getStorage:function(){// 获取本地userInfo
  126. return new Promise(function(resolve,reject) {
  127. uni.getStorage({
  128. key: 'userInfo',
  129. success: function (res){
  130. resolve(res.data)
  131. },
  132. fail: function(res){
  133. reject(false)
  134. }
  135. })
  136. })
  137. },
  138. getStorageAddressKey:function(){// 获取本地地址信息
  139. return new Promise(function(resolve,reject) {
  140. uni.getStorage({
  141. key: 'address_key',
  142. success: function (res){
  143. resolve(res.data)
  144. }
  145. })
  146. })
  147. },
  148. loginStatus:function(){
  149. // 获取用户是否登陆 1:已登陆,否则未登陆
  150. return new Promise(function(resolve,reject) {
  151. uni.getStorage({
  152. key: 'userInfo',
  153. success: function (res){
  154. if(res.data.code == '1'){
  155. resolve(true)
  156. } else {
  157. resolve(false)
  158. }
  159. }
  160. })
  161. })
  162. },
  163. navToListPage:function({type,value,id,lType} = {}){
  164. // 跳转到列表页
  165. if(lType=='4'){
  166. const pages = getCurrentPages()
  167. const prevPage = pages[pages.length-2]
  168. prevPage.refresh = true
  169. prevPage.listData = {
  170. type: type,
  171. from: value,
  172. id: id
  173. }
  174. uni.navigateBack({
  175. delta: 1
  176. })
  177. }else{
  178. uni.navigateTo({
  179. url:`/pages/goods/goods?type=${type}&from=${value}&id=${id}`
  180. })
  181. }
  182. },
  183. navigateBack:function(page){
  184. uni.navigateBack({
  185. delta: page
  186. })
  187. },
  188. navigateTo:function(url){
  189. //路由跳转:页面之间路由跳转
  190. uni.navigateTo({
  191. url:url
  192. })
  193. },
  194. redirectTo:function(url){
  195. //路由跳转:关闭当前页跳转到新页面
  196. uni.redirectTo({
  197. url:url
  198. })
  199. },
  200. switchTabTo:function(url){
  201. //路由跳转:底部 tab页
  202. uni.switchTab({
  203. url:url
  204. })
  205. },
  206. isNumber:function(value){
  207. //验证是否为数字
  208. var patrn = /^(-)?\d+(\.\d+)?$/
  209. if (patrn.exec(value) == null || value == '') {
  210. return false
  211. } else {
  212. return true
  213. }
  214. },
  215. getWindowHeight:function(){
  216. // 获取窗口高度
  217. const {windowHeight, pixelRatio} = wx.getSystemInfoSync()
  218. return windowHeight
  219. },
  220. adaptRichTextImg:function(res){
  221. /**
  222. *@富文本实现图片自适应
  223. *@style再添加自适应样式
  224. */
  225. const html = res.replace(/<img[^>]*>/gi,function(match,capture){
  226. let match1 = match.replace(/<img*/gi, '<img style="width:100% !important;height:auto !important;float:left !important;"'),
  227. results = match1.replace(/style=/gi, 'style="width:100%;height:auto;float:left;"')
  228. return results
  229. })
  230. return html
  231. },
  232. FormatMoney:function(num){
  233. // 金额千分位
  234. return num.toString().replace(/\d+/, function (n) { // 先提取整数部分
  235. return n.replace(/(\d)(?=(\d{3})+$)/g, function ($1) { // 对整数部分添加分隔符
  236. return $1 + ','
  237. })
  238. })
  239. },
  240. formatDate:function(){
  241. //获取当前时间
  242. let date = new Date()
  243. let y = date.getFullYear()
  244. let MM = date.getMonth() + 1
  245. MM = MM < 10 ? ('0' + MM) : MM
  246. let d = date.getDate()
  247. d = d < 10 ? ('0' + d) : d
  248. let h = date.getHours()
  249. h = h < 10 ? ('0' + h) : h
  250. let m = date.getMinutes()
  251. m = m < 10 ? ('0' + m) : m
  252. let s = date.getSeconds()
  253. s = s < 10 ? ('0' + s) : s
  254. return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
  255. },
  256. regexSets:function() {
  257. let sets = {
  258. 'companyName': /^[\u4e00-\u9fa5\(\)()\s\da-zA-Z&]{2,50}$/gi,
  259. 'phoneAndTelephone': /^([1]\d{10}|([\((]?0[0-9]{2,3}[)\)]?[-]?)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?)$/,
  260. 'bankNum': /^([1-9]{1})(\d{18})$/,
  261. 'invalidChar': /^[\s\u4e00-\u9fa5a-z0-9_-]{0,}$/
  262. }
  263. return sets
  264. },
  265. timestampToTime:function(timestamp) {
  266. // 时间戳转日期
  267. let date = new Date(timestamp * 1000)//时间戳为10位需*1000,时间戳为13位的话不需乘1000
  268. let Y = date.getFullYear() + '-'
  269. let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-'
  270. let D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate()) + ' '
  271. let h = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours()) + ':'
  272. let m = (date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes()) + ':'
  273. let s = (date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds())
  274. return `${Y}${M}${D}${h}${m}${s}`
  275. },
  276. getNowFormatDate:function () {
  277. var date = new Date()
  278. var seperator1 = '-'
  279. var year = date.getFullYear()
  280. var month = date.getMonth() + 1
  281. var strDate = date.getDate()
  282. var bours =date.getHours()
  283. var min = date.getMinutes()
  284. var s = date.getSeconds()
  285. if (month >= 1 && month <= 9) {
  286. month = '0' + month
  287. }
  288. if (strDate >= 0 && strDate <= 9) {
  289. strDate = '0' + strDate
  290. }
  291. if (bours >= 0 && bours <= 9) {
  292. bours = '0' + bours
  293. }
  294. if (min >= 0 && min <= 9) {
  295. min = '0' + min
  296. }
  297. if (s >= 0 && s <= 9) {
  298. s = '0' + s
  299. }
  300. var currentdate = year + seperator1 + month + seperator1 + strDate +' ' + bours+ ':'+ min+ ':'+ s
  301. return currentdate
  302. }
  303. }
  304. /**
  305. *@导出
  306. */
  307. module.exports = {
  308. get: Tool.get,
  309. post: Tool.post,
  310. lodingGet: Tool.lodingGet,
  311. isNumber: Tool.isNumber,
  312. FormatMoney: Tool.FormatMoney,
  313. navigateTo: Tool.navigateTo,
  314. redirectTo: Tool.redirectTo,
  315. switchTabTo: Tool.switchTabTo,
  316. navigateBack: Tool.navigateBack,
  317. formatDate: Tool.formatDate,
  318. loginStatus: Tool.loginStatus,
  319. setStorage: Tool.setStorage,
  320. getStorage: Tool.getStorage,
  321. getComStorage: Tool.getComStorage,
  322. navToListPage: Tool.navToListPage,
  323. getWindowHeight: Tool.getWindowHeight,
  324. adaptRichTextImg: Tool.adaptRichTextImg,
  325. getStorageAddressKey: Tool.getStorageAddressKey,
  326. regexSets: Tool.regexSets,
  327. timestampToTime: Tool.timestampToTime,
  328. getNowFormatDate : Tool.getNowFormatDate
  329. }