index.vue 7.1 KB

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