caimeiApi.js 7.9 KB

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