tools.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import config from '@/utils/config.js'
  2. import interfaces from '@/utils/interfaces.js'
  3. const tools = {
  4. current_navigator_page_index: 0,
  5. // 是否登录
  6. is_login() {
  7. let user_data = this.get_user_info()
  8. if(Object.keys(user_data).length === 0 || tools.isNull(user_data.mobile)) {
  9. return false
  10. }
  11. return true
  12. },
  13. // 是否登录并进入登录界面
  14. /*
  15. * index: -1正常, 1:从AI聊天入口登录 2:从AI画图入口登录
  16. */
  17. is_login_gotoLogin(index) {
  18. if(this.is_login()) {
  19. return true
  20. }
  21. let url = '/pages/login/login'
  22. if(index > -1) {
  23. url = '/pages/login/login' +'?index=' + index
  24. }
  25. uni.navigateTo({
  26. url:url
  27. })
  28. return false
  29. },
  30. // 获取用户信息
  31. /*
  32. * 获取let user_data = get_user_info()
  33. * 使用 user_data.mobile 即可
  34. */
  35. get_user_info() {
  36. return uni.getStorageSync(config.userData)||{}
  37. },
  38. // 设置用户信息
  39. set_user_info(data) {
  40. uni.setStorageSync(config.userData, data)
  41. },
  42. // 是否null
  43. isNull(obj){
  44. if(obj!=null&&obj!=undefined&&obj!=''&&obj.length!=0){
  45. return false;
  46. }
  47. return true;
  48. },
  49. // 请求登录
  50. request_login(that, mobile, password, callback) {
  51. // 测试账号: 18612220000 18612221111 18612221221 18612221211
  52. let self = that
  53. self.request({
  54. url: interfaces.login,
  55. data:{'mobile': mobile},
  56. method:config.post,
  57. success:(res)=> {
  58. if(res.code === config.SUCCESS) {
  59. tools.set_user_info({'mobile': mobile})
  60. uni.$emit(config.notification.login_state_changed)
  61. callback(true)
  62. } else {
  63. callback(false)
  64. }
  65. },
  66. fail:(e)=> {
  67. callback(false)
  68. }
  69. })
  70. },
  71. // 请求退出登录
  72. request_logout(that, callback) {
  73. uni.showLoading({title:"加载中..."})
  74. let self = that
  75. setTimeout(function() {
  76. tools.set_user_info({})
  77. uni.$emit(config.notification.login_state_changed)
  78. uni.$emit(config.notification.switch_page_index, 0)
  79. callback(true)
  80. uni.hideLoading()
  81. }, 1000);
  82. }
  83. }
  84. module.exports = tools;