login.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import { defineComponent } from 'vue'
  2. import {
  3. CellGroup,
  4. Field,
  5. Button,
  6. CountDown,
  7. Row,
  8. Col,
  9. showToast,
  10. Popup,
  11. showDialog,
  12. closeToast
  13. } from 'vant'
  14. import ImgCode from '@/components/o-img-code'
  15. import { checkPhone } from '@/helpers/validate'
  16. import { goWechatAuth, setLogin, state } from '@/state'
  17. import { removeAuth, setAuth } from './utils'
  18. import styles from './index.module.less'
  19. import request from '@/helpers/request'
  20. import { browser, getUrlCode } from '@/helpers/utils'
  21. import qs from 'query-string'
  22. type loginType = 'PWD' | 'SMS'
  23. export default defineComponent({
  24. name: 'login-music',
  25. data() {
  26. return {
  27. loginType: 'SMS' as loginType,
  28. username: '',
  29. password: '',
  30. smsCode: '',
  31. countDownStatus: true, // 是否发送验证码
  32. countDownTime: 1000 * 120, // 倒计时时间
  33. // countDownRef: null as any, // 倒计时实例
  34. imgCodeStatus: false,
  35. showPopup: false,
  36. showPopupMessage: '请使用微信打开',
  37. wxAppId: '', //
  38. code: '', // 授权code码
  39. orchestraPath: 'preApply' as any,
  40. orchestraInfo: {} as any,
  41. hasBuyInstruments: false, // 是否购买商品
  42. // 是否开启微信登录(测试使用)默认为false
  43. testIsWeixin: false,
  44. noPaymentList: [
  45. '1656572729093021697',
  46. '1656488025144713217',
  47. '1661221287237820418',
  48. '1661222465069686785'
  49. ]
  50. }
  51. },
  52. computed: {
  53. codeDisable() {
  54. let status = true
  55. this.username && this.smsCode && (status = false)
  56. return status
  57. }
  58. },
  59. mounted() {
  60. const { returnUrl } = this.$route.query
  61. this.orchestraPath = returnUrl
  62. removeAuth()
  63. this.directNext()
  64. // 判断是否是微信,只能微信中打开
  65. if (!browser().weixin && !this.testIsWeixin) {
  66. this.showPopup = true
  67. }
  68. if (this.$route.query.id && this.noPaymentList.indexOf(this.$route.query.id as string) != -1) {
  69. console.log('提示乐团报名失败')
  70. // 提示乐团报名失败
  71. this.showPopupMessage = '乐团报名时间已过,请等待通知'
  72. this.showPopup = true
  73. }
  74. this.getDetails()
  75. console.log(this.$route.query.returnUrl)
  76. },
  77. methods: {
  78. async getDetails() {
  79. try {
  80. const id = this.$route.query.id
  81. if (!id) return
  82. const { data } = await request.get(
  83. '/api-student/open/orchestra/detail/' + this.$route.query.id
  84. )
  85. this.orchestraInfo = data || {}
  86. if (!data.canSignUp) {
  87. this.showPopup = true
  88. this.showPopupMessage = '学校已关闭'
  89. }
  90. } catch {
  91. //
  92. }
  93. },
  94. async getAppIdAndCode(url?: string) {
  95. try {
  96. const { data } = await request.get('/api-student/open/paramConfig/wechatAppId')
  97. // 判断是否有微信appId
  98. if (data) {
  99. closeToast()
  100. goWechatAuth(data, url)
  101. // this.goAuth(data, url)
  102. }
  103. } catch {
  104. //
  105. }
  106. },
  107. // goAuth(wxAppId: string, urlString?: string) {
  108. // // 用户授权
  109. // console.log(urlString || window.location.href, 'urlString || window.location.href')
  110. // const urlNow = encodeURIComponent(urlString || window.location.href)
  111. // console.log(urlNow, 'urlNow')
  112. // const scope = 'snsapi_base' //snsapi_userinfo //静默授权 用户无感知
  113. // const appid = wxAppId || 'wx8654c671631cfade'
  114. // const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=STATE&connect_redirect=1#wechat_redirect`
  115. // window.location.replace(url)
  116. // },
  117. async directNext() {
  118. if (state.user.status === 'login' || state.user.status === 'error') {
  119. const { returnUrl, isRegister, ...rest } = this.$route.query
  120. let newUrl =
  121. window.location.origin +
  122. window.location.pathname +
  123. '#' +
  124. returnUrl +
  125. '?' +
  126. qs.stringify({
  127. ...rest
  128. })
  129. // 判断是否购买的商品 - 只在商品购买乐团
  130. if (this.hasBuyInstruments) {
  131. newUrl = window.location.origin + window.location.pathname + '#/preGoodsSuccess?t=pay'
  132. window.location.href = newUrl
  133. return
  134. }
  135. if (this.testIsWeixin) {
  136. // 直接跳转到授权页面
  137. this.locationReplace(newUrl)
  138. } else {
  139. await this.getAppIdAndCode(newUrl)
  140. }
  141. }
  142. },
  143. locationReplace(url: any) {
  144. // 只允许同域名
  145. console.log(history.replaceState, 'window.history.replaceState', url)
  146. if (history.replaceState) {
  147. history.replaceState(null, document.title, url)
  148. history.go(0)
  149. } else {
  150. location.replace(url)
  151. }
  152. },
  153. async onLogin() {
  154. try {
  155. if (!this.orchestraInfo.canSignUp) {
  156. this.showPopup = true
  157. this.showPopupMessage = '学校已关闭'
  158. return
  159. }
  160. // let res: any
  161. const forms: any = {
  162. username: this.username,
  163. client_id: 'jmedu-student',
  164. client_secret: 'jmedu-student',
  165. autoRegister: true,
  166. password: this.smsCode,
  167. loginType: 'SMS',
  168. grant_type: 'password',
  169. extra: this.$route.query.id
  170. }
  171. const { data } = await request.post('/api-oauth/userlogin', {
  172. requestType: 'form',
  173. data: {
  174. ...forms
  175. }
  176. })
  177. setAuth(data.token_type + ' ' + data.access_token)
  178. const userCash = await request.get('/api-student/user/getUserInfo', {
  179. initRequest: true // 初始化接口
  180. })
  181. setLogin(userCash.data)
  182. // 判断是否是商品报名 - 先要判断是否已经购买商品
  183. const { returnUrl, id } = this.$route.query
  184. if (returnUrl === '/preGoodsApply') {
  185. const registerInfo = await request.get(
  186. '/api-student/orchestraRegister/registerStatus/' + id
  187. )
  188. this.hasBuyInstruments = registerInfo.data.hasBuyInstruments || false
  189. }
  190. this.directNext()
  191. } catch {
  192. //
  193. }
  194. },
  195. async onSendCode() {
  196. // 发送验证码
  197. if (!checkPhone(this.username)) {
  198. return showToast('请输入正确的手机号码')
  199. }
  200. this.imgCodeStatus = true
  201. },
  202. onCodeSend() {
  203. this.countDownStatus = false
  204. this.$nextTick(() => {
  205. ;(this.$refs.countDownRef as any).start()
  206. })
  207. },
  208. onFinished() {
  209. this.countDownStatus = true
  210. ;(this.$refs.countDownRef as any).reset()
  211. },
  212. onChange() {
  213. if (this.loginType === 'PWD') {
  214. this.loginType = 'SMS'
  215. } else if (this.loginType === 'SMS') {
  216. this.loginType = 'PWD'
  217. }
  218. }
  219. },
  220. render() {
  221. return (
  222. <div class={styles.login}>
  223. <div class={styles.loginTitle}>
  224. {this.orchestraPath === '/preGoodsApply' ? (
  225. '乐团报名'
  226. ) : (
  227. <>
  228. 您好,
  229. <br /> 欢迎使用管乐团学员端{' '}
  230. </>
  231. )}
  232. </div>
  233. <CellGroup class={styles.margin34} border={false}>
  234. <Row style={{ marginBottom: '16px' }}>
  235. <Col span={24} class={styles.formTitle}>
  236. 手机号
  237. </Col>
  238. <Col span={24} class="van-hairline--bottom">
  239. <Field
  240. v-model={this.username}
  241. name="手机号"
  242. placeholder="请输入您的手机号"
  243. type="tel"
  244. maxlength={11}
  245. />
  246. </Col>
  247. </Row>
  248. <Row>
  249. <Col span={24} class={styles.formTitle}>
  250. 验证码
  251. </Col>
  252. <Col span={24} class="van-hairline--bottom">
  253. <Field
  254. v-model={this.smsCode}
  255. name="验证码"
  256. placeholder="请输入验证码"
  257. type="tel"
  258. maxlength={6}
  259. v-slots={{
  260. button: () =>
  261. this.countDownStatus ? (
  262. <span class={styles.codeText} onClick={this.onSendCode}>
  263. 获取验证码
  264. </span>
  265. ) : (
  266. <CountDown
  267. ref="countDownRef"
  268. auto-start={false}
  269. time={this.countDownTime}
  270. onFinish={this.onFinished}
  271. format="ss秒"
  272. />
  273. )
  274. }}
  275. />
  276. </Col>
  277. </Row>
  278. </CellGroup>
  279. <div class={styles.margin34}>
  280. <Button round block type="primary" disabled={this.codeDisable} onClick={this.onLogin}>
  281. 提交
  282. </Button>
  283. </div>
  284. {this.imgCodeStatus ? (
  285. <ImgCode
  286. v-model:value={this.imgCodeStatus}
  287. phone={this.username}
  288. type="REGISTER"
  289. onClose={() => {
  290. this.imgCodeStatus = false
  291. }}
  292. onSendCode={this.onCodeSend}
  293. />
  294. ) : null}
  295. <Popup
  296. v-model:show={this.showPopup}
  297. round
  298. style={{ width: '88%' }}
  299. closeOnClickOverlay={false}
  300. class={styles.wxPopupDialog}
  301. >
  302. <div class={styles.popupContainer}>
  303. <p class={styles.title}>温馨提示</p>
  304. <p class={styles.popupTips} v-html={this.showPopupMessage}></p>
  305. </div>
  306. </Popup>
  307. </div>
  308. )
  309. }
  310. })