1f7c4138cbc2e277411526b9542d81ed2addb986.svn-base 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. *第三方登录
  3. */
  4. import { mapActions } from 'vuex'
  5. import { postAction } from '@api/manage'
  6. import { timeFix } from '@/utils/util'
  7. export const JeecgThirdLoginMixin = {
  8. data() {
  9. return {
  10. //第三方登录相关信息
  11. thirdLoginInfo: '',
  12. thirdPasswordShow: false,
  13. thirdLoginPassword: '',
  14. thirdLoginUser: '',
  15. thirdConfirmShow: false,
  16. thirdCreateUserLoding: false,
  17. thirdLoginState: false,
  18. //绑定手机号弹窗
  19. bindingPhoneModal: false,
  20. thirdPhone: '',
  21. thirdCaptcha: '',
  22. //获取验证码按钮30s之内是否可点击
  23. thirdState: {
  24. time: 30,
  25. smsSendBtn: false
  26. },
  27. //第三方用户UUID
  28. thirdUserUuid: '',
  29. thirdType: '',
  30. url: {
  31. bindingThirdPhone: '/sys/thirdLogin/bindingThirdPhone'
  32. }
  33. }
  34. },
  35. created() {
  36. },
  37. methods: {
  38. ...mapActions(['ThirdLogin']),
  39. //第三方登录
  40. onThirdLogin(source) {
  41. let url = window._CONFIG['domianURL'] + `/sys/thirdLogin/render/${source}`
  42. window.open(url, `login ${source}`, 'height=500, width=500, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no')
  43. let that = this
  44. that.thirdType = source
  45. that.thirdLoginInfo = ''
  46. that.thirdLoginState = false
  47. let receiveMessage = function(event) {
  48. let token = event.data
  49. if (typeof token === 'string') {
  50. //如果是字符串类型 说明是token信息
  51. if (token === '登录失败') {
  52. that.$message.warning(token)
  53. } else if (token.includes('绑定手机号')) {
  54. that.bindingPhoneModal = true
  55. let strings = token.split(',')
  56. that.thirdUserUuid = strings[1]
  57. } else {
  58. that.doThirdLogin(token)
  59. }
  60. } else if (typeof token === 'object') {
  61. //对象类型 说明需要提示是否绑定现有账号
  62. if (token['isObj'] === true) {
  63. that.thirdConfirmShow = true
  64. that.thirdLoginInfo = { ...token }
  65. }
  66. } else {
  67. that.$message.warning('不识别的信息传递')
  68. }
  69. }
  70. window.addEventListener('message', receiveMessage, false)
  71. },
  72. // 根据token执行登录
  73. doThirdLogin(token) {
  74. if (this.thirdLoginState === false) {
  75. this.thirdLoginState = true
  76. let param={};
  77. param.thirdType=this.thirdType
  78. param.token=token
  79. this.ThirdLogin(param).then(res => {
  80. if (res.success) {
  81. this.loginSuccess()
  82. } else {
  83. this.requestFailed(res)
  84. }
  85. })
  86. }
  87. },
  88. // 绑定已有账号 需要输入密码
  89. thirdLoginUserBind() {
  90. this.thirdLoginPassword = ''
  91. this.thirdLoginUser = this.thirdLoginInfo.uuid
  92. this.thirdConfirmShow = false
  93. this.thirdPasswordShow = true
  94. },
  95. //创建新账号
  96. thirdLoginUserCreate() {
  97. this.thirdCreateUserLoding = true
  98. // 账号名后面添加两位随机数
  99. this.thirdLoginInfo['suffix'] = parseInt(Math.random() * 98 + 1)
  100. //this.thirdLoginInfo.operateCode = 123 //测试校验失败
  101. postAction('/sys/third/user/create', this.thirdLoginInfo).then(res => {
  102. if (res.success) {
  103. let token = res.result
  104. console.log('thirdCreateNewAccount', token)
  105. this.doThirdLogin(token)
  106. this.thirdConfirmShow = false
  107. } else {
  108. this.$message.warning(res.message)
  109. }
  110. }).finally(() => {
  111. this.thirdCreateUserLoding = false
  112. })
  113. },
  114. // 核实密码
  115. thirdLoginCheckPassword() {
  116. //this.thirdLoginInfo.operateCode = 123 //测试校验失败
  117. let param = Object.assign({}, this.thirdLoginInfo, { password: this.thirdLoginPassword })
  118. postAction('/sys/third/user/checkPassword', param).then(res => {
  119. if (res.success) {
  120. this.thirdLoginNoPassword()
  121. this.doThirdLogin(res.result)
  122. } else {
  123. this.$message.warning(res.message)
  124. }
  125. })
  126. },
  127. // 没有密码 取消操作
  128. thirdLoginNoPassword() {
  129. this.thirdPasswordShow = false
  130. this.thirdLoginPassword = ''
  131. this.thirdLoginUser = ''
  132. },
  133. //获取第三方验证码
  134. getThirdCaptcha() {
  135. let that = this
  136. if (!this.thirdPhone) {
  137. that.cmsFailed('请输入手机号')
  138. } else {
  139. this.thirdState.smsSendBtn = true
  140. let interval = window.setInterval(() => {
  141. if (that.thirdState.time-- <= 0) {
  142. that.thirdState.time = 30
  143. that.thirdState.smsSendBtn = false
  144. window.clearInterval(interval)
  145. }
  146. }, 1000)
  147. const hide = this.$message.loading('验证码发送中..', 0)
  148. let smsParams = {}
  149. smsParams.mobile = this.thirdPhone
  150. smsParams.smsmode = '0'
  151. postAction('/sys/sms', smsParams).then(res => {
  152. if (!res.success) {
  153. setTimeout(hide, 0)
  154. this.cmsFailed(res.message)
  155. }
  156. setTimeout(hide, 500)
  157. }).catch(err => {
  158. setTimeout(hide, 1)
  159. clearInterval(interval)
  160. that.thirdState.time = 30
  161. that.thirdState.smsSendBtn = false
  162. this.requestFailed(err)
  163. })
  164. }
  165. },
  166. //绑定手机号点击确定按钮
  167. thirdHandleOk() {
  168. let bingingParams = {}
  169. bingingParams.mobile = this.thirdPhone
  170. bingingParams.captcha = this.thirdCaptcha
  171. bingingParams.thirdUserUuid = this.thirdUserUuid
  172. postAction(this.url.bindingThirdPhone, bingingParams).then(res => {
  173. if (res.success) {
  174. this.bindingPhoneModal = false
  175. this.doThirdLogin(res.result)
  176. } else {
  177. this.$message.warning(res.message)
  178. }
  179. })
  180. },
  181. loginSuccess () {
  182. // update-begin- author:sunjianlei --- date:20190812 --- for: 登录成功后不解除禁用按钮,防止多次点击
  183. // this.loginBtn = false
  184. // update-end- author:sunjianlei --- date:20190812 --- for: 登录成功后不解除禁用按钮,防止多次点击
  185. this.$router.push({ path: "/dashboard/analysis" }).catch(()=>{
  186. console.log('登录跳转首页出错,这个错误从哪里来的')
  187. })
  188. this.$notification.success({
  189. message: '欢迎',
  190. description: `${timeFix()},欢迎回来`,
  191. });
  192. },
  193. cmsFailed(err){
  194. this.$notification[ 'error' ]({
  195. message: "登录失败",
  196. description:err,
  197. duration: 4,
  198. });
  199. },
  200. requestFailed (err) {
  201. this.$notification[ 'error' ]({
  202. message: '登录失败',
  203. description: ((err.response || {}).data || {}).message || err.message || "请求出现错误,请稍后再试",
  204. duration: 4,
  205. });
  206. this.loginBtn = false;
  207. },
  208. }
  209. }