123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- import config from './config';
- import dever from './core';
- const page = {
- value: 1,
- data: [],
- key: '',
- state: true,
- record: {},
- get(api, configParam, vue, url, options = {}, callback, errCallback) {
- options = api.option(options);
- if (typeof(configParam) == 'object') {
- var state = configParam[0];
- this.key = configParam[1];
- } else {
- var state = configParam;
- this.key = '';
- }
- if (state == 1) {
- this.value = 1;
- this.data = [];
- } else {
- this.value++;
- }
- if (this.state == false && state != 1) {
- return;
- }
- options.page = this.value;
- return api.fetch(vue, url, options, callback, errCallback);
- },
- merge(result, options) {
- this.state = true;
- const pageKey = this.key;
- if (pageKey && result[pageKey]?.length) {
- if (options.concat === -1) this.data = result[pageKey].concat(this.data);
- else this.data = this.data.concat(result[pageKey]);
- result[pageKey] = this.data;
- } else if (result?.length) {
- if (options.concat === -1) this.data = result.reverse().concat(this.data);
- else this.data = this.data.concat(result);
- result = this.data;
- } else {
- this.state = false;
- if (pageKey) {
- result[pageKey] = this.data;
- } else {
- result = this.data;
- }
- }
- return result
- },
- }
- const upload = {
- data: {},
- name: 'file',
- act(item, key, count, callback) {
- if (!count) {
- count = 1;
- }
- var type = 1;
- if (count > 1) {
- type = 2;
- }
- uni.uploadFile({
- url: config.server.upload,
- filePath: item,
- name: this.name,
- formData: api.option(),
- success: res => {
- dever.debug(res);
- var data = JSON.parse(res.data);
- uni.hideLoading();
- if (data.data.url) {
- var backUrl = data.data.url;
- if (count > 1) {
- if (!this.data[key]) {
- this.data[key] = [];
- }
- if (this.data[key].length < count) {
- this.data[key].push(backUrl);
- }
- } else {
- this.data[key] = backUrl;
- }
- if (callback) {
- callback(type, this.data[key]);
- }
- } else {
- uni.showToast({
- title: data.data.msg,
- icon: 'none'
- });
- }
- }
- });
- },
- }
- const api = {
- key: 'd',
- code: 'p',
- token: 't',
- env: 'env',
- platform: 'platform',
- record: {},
-
- // token / code 存取
- getCode() {
- return uni.getStorageSync(this.key + '_' + this.code);
- },
- setCode(value) {
- return uni.setStorageSync(this.key + '_' + this.code, value);
- },
- getToken() {
- return uni.getStorageSync(this.key + '_' + this.token);
- },
- setToken(value) {
- return uni.setStorageSync(this.key + '_' + this.token, value);
- },
- checkLogin(refer) {
- if (!this.getToken()) dever.goLogin(refer);
- },
- option(options = {}) {
- options[this.env] = dever.env;
- options[this.platform] = dever.platform;
- options[this.code] = this.getCode();
- options[this.token] = this.getToken();
- return options;
- },
- //静默注册
- async reg() {
- if (config.base.reg && !this.getToken()) {
- // 未登录,自动注册去
- const token = await this.register(config.base.reg)
- if (token) {
- this.setToken(token)
- }
- }
- },
- // GET / POST 封装
- get(vue, url, options = {}, callback, errCallback) {
- return this.fetch(vue, url, options, callback, errCallback);
- },
- post(url, options = {}, callback, errCallback) {
- return this.fetch('post', url, options, callback, errCallback);
- },
- // 分页
- page(configParam, vue, url, options = {}, callback, errCallback) {
- return page.get(this, configParam, vue, url, options, callback, errCallback);
- },
- //上传
- upload(item, key, count, callback) {
- return upload.act(item, key, count, callback);
- },
- // 核心请求
- fetch(vue, url, options = {}, callback, errCallback) {
- const jump = dever.data('jump');
- if (jump) {
- dever.data('jump', 'del')
- return dever.location(jump);
- }
- var method = 'GET'
- if (vue == 'post') {
- method = 'POST';
- if (typeof(url) == 'object') {
- vue = url[1];
- url = url[0];
- } else {
- vue = false;
- }
- }
- if (vue) vue.isLoading = true;
- this.request(method, url, options).then((result) => {
- if (vue) {
- if (!vue.fetch) {
- vue.fetch = {}
- }
- vue.isLoading = false;
- vue.fetch = Object.assign({}, vue.fetch, result)
- }
- if (callback) callback(result);
- }).catch((result) => {
- if (vue) vue.isLoading = false;
- if (errCallback) {
- errCallback(result);
- } else {
- dever.alert(result.msg)
- }
- })
- },
- request: function(method, url, options) {
- options = this.option(options);
- config.request.method = method;
- if (config.server[url]) config.request.url = config.server[url];
- else config.request.url = url;
- if (config.request.url.indexOf('http') == -1) {
- config.request.url = config.request.host + config.request.url;
- }
- if (!options.noconcat) options.noconcat = false;
- if (options.page && options.page > 0) options.pg = options.page;
- config.request.data = options;
- return new Promise((resolve, reject) => {
- config.request.complete = (response) => {
- dever.debug(response);
- if (response.statusCode === 200) {
- if (response.data.code === 200) {
- if (url != config.base.init && response.data.u) {
- dever.upConfig(response.data.u);
- }
- this.record = response.data.page || {};
- var result = response.data.data
- if (options.page && options.page > 0 && !options.noconcat) {
- result = page.merge(result, options);
- }
- resolve(result);
- } else if (response.data.code === -1) {
- //退出
- this.setToken('');
- dever.goHome();
- } else if (response.data.code === -2) {
- dever.confirm('您没有权限', () => {
- dever.goHome();
- }, () => {
- dever.goHome();
- });
- } else if (response.data.code === -3) {
- dever.confirm('您没有权限', () => {
- dever.goUser();
- }, () => {
- dever.goHome();
- });
- } else if (response.data.code === -4) {
- dever.confirm('您需要登录才能继续操作', () => {
- this.setToken('');
- dever.goLogin();
- }, () => {
- dever.goHome();
- });
- } else if (response.data.code === -5) {
- dever.confirm('您需要绑定手机号才能继续操作', () => {
- dever.goBindMobile();
- }, () => {
- dever.goHome();
- });
- } else if (response.data.code === -6) {
- dever.confirm('您需要绑定邮箱才能继续操作', () => {
- dever.goBindEmail();
- }, () => {
- dever.goHome();
- });
- } else {
- reject(response.data);
- }
- } else {
- reject(response);
- }
- if (!options.noloading) this.hide(config.request.method, options, url);
- };
- if (!options.noloading) this.show(config.request.method, options, url);
- uni.request(config.request);
- });
- },
- show: function(method, options, url) {
- var title = '加载中';
- if (method == 'POST') {
- title = '提交中';
- }
- uni.showLoading({
- title: title,
- mask: true
- });
- //uni.showNavigationBarLoading();
- uni.stopPullDownRefresh();
- },
- hide: function(method, options, url) {
- uni.hideLoading();
- //uni.hideNavigationBarLoading();
- },
- async register(url) {
- try {
- let payload = {}
- // -------------------- 小程序 --------------------
- if (dever.env === 5) {
- const loginRes = await new Promise((resolve, reject) => {
- uni.login({
- provider: dever.platform,
- success: resolve,
- fail: reject
- })
- })
- payload.applet_code = loginRes.code
- payload.uuid = loginRes.code
- // -------------------- App --------------------
- } else if (dever.env === 4) {
- return false;
- const uuid = plus.device.uuid
- payload.uuid = uuid
- // -------------------- H5/PC --------------------
- } else {
- //return false
- const {
- v4: uuidv4
- } = await import('uuid')
- let uuid = uni.getStorageSync('uuid')
- if (!uuid) {
- uuid = uuidv4()
- uni.setStorageSync('uuid', uuid)
- }
- payload.uuid = uuid
- }
- // 调用静默注册接口
- const res = await this.request('get', url, {
- noloading: 1,
- ...payload
- })
- console.info(res.t)
- if (res && res.t) {
- return res.t
- }
- return false;
- } catch (err) {
- console.error('静默注册失败:', err)
- throw err
- }
- },
- };
- export default api;
|