caimeiApi.js 7.9 KB

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