| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- const env = process.env.NODE_ENV === "development" ? "dev" : "prd";
- let apis = {
- dev: {
- serverUrl: "https://cloud.kuaiyuman.cn/admin/",
- fileUrl: "https://zyp-1258963180.cos.ap-guangzhou.myqcloud.com/"
- },
- prd: {
- serverUrl: "https://cloud.kuaiyuman.cn/admin/",
- fileUrl: "https://zyp-1258963180.cos.ap-guangzhou.myqcloud.com/",
- },
- };
- const cfg = {
- key: {
- token: 'kuaiyuman.token',
- user: 'kuaiyuman.user',
- },
- env: env,
- api: {
- serverUrl: apis[env].serverUrl,
- fileUrl: apis[env].fileUrl,
- uploadUrl: apis[env].serverUrl + "file/upload"
- }
- };
- const serverUrl = cfg.api.serverUrl;
- const fileUrl = cfg.api.fileUrl;
- const isEmptyOrNull = function (exp) {
- return !exp || typeof (exp) == "undefined" || exp.length === 0 || exp === '' || JSON.stringify(exp) === "{}";
- };
- /**
- * get请求封装
- * @param url
- * @param param
- */
- const get = (url, param = {}) => {
- let token = uni.getStorageSync(cfg.key.token) || "";
- if (!isEmptyOrNull(param)) {
- var params = [];
- for (var key in param) {
- params.push(encodeURIComponent(key) + "=" + encodeURIComponent(param[key]));
- }
- param = params.join("&")
- }
- let options = {
- url: fillUrl(url) + (isEmptyOrNull(param) ? "" : "?" + param),
- data: param,
- method: 'GET',
- header: {
- "Accept": "application/json",
- "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
- "satoken": token,
- },
- };
- return request(options)
- };
- const post = (url, param = {}) => {
- let token = uni.getStorageSync(cfg.key.token) || "";
- let options = {
- url: fillUrl(url),
- data: param,
- method: 'POST',
- header: {
- 'X-Requested-With': 'XMLHttpRequest',
- "Accept": "application/json",
- "Content-Type": "application/json; charset=UTF-8",
- "satoken": token,
- },
- dataType: 'json'
- };
- return request(options);
- };
- const request = (options) => {
- return new Promise((resolve, reject) => {
- uni.request({
- url: options.url,
- data: options.data,
- method: options.method,
- header: options.header,
- dataType: options.dataType
- }).then(res => {
- console.log("resp>>>", res)
- let response = res.data;
- if (response.code !== 200) {
- if (response.code == 10001) {
- uni.showToast({
- title: response.message,
- icon: 'none'
- });
- setTimeout(() => {
- uni.navigateTo({
- url: `/pages/login/index`
- })
- }, 300)
- } else {
- let errMsg = response.message || '网络异常,请稍后重试';
- uni.showToast({
- title: errMsg,
- icon: 'none'
- });
- reject(errMsg);
- }
- } else {
- resolve(response.data);
- }
- }).catch(error => {
- uni.hideLoading();
- uni.showToast({
- title: '网络异常,请稍后重试',
- icon: 'none'
- });
- console.error("error=>", error);
- reject(error.msg);
- })
- });
- };
- const upload = opt => {
- opt = opt || {};
- opt.url = opt.url || '';
- opt.filePath = opt.filePath || null;//要上传文件资源的路径。
- opt.name = opt.name || null;//文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
- opt.filePath = opt.filePath || null;
- opt.success = opt.success || function () {
- };
- uni.uploadFile({
- url: fillUrl(opt.url),
- filePath: opt.filePath,
- name: opt.name,
- success: function (res) {
- console.log("upload file=>", res)
- opt.success(res);
- },
- fail: function () {
- uni.showToast({
- title: '请稍后重试'
- });
- }
- })
- };
- const fillUrl = function (url) {
- if (url.indexOf("http") === 0) {
- return url;
- } else {
- return serverUrl + url;
- }
- };
- const fmtUrl = v => {
- if (v == null || v == "") {
- return "/static/missing-face.png";
- }
- if (v.indexOf("http") === 0) {
- return v;
- }
- return fileUrl + v.replace(/\\/g, "/");
- };
- const fmtMoney = function (money) {
- if (!money) {
- return "0.00";
- }
- return (money / 100).toFixed(2);
- }
- const fmtDate = function (time, format) {
- time = time ? new Date(time) : new Date()
- format = format || 'YYYY-MM-DD'
- function tf(i) {
- return (i < 10 ? '0' : '') + i
- }
- // @ts-ignore
- return format.replace(/YYYY|MM|DD|hh|mm|ss|WW/g, function (a) {
- switch (a) {
- case 'YYYY':
- return tf(time.getFullYear())
- case 'MM':
- return tf(time.getMonth() + 1)
- case 'DD':
- return tf(time.getDate())
- case 'mm':
- return tf(time.getMinutes())
- case 'hh':
- return tf(time.getHours())
- case 'ss':
- return tf(time.getSeconds())
- case 'WW':
- return ['日', '一', '二', '三', '四', '五', '六'][time.getDay()]
- }
- })
- }
- const msg = (title, icon = 'none', duration = 1800, mask = false) => {
- //统一提示方便全局修改
- if (Boolean(title) === false) {
- return;
- }
- uni.showToast({
- title,
- duration,
- mask,
- icon
- });
- };
- export {
- get, post, upload, cfg, serverUrl, fileUrl, fmtUrl, fmtMoney, msg, fmtDate
- }
|