123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- import ext from "./ext.js";
- // 请求域名
- var domain = 'https://openwork.dfwy.tech/'
- //var domain = 'http://saas.com/'
- // #ifdef MP-WEIXIN
- // 'http://127.0.0.1:8000/';
- // domain = uni.getAccountInfoSync().miniProgram.envVersion == 'release' ? 'https://openwork.dfwy.tech/' : 'https://openwork.dfwy.tech/';
- // domain = uni.getAccountInfoSync().miniProgram.envVersion == 'release' ? ext.getExtStoreId('release_host_url') : ext.getExtStoreId('host_url');
- var app_id = ext.getExtStoreId('app_id') ? ext.getExtStoreId('app_id') : '';
- // #endif
- // 发送网络请求的函数
- export const request = (url, data = {}, method = 'GET') => {
- // 获取登录标识
- let userLogin = uni.getStorageSync('userLogin');
- let shopId = uni.getStorageSync("shopId");
- // 合并参数
- if( userLogin && userLogin.authcode ) data = Object.assign({authcode:userLogin.authcode},data);
- if (shopId) data = Object.assign({shop_id:shopId},data);
- if (app_id) data = Object.assign({app_id:app_id},data);
- // 封装
- return new Promise((resolve, reject) => {
- uni.request({
- url: domain+url,
- method: method,
- data: data,
- success: (res) => {
- // 登录提示
- if(res.data.code == 'no_login'){
- // 清空登录标识
- uni.setStorageSync('userLogin',null);
- // 清空用户信息
- uni.setStorageSync('userInfo',null);
- // 前去登录
- uni.showModal({
- title:"请登录",
- success(res){
- if (res.confirm) {
- // 用户点击确定按钮
- uni.navigateTo({
- url:"/pages/login/index"
- })
- }
- }
- })
- }
- // 返回结果
- resolve(res.data)
- },
- fail: (err) => {
- reject(err)
- }
- })
- })
- }
- const fileupload = (url, filePath,data = {}) =>{
- // 获取登录标识
- let userLogin = uni.getStorageSync('userLogin');
- // 合并参数
- if( userLogin && userLogin.authcode ) data = Object.assign({authcode:userLogin.authcode},data);
- if (app_id) data = Object.assign({app_id:app_id},data);
- // 封装
- return new Promise((resolve, reject) => {
- uni.uploadFile({
- url: domain+url, // 你的上传接口地址
- filePath: filePath,
- name: 'file', // 必须填写,为了后端接收文件流的参数名字
- formData: data,// 其他要上传的参数
- success: (res) => {
- // 登录提示
- if(res.data.code == 'no_login'){
- // 前去登录
- uni.showModal({
- title:"请登录",
- success(res){
- if (res.confirm) {
- // 用户点击确定按钮
- uni.navigateTo({
- url:"/pages/login/index"
- })
- }
- }
- })
- }
- // 转json,php返回可能会带bom头需要先替换
- let resdata = JSON.parse(res.data.replace('\uFEFF',''));
- resolve(resdata)
- },
- fail: (err) => {
- reject(err)
- }
- });
- })
- }
- // 字符串键值对转参数对象
- const strToParam = (str, separator = '&')=> {
- // 先转码
- str = decodeURIComponent(str);
- let pairs = str.split(separator);
- let result = {};
- pairs.forEach(pair => {
- let [key, value] = pair.split('=');
- result[key] = value;
- });
- // 返回结果
- return result;
- }
- // 模块导出,{name:object} 为对象式,导出多个使用
- // export default {request:request,request1:request1};
- // 单个对象,直接导出如下
- export default {request:request,fileupload:fileupload,strToParam:strToParam}
|