api.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. import config from './config';
  2. import dever from './core';
  3. const page = {
  4. value: 1,
  5. data: [],
  6. key: '',
  7. state: true,
  8. record: {},
  9. get(api, configParam, vue, url, options = {}, callback, errCallback) {
  10. options = api.option(options);
  11. if (typeof(configParam) == 'object') {
  12. var state = configParam[0];
  13. this.key = configParam[1];
  14. } else {
  15. var state = configParam;
  16. this.key = '';
  17. }
  18. if (state == 1) {
  19. this.value = 1;
  20. this.data = [];
  21. } else {
  22. this.value++;
  23. }
  24. if (this.state == false && state != 1) {
  25. return;
  26. }
  27. options.page = this.value;
  28. return api.fetch(vue, url, options, callback, errCallback);
  29. },
  30. merge(result, options) {
  31. this.state = true;
  32. const pageKey = this.key;
  33. if (pageKey && result[pageKey]?.length) {
  34. if (options.concat === -1) this.data = result[pageKey].concat(this.data);
  35. else this.data = this.data.concat(result[pageKey]);
  36. result[pageKey] = this.data;
  37. } else if (result?.length) {
  38. if (options.concat === -1) this.data = result.reverse().concat(this.data);
  39. else this.data = this.data.concat(result);
  40. result = this.data;
  41. } else {
  42. this.state = false;
  43. if (pageKey) {
  44. result[pageKey] = this.data;
  45. } else {
  46. result = this.data;
  47. }
  48. }
  49. return result
  50. },
  51. }
  52. const upload = {
  53. data: {},
  54. name: 'file',
  55. act(item, key, count, callback) {
  56. if (!count) {
  57. count = 1;
  58. }
  59. var type = 1;
  60. if (count > 1) {
  61. type = 2;
  62. }
  63. uni.uploadFile({
  64. url: config.server.upload,
  65. filePath: item,
  66. name: this.name,
  67. formData: api.option(),
  68. success: res => {
  69. dever.debug(res);
  70. var data = JSON.parse(res.data);
  71. uni.hideLoading();
  72. if (data.data.url) {
  73. var backUrl = data.data.url;
  74. if (count > 1) {
  75. if (!this.data[key]) {
  76. this.data[key] = [];
  77. }
  78. if (this.data[key].length < count) {
  79. this.data[key].push(backUrl);
  80. }
  81. } else {
  82. this.data[key] = backUrl;
  83. }
  84. if (callback) {
  85. callback(type, this.data[key]);
  86. }
  87. } else {
  88. uni.showToast({
  89. title: data.data.msg,
  90. icon: 'none'
  91. });
  92. }
  93. }
  94. });
  95. },
  96. }
  97. const api = {
  98. key: 'd',
  99. code: 'p',
  100. token: 't',
  101. env: 'env',
  102. platform: 'platform',
  103. record: {},
  104. // token / code 存取
  105. getCode() {
  106. return uni.getStorageSync(this.key + '_' + this.code);
  107. },
  108. setCode(value) {
  109. return uni.setStorageSync(this.key + '_' + this.code, value);
  110. },
  111. getToken() {
  112. return uni.getStorageSync(this.key + '_' + this.token);
  113. },
  114. setToken(value) {
  115. return uni.setStorageSync(this.key + '_' + this.token, value);
  116. },
  117. checkLogin(refer) {
  118. if (!this.getToken()) dever.goLogin(refer);
  119. },
  120. option(options = {}) {
  121. options[this.env] = dever.env;
  122. options[this.platform] = dever.platform;
  123. options[this.code] = this.getCode();
  124. options[this.token] = this.getToken();
  125. return options;
  126. },
  127. //静默注册
  128. async reg() {
  129. if (config.base.reg && !this.getToken()) {
  130. // 未登录,自动注册去
  131. const token = await this.register(config.base.reg)
  132. if (token) {
  133. this.setToken(token)
  134. }
  135. }
  136. },
  137. // GET / POST 封装
  138. get(vue, url, options = {}, callback, errCallback) {
  139. return this.fetch(vue, url, options, callback, errCallback);
  140. },
  141. post(url, options = {}, callback, errCallback) {
  142. return this.fetch('post', url, options, callback, errCallback);
  143. },
  144. // 分页
  145. page(configParam, vue, url, options = {}, callback, errCallback) {
  146. return page.get(this, configParam, vue, url, options, callback, errCallback);
  147. },
  148. //上传
  149. upload(item, key, count, callback) {
  150. return upload.act(item, key, count, callback);
  151. },
  152. // 核心请求
  153. fetch(vue, url, options = {}, callback, errCallback) {
  154. const jump = dever.data('jump');
  155. if (jump) {
  156. dever.data('jump', 'del')
  157. return dever.location(jump);
  158. }
  159. var method = 'GET'
  160. if (vue == 'post') {
  161. method = 'POST';
  162. if (typeof(url) == 'object') {
  163. vue = url[1];
  164. url = url[0];
  165. } else {
  166. vue = false;
  167. }
  168. }
  169. if (vue) vue.isLoading = true;
  170. this.request(method, url, options).then((result) => {
  171. if (vue) {
  172. if (!vue.fetch) {
  173. vue.fetch = {}
  174. }
  175. vue.isLoading = false;
  176. vue.fetch = Object.assign({}, vue.fetch, result)
  177. }
  178. if (callback) callback(result);
  179. }).catch((result) => {
  180. if (vue) vue.isLoading = false;
  181. if (errCallback) {
  182. errCallback(result);
  183. } else {
  184. dever.alert(result.msg)
  185. }
  186. })
  187. },
  188. request: function(method, url, options) {
  189. options = this.option(options);
  190. config.request.method = method;
  191. if (config.server[url]) config.request.url = config.server[url];
  192. else config.request.url = url;
  193. if (config.request.url.indexOf('http') == -1) {
  194. config.request.url = config.request.host + config.request.url;
  195. }
  196. if (!options.noconcat) options.noconcat = false;
  197. if (options.page && options.page > 0) options.pg = options.page;
  198. config.request.data = options;
  199. return new Promise((resolve, reject) => {
  200. config.request.complete = (response) => {
  201. dever.debug(response);
  202. if (response.statusCode === 200) {
  203. if (response.data.code === 200) {
  204. if (url != config.base.init && response.data.u) {
  205. dever.upConfig(response.data.u);
  206. }
  207. this.record = response.data.page || {};
  208. var result = response.data.data
  209. if (options.page && options.page > 0 && !options.noconcat) {
  210. result = page.merge(result, options);
  211. }
  212. resolve(result);
  213. } else if (response.data.code === -1) {
  214. //退出
  215. this.setToken('');
  216. dever.goHome();
  217. } else if (response.data.code === -2) {
  218. dever.confirm('您没有权限', () => {
  219. dever.goHome();
  220. }, () => {
  221. dever.goHome();
  222. });
  223. } else if (response.data.code === -3) {
  224. dever.confirm('您没有权限', () => {
  225. dever.goUser();
  226. }, () => {
  227. dever.goHome();
  228. });
  229. } else if (response.data.code === -4) {
  230. dever.confirm('您需要登录才能继续操作', () => {
  231. this.setToken('');
  232. dever.goLogin();
  233. }, () => {
  234. dever.goHome();
  235. });
  236. } else if (response.data.code === -5) {
  237. dever.confirm('您需要绑定手机号才能继续操作', () => {
  238. dever.goBindMobile();
  239. }, () => {
  240. dever.goHome();
  241. });
  242. } else if (response.data.code === -6) {
  243. dever.confirm('您需要绑定邮箱才能继续操作', () => {
  244. dever.goBindEmail();
  245. }, () => {
  246. dever.goHome();
  247. });
  248. } else {
  249. reject(response.data);
  250. }
  251. } else {
  252. reject(response);
  253. }
  254. if (!options.noloading) this.hide(config.request.method, options, url);
  255. };
  256. if (!options.noloading) this.show(config.request.method, options, url);
  257. uni.request(config.request);
  258. });
  259. },
  260. show: function(method, options, url) {
  261. var title = '加载中';
  262. if (method == 'POST') {
  263. title = '提交中';
  264. }
  265. uni.showLoading({
  266. title: title,
  267. mask: true
  268. });
  269. //uni.showNavigationBarLoading();
  270. uni.stopPullDownRefresh();
  271. },
  272. hide: function(method, options, url) {
  273. uni.hideLoading();
  274. //uni.hideNavigationBarLoading();
  275. },
  276. async register(url) {
  277. try {
  278. let payload = {}
  279. // -------------------- 小程序 --------------------
  280. if (dever.env === 5) {
  281. const loginRes = await new Promise((resolve, reject) => {
  282. uni.login({
  283. provider: dever.platform,
  284. success: resolve,
  285. fail: reject
  286. })
  287. })
  288. payload.applet_code = loginRes.code
  289. payload.uuid = loginRes.code
  290. // -------------------- App --------------------
  291. } else if (dever.env === 4) {
  292. return false;
  293. const uuid = plus.device.uuid
  294. payload.uuid = uuid
  295. // -------------------- H5/PC --------------------
  296. } else {
  297. //return false
  298. const {
  299. v4: uuidv4
  300. } = await import('uuid')
  301. let uuid = uni.getStorageSync('uuid')
  302. if (!uuid) {
  303. uuid = uuidv4()
  304. uni.setStorageSync('uuid', uuid)
  305. }
  306. payload.uuid = uuid
  307. }
  308. // 调用静默注册接口
  309. const res = await this.request('get', url, {
  310. noloading: 1,
  311. ...payload
  312. })
  313. console.info(res.t)
  314. if (res && res.t) {
  315. return res.t
  316. }
  317. return false;
  318. } catch (err) {
  319. console.error('静默注册失败:', err)
  320. throw err
  321. }
  322. },
  323. };
  324. export default api;