common.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. import store from '@/store'
  2. import config from '@/config'
  3. import { getToken } from '@/utils/auth'
  4. import errorCode from '@/utils/errorCode'
  5. let timeout = 10000
  6. const baseUrl = config.baseUrl
  7. /**
  8. * 显示消息提示框
  9. * @param content 提示的标题
  10. */
  11. export function toast(content) {
  12. uni.showToast({
  13. icon: 'none',
  14. title: content
  15. })
  16. }
  17. /**
  18. * 显示模态弹窗
  19. * @param content 提示的标题
  20. */
  21. export function showConfirm(content) {
  22. return new Promise((resolve, reject) => {
  23. uni.showModal({
  24. title: '提示',
  25. content: content,
  26. cancelText: '取消',
  27. confirmText: '确定',
  28. success: function(res) {
  29. resolve(res)
  30. }
  31. })
  32. })
  33. }
  34. // 字典值匹配
  35. export function selectDictValue(datas, value) {
  36. var actions = [];
  37. Object.keys(datas).some((key) => {
  38. if (datas[key].dictValue == ('' + value)) {
  39. actions.push(datas[key].dictLabel);
  40. return true;
  41. }
  42. })
  43. return actions.join('');
  44. }
  45. export function selectDictLabel(datas, value) {
  46. var actions = [];
  47. Object.keys(datas).some((key) => {
  48. if (datas[key].dictLabel == ('' + value)) {
  49. actions.push(datas[key].dictValue);
  50. return true;
  51. }
  52. })
  53. return actions.join('');
  54. }
  55. export function selectValueKey(datas, value) {
  56. var actions = [];
  57. var idx=0;
  58. Object.keys(datas).some((key) => {
  59. if (datas[key].value == ('' + value)) {
  60. idx=key;
  61. actions.push(datas[key].label);
  62. return true;
  63. }
  64. })
  65. var newObj={
  66. actions:actions.join(''),
  67. key:idx
  68. }
  69. return newObj
  70. }
  71. export function selectValue(datas, value) {
  72. var actions = [];
  73. var idx=0;
  74. Object.keys(datas).some((key) => {
  75. if (datas[key].value == ('' + value)) {
  76. actions.push(datas[key].label);
  77. return true;
  78. }
  79. })
  80. return actions.join('')
  81. }
  82. export function selectValuetext(datas, value) {
  83. var actions = [];
  84. var idx=0;
  85. Object.keys(datas).some((key) => {
  86. if (datas[key].value == ('' + value)) {
  87. actions.push(datas[key].text);
  88. return true;
  89. }
  90. })
  91. return actions.join('')
  92. }
  93. /**
  94. * 参数处理
  95. * @param params 参数
  96. */
  97. export function tansParams(params) {
  98. let result = ''
  99. for (const propName of Object.keys(params)) {
  100. const value = params[propName]
  101. var part = encodeURIComponent(propName) + "="
  102. if (value !== null && value !== "" && typeof (value) !== "undefined") {
  103. if (typeof value === 'object') {
  104. for (const key of Object.keys(value)) {
  105. if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
  106. let params = propName + '[' + key + ']'
  107. var subPart = encodeURIComponent(params) + "="
  108. result += subPart + encodeURIComponent(value[key]) + "&"
  109. }
  110. }
  111. } else {
  112. result += part + encodeURIComponent(value) + "&"
  113. }
  114. }
  115. }
  116. return result
  117. }
  118. // 上传
  119. //上传图片(本地地址识别一张)
  120. export function uploadIdentify(api, filePaths, successUp, failUp, i, length, files, callback) {
  121. const isToken = (config.headers || {}).isToken === false
  122. config.header = config.header || {}
  123. if (getToken() && !isToken) {
  124. config.header['Authorization'] = 'Bearer ' + getToken()
  125. }
  126. // get请求映射params参数
  127. if (config.params) {
  128. let url = config.url + '?' + tansParams(config.params)
  129. url = url.slice(0, -1)
  130. config.url = url
  131. }
  132. uni.showLoading({
  133. title: '上传中'
  134. })
  135. var failfile = [];
  136. uni.uploadFile({
  137. timeout: config.timeout || timeout,
  138. url: baseUrl + api, //仅为示例,非真实的接口地址
  139. filePath: filePaths[i],
  140. name: 'file',
  141. header: config.header,
  142. formData: config.formData,
  143. success: function(resp) {
  144. uni.hideLoading();
  145. let result = JSON.parse(resp.data)
  146. const code = result.code || 200
  147. const msg = errorCode[code] || result.msg || errorCode['default']
  148. // console.log(result.fileName,8)
  149. if (result.code == 200) {
  150. successUp++;
  151. files[i] = result;
  152. } else if(result.code==401) {
  153. showConfirm("登录状态已过期,您可以继续留在该页面,或者重新登录?").then(res => {
  154. if (res.confirm) {
  155. store.dispatch('LogOut').then(res => {
  156. uni.reLaunch({ url: '/pages/login/login' })
  157. })
  158. }
  159. })
  160. callback('无效的会话,或者会话已过期,请重新登录。');
  161. }else{
  162. failfile = failfile.concat(filePaths[i])
  163. failUp++;
  164. }
  165. },
  166. fail: function(res) {
  167. uni.hideLoading();
  168. failfile = failfile.concat(filePaths[i])
  169. failUp++;
  170. },
  171. complete: function(rsp) {
  172. // console.log(rsp, filePaths[i])
  173. uni.hideLoading();
  174. i++;
  175. if (i == length) {
  176. uni.showToast({
  177. title: '总共' + successUp + '张上传成功,' + failUp + '张上传失败!',
  178. icon: 'none',
  179. duration: 2000
  180. });
  181. callback(files);
  182. } else { //递归调用upload函数
  183. uploadIdentify(api, filePaths, successUp, failUp, i, length, files, callback);
  184. }
  185. }
  186. });
  187. }
  188. //上传图片
  189. export function uploadmore(api, filePaths, successUp, failUp, i, length, files, callback) {
  190. const isToken = (config.headers || {}).isToken === false
  191. config.header = config.header || {}
  192. if (getToken() && !isToken) {
  193. config.header['Authorization'] = 'Bearer ' + getToken()
  194. }
  195. // get请求映射params参数
  196. if (config.params) {
  197. let url = config.url + '?' + tansParams(config.params)
  198. url = url.slice(0, -1)
  199. config.url = url
  200. }
  201. uni.showLoading({
  202. title: '上传中'
  203. })
  204. var failfile = [];
  205. uni.uploadFile({
  206. timeout: config.timeout || timeout,
  207. url: baseUrl + api, //仅为示例,非真实的接口地址
  208. filePath: filePaths[i],
  209. name: 'file',
  210. header: config.header,
  211. formData: config.formData,
  212. success: function(resp) {
  213. uni.hideLoading();
  214. let result = JSON.parse(resp.data)
  215. const code = result.code || 200
  216. const msg = errorCode[code] || result.msg || errorCode['default']
  217. // console.log(result.fileName,8)
  218. if (result.code == 200) {
  219. successUp++;
  220. files[i] = result.fileName;
  221. } else if(result.code==401) {
  222. showConfirm("登录状态已过期,您可以继续留在该页面,或者重新登录?").then(res => {
  223. if (res.confirm) {
  224. store.dispatch('LogOut').then(res => {
  225. uni.reLaunch({ url: '/pages/login/login' })
  226. })
  227. }
  228. })
  229. callback('无效的会话,或者会话已过期,请重新登录。');
  230. }else{
  231. failfile = failfile.concat(filePaths[i])
  232. failUp++;
  233. }
  234. },
  235. fail: function(res) {
  236. uni.hideLoading();
  237. failfile = failfile.concat(filePaths[i])
  238. failUp++;
  239. },
  240. complete: function(rsp) {
  241. // console.log(rsp, filePaths[i])
  242. uni.hideLoading();
  243. i++;
  244. if (i == length) {
  245. uni.showToast({
  246. title: '总共' + successUp + '张上传成功,' + failUp + '张上传失败!',
  247. icon: 'none',
  248. duration: 2000
  249. });
  250. callback(files);
  251. } else { //递归调用upload函数
  252. uploadmore(api, filePaths, successUp, failUp, i, length, files, callback);
  253. }
  254. }
  255. });
  256. }
  257. // 判断权限
  258. export function setPermissions(callback) {
  259. // #ifdef APP-PLUS
  260. if (plus.os.name == 'Android') { // 判断是Android
  261. var main = plus.android.runtimeMainActivity();
  262. var pkName = main.getPackageName();
  263. var uid = main.getApplicationInfo().plusGetAttribute("uid");
  264. var NotificationManagerCompat = plus.android.importClass("android.support.v4.app.NotificationManagerCompat");
  265. //android.support.v4升级为androidx
  266. if (NotificationManagerCompat == null) {
  267. NotificationManagerCompat = plus.android.importClass("androidx.core.app.NotificationManagerCompat");
  268. }
  269. var areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
  270. console.log(areNotificationsEnabled,223)
  271. // 未开通‘允许通知’权限,则弹窗提醒开通,并点击确认后,跳转到系统设置页面进行设置
  272. if (!areNotificationsEnabled) {
  273. uni.showModal({
  274. title: '通知权限开启提醒',
  275. content: '您还没有开启通知权限,无法接受到消息通知,请前往设置!',
  276. // showCancel: false,
  277. confirmText: '去设置',
  278. success: function(res) {
  279. if (res.confirm) {
  280. var Intent = plus.android.importClass('android.content.Intent');
  281. var Build = plus.android.importClass("android.os.Build");
  282. //android 8.0引导
  283. if (Build.VERSION.SDK_INT >= 26) {
  284. var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
  285. intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
  286. } else if (Build.VERSION.SDK_INT >= 21) { //android 5.0-7.0
  287. var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
  288. intent.putExtra("app_package", pkName);
  289. intent.putExtra("app_uid", uid);
  290. } else { //(<21)其他--跳转到该应用管理的详情页
  291. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  292. var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
  293. intent.setData(uri);
  294. }
  295. // 跳转到该应用的系统通知设置页
  296. main.startActivity(intent);
  297. }
  298. }
  299. });
  300. }else{
  301. callback(1);
  302. }
  303. } else if (plus.os.name == 'iOS') { // 判断是ISO
  304. var isOn = undefined;
  305. var types = 0;
  306. var app = plus.ios.invoke('UIApplication', 'sharedApplication');
  307. var settings = plus.ios.invoke(app, 'currentUserNotificationSettings');
  308. if (settings) {
  309. types = settings.plusGetAttribute('types');
  310. plus.ios.deleteObject(settings);
  311. } else {
  312. types = plus.ios.invoke(app, 'enabledRemoteNotificationTypes');
  313. }
  314. plus.ios.deleteObject(app);
  315. isOn = (0 != types);
  316. if (isOn == false) {
  317. uni.showModal({
  318. title: '通知权限开启提醒',
  319. content: '您还没有开启通知权限,无法接受到消息通知,请前往设置!',
  320. showCancel: false,
  321. confirmText: '去设置',
  322. success: function(res) {
  323. if (res.confirm) {
  324. var app = plus.ios.invoke('UIApplication', 'sharedApplication');
  325. var setting = plus.ios.invoke('NSURL', 'URLWithString:', 'app-settings:');
  326. plus.ios.invoke(app, 'openURL:', setting);
  327. plus.ios.deleteObject(setting);
  328. plus.ios.deleteObject(app);
  329. }
  330. }
  331. });
  332. }
  333. }
  334. // #endif
  335. }