mixin.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 credential = this.userLogin.credential
  166. this.userLogin.credential = this.encryptPassword(credential, this.pubkey, this.pubkeyR)
  167. // 显示加载效果
  168. this.isLoading = true
  169. login(this.userLogin).then(resp => {
  170. if (resp.code === 0) {
  171. const respData = resp.data
  172. const userInfo = respData.accountInfo
  173. const userToken = respData.accountToken
  174. // 保存授权信息到本地缓存
  175. setUserToken(userToken)
  176. store.commit('SET_USER_INFO', userInfo)
  177. // 刷新当前页面
  178. this.$router.go(0)
  179. // 返回上个页面
  180. // this.$router.go(-1)
  181. // 返回上上个页面
  182. // this.$router.go(-2)
  183. this.userLogin = {
  184. principal: null,
  185. credential: null,
  186. captchaCode: null,
  187. loginType: 2,
  188. plat: 1
  189. }
  190. } else {
  191. // 登入失败
  192. this.$message.warning(resp.msg)
  193. this.userLogin.credential = credential
  194. }
  195. }).catch(error => {
  196. // 登入请求错误
  197. this.$message.error(error.message)
  198. }).finally(() => {
  199. this.isLoading = false
  200. })
  201. },
  202. registerBtn() {
  203. if (this.userRegistry.principal === '' || this.userRegistry.principal === null) {
  204. this.$message.warning('手机号不能为空')
  205. return
  206. }
  207. if (this.userRegistry.credential === '' || this.userRegistry.credential === null) {
  208. this.$message.warning('密码不能为空')
  209. return
  210. }
  211. if (this.userRegistry.verifyCode === '' || this.userRegistry.verifyCode === null) {
  212. this.$message.warning('短信验证码不能为空')
  213. return
  214. }
  215. if (this.userRegistry.captchaCode === '' || this.userRegistry.captchaCode === null) {
  216. this.$message.warning('图形验证码不能为空')
  217. return
  218. }
  219. this.userRegistry.credential = this.encryptPassword(this.userRegistry.credential, this.pubkey, this.pubkeyR)
  220. // 显示加载效果
  221. this.isLoading = true
  222. register(this.userRegistry).then(resp => {
  223. if (resp.code === 0) {
  224. // 关闭弹窗并刷新页面
  225. this.dialogVisible = false
  226. this.$message.info('帐号已注册, 稍后会转到登入页面')
  227. setInterval(() => {
  228. this.$router.push('/login')
  229. }, 3000)
  230. } else {
  231. this.$message.warning(resp.msg)
  232. }
  233. }).catch(error => {
  234. this.$message.error(error.message)
  235. }).finally(() => {
  236. this.isLoading = false
  237. })
  238. },
  239. forgotBtn() {
  240. if (this.userForgot.principal === '' || this.userForgot.principal === null) {
  241. this.$message.warning('手机号不能为空')
  242. return
  243. }
  244. if (this.userForgot.newCredential === '' || this.userForgot.newCredential === null) {
  245. this.$message.warning('密码不能为空')
  246. return
  247. }
  248. if (this.userForgot.verifyCode === '' || this.userForgot.verifyCode === null) {
  249. this.$message.warning('短信验证码不能为空')
  250. return
  251. }
  252. if (this.userForgot.captchaCode === '' || this.userForgot.captchaCode === null) {
  253. this.$message.warning('图形验证码不能为空')
  254. return
  255. }
  256. this.userForgot.newCredential = this.encryptPassword(this.userForgot.newCredential, this.pubkey, this.pubkeyR)
  257. // 显示加载效果
  258. this.isLoading = true
  259. forgot(this.userForgot).then(resp => {
  260. if (resp.code === 0) {
  261. // 关闭弹窗并刷新页面
  262. this.dialogVisible = false
  263. this.$message.info('密码已重置, 请返回登入页面登入帐号')
  264. } else {
  265. this.$message.warning(resp.msg)
  266. }
  267. }).catch(error => {
  268. this.$message.error(error.message)
  269. }).finally(() => {
  270. this.isLoading = false
  271. this.userForgot = {
  272. principal: null,
  273. verifyCode: null,
  274. newCredential: null,
  275. captchaCode: null
  276. }
  277. })
  278. },
  279. goToHome() {
  280. this.$router.push('/')
  281. },
  282. goToLogout() {
  283. this.$confirm('退出登入, 是否继续?', '提示', {
  284. confirmButtonText: '确定',
  285. cancelButtonText: '取消',
  286. type: 'warning'
  287. }).then(() => {
  288. logout().then(resp => {
  289. if (resp.code === 0) {
  290. Vue.$cookies.remove('token')
  291. store.commit('USER_LOGOUT')
  292. removeAll()
  293. } else {
  294. this.$notify.error({
  295. title: '提示',
  296. message: resp.msg
  297. })
  298. }
  299. }).catch((e) => {
  300. console.log(e)
  301. this.$notify({
  302. message: '网络错误,请稍后再试...'
  303. })
  304. }).finally(() => {
  305. // 刷新当前页面
  306. this.$router.go(0)
  307. // this.$router.push('/')
  308. this.isLoading = false
  309. })
  310. }).catch(() => {
  311. this.$message({
  312. type: 'info',
  313. message: '已取消'
  314. })
  315. })
  316. }
  317. }
  318. }