admin.js 6.3 KB

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