mixin.js 9.7 KB

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