健康同学微信公众号h5项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import Vue from 'vue/types/umd';
  2. <!-- -->
  3. <template>
  4. <div class="login">
  5. <div class="login-con">
  6. <div class="login-bg-img">
  7. <van-image width="100%" height="400" :src="bgImgPath" fit="contain" />
  8. </div>
  9. <div class="login-btn">
  10. <van-button type="primary" size="large" round @click="onWxAutoLogin" :color="$green" class="login-text"
  11. >用户一键登录</van-button
  12. >
  13. </div>
  14. </div>
  15. <van-popup v-model="show" round position="bottom" :style="{ height: '50%' }">
  16. <div class="popup">
  17. <van-cell-group>
  18. <van-field
  19. v-model="phone"
  20. type="tel"
  21. maxlength="12"
  22. placeholder="请输入监护人手机号码"
  23. :error-message="errPhoneMsg"
  24. >
  25. <template #left-icon> </template>
  26. </van-field>
  27. <van-field
  28. v-model="verificacode"
  29. type="digit"
  30. maxlength="6"
  31. placeholder="请输入短信验证码"
  32. :error-message="errCodeMsg"
  33. >
  34. <template #button>
  35. <van-button size="small" round :color="$green" @click="sendCode" :disabled="sendMsgDisabled">{{
  36. sendMsgDisabled ? '已发送(' + codeTime + ')' : '发送验证码'
  37. }}</van-button>
  38. </template>
  39. </van-field>
  40. </van-cell-group>
  41. <van-button size="large" round @click="onLogin" :color="$green" class="login-text">注册/登录</van-button>
  42. </div>
  43. </van-popup>
  44. </div>
  45. </template>
  46. <script>
  47. import APIUser from '@/api/user';
  48. import NotifyService from '@/services/notify-service';
  49. import AppId from '@/config/appId';
  50. import APIWx from '@/api/wx';
  51. export default {
  52. data() {
  53. const time = 120;
  54. return {
  55. bgImgPath: require('../../assets/com-imges/55_06.png'),
  56. show: false,
  57. verificacode: '',
  58. phone: '',
  59. errPhoneMsg: '',
  60. errCodeMsg: '',
  61. codeTime: time, //倒计时间
  62. codeInterval: time, //倒计时间
  63. sendMsgDisabled: false,
  64. verificationCode: ' '
  65. };
  66. },
  67. created() {},
  68. mounted() {
  69. // TODO 获取code再拿code获取openId
  70. this.getCode();
  71. },
  72. methods: {
  73. getCode() {
  74. let url = window.location.href;
  75. if (url.indexOf('code') > -1) {
  76. let codeUrl = window.location.href.split('?code=');
  77. console.log('获取code的url', codeUrl);
  78. if (codeUrl) {
  79. let timeStamp = new Date().getTime();
  80. let code = codeUrl[1].split('&')[0];
  81. if (this.$own.isNotNull(code)) {
  82. this.$store.commit('code', `${code}-${timeStamp}`);
  83. this.checkedRegister(code);
  84. }
  85. }
  86. }
  87. },
  88. // 通过code拿去openId
  89. checkedRegister(code) {
  90. if (code) {
  91. let reqBody = {
  92. loginCode: code.split('-')[0],
  93. appId: AppId
  94. };
  95. APIWx.checkIsNewCustomer(reqBody).then(res => {
  96. if (res.data.stateCode === 1 || res.data.stateCode === 2) {
  97. this.$store.commit('openId', res.data.wxOpenId);
  98. }
  99. });
  100. }
  101. },
  102. sendCode() {
  103. // 验证是否输正确的手机号码,
  104. let reg = /^1[3456789]\d{9}$/;
  105. if (this.$own.isNull(this.phone)) {
  106. console.log('手机号码为空');
  107. this.errPhoneMsg = '手机号码不能为空';
  108. } else if (!reg.test(this.phone)) {
  109. console.log('手机号码为空222');
  110. this.errPhoneMsg = '请输入正确的手机号码';
  111. } else {
  112. this.errPhoneMsg = '';
  113. // 调取发送/获取验证码接口
  114. this.getVerificationCode();
  115. }
  116. },
  117. getVerificationCode() {
  118. let reqBody = {
  119. mobile: this.phone,
  120. validateType: 2 // 登录页面 后端约定为 2
  121. };
  122. APIUser.getVerificationCode(reqBody)
  123. .then(res => {
  124. let item = res.data;
  125. if (item.stateCode === 1 && item.code !== null) {
  126. NotifyService.notify({ message: '已发送验证码到该手机', type: 'success' });
  127. this.verificationCode = item.code;
  128. this.sendMsgDisabled = true;
  129. this.countDown();
  130. }
  131. if (item.stateCode === 0 && item.code === null) {
  132. //this.notify('请勿频繁获取,验证码5分钟内有效');
  133. NotifyService.notify({ message: item.message });
  134. }
  135. })
  136. .catch(() => {
  137. NotifyService.notify({ message: '获取验证码过于频繁.请稍后再试', type: 'warning' });
  138. })
  139. .finally(() => (this.canSendVerificationCode = true));
  140. },
  141. countDown() {
  142. let codeInterval = window.setInterval(() => {
  143. if (--this.codeTime <= 0) {
  144. this.codeTime = this.codeInterval;
  145. this.sendMsgDisabled = false;
  146. window.clearInterval(codeInterval);
  147. }
  148. }, 1000);
  149. },
  150. onLogin() {
  151. // 一键登录之前调取登录接口查询是否是旧用户,如果是旧用户直接登录无需再输入手机号码和验证码登录,否则需要
  152. // 现在模拟是新用户,待接口完成
  153. let reg = /^1[3456789]\d{9}$/;
  154. if (this.phone === '' || !reg.test(this.phone)) {
  155. this.errPhoneMsg = '请输入正确的手机号码';
  156. } else if (this.verificacode === '') {
  157. this.errCodeMsg = '请输入正确的验证码';
  158. } else {
  159. // 格式正确
  160. this.errPhoneMsg = '';
  161. this.loginRequest();
  162. }
  163. },
  164. // 使用微信code得到的openId 自动登录
  165. onWxAutoLogin() {
  166. let reqBody = {
  167. appId: AppId,
  168. openId: /* this.$store.getters.openId || */ 'o7Z9t0zyOyZxojWy0VwsLNChddzs'
  169. };
  170. APIUser.wxAutoLogin(reqBody)
  171. .then(res => {
  172. if (res.data.stateCode === 1) {
  173. if (res.data) {
  174. let item = res.data;
  175. this.$store.commit('authToken', item.authToken);
  176. this.$store.commit('userId', item.userId);
  177. this.$store.commit('code', '');
  178. this.$store.commit('openId', item.wxOpenId || '');
  179. NotifyService.notify({ message: '登录成功!正在为您跳转...', type: 'success' });
  180. this.$store.commit('isLogin', 'true');
  181. setTimeout(() => {
  182. this.$router.push({ name: 'Index' });
  183. }, 1000);
  184. }
  185. } else {
  186. this.show = true;
  187. }
  188. })
  189. .catch(e => {
  190. console.log(e);
  191. NotifyService.notify({ message: `${e.message}` });
  192. });
  193. },
  194. loginRequest() {
  195. /* let reqBody = {
  196. phoneNumber: this.phone,
  197. loginCode: this.$store.getters.openId,
  198. appId: AppId,
  199. loginType: 2,
  200. verificationCode: this.verificacode
  201. }; */
  202. let reqBody = {
  203. loginName: /* this.phone */ '18664272743',
  204. password: /* this.password */ '123456',
  205. loginCode: /* this.$store.getters.openId */ 'ofTDa6FWgxD-HKoeYtJcrr_9StlE',
  206. appId: AppId
  207. };
  208. APIUser.wxLogin(reqBody)
  209. .then(res => {
  210. let item = res.data;
  211. if (item.stateCode === 0) {
  212. NotifyService.notify({ message: item.message });
  213. } else if (item.stateCode === 1) {
  214. this.$store.commit('authToken', item.authToken);
  215. this.$store.commit('userId', item.userId);
  216. this.$store.commit('code', '');
  217. NotifyService.notify({ message: '登录成功!正在为您跳转...', type: 'success' });
  218. this.$store.commit('isLogin', 'true');
  219. setTimeout(() => {
  220. this.$router.push({
  221. name: 'personInfos',
  222. query: {
  223. from: 'login',
  224. toRouter: 'Index'
  225. }
  226. });
  227. }, 1000);
  228. } else {
  229. NotifyService.notify({ message: '获取openId失败,请您重新进入公众号' });
  230. }
  231. })
  232. .catch(e => {
  233. console.log(e);
  234. NotifyService.notify({ message: '请求失败,请联系系统管理员' });
  235. });
  236. }
  237. }
  238. };
  239. </script>
  240. <style scoped lang="scss">
  241. /* @import url(); 引入css类 */
  242. @import './login.scss';
  243. </style>