request.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. const app = getApp();
  2. // download.zhiqiyun.com 域名
  3. const env = {
  4. NODE_ENV: 'prd',
  5. dev: {
  6. apiUrl: 'https://apitest.tongyu99.com',
  7. },
  8. prd: {
  9. apiUrl: 'https://apimall.zhiqiyun.com',
  10. },
  11. pota: {
  12. apiUrl: 'http://192.168.110.59:8098' //
  13. }
  14. }
  15. var header = {
  16. 'content-type': 'application/x-www-form-urlencoded',
  17. /*--商户的appi通过参数传入--*/
  18. 'appId': '',
  19. // 'appId': 'ZQesLvuSYEejOJB', //测试
  20. // 'appId': 'ZQWYx7XoOOhv4Ns', //正式
  21. /*----*/
  22. }
  23. /**小程序配置 */
  24. const publics = {
  25. }
  26. /*
  27. * 获取token
  28. */
  29. const getToken = (success) => {
  30. let token = getStorage('AUTH_TOKEN');
  31. if (token) {
  32. success.call(this, token);
  33. return false;
  34. } else {
  35. loadToken(data => {
  36. success.call(this, data);
  37. });
  38. }
  39. }
  40. const loadToken = (success) => {
  41. let that = this
  42. uni.request({
  43. url: env[env.NODE_ENV].apiUrl + '/api/access_token',
  44. data: {},
  45. method: 'get',
  46. header: header,
  47. success(json) {
  48. if (json.statusCode !== 200) return msg(json.data.message);
  49. if (json.data.code !== 0) return msg(json.data.msg)
  50. setStorage('AUTH_TOKEN', json.data.data);
  51. success.call(that, json.data.data);
  52. },
  53. fail() {
  54. msg('access_token 获取失败');
  55. }
  56. })
  57. }
  58. const clearValueEmpty = (data) => {
  59. let keyValue = {};
  60. for (let key in data) {
  61. let value = typeof data[key];
  62. if (value == 'string' && value) {
  63. if (data[key] != 'undefined' && data[key] != " " && data[key] != undefined && data[key] != null) {
  64. keyValue[key] = data[key];
  65. }
  66. } else if (value == 'number' && value != null) {
  67. keyValue[key] = data[key];
  68. } else if (value == 'boolean') {
  69. keyValue[key] = data[key];
  70. } else {
  71. if (data[key]) keyValue[key] = data[key];
  72. }
  73. }
  74. return keyValue;
  75. }
  76. /**
  77. * 公共请求
  78. *
  79. * @param {*} url 请求url
  80. * @param {*} data 请求参数
  81. * @param {*} method 请求方法
  82. * @param {*} success 成功函数
  83. * @param {*} isLoad 是否显示加载提示
  84. */
  85. const baseRequest = (url, appId, data, method, success, isLoad) => {
  86. header['appId'] = appId;
  87. getToken(token => {
  88. if (isLoad) {
  89. load('加载中')
  90. }
  91. header.authorityToken = token;
  92. if (url.indexOf('/v3') != -1 || url.indexOf('/paper/create') != -1) {
  93. header['content-type'] = 'application/json;charset=UTF-8'
  94. } else {
  95. header['content-type'] = 'application/x-www-form-urlencoded'
  96. }
  97. try {
  98. var device_info = {};
  99. //第三放巨量投放渠道标记
  100. var pickleChannel = getStorage('pickleChannel');
  101. device_info.pickleChannel = pickleChannel;
  102. var packageChannel = getStorage('packageChannel');
  103. device_info.packageChannel = packageChannel;
  104. var deviceinfoString = JSON.stringify(device_info);
  105. deviceinfoString = deviceinfoString.replace(/{/g, '').replace(/}/g, '').replace(/:/g, '=')
  106. .replace(
  107. /,/g,
  108. ';').replace(/"/g,
  109. '');
  110. header['device-info'] = deviceinfoString;
  111. } catch (e) {}
  112. uni.request({
  113. url: env[env.NODE_ENV].apiUrl + url,
  114. data: clearValueEmpty(data),
  115. method: method,
  116. header: header,
  117. success(json) {
  118. if (isLoad) {
  119. uni.hideLoading();
  120. }
  121. if (json.statusCode !== 200) return msg(json.data.message);
  122. if (json.data.code !== 0) {
  123. return msg(json.data.msg)
  124. }
  125. let data = json.data.data;
  126. if (typeof data === 'string' && data.indexOf("{") === 0) {
  127. data = JSON.parse(data);
  128. }
  129. success.call(this, data);
  130. },
  131. fail() {
  132. if (isLoad) {
  133. uni.hideLoading();
  134. }
  135. },
  136. })
  137. })
  138. }
  139. const loadIng = (msg) => {
  140. uni.showLoading({
  141. title: msg,
  142. mask: true
  143. })
  144. }
  145. /**POST请求 */
  146. const postRequest = (url, appId, data, success, isLoad) => {
  147. header['content-type'] = 'application/x-www-form-urlencoded'
  148. baseRequest(url, appId, data, 'post', success, isLoad);
  149. }
  150. const postRequestLoding = (url, appId, data, success, isLoding) => {
  151. header['content-type'] = 'application/x-www-form-urlencoded'
  152. baseRequest(url, appId, data, 'post', success, false, isLoding);
  153. }
  154. /**GET请求 */
  155. const getRequest = (url, appId, data, success, isLoad) => {
  156. header['content-type'] = 'application/x-www-form-urlencoded'
  157. baseRequest(url, appId, data, 'get', success, isLoad);
  158. }
  159. /**Put请求 */
  160. const putRequest = (url, appId, data, success, isLoad) => {
  161. header['content-type'] = 'application/x-www-form-urlencoded'
  162. baseRequest(url, appId, data, 'put', success, isLoad)
  163. }
  164. const putRequestJson = (url, appId, data, success, isLoad) => {
  165. header['content-type'] = 'application/json;charset=UTF-8'
  166. baseRequest(url, appId, data, 'put', success, isLoad)
  167. }
  168. /**弹窗 */
  169. const msg = (title, success) => {
  170. if (title) {
  171. uni.showToast({
  172. title: title,
  173. icon: 'none',
  174. success() {
  175. if (success) success.call(this);
  176. }
  177. });
  178. }
  179. return false;
  180. }
  181. const load = msg => {
  182. uni.showLoading({
  183. title: msg,
  184. mask: true
  185. })
  186. }
  187. // 去掉字符串中的特殊字符和转义字符
  188. const excludeSpecial = (s) => {
  189. // 去掉转义字符
  190. // s = s.replace(/[\'\"\\\/\b\f\n\r\t]/g, '');
  191. const pattern = /[`~!@#$^&*()=|{}':;',\\\[\]\.<>\/?~!@#¥……&*()——|{}【】';:""' + - - _ % 。,、?\s]/g;
  192. if (s != undefined || s != null) {
  193. s = s.replace(pattern, "")
  194. }
  195. return s;
  196. }
  197. const setStorage = (key, value) => {
  198. uni.setStorageSync(env.NODE_ENV + "_" + key, value)
  199. }
  200. const getStorage = (key) => {
  201. return uni.getStorageSync(env.NODE_ENV + "_" + key)
  202. }
  203. const removeStorage = (key) => {
  204. return uni.removeStorageSync(env.NODE_ENV + "_" + key);
  205. }
  206. module.exports = {
  207. setStorage: setStorage,
  208. getStorage: getStorage,
  209. postRequest: postRequest,
  210. postRequestLoding: postRequestLoding,
  211. getRequest: getRequest,
  212. putRequest: putRequest,
  213. putRequestJson: putRequestJson,
  214. msg: msg,
  215. load: load,
  216. getToken: getToken,
  217. loadIng: loadIng,
  218. removeStorage: removeStorage,
  219. public: publics,
  220. header: header,
  221. excludeSpecial: excludeSpecial,
  222. }