index.vue 6.2 KB

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