index.vue 6.4 KB

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