caimeiApi.js 7.7 KB

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