admin.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // 引用工具类
  2. const utils = require('../common/util.js');
  3. //服务接口访问配置
  4. var config = {
  5. // sysweburl: "http://110.188.24.28:50300", // 开发
  6. // sysweburl: "http://110.188.24.28:50200", // 测试
  7. // sysweburl: "http://171.94.154.2:50105/hl", // 正式
  8. sysweburl: "https://new.zg12345.cn/hl", // 正式
  9. // upfileurl: "http://110.188.24.28:50120/file/upload", // 测试
  10. // upfileurl: "http://171.94.154.2:50105/hlfs/file/upload", // 正式
  11. upfileurl: "https://new.zg12345.cn/hlfs/file/upload", // 正式
  12. // getfileurl: "http://110.188.24.28:50120", // 测试
  13. // getfileurl: "http://171.94.154.2:50105/hlfs", // 正式
  14. getfileurl: "https://new.zg12345.cn/hlfs", // 正式
  15. }
  16. // 服务器返回字段
  17. var response = { //自定义响应字段
  18. statusName: 'code', //数据状态的字段名称
  19. msgName: 'msg', //状态信息的字段名称
  20. dataName: 'data' ,//数据详情的字段名称
  21. }
  22. // http状态码
  23. var statusCode = {
  24. ok: 200, //数据状态一切正常的状态码
  25. logout: 401, //登录状态失效的状态码
  26. tokenErr: 403, // openID错误,后端生成不了token的状态码
  27. }
  28. //录音对象
  29. const recorderManager = uni.getRecorderManager();
  30. //播放对象
  31. const innerAudioContext = uni.createInnerAudioContext();
  32. // 全局变量请求头
  33. var _token = ''; // token
  34. // 登录失效提示方法
  35. const logErrTip = () => {
  36. uni.hideLoading();
  37. uni.showModal({
  38. title: '登录提示',
  39. content: '您未授权登录,请授权',
  40. showCancel: true,
  41. confirmText: '确定',
  42. success: (e) => {
  43. if (e.confirm) {
  44. // 获取微信登录授权
  45. uni.switchTab({
  46. url: "/pages/Index/Mine"
  47. })
  48. }
  49. },
  50. fail: () => {},
  51. complete: () => {}
  52. })
  53. }
  54. // 清除缓存,引导用户重新登录
  55. const tokenErrTip = () => {
  56. uni.hideLoading();
  57. // 清楚缓存
  58. uni.removeStorageSync('userInfo');
  59. uni.showModal({
  60. title: '登录提示',
  61. content: '登录失效,请您重新登录!',
  62. showCancel: true,
  63. confirmText: '确定',
  64. success: (e) => {
  65. if (e.confirm) {
  66. // 获取微信登录授权
  67. uni.switchTab({
  68. url: "/pages/Index/Mine"
  69. })
  70. }
  71. },
  72. fail: () => {},
  73. complete: () => {}
  74. })
  75. }
  76. // 自动登录
  77. const autoLogin = (fun) => {
  78. reqDirect({
  79. method: 'GET',
  80. url: '/api/v1/Identity/third/refresh',
  81. data: {openId: uni.getStorageSync('userInfo').openID}
  82. }).then(res => {
  83. saveUserInfo(res, function(){
  84. typeof fun === 'function' && fun();
  85. });
  86. }, err => {
  87. utils.msg(err, function() {
  88. typeof fun === 'function' && fun();
  89. });
  90. });
  91. }
  92. // 缓存登录用户信息
  93. const saveUserInfo = (data, fun) => {
  94. _token = data.token;
  95. uni.setStorageSync('userInfo', {
  96. openID: data.openId,
  97. userName: data.userName || '',
  98. userImg: '',
  99. userTel: data.phoneNumber || '',
  100. userType: data.userType || '1', // 登录用户类型 1:市民 2:网格员
  101. YQCode: data.invitationCode, // 邀请码
  102. isVolunteer: data.isVolunteer // 是否为志愿者
  103. })
  104. typeof fun === 'function' && fun();
  105. }
  106. // 调用数据请求入口方法——需验证是否登录
  107. const req = (_options, hideLoading) => {
  108. if (!(hideLoading || false)) {
  109. uni.showLoading({
  110. title: '请求中...',
  111. mask: true
  112. })
  113. }
  114. // 获取当前缓存的微信openID
  115. let openID = uni.getStorageSync('userInfo').openID;
  116. // 异步请求数据
  117. return new Promise((resolve, reject) => {
  118. if (!openID) { // 检查openID是否存在
  119. // 登录失效提示方法
  120. logErrTip();
  121. reject();
  122. } else {
  123. if(!_token){ //检查token是否存在
  124. // 不存在-自动登录
  125. autoLogin(function(){
  126. ajax(resolve, reject, _options);
  127. })
  128. }else{
  129. // 存在-直接请求
  130. ajax(resolve, reject, _options);
  131. }
  132. }
  133. })
  134. }
  135. // 调用数据请求入口方法——不用验证是否登录
  136. const reqDirect = (_options, hideLoading) => {
  137. if (!(hideLoading || false)) {
  138. uni.showLoading({
  139. title: '请求中...',
  140. mask: true
  141. })
  142. }
  143. // 异步请求数据
  144. return new Promise((resolve, reject) => {
  145. ajax(resolve, reject, _options);
  146. })
  147. }
  148. /* *
  149. * 请求接口数据
  150. * */
  151. const ajax = (resolve, reject, _options) => {
  152. uni.request({
  153. method: _options.method || 'POST',
  154. url: config.sysweburl + _options.url,
  155. data: _options.data || {},
  156. header: {
  157. // 'Content-Type': _options.contentType || 'application/x-www-form-urlencoded; charset=UTF-8',
  158. 'Authorization': 'Bearer ' + _token
  159. },
  160. success: function(res) {
  161. console.log(res);
  162. uni.hideLoading();
  163. if(res.statusCode == statusCode.ok){
  164. let jsonResult = {};
  165. jsonResult[response.statusName] = res.data.code;
  166. jsonResult[response.msgName] = res.data.error;
  167. jsonResult[response.dataName] = res.data.result;
  168. if (jsonResult[response.statusName] == 0){
  169. resolve(jsonResult[response.dataName]);
  170. }else {
  171. if (reject) {
  172. reject(jsonResult[response.msgName]);
  173. } else {
  174. utils.msg(jsonResult[response.msgName]);
  175. }
  176. }
  177. }else if (res.statusCode == statusCode.logout){
  178. // 登录失效提示方法
  179. logErrTip()
  180. }else if (res.statusCode == statusCode.tokenErr){
  181. // 清除缓存重新登录失效提示方法
  182. tokenErrTip()
  183. }else {
  184. utils.msg("服务异常,请稍后重试");
  185. }
  186. },
  187. fail: function() {
  188. uni.hideLoading();
  189. utils.msg('服务异常,请稍后重试!');
  190. }
  191. });
  192. }
  193. // 附件上传
  194. const onUploadFiles = (files, fun) => {
  195. uni.showLoading({
  196. title: '上传中,请稍后',
  197. mask: true
  198. })
  199. let pArr = [];
  200. if (files && files.length > 0) {
  201. files.forEach((file, i, array) => {
  202. pArr.push(new Promise(function(resolve, reject) {
  203. onUpLoadSingleFile(file, "uploadkey" + i, resolve, reject);
  204. }))
  205. });
  206. Promise.all(pArr).then(res => {
  207. let strRes = res.toString();
  208. uni.hideLoading();
  209. typeof fun === 'function' && fun(strRes);
  210. }, err => {
  211. uni.hideLoading();
  212. util.msg(err.msg);
  213. })
  214. } else {
  215. uni.hideLoading();
  216. typeof fun === 'function' && fun();
  217. }
  218. }
  219. // 单个附件上传
  220. const onUpLoadSingleFile = (file, key, resolve, reject) => {
  221. let fileObj = {
  222. fileType: file.fileType,
  223. msg: ''
  224. };
  225. switch (fileObj.fileType) {
  226. case 'vioce':
  227. fileObj.msg = '录音上传失败';
  228. break;
  229. case 'image':
  230. fileObj.msg = '图片上传失败';
  231. break;
  232. case 'video':
  233. fileObj.msg = '视频上传失败';
  234. break;
  235. default:
  236. break;
  237. }
  238. uni.uploadFile({
  239. url: config.upfileurl,
  240. filePath: file.tempFilePath,
  241. name: key,
  242. // formData: {
  243. // Code: 'ZGSSP20240102',
  244. // Duration: file.duration || ''
  245. // },
  246. timeout: 10000,
  247. success: res => {
  248. if (res.data) {
  249. var data = res.data;
  250. try {
  251. data = JSON.parse(data);
  252. if (data[0].code == '1') {
  253. return resolve(data[0].data[0].fid);
  254. } else {
  255. return reject(fileObj)
  256. }
  257. } catch (d) {
  258. return reject(fileObj)
  259. }
  260. } else {
  261. return reject(fileObj)
  262. }
  263. },
  264. fail: res => {
  265. return reject(fileObj)
  266. },
  267. })
  268. }
  269. //暴露模块
  270. module.exports = {
  271. config: config,
  272. req: req,
  273. reqDirect: reqDirect,
  274. onUploadFiles: onUploadFiles,
  275. recorderManager: recorderManager,
  276. innerAudioContext: innerAudioContext,
  277. saveUserInfo: saveUserInfo
  278. }