login.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <template>
  2. <view class="u-page content">
  3. <u-icon class="back-btn" name="arrow-left" color="black" size="20" @click="navBack"></u-icon>
  4. <view class="wrapper">
  5. <image style='width:38px;height:38px;margin-bottom:10px;' src='@/static/logo.png'></image>
  6. <h2 class="title">欢迎使用网络货运</h2>
  7. <u--form labelPosition="left" :model="dataDetails" ref="loginForm" labelWidth="80">
  8. <u-form-item label="电话" prop="phone" borderBottom>
  9. <u--input v-model="dataDetails.phone" border="none" placeholder="请输入手机号码"></u--input>
  10. </u-form-item>
  11. <u-form-item label="验证码" prop="code" labelWidth="80" borderBottom>
  12. <input v-model="dataDetails.code" border="none" placeholder="请填写验证码"></input>
  13. <u-button slot="right" @tap="getCode" :text="tips" class="code-style" size="mini"
  14. :disabled="disabled1">
  15. </u-button>
  16. </u-form-item>
  17. <u-code ref="uCode" @change="codeChange" seconds="20" @start="disabled1 = true"
  18. @end="disabled1 = false">
  19. </u-code>
  20. </u--form>
  21. <button @tap="login" class="code-style submit">登录</button>
  22. <view @click='gonumber' class="use-password" style='text-align:center;color:#6A6A6A;'>使用账号密码登录</view>
  23. <!-- <u-button type="primary" text="登录" customStyle="margin-top: 50px" @click="login()"></u-button>
  24. <u-button type="primary" @click='goregister'>手机号一键注册</u-button> -->
  25. <!-- <u-loading-page :loading="isLoading" bg-color="#e8e8e8"></u-loading-page> -->
  26. </view>
  27. <u-toast ref="uToast"></u-toast>
  28. </view>
  29. </template>
  30. <script>
  31. var _this
  32. import helper from '@/common/helper.js';
  33. export default {
  34. data() {
  35. return {
  36. isLoading: false,
  37. disabled1: false,
  38. tips: '',
  39. showCalendar: false,
  40. showBirthday: false,
  41. dataDetails: {
  42. phone: '',
  43. code: ''
  44. },
  45. rules: {
  46. phone: [{
  47. required: true,
  48. message: '请输入手机号',
  49. trigger: ['change', 'blur'],
  50. },
  51. {
  52. // 自定义验证函数,见上说明
  53. validator: (rule, value, callback) => {
  54. // 上面有说,返回true表示校验通过,返回false表示不通过
  55. // uni.$u.test.mobile()就是返回true或者false的
  56. return uni.$u.test.mobile(value);
  57. },
  58. message: '手机号码不正确',
  59. // 触发器可以同时用blur和change
  60. trigger: ['blur'],
  61. }
  62. ],
  63. code: {
  64. type: 'string',
  65. required: true,
  66. len: 6,
  67. message: '请填写6位验证码',
  68. trigger: ['blur'],
  69. },
  70. }
  71. }
  72. },
  73. onReady() {
  74. // 如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则
  75. this.$refs.loginForm.setRules(this.rules)
  76. },
  77. onLoad() {
  78. _this = this
  79. },
  80. methods: {
  81. gonumber() {
  82. uni.navigateTo({
  83. url: '/pages/public/lginOther'
  84. })
  85. },
  86. navBack() {
  87. uni.navigateBack();
  88. },
  89. codeChange(text) {
  90. this.tips = text;
  91. },
  92. getCode() {
  93. if (this.dataDetails.phone.length != 11) {
  94. this.$refs.uToast.show({
  95. type: 'error',
  96. message: "手机号输入错误",
  97. })
  98. } else {
  99. if (this.$refs.uCode.canGetCode) {
  100. // 模拟向后端请求验证码
  101. uni.showLoading({
  102. title: '正在获取验证码'
  103. })
  104. _this.$request.baseRequest('get', '/commonUser/sendVerifyCode', {
  105. phone: this.dataDetails.phone
  106. }).then(res => {
  107. uni.hideLoading();
  108. // 这里此提示会被this.start()方法中的提示覆盖
  109. uni.$u.toast('验证码已发送');
  110. // 通知验证码组件内部开始倒计时
  111. this.$refs.uCode.start();
  112. })
  113. .catch(res => {
  114. uni.$u.toast(res.message);
  115. });
  116. } else {
  117. uni.$u.toast('倒计时结束后再发送');
  118. }
  119. }
  120. },
  121. login() {
  122. // 如果有错误,会在catch中返回报错信息数组,校验通过则在then中返回true
  123. this.$refs.loginForm.validate().then(res => {
  124. uni.$u.toast('校验通过')
  125. this.isLoading = true
  126. _this.$request.baseRequest('get', '/commonUser/loginVerifyCode', {
  127. phone: _this.dataDetails.phone,
  128. verifyCode: _this.dataDetails.code,
  129. loginFlag: 2
  130. }).then(res => {
  131. if (res.data.code == '11002') {
  132. uni.$u.toast('验证码错误或过期,请重新获取')
  133. return
  134. } else if (res.data.code == '11004') {
  135. uni.$u.toast('该手机号已注册,可直接登录')
  136. return
  137. } else if (res.data.code == '11006') {
  138. uni.$u.toast('手机号或密码错误!')
  139. return
  140. }
  141. _this.$request.baseRequest('', '/driverInfo/firstAuthentication', {
  142. driverPhone: _this.dataDetails.phone,
  143. }).then(res1 => {
  144. uni.setStorageSync('pcUserInfo', res.data)
  145. uni.setStorageSync('userInfo', res.data)
  146. uni.setStorageSync('firstAuthentication', res1.data)
  147. helper.getListByUserId()
  148. _this.$store.commit('login', res.data)
  149. // that.liangxinLogin()
  150. uni.switchTab({
  151. url: '/pages/goodSource/index'
  152. });
  153. _this.isLoading = false
  154. //
  155. //
  156. })
  157. .catch(res => {
  158. uni.$u.toast(res.message);
  159. });
  160. // that.$request.TokenRequest('post', '/commonUser/api/loginQuickly', {
  161. // mobilePhone: that.model1.phone,
  162. // veriCode: that.model1.code
  163. // }).then(res1 => {
  164. // uni.setStorageSync('pcUserInfo', res1.data)
  165. // uni.setStorageSync('userInfo', res.data)
  166. // helper.getListByUserId()
  167. // that.$store.commit('login', res.data)
  168. // // that.liangxinLogin()
  169. // uni.switchTab({
  170. // url: '/pages/index/index'
  171. // });
  172. // this.isLoading = false
  173. // })
  174. })
  175. .catch(res => {
  176. uni.showToast({
  177. title: res.message,
  178. icon: 'none',
  179. duration: 2000
  180. })
  181. });
  182. }).catch(errors => {
  183. this.$refs.uToast.show({
  184. type: 'error',
  185. message: "校验失败",
  186. })
  187. })
  188. },
  189. reset() {
  190. const validateList = ['userInfo.name', 'userInfo.sex', 'radiovalue1', 'checkboxValue1', 'intro',
  191. 'hotel', 'code', 'userInfo.birthday'
  192. ]
  193. this.$refs.form1.resetFields()
  194. this.$refs.form1.clearValidate()
  195. setTimeout(() => {
  196. this.$refs.form1.clearValidate(validateList)
  197. // 或者使用 this.$refs.form1.clearValidate()
  198. }, 10)
  199. },
  200. hideKeyboard() {
  201. uni.hideKeyboard()
  202. },
  203. goregister() {
  204. uni.$u.route('/pages/public/register');
  205. },
  206. },
  207. }
  208. </script>
  209. <style lang="scss">
  210. .content {
  211. background: url(../../static/images/mine/bg@2x.png);
  212. background-size: 100% 100%;
  213. height: 100vh;
  214. padding-top: 85px;
  215. position: relative;
  216. }
  217. .back-btn {
  218. position: absolute;
  219. left: 40upx;
  220. z-index: 9999;
  221. padding-top: var(--status-bar-height);
  222. top: 40upx;
  223. font-size: 40upx;
  224. color: red;
  225. }
  226. .wrapper {
  227. position: relative;
  228. z-index: 90;
  229. padding-bottom: 40upx;
  230. height: 100%;
  231. padding: 0 30px;
  232. }
  233. .code-style {
  234. background: #2772FB;
  235. color: white;
  236. }
  237. .use-password {
  238. margin-top: 31rpx;
  239. }
  240. .submit {
  241. margin-top: 40rpx;
  242. }
  243. </style>