index.vue 6.9 KB

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