mixin.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /**
  2. * 混入对象,抽取Vue中公共的部分
  3. */
  4. import { getPubkey, getCaptchaCode, getVerifyCode, login, logout, register, forgot } from '@/api/account'
  5. import { setUserToken, removeAll } from '@/utils/auth'
  6. import { JSEncrypt } from 'jsencrypt'
  7. import Vue from 'vue'
  8. import store from '@/store'
  9. export const userMixin = {
  10. data() {
  11. return {
  12. pubkey: '',
  13. pubkeyR: '',
  14. captchaCode: '',
  15. userLogin: {
  16. principal: null,
  17. credential: null,
  18. captchaCode: null,
  19. loginType: 2,
  20. plat: 2
  21. },
  22. userRegistry: {
  23. principal: null,
  24. verifyCode: null,
  25. credential: null,
  26. captchaCode: null,
  27. plat: 2
  28. },
  29. verifyCodeForm: {
  30. principal: null,
  31. channel: 2
  32. },
  33. userForgot: {
  34. principal: null,
  35. verifyCode: null,
  36. newCredential: null,
  37. captchaCode: null
  38. },
  39. loginDialog: false,
  40. registerDialog: false,
  41. dialogVisible: false,
  42. dialogVisible2: false,
  43. nickname: '', // 用户昵称
  44. phone: '', // 电话
  45. mobile: '', // 电话
  46. username: '', // 电话
  47. password: '', // 用户密码
  48. repassword: '', // 确认密码
  49. avatarurl: '', // 用户头像路径
  50. rcode: '', // 注册验证码
  51. isLoading: false, // 登入加载效果
  52. user: this.$user, // 登入用户对象
  53. code: '获取验证码',
  54. isBtn: false,
  55. imageUrl: require('assets/img/avatar.png')
  56. }
  57. },
  58. methods: {
  59. fetchPubkey(channel) {
  60. getPubkey(channel).then(resp => {
  61. if (resp.code === 0) {
  62. this.pubkey = resp.data.pubkey
  63. this.pubkeyR = resp.data.r
  64. this.getCaptcha()
  65. } else {
  66. this.message = resp.msg
  67. this.showMessage = true
  68. }
  69. }).catch(error => {
  70. this.message = error.message
  71. this.showMessage = true
  72. })
  73. },
  74. getCaptcha() {
  75. getCaptchaCode().then(resp => {
  76. if (resp.code === 0) {
  77. this.captchaCode = resp.data
  78. this.dialogVisible = true
  79. } else {
  80. this.message = '获取图形验证码失败, 请重新刷新页面'
  81. this.showMessage = true
  82. }
  83. })
  84. },
  85. encryptPassword(password, pubkey, pubkeyR) {
  86. var encryptor = new JSEncrypt()
  87. encryptor.setPublicKey(pubkey)
  88. return encryptor.encrypt(pubkeyR + password)
  89. },
  90. registerVerifyCode() {
  91. if (this.userRegistry.principal === null || this.userRegistry.principal === '') {
  92. this.$message.success('请填写邮箱或手机号')
  93. return
  94. }
  95. this.verifyCodeForm.channel = 1
  96. this.verifyCodeForm.receiver = this.userRegistry.principal
  97. this.getVerifyCodeWrapper(this.verifyCodeForm)
  98. },
  99. loginVerifyCode() {
  100. if (this.userLogin.principal === null || this.userLogin.principal === '') {
  101. this.$message.success('请填写邮箱或手机号')
  102. return
  103. }
  104. this.verifyCodeForm.channel = 2
  105. this.verifyCodeForm.receiver = this.userLogin.principal
  106. this.getVerifyCodeWrapper(this.verifyCodeForm)
  107. },
  108. forgotVerifyCode() {
  109. if (this.userForgot.principal === null || this.userForgot.principal === '') {
  110. this.$message.success('请填写邮箱或手机号')
  111. return
  112. }
  113. this.verifyCodeForm.channel = 3
  114. this.verifyCodeForm.receiver = this.userRegistry.principal
  115. this.getVerifyCodeWrapper(this.verifyCodeForm)
  116. },
  117. getVerifyCodeWrapper(verifyCodeForm) {
  118. this.isBtn = true
  119. let time = 60
  120. const timeout = setInterval(() => {
  121. if (time !== 0) {
  122. time--
  123. this.code = time + 's后重新获取'
  124. } else {
  125. this.isBtn = false
  126. this.code = '重新获取验证码'
  127. clearTimeout(timeout)
  128. }
  129. }, 1000)
  130. getVerifyCode(verifyCodeForm).then(resp => {
  131. if (resp.code === 0) {
  132. this.$message.success('验证码已发送, 请注意查收')
  133. } else {
  134. this.$message.warning(resp.msg)
  135. }
  136. }).catch(error => {
  137. this.$message.error(error.message)
  138. })
  139. },
  140. // 昵称校验
  141. nickNameBlur() {
  142. if (this.nickname.length <= 0) {
  143. this.$message.warning('昵称不能为空')
  144. }
  145. },
  146. // 电话校验
  147. phoneBlur() {
  148. if (!(/^1[3456789]\d{9}$/.test(this.mobile))) {
  149. this.$message.warning('电话号码格式有误')
  150. }
  151. },
  152. loginByVerifyCode() {
  153. this.userLogin.loginType = 1
  154. this.loginBtn()
  155. },
  156. loginBtn() {
  157. if (this.userLogin.principal === '' || this.userLogin.principal === null) {
  158. this.$message.warning('帐号不能为空')
  159. return
  160. }
  161. if (this.userLogin.credential === '' || this.userLogin.credential === null) {
  162. this.$message.warning('密码不能为空')
  163. return
  164. }
  165. const devops = process.env.VUE_APP_DEVOPS
  166. if (!devops) {
  167. if (this.userLogin.captchaCode === '' || this.userLogin.captchaCode === null) {
  168. this.$message.warning('图形验证码不能为空')
  169. return
  170. }
  171. }
  172. const credential = this.userLogin.credential
  173. this.userLogin.credential = this.encryptPassword(credential, this.pubkey, this.pubkeyR)
  174. // 显示加载效果
  175. this.isLoading = true
  176. login(this.userLogin).then(resp => {
  177. if (resp.code === 0) {
  178. const respData = resp.data
  179. const userInfo = respData.accountInfo
  180. const userToken = respData.accountToken
  181. // 保存授权信息到本地缓存
  182. setUserToken(userToken)
  183. store.commit('SET_USER_INFO', userInfo)
  184. // 刷新当前页面
  185. // this.$router.go(0)
  186. // 返回上个页面
  187. this.$router.go(-1)
  188. // 返回上上个页面
  189. // this.$router.go(-2)
  190. this.userLogin = {
  191. principal: null,
  192. credential: null,
  193. captchaCode: null,
  194. loginType: 2,
  195. plat: 1
  196. }
  197. } else {
  198. // 登入失败
  199. this.$message.warning(resp.msg)
  200. this.userLogin.credential = credential
  201. }
  202. }).catch(error => {
  203. // 登入请求错误
  204. this.$message.error(error.message)
  205. }).finally(() => {
  206. this.isLoading = false
  207. })
  208. },
  209. registerBtn() {
  210. if (this.userRegistry.principal === '' || this.userRegistry.principal === null) {
  211. this.$message.warning('手机号不能为空')
  212. return
  213. }
  214. if (this.userRegistry.credential === '' || this.userRegistry.credential === null) {
  215. this.$message.warning('密码不能为空')
  216. return
  217. }
  218. if (this.userRegistry.verifyCode === '' || this.userRegistry.verifyCode === null) {
  219. this.$message.warning('短信验证码不能为空')
  220. return
  221. }
  222. if (this.userRegistry.captchaCode === '' || this.userRegistry.captchaCode === null) {
  223. this.$message.warning('图形验证码不能为空')
  224. return
  225. }
  226. this.userRegistry.credential = this.encryptPassword(this.userRegistry.credential, this.pubkey, this.pubkeyR)
  227. // 显示加载效果
  228. this.isLoading = true
  229. register(this.userRegistry).then(resp => {
  230. if (resp.code === 0) {
  231. // 关闭弹窗并刷新页面
  232. this.dialogVisible = false
  233. this.$message.info('帐号已注册, 稍后会转到登入页面')
  234. setInterval(() => {
  235. this.$router.push('/login')
  236. }, 3000)
  237. } else {
  238. this.$message.warning(resp.msg)
  239. }
  240. }).catch(error => {
  241. this.$message.error(error.message)
  242. }).finally(() => {
  243. this.isLoading = false
  244. })
  245. },
  246. forgotBtn() {
  247. if (this.userForgot.principal === '' || this.userForgot.principal === null) {
  248. this.$message.warning('手机号不能为空')
  249. return
  250. }
  251. if (this.userForgot.newCredential === '' || this.userForgot.newCredential === null) {
  252. this.$message.warning('密码不能为空')
  253. return
  254. }
  255. if (this.userForgot.verifyCode === '' || this.userForgot.verifyCode === null) {
  256. this.$message.warning('短信验证码不能为空')
  257. return
  258. }
  259. if (this.userForgot.captchaCode === '' || this.userForgot.captchaCode === null) {
  260. this.$message.warning('图形验证码不能为空')
  261. return
  262. }
  263. this.userForgot.newCredential = this.encryptPassword(this.userForgot.newCredential, this.pubkey, this.pubkeyR)
  264. // 显示加载效果
  265. this.isLoading = true
  266. forgot(this.userForgot).then(resp => {
  267. if (resp.code === 0) {
  268. // 关闭弹窗并刷新页面
  269. this.dialogVisible = false
  270. this.$message.info('密码已重置, 请返回登入页面登入帐号')
  271. } else {
  272. this.$message.warning(resp.msg)
  273. }
  274. }).catch(error => {
  275. this.$message.error(error.message)
  276. }).finally(() => {
  277. this.isLoading = false
  278. this.userForgot = {
  279. principal: null,
  280. verifyCode: null,
  281. newCredential: null,
  282. captchaCode: null
  283. }
  284. })
  285. },
  286. goToHome() {
  287. this.$router.push('/')
  288. },
  289. goToLogout() {
  290. this.$confirm('退出登入, 是否继续?', '提示', {
  291. confirmButtonText: '确定',
  292. cancelButtonText: '取消',
  293. type: 'warning'
  294. }).then(() => {
  295. logout().then(resp => {
  296. if (resp.code === 0) {
  297. Vue.$cookies.remove('token')
  298. store.commit('USER_LOGOUT')
  299. removeAll()
  300. } else {
  301. this.$notify.error({
  302. title: '提示',
  303. message: resp.msg
  304. })
  305. }
  306. }).catch((e) => {
  307. console.log(e)
  308. this.$notify({
  309. message: '网络错误,请稍后再试...'
  310. })
  311. }).finally(() => {
  312. // 刷新当前页面
  313. this.$router.go(0)
  314. // this.$router.push('/')
  315. this.isLoading = false
  316. })
  317. }).catch(() => {
  318. this.$message({
  319. type: 'info',
  320. message: '已取消'
  321. })
  322. })
  323. }
  324. }
  325. }