caimeiApi.js 7.2 KB

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