caimeiApi.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /**
  2. * @Time 2019-12-12
  3. * @Author Zhengjingyi
  4. * @Action 全局公共方法
  5. */
  6. import requestUrl from '@/services/config.env.js'
  7. const caimeiApi = {
  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. fail: function(res){
  113. reject(false)
  114. }
  115. })
  116. })
  117. },
  118. setStorage:function(key,data){// 存储本地Storage
  119. return new Promise(function(resolve,reject) {
  120. uni.setStorage({
  121. key: key,
  122. data:data,
  123. success: function (res){
  124. }
  125. })
  126. })
  127. },
  128. getStorage:function(){// 获取本地userInfo
  129. return new Promise(function(resolve,reject) {
  130. uni.getStorage({
  131. key: 'userInfo',
  132. success: function (res){
  133. resolve(res.data)
  134. },
  135. fail: function(res){
  136. reject(false)
  137. }
  138. })
  139. })
  140. },
  141. removeStorage:function(key){// 获取本地userInfo
  142. return new Promise(function(resolve,reject) {
  143. uni.removeStorage({
  144. key: key,
  145. success: function (res) {
  146. console.log('success')
  147. }
  148. })
  149. })
  150. },
  151. getStorageAddressKey:function(){// 获取本地地址信息
  152. return new Promise(function(resolve,reject) {
  153. uni.getStorage({
  154. key: 'address_key',
  155. success: function (res){
  156. resolve(res.data)
  157. }
  158. })
  159. })
  160. },
  161. loginStatus:function(){
  162. // 获取用户是否登陆 1:已登陆,否则未登陆
  163. return new Promise(function(resolve,reject) {
  164. uni.getStorage({
  165. key: 'userInfo',
  166. success: function (res){
  167. if(res.data.code == '1'){
  168. resolve(true)
  169. } else {
  170. resolve(false)
  171. }
  172. }
  173. })
  174. })
  175. },
  176. navToListPage:function({type,value,id,lType} = {}){
  177. // 跳转到列表页
  178. if(lType=='4'){
  179. const pages = getCurrentPages()
  180. const prevPage = pages[pages.length-2]
  181. prevPage.refresh = true
  182. prevPage.listData = {
  183. type: type,
  184. from: value,
  185. id: id
  186. }
  187. uni.navigateBack({
  188. delta: 1
  189. })
  190. }else{
  191. uni.navigateTo({
  192. url:`/pages/goods/goods?type=${type}&from=${value}&id=${id}`
  193. })
  194. }
  195. },
  196. navigateToGoods:function({type,value,id,lType} = {}){
  197. // 跳转到列表页
  198. if(lType=='4'){
  199. const pages = getCurrentPages()
  200. const prevPage = pages[pages.length-2]
  201. prevPage.refresh = true
  202. prevPage.listData = {
  203. type: type,
  204. from: value,
  205. id: id
  206. }
  207. uni.navigateBack({
  208. delta: 1
  209. })
  210. }else{
  211. uni.navigateTo({
  212. url:`/pages/goods/goods-classify?type=${type}&from=${value}&id=${id}`
  213. })
  214. }
  215. },
  216. FlooryNavigateTo:function(pros){
  217. if(pros.listType == 1){
  218. if(pros.product.productCategory == '1'){
  219. uni.navigateTo({
  220. url:`/pages/goods/product?id=${pros.product.productId}`
  221. })
  222. }else{
  223. uni.navigateTo({
  224. url:`/pages/second/product/product-details?id=${pros.product.productId}`
  225. })
  226. }
  227. }else{
  228. /**
  229. * 页面跳转类型
  230. * 1、二级页面,2、搜索项目仪器,3、直播页面,4、自由页面,5、商品详情,6、仪器项目详情,7、供应商主页
  231. * 8、专题活动页,9、二手市场介绍,10、二手商品列表,11、二手商品发布,12、商品搜索,13、信息详情
  232. * 14、品牌招商介绍页,15、维修保养介绍页,16、首页,17、注册页,18、信息中心,19、供应商列表
  233. **/
  234. if(pros.linkType){
  235. console.log(pros.linkType)
  236. const typeMap = {
  237. 1:`/pages/goods/goods-instrument?linkId=${pros.linkParam.id}`,
  238. 2:`/pages/goods/instrument-details?id=${pros.linkParam.id}`,
  239. 3:`/pages/h5/article/page-image?image=${pros.adsImage}&title=专题直播`,
  240. 4:`/pages/h5/activity/activity?link=${pros.crmLink}&linkId=${pros.linkParam.id}`,
  241. 5:`/pages/goods/product?id=${pros.linkParam.id}`,
  242. 7:`/pages/supplier/user/my-shop?shopId=${pros.linkParam.id}`,
  243. 8:'/pages/h5/activity/activity-list',
  244. 9:'/pages/second/form/introduce',
  245. 10:'/pages/second/product/product-list',
  246. 11:'/pages/second/form/form',
  247. 12:`/pages/search/search?keyWord=${pros.linkParam.keyword}`,
  248. 13:`/pages/h5/article/path?link=${pros.link}`,
  249. 14:`/pages/h5/article/path?link=${pros.link}`,
  250. 15:`/pages/h5/article/path?link=${pros.link}`,
  251. // '-1':`/pages/h5/article/path?link=${pros.link}`,
  252. 17:'/pages/login/register-select',
  253. 18:`/pages/h5/article/path?link=${pros.link}`,
  254. 19:`/pages/search/search-supplier?keyWord=${pros.linkParam.keyword}`,
  255. 21:'/pages/h5/activity/meobohui',
  256. 23:`/pages/h5/activity/activity-topic?linkId=${pros.linkParam.id}`,
  257. 24:'/pages/user/coupon/coupon',
  258. 25:'/pages/goods/goods-doc-list',//美业资料
  259. 28:`/pages/h5/article/path?link=${pros.link}`, // 采美认证通
  260. 29:'/pages/user/coupon/coupon-collection',//领券中心
  261. }
  262. const url = typeMap[pros.linkType]
  263. uni.navigateTo({
  264. url:url
  265. })
  266. }
  267. }
  268. },
  269. BannerNavigateTo:function(linkType,linkId,linkHref,keyword){//楼层跳转判断
  270. if(linkType){
  271. const typeMap = {
  272. 1:`/pages/goods/goods-instrument?linkId=${linkId}`,
  273. 2:`/pages/goods/instrument-details?id=${linkId}`,
  274. 4:`/pages/h5/activity/activity?link=${linkHref}&linkId=${linkId}`,
  275. 5:`/pages/goods/product?id=${linkId}`,
  276. 7:`/pages/supplier/user/my-shop?shopId=${linkId}`,
  277. 8:'/pages/h5/activity/activity-list',
  278. 9:'/pages/second/form/introduce',
  279. 10:'/pages/second/product/product-list',
  280. 11:'/pages/second/form/form',
  281. 12:`/pages/search/search?keyWord=${keyword}`,
  282. 13:`/pages/h5/article/path?link=${linkHref}`,
  283. 14:`/pages/h5/article/path?link=${linkHref}`,
  284. 15:`/pages/h5/article/path?link=${linkHref}`,
  285. 17:'/pages/login/register-select',
  286. 18:`/pages/h5/article/path?link=${linkHref}`,
  287. 19:`/pages/search/search-supplier?keyWord=${keyword}`,
  288. 21:'/pages/h5/activity/meobohui',
  289. // '-1':`/pages/h5/article/path?link=${linkHref}`
  290. 24:'/pages/user/coupon/coupon',
  291. 25:'/pages/goods/goods-doc-list', //美业资料
  292. 28:`/pages/h5/article/path?link=${linkHref}`, // 采美认证通
  293. 29:'/pages/user/coupon/coupon-collection',//领券中心
  294. }
  295. const url = typeMap[linkType]
  296. uni.navigateTo({
  297. url:url
  298. })
  299. }
  300. },
  301. navigateTo:function(url){
  302. //路由跳转:页面之间路由跳转
  303. uni.navigateTo({
  304. url:url
  305. })
  306. },
  307. redirectTo:function(url){
  308. //路由跳转:关闭当前页跳转到新页面
  309. uni.redirectTo({
  310. url:url
  311. })
  312. },
  313. reLaunch:function(url){
  314. //路由跳转:关闭当前页跳转到新页面
  315. uni.reLaunch({
  316. url:url
  317. })
  318. },
  319. switchTabTo:function(url){
  320. //路由跳转:底部 tab页
  321. uni.switchTab({
  322. url:url
  323. })
  324. },
  325. navigateBack:function(page){
  326. uni.navigateBack({
  327. delta: page
  328. })
  329. },
  330. isNumber:function(value){//验证是否为数字
  331. var patrn = /^(-)?\d+(\.\d+)?$/
  332. if (patrn.exec(value) == null || value == '') {
  333. return false
  334. } else {
  335. return true
  336. }
  337. },
  338. getWindowHeight:function(){
  339. // 获取窗口高度
  340. const {windowHeight, pixelRatio} = wx.getSystemInfoSync()
  341. return windowHeight
  342. },
  343. adaptRichTextImg:function(res){
  344. /**
  345. *@富文本实现图片自适应
  346. *@style再添加自适应样式
  347. */
  348. const html = res.replace(/<img[^>]*>/gi,function(match,capture){
  349. let match1 = match.replace(/<img*/gi, '<img style="width:100% !important;height:auto !important;float:left !important;"'),
  350. results = match1.replace(/style=/gi, 'style="width:100%;height:auto;float:left;"')
  351. return results
  352. })
  353. return html
  354. },
  355. FormatMoney:function(num){
  356. // 金额千分位
  357. return num.toString().replace(/\d+/, function (n) { // 先提取整数部分
  358. return n.replace(/(\d)(?=(\d{3})+$)/g, function ($1) { // 对整数部分添加分隔符
  359. return $1 + ','
  360. })
  361. })
  362. },
  363. formatDate:function(){
  364. //获取当前时间
  365. let date = new Date()
  366. let y = date.getFullYear()
  367. let MM = date.getMonth() + 1
  368. MM = MM < 10 ? ('0' + MM) : MM
  369. let d = date.getDate()
  370. d = d < 10 ? ('0' + d) : d
  371. let h = date.getHours()
  372. h = h < 10 ? ('0' + h) : h
  373. let m = date.getMinutes()
  374. m = m < 10 ? ('0' + m) : m
  375. let s = date.getSeconds()
  376. s = s < 10 ? ('0' + s) : s
  377. return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
  378. },
  379. regexSets:function() {
  380. let sets = {
  381. 'companyName': /^[\u4e00-\u9fa5\(\)()\s\da-zA-Z&]{2,50}$/gi,
  382. 'phoneAndTelephone': /^([1]\d{10}|([\((]?0[0-9]{2,3}[)\)]?[-]?)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?)$/,
  383. 'bankNum': /^([1-9]{1})(\d{18})$/,
  384. 'invalidChar': /^[\s\u4e00-\u9fa5a-z0-9_-]{0,}$/
  385. }
  386. return sets
  387. },
  388. timestampToTime:function(timestamp) {
  389. // 时间戳转日期
  390. let date = new Date(timestamp * 1000)//时间戳为10位需*1000,时间戳为13位的话不需乘1000
  391. let Y = date.getFullYear() + '-'
  392. let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-'
  393. let D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate()) + ' '
  394. let h = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours()) + ':'
  395. let m = (date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes()) + ':'
  396. let s = (date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds())
  397. return `${Y}${M}${D}${h}${m}${s}`
  398. },
  399. easyFormatData(timestamp){
  400. //时间转换
  401. let a = new Date(timestamp).getTime()
  402. const date = new Date(a)
  403. const Y = date.getFullYear() + '-'
  404. const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
  405. const D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate()) + ' '
  406. const h = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours()) + ':'
  407. const m = (date.getMinutes() <10 ? '0'+date.getMinutes() : date.getMinutes())
  408. // const s = date.getSeconds(); // 秒
  409. const dateString = Y + M + D + h + m
  410. // console.log('dateString', dateString); // > dateString 2021-07-06 14:23
  411. return dateString
  412. }
  413. }
  414. /**
  415. *@导出
  416. */
  417. module.exports = {
  418. get: caimeiApi.get,
  419. post: caimeiApi.post,
  420. lodingGet: caimeiApi.lodingGet,
  421. isNumber: caimeiApi.isNumber,
  422. FormatMoney: caimeiApi.FormatMoney,
  423. navigateTo: caimeiApi.navigateTo,
  424. reLaunch: caimeiApi.reLaunch,
  425. redirectTo: caimeiApi.redirectTo,
  426. switchTabTo: caimeiApi.switchTabTo,
  427. navigateBack: caimeiApi.navigateBack,
  428. formatDate: caimeiApi.formatDate,
  429. loginStatus: caimeiApi.loginStatus,
  430. setStorage: caimeiApi.setStorage,
  431. getStorage: caimeiApi.getStorage,
  432. removeStorage: caimeiApi.removeStorage,
  433. getComStorage: caimeiApi.getComStorage,
  434. navToListPage: caimeiApi.navToListPage,
  435. navigateToGoods: caimeiApi.navigateToGoods,
  436. getWindowHeight: caimeiApi.getWindowHeight,
  437. adaptRichTextImg: caimeiApi.adaptRichTextImg,
  438. getStorageAddressKey: caimeiApi.getStorageAddressKey,
  439. regexSets: caimeiApi.regexSets,
  440. timestampToTime: caimeiApi.timestampToTime,
  441. BannerNavigateTo:caimeiApi.BannerNavigateTo,
  442. FlooryNavigateTo:caimeiApi.FlooryNavigateTo,
  443. easyFormatData:caimeiApi.easyFormatData
  444. }