useThirdLogin.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { ref, unref } from 'vue';
  2. import { defHttp } from '/@/utils/http/axios';
  3. import { useGlobSetting } from '/@/hooks/setting';
  4. import { useMessage } from '/@/hooks/web/useMessage';
  5. import { useUserStore } from '/@/store/modules/user';
  6. import { setThirdCaptcha, getCaptcha } from '/@/api/sys/user';
  7. import { useI18n } from '/@/hooks/web/useI18n';
  8. export function useThirdLogin() {
  9. const { createMessage, notification } = useMessage();
  10. const { t } = useI18n();
  11. const glob = useGlobSetting();
  12. const userStore = useUserStore();
  13. //第三方类型
  14. const thirdType = ref('');
  15. //第三方登录相关信息
  16. const thirdLoginInfo = ref<any>({});
  17. //状态
  18. const thirdLoginState = ref(false);
  19. //绑定手机号弹窗
  20. const bindingPhoneModal = ref(false);
  21. //第三方用户UUID
  22. const thirdUserUuid = ref('');
  23. //提示窗
  24. const thirdConfirmShow = ref(false);
  25. //绑定密码弹窗
  26. const thirdPasswordShow = ref(false);
  27. //绑定密码
  28. const thirdLoginPassword = ref('');
  29. //绑定用户
  30. const thirdLoginUser = ref('');
  31. //加载中
  32. const thirdCreateUserLoding = ref(false);
  33. //绑定手机号
  34. const thirdPhone = ref('');
  35. //验证码
  36. const thirdCaptcha = ref('');
  37. //第三方登录
  38. function onThirdLogin(source) {
  39. let url = `${glob.uploadUrl}/sys/thirdLogin/render/${source}`;
  40. window.open(
  41. url,
  42. `login ${source}`,
  43. 'height=500, width=500, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no'
  44. );
  45. thirdType.value = source;
  46. thirdLoginInfo.value = {};
  47. thirdLoginState.value = false;
  48. let receiveMessage = function (event) {
  49. let token = event.data;
  50. if (typeof token === 'string') {
  51. //如果是字符串类型 说明是token信息
  52. if (token === '登录失败') {
  53. createMessage.warning(token);
  54. } else if (token.includes('绑定手机号')) {
  55. bindingPhoneModal.value = true;
  56. let strings = token.split(',');
  57. thirdUserUuid.value = strings[1];
  58. } else {
  59. doThirdLogin(token);
  60. }
  61. } else if (typeof token === 'object') {
  62. //对象类型 说明需要提示是否绑定现有账号
  63. if (token['isObj'] === true) {
  64. thirdConfirmShow.value = true;
  65. thirdLoginInfo.value = { ...token };
  66. }
  67. } else {
  68. createMessage.warning('不识别的信息传递');
  69. }
  70. };
  71. window.addEventListener('message', receiveMessage, false);
  72. }
  73. // 根据token执行登录
  74. function doThirdLogin(token) {
  75. if (unref(thirdLoginState) === false) {
  76. thirdLoginState.value = true;
  77. userStore.ThirdLogin({ token, thirdType: unref(thirdType) }).then((res) => {
  78. console.log('res====>doThirdLogin', res);
  79. if (res && res.userInfo) {
  80. notification.success({
  81. message: t('sys.login.loginSuccessTitle'),
  82. description: `${t('sys.login.loginSuccessDesc')}: ${res.userInfo.realname}`,
  83. duration: 3,
  84. });
  85. } else {
  86. requestFailed(res);
  87. }
  88. });
  89. }
  90. }
  91. function requestFailed(err) {
  92. notification.error({
  93. message: '登录失败',
  94. description: ((err.response || {}).data || {}).message || err.message || '请求出现错误,请稍后再试',
  95. duration: 4,
  96. });
  97. }
  98. // 绑定已有账号 需要输入密码
  99. function thirdLoginUserBind() {
  100. thirdLoginPassword.value = '';
  101. thirdLoginUser.value = thirdLoginInfo.value.uuid;
  102. thirdConfirmShow.value = false;
  103. thirdPasswordShow.value = true;
  104. }
  105. //创建新账号
  106. function thirdLoginUserCreate() {
  107. thirdCreateUserLoding.value = true;
  108. // 账号名后面添加两位随机数
  109. thirdLoginInfo.value.suffix = parseInt(Math.random() * 98 + 1);
  110. defHttp
  111. .post({ url: '/sys/third/user/create', params: { thirdLoginInfo: unref(thirdLoginInfo) } }, { isTransformResponse: false })
  112. .then((res) => {
  113. if (res.success) {
  114. let token = res.result;
  115. doThirdLogin(token);
  116. thirdConfirmShow.value = false;
  117. } else {
  118. createMessage.warning(res.message);
  119. }
  120. })
  121. .finally(() => {
  122. thirdCreateUserLoding.value = false;
  123. });
  124. }
  125. // 核实密码
  126. function thirdLoginCheckPassword() {
  127. let params = Object.assign({}, unref(thirdLoginInfo), { password: unref(thirdLoginPassword) });
  128. defHttp.post({ url: '/sys/third/user/checkPassword', params }, { isTransformResponse: false }).then((res) => {
  129. if (res.success) {
  130. thirdLoginNoPassword();
  131. doThirdLogin(res.result);
  132. } else {
  133. createMessage.warning(res.message);
  134. }
  135. });
  136. }
  137. // 没有密码 取消操作
  138. function thirdLoginNoPassword() {
  139. thirdPasswordShow.value = false;
  140. thirdLoginPassword.value = '';
  141. thirdLoginUser.value = '';
  142. }
  143. //倒计时执行前的函数
  144. function sendCodeApi() {
  145. //return setThirdCaptcha({mobile:unref(thirdPhone)});
  146. return getCaptcha({ mobile: unref(thirdPhone), smsmode: '0' });
  147. }
  148. //绑定手机号点击确定按钮
  149. function thirdHandleOk() {
  150. if (!unref(thirdPhone)) {
  151. cmsFailed('请输入手机号');
  152. }
  153. if (!unref(thirdCaptcha)) {
  154. cmsFailed('请输入验证码');
  155. }
  156. let params = {
  157. mobile: unref(thirdPhone),
  158. captcha: unref(thirdCaptcha),
  159. thirdUserUuid: unref(thirdUserUuid),
  160. };
  161. defHttp.post({ url: '/sys/thirdLogin/bindingThirdPhone', params }, { isTransformResponse: false }).then((res) => {
  162. if (res.success) {
  163. bindingPhoneModal.value = false;
  164. doThirdLogin(res.result);
  165. } else {
  166. createMessage.warning(res.message);
  167. }
  168. });
  169. }
  170. function cmsFailed(err) {
  171. notification.error({
  172. message: '登录失败',
  173. description: err,
  174. duration: 4,
  175. });
  176. return;
  177. }
  178. //返回数据和方法
  179. return {
  180. thirdPasswordShow,
  181. thirdLoginCheckPassword,
  182. thirdLoginNoPassword,
  183. thirdLoginPassword,
  184. thirdConfirmShow,
  185. thirdCreateUserLoding,
  186. thirdLoginUserCreate,
  187. thirdLoginUserBind,
  188. bindingPhoneModal,
  189. thirdHandleOk,
  190. thirdPhone,
  191. thirdCaptcha,
  192. onThirdLogin,
  193. sendCodeApi,
  194. };
  195. }