index.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <template>
  2. <view v-if="isUp">
  3. <view class="mobile-bg" @click="close"></view>
  4. <view class="mobile-mask animated" :class="{slideInUp:isUp}">
  5. <view class="input-item">
  6. <input type="text" v-model="account" :placeholder="$t(`请输入登录账号`)" maxlength="11" />
  7. </view>
  8. <view class="input-item">
  9. <input type="password" v-model="password" :placeholder="$t(`请输入登录密码`)" maxlength="30" />
  10. </view>
  11. <!-- <view class="input-item">
  12. <input type="text" v-model="codeNum" :placeholder="$t(`输入验证码`)" maxlength="6" />
  13. <button class="code" :disabled="disabled" @click="code">{{text}}</button>
  14. </view> -->
  15. <view class="sub_btn" @click="loginBtn">{{$t(`立即登录`)}}</view>
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. const app = getApp();
  21. import sendVerifyCode from "@/mixins/SendVerifyCode";
  22. import Routine from '@/libs/routine';
  23. import {
  24. loginMobile,
  25. registerVerify,
  26. getCodeApi,
  27. getUserInfo,
  28. phoneSilenceAuth,
  29. phoneWxSilenceAuth,
  30. getLoginAccount
  31. } from "@/api/user";
  32. import {
  33. bindingPhone
  34. } from '@/api/api.js'
  35. export default {
  36. name: 'login_mobile',
  37. props: {
  38. isUp: {
  39. type: Boolean,
  40. default: false,
  41. },
  42. authKey: {
  43. type: String,
  44. default: '',
  45. },
  46. wxcode: {
  47. type: String,
  48. default: '',
  49. }
  50. },
  51. data() {
  52. return {
  53. password: '',
  54. keyCode: '',
  55. account: '',
  56. codeNum: ''
  57. }
  58. },
  59. mixins: [],
  60. mounted() {
  61. // this.getCode();
  62. },
  63. methods: {
  64. // 获取验证码
  65. async code() {
  66. let that = this;
  67. if (!that.account) return that.$util.Tips({
  68. title: that.$t(`请填写登录账号`)
  69. });
  70. // if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(that.account)) return that.$util.Tips({
  71. // title: that.$t(`请输入正确的手机号码`)
  72. // });
  73. await registerVerify({
  74. phone: that.account,
  75. key: that.keyCode,
  76. }).then(res => {
  77. that.$util.Tips({
  78. title: res.msg
  79. });
  80. that.sendCode();
  81. }).catch(err => {
  82. return that.$util.Tips({
  83. title: err
  84. })
  85. })
  86. },
  87. // 获取验证码api
  88. getCode() {
  89. let that = this
  90. getCodeApi().then(res => {
  91. that.keyCode = res.data.key;
  92. }).catch(res => {
  93. that.$util.Tips({
  94. title: res
  95. });
  96. });
  97. },
  98. close() {
  99. this.$emit('close', false)
  100. },
  101. // 登录
  102. loginBtn() {
  103. let that = this
  104. // #ifdef MP
  105. if (!that.account) return that.$util.Tips({
  106. title: that.$t(`请填写登录账号`)
  107. });
  108. // if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(that.account)) return that.$util.Tips({
  109. // title: that.$t(`请输入正确的手机号码`)
  110. // });
  111. if (!that.password) return that.$util.Tips({
  112. title: that.$t(`请填写登录密码`)
  113. });
  114. // if (!that.codeNum) return that.$util.Tips({
  115. // title: that.$t(`请填写验证码`)
  116. // });
  117. // if (!/^[\w\d]+$/i.test(that.codeNum)) return that.$util.Tips({
  118. // title: that.$t(`请输入正确的验证码`)
  119. // });
  120. uni.showLoading({
  121. title: that.$t(`正在登录中`)
  122. });
  123. getLoginAccount({
  124. username: that.account,
  125. password: that.password,
  126. wxcode: that.wxcode
  127. }).then(res => {
  128. this.$store.commit('LOGIN', {
  129. token: res.data.access_token,
  130. time: res.data.expires_in
  131. });
  132. this.$store.commit('OPENID',res.data.openId);
  133. this.getUserInfo();
  134. })
  135. // Routine.getCode()
  136. // .then(code => {
  137. // this.phoneSilenceAuth(code);
  138. // })
  139. // .catch(error => {
  140. // uni.hideLoading();
  141. // });
  142. // #endif
  143. // #ifdef H5
  144. if (!that.account) return that.$util.Tips({
  145. title: that.$t(`请填写登录账号`)
  146. });
  147. // if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(that.account)) return that.$util.Tips({
  148. // title: that.$t(`请输入正确的手机号码`)
  149. // });
  150. if (!that.codeNum) return that.$util.Tips({
  151. title: that.$t(`请填写验证码`)
  152. });
  153. if (!/^[\w\d]+$/i.test(that.codeNum)) return that.$util.Tips({
  154. title: that.$t(`请输入正确的验证码`)
  155. });
  156. uni.showLoading({
  157. title: that.$t(`正在登录中`)
  158. });
  159. if (this.authKey) {
  160. phoneWxSilenceAuth({
  161. spid: app.globalData.spid,
  162. spread: app.globalData.code,
  163. phone: this.account,
  164. captcha: this.codeNum,
  165. key: this.authKey
  166. }).then(res => {
  167. let time = res.data.expires_time - this.$Cache.time();
  168. this.$store.commit('LOGIN', {
  169. token: res.data.token,
  170. time: time
  171. });
  172. this.getUserInfo();
  173. }).catch(error => {
  174. uni.hideLoading()
  175. this.$util.Tips({
  176. title: error
  177. })
  178. })
  179. } else {
  180. loginMobile({
  181. phone: this.account,
  182. captcha: this.codeNum,
  183. spread: app.globalData.spid,
  184. }).then(res => {
  185. let time = res.data.expires_time - this.$Cache.time();
  186. this.$store.commit('LOGIN', {
  187. token: res.data.token,
  188. time: time
  189. });
  190. this.$Cache.clear('snsapiKey');
  191. this.getUserInfo();
  192. }).catch(error => {
  193. uni.hideLoading()
  194. this.$util.Tips({
  195. title: error
  196. })
  197. })
  198. }
  199. // #endif
  200. },
  201. // #ifdef MP
  202. phoneSilenceAuth(code) {
  203. let self = this
  204. phoneSilenceAuth({
  205. code: code,
  206. spread_spid: app.globalData.spid,
  207. spread_code: app.globalData.code,
  208. phone: this.account,
  209. captcha: this.codeNum
  210. }).then(res => {
  211. let time = res.data.expires_time - this.$Cache.time();
  212. this.$store.commit('LOGIN', {
  213. token: res.data.token,
  214. time: time
  215. });
  216. this.$store.commit('SETUID', {
  217. token: res.data.user_id
  218. });
  219. this.getUserInfo();
  220. }).catch(error => {
  221. self.$util.Tips({
  222. title: error
  223. })
  224. })
  225. },
  226. // #endif
  227. /**
  228. * 获取个人用户信息
  229. */
  230. getUserInfo: function() {
  231. let that = this;
  232. getUserInfo().then(res => {
  233. uni.hideLoading();
  234. that.userInfo = res.data
  235. that.$store.commit("SETUID", res.data.userId);
  236. that.$store.commit("UPDATE_USERINFO", res.data);
  237. // #ifdef MP
  238. that.$util.Tips({
  239. title: that.$t(`登录成功`),
  240. icon: 'success'
  241. }, {
  242. tab: 3
  243. })
  244. that.close()
  245. // #endif
  246. // #ifdef H5
  247. that.$emit('wechatPhone', true)
  248. // #endif
  249. });
  250. },
  251. }
  252. }
  253. </script>
  254. <style lang="stylus">
  255. .mobile-bg {
  256. position: fixed;
  257. left: 0;
  258. top: 0;
  259. width: 100%;
  260. height: 100%;
  261. background: rgba(0, 0, 0, 0.5);
  262. }
  263. .mobile-mask {
  264. z-index: 20;
  265. position: fixed;
  266. left: 0;
  267. bottom: 0;
  268. width: 100%;
  269. padding: 67rpx 30rpx;
  270. background: #fff;
  271. .input-item {
  272. display: flex;
  273. justify-content: space-between;
  274. width: 100%;
  275. height: 86rpx;
  276. margin-bottom: 38rpx;
  277. input {
  278. flex: 1;
  279. display: block;
  280. height: 100%;
  281. padding-left: 40rpx;
  282. border-radius: 43rpx;
  283. border: 1px solid #DCDCDC;
  284. }
  285. .code {
  286. display: flex;
  287. align-items: center;
  288. justify-content: center;
  289. width: 220rpx;
  290. height: 86rpx;
  291. margin-left: 30rpx;
  292. background: var(--view-minorColorT);
  293. font-size: 28rpx;
  294. color: var(--view-theme);
  295. border-radius: 43rpx;
  296. &[disabled] {
  297. background: rgba(0, 0, 0, 0.05);
  298. color: #999;
  299. }
  300. }
  301. }
  302. .sub_btn {
  303. width: 690rpx;
  304. height: 86rpx;
  305. line-height: 86rpx;
  306. margin-top: 60rpx;
  307. background: var(--view-theme);
  308. border-radius: 43rpx;
  309. color: #fff;
  310. font-size: 28rpx;
  311. text-align: center;
  312. }
  313. }
  314. .animated {
  315. animation-duration: .4s
  316. }
  317. </style>