homePage.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <view>
  3. </view>
  4. </template>
  5. <script setup>
  6. // 添加路由拦截
  7. import '@/interceptor/routeInterceptor.js'
  8. import { encryptByEnAESLogin } from '@/utils/index.js'
  9. import { onLoad } from '@dcloudio/uni-app'
  10. import { defaultColor } from '@/static/js/theme.js'
  11. import { api_wechatLoginEncrypt, api_loginEncrypt, api_systemConfiguration, api_getDictionary} from "@/http/api.js"
  12. import { useWechatAuth } from '@/share/useWechatAuth.js'
  13. import { useLoginSuccess } from '@/share/useLoginSuccess.js'
  14. import { useSetTitle } from '@/share/useSetTitle.js'
  15. useSetTitle()
  16. const { wechatAuth } = useWechatAuth()
  17. const { loginSuccess, transform } = useLoginSuccess()
  18. /**
  19. * 微信登录
  20. */
  21. function wechatLoginEncrypt() {
  22. const url = window.location.href.split("?");
  23. let wenhaohoumian = window.location.href.split("?code="); //截取问号地址数组
  24. let codeStr = wenhaohoumian[1]; //地址数组赋值
  25. let loginType = url[1].split("#/")
  26. let str = loginType[0].split("=")
  27. if(str[0]=='loginType'){
  28. uni.setStorageSync('loginType',str[1])
  29. if (!url[2]) {
  30. wechatAuth();
  31. } else {
  32. codeStr = codeStr.split("&");
  33. codeStr = codeStr[0];
  34. uni.showLoading({
  35. title: "登录中",
  36. mask: true,
  37. });
  38. api_wechatLoginEncrypt({
  39. code:codeStr
  40. }).then(res => {
  41. uni.hideLoading();
  42. if (res.status == 200) {
  43. getConfig(res.user);
  44. changeGroup();
  45. } else if (res.status == 501) {
  46. uni.showModal({
  47. title: '提示',
  48. content: '您的账号被删除,请联系管理员',
  49. showCancel: false,
  50. confirmColor: defaultColor,
  51. confirmText: '取消',
  52. });
  53. } else if (res.status == 508){
  54. uni.showModal({
  55. title: '提示',
  56. content: res.remarks,
  57. showCancel: false,
  58. confirmColor: '#49b856',
  59. success: function(res1) {
  60. if (res1.confirm) {
  61. console.log('用户点击确定');
  62. } else if (res1.cancel) {
  63. console.log('用户点击取消');
  64. }
  65. }
  66. });
  67. }else if (res.status == 555) {
  68. uni.reLaunch({
  69. url: `/pages/initBind/initBind?wechat=${res.wechat}`
  70. })
  71. } else {
  72. uni.showToast({
  73. icon: 'none',
  74. title: res.remarks || '请求数据失败!'
  75. });
  76. }
  77. });
  78. }
  79. }else{
  80. // uni.setStorageSync('loginType','2')
  81. if (!url[1]) {
  82. wechatAuth();
  83. } else {
  84. codeStr = codeStr.split("&");
  85. codeStr = codeStr[0];
  86. uni.showLoading({
  87. title: "登录中",
  88. mask: true,
  89. });
  90. api_wechatLoginEncrypt({
  91. code:codeStr
  92. }).then(res => {
  93. uni.hideLoading();
  94. if (res.status == 200) {
  95. getConfig(res.user);
  96. changeGroup();
  97. } else if (res.status == 501) {
  98. uni.showModal({
  99. title: '提示',
  100. content: '您的账号被删除,请联系管理员',
  101. showCancel: false,
  102. confirmColor: defaultColor,
  103. confirmText: '取消',
  104. });
  105. } else if (res.status == 508){
  106. uni.showModal({
  107. title: '提示',
  108. content: res.remarks,
  109. showCancel: false,
  110. confirmColor: '#49b856',
  111. success: function(res1) {
  112. if (res1.confirm) {
  113. console.log('用户点击确定');
  114. } else if (res1.cancel) {
  115. console.log('用户点击取消');
  116. }
  117. }
  118. });
  119. }else if (res.status == 555) {
  120. uni.reLaunch({
  121. url: `/pages/initBind/initBind?wechat=${res.wechat}`
  122. })
  123. } else {
  124. uni.showToast({
  125. icon: 'none',
  126. title: res.remarks || '请求数据失败!'
  127. });
  128. }
  129. });
  130. }
  131. }
  132. }
  133. /**
  134. * 账号密码登录
  135. */
  136. function loginEncrypt(username, password) {
  137. uni.showLoading({
  138. title: "登录中",
  139. mask: true,
  140. });
  141. let postData = {
  142. k: encryptByEnAESLogin(JSON.stringify({username, password}))
  143. };
  144. api_loginEncrypt(postData).then(res => {
  145. uni.hideLoading();
  146. getConfig(res.data);
  147. changeGroup();
  148. if (res.state == 200) {
  149. loginSuccess(res.data);
  150. transform(res.data, 'menu');
  151. } else {
  152. uni.showToast({
  153. icon: 'none',
  154. title: res.remarks || '请求数据失败!'
  155. });
  156. }
  157. });
  158. }
  159. // 获取配置项
  160. function getConfig(data){
  161. api_systemConfiguration({
  162. idx: 0,
  163. sum: 9999,
  164. }).then(res=>{
  165. uni.setStorageSync('sysData',JSON.stringify(res.list))
  166. setTimeout(_=>{
  167. loginSuccess(data);
  168. transform(data, 'menu');
  169. },500)
  170. })
  171. }
  172. // 获取工作组
  173. function changeGroup(){
  174. let postData = {
  175. "key": 'usertype',
  176. "type": "list",
  177. };
  178. api_getDictionary(postData).then((data) => {
  179. uni.setStorageSync('groupData',JSON.stringify(data))
  180. });
  181. }
  182. onLoad((option) => {
  183. uni.removeStorageSync('sysData');
  184. uni.removeStorageSync('groupData');
  185. uni.removeStorageSync('loginUser');
  186. // 获取当前页面的实例
  187. const pages = getCurrentPages();
  188. const currentPage = pages[pages.length - 1];
  189. // 获取URL参数
  190. const options = currentPage.options;
  191. if (process.env.NODE_ENV === 'development' && options.username && options.password) {
  192. // http://localhost:8081/user/#/?username=liaomingming&password=Dstech@123
  193. // 本地开发调试用
  194. loginEncrypt(options.username, options.password)
  195. } else {
  196. wechatLoginEncrypt();
  197. }
  198. })
  199. </script>