index.vue 6.4 KB

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