api.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. if (jump > 0) {
  158. var uri = 'source/info?id=' + jump;
  159. return dever.location(uri);
  160. }
  161. }
  162. var method = 'GET'
  163. if (vue == 'post') {
  164. method = 'POST';
  165. if (typeof(url) == 'object') {
  166. vue = url[1];
  167. url = url[0];
  168. } else {
  169. vue = false;
  170. }
  171. if (options.submit !== undefined) {
  172. if (options.submit == true) {
  173. return
  174. }
  175. options.submit = true
  176. }
  177. }
  178. this.request(method, url, options).then((result) => {
  179. if (vue) {
  180. if (!vue.fetch) {
  181. vue.fetch = {}
  182. }
  183. vue.fetch = Object.assign({}, vue.fetch, result)
  184. }
  185. if (callback) callback(result);
  186. if (options.submit !== undefined) {
  187. options.submit = false
  188. }
  189. }).catch((result) => {
  190. if (errCallback) {
  191. errCallback(result);
  192. } else {
  193. dever.alert(result.msg)
  194. }
  195. if (options.submit !== undefined) {
  196. options.submit = false
  197. }
  198. })
  199. },
  200. request: function(method, url, options) {
  201. options = this.option(options);
  202. config.request.method = method;
  203. if (config.server[url]) config.request.url = config.server[url];
  204. else config.request.url = url;
  205. if (config.request.url.indexOf('http') == -1) {
  206. config.request.url = config.request.host + config.request.url;
  207. }
  208. if (!options.noconcat) options.noconcat = false;
  209. if (options.page && options.page > 0) options.pg = options.page;
  210. config.request.data = options;
  211. return new Promise((resolve, reject) => {
  212. config.request.complete = (response) => {
  213. dever.debug(response);
  214. if (response.statusCode === 200) {
  215. if (response.data.code === 200) {
  216. if (url != config.base.init && response.data.u) {
  217. dever.upConfig(response.data.u);
  218. }
  219. this.record = response.data.page || {};
  220. var result = response.data.data
  221. if (options.page && options.page > 0 && !options.noconcat) {
  222. result = page.merge(result, options);
  223. }
  224. resolve(result);
  225. } else if (response.data.code === -1) {
  226. //退出
  227. this.setToken('');
  228. dever.goHome();
  229. } else if (response.data.code === -2) {
  230. dever.confirm('您没有权限', () => {
  231. dever.goHome();
  232. }, () => {
  233. dever.goHome();
  234. });
  235. } else if (response.data.code === -3) {
  236. dever.confirm('您没有权限', () => {
  237. dever.goUser();
  238. }, () => {
  239. dever.goHome();
  240. });
  241. } else if (response.data.code === -4) {
  242. dever.confirm('您需要登录才能继续操作', () => {
  243. this.setToken('');
  244. dever.goLogin();
  245. });
  246. } else if (response.data.code === -5) {
  247. dever.confirm('您需要绑定手机号才能继续操作', () => {
  248. dever.goBindMobile();
  249. }, () => {
  250. dever.goHome();
  251. });
  252. } else if (response.data.code === -6) {
  253. dever.confirm('您需要绑定邮箱才能继续操作', () => {
  254. dever.goBindEmail();
  255. }, () => {
  256. dever.goHome();
  257. });
  258. } else {
  259. reject(response.data);
  260. }
  261. } else {
  262. reject(response);
  263. }
  264. if (!options.noloading) this.hide(config.request.method, options, url);
  265. };
  266. if (!options.noloading) this.show(config.request.method, options, url);
  267. uni.request(config.request);
  268. });
  269. },
  270. show: function(method, options, url) {
  271. var title = '加载中';
  272. if (method == 'POST') {
  273. title = '提交中';
  274. }
  275. uni.showLoading({
  276. title: title,
  277. mask: true
  278. });
  279. //uni.showNavigationBarLoading();
  280. uni.stopPullDownRefresh();
  281. },
  282. hide: function(method, options, url) {
  283. uni.hideLoading();
  284. //uni.hideNavigationBarLoading();
  285. },
  286. async register(url) {
  287. try {
  288. let payload = {}
  289. // -------------------- 小程序 --------------------
  290. if (dever.env === 5) {
  291. const loginRes = await new Promise((resolve, reject) => {
  292. uni.login({
  293. provider: dever.platform,
  294. success: resolve,
  295. fail: reject
  296. })
  297. })
  298. payload.applet_code = loginRes.code
  299. payload.uuid = loginRes.code
  300. // -------------------- App --------------------
  301. } else if (dever.env === 4) {
  302. return false;
  303. const uuid = plus.device.uuid
  304. payload.uuid = uuid
  305. // -------------------- H5/PC --------------------
  306. } else {
  307. //return false
  308. const {
  309. v4: uuidv4
  310. } = await import('uuid')
  311. let uuid = uni.getStorageSync('uuid')
  312. if (!uuid) {
  313. uuid = uuidv4()
  314. uni.setStorageSync('uuid', uuid)
  315. }
  316. payload.uuid = uuid
  317. }
  318. // 调用静默注册接口
  319. const res = await this.request('get', url, {
  320. noloading: 1,
  321. ...payload
  322. })
  323. console.info(res.t)
  324. if (res && res.t) {
  325. return res.t
  326. }
  327. return false;
  328. } catch (err) {
  329. console.error('静默注册失败:', err)
  330. throw err
  331. }
  332. },
  333. };
  334. export default api;