caimeiApi.js 7.4 KB

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