login.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <template>
  2. <view style="height: 100%;">
  3. <image mode="widthFix" src="/static/login_bg.jpg" class="full_img"></image>
  4. <u-navbar title="登录" :border-bottom="false" background="{ background: '#ffffff',opacity:0.4; }" title-color="#ffffff" back-icon-color="#ffffff"></u-navbar>
  5. <view style="padding: 30rpx;">
  6. <view style="width: 400rpx;">
  7. <u-subsection @change="sectionChange" :list="list" :current="current" active-color="#007AFF" inactive-color="#ffffff" bg-color="rgba(255,255,255,0.52)"></u-subsection>
  8. </view>
  9. <view class="login_box">
  10. <u-form :model="form" ref="uForm" v-if="current==0">
  11. <u-form-item ><u-input v-model="form.account" placeholder="手机号/学员身份证"/></u-form-item>
  12. <u-form-item ><u-input v-model="form.pwd" type="password" placeholder="登录密码"/></u-form-item>
  13. </u-form>
  14. <u-form :model="form" ref="uForm" v-if="current==1">
  15. <u-form-item ><u-input v-model="form.tel" placeholder="手机号"/></u-form-item>
  16. <u-form-item >
  17. <u-input v-model="form.code" placeholder="验证码"/>
  18. <u-button slot="right" size="mini" @click="getCode">{{codeTips}}</u-button>
  19. </u-form-item>
  20. </u-form>
  21. </view>
  22. <button class="loginBtn" @click="login" v-if="current==0">
  23. 登录
  24. </button>
  25. <button :disabled="isUse" class="loginBtn" @click="sms_login" v-if="current==1">
  26. 登录
  27. </button>
  28. <view style="width: 100%;margin: 40rpx auto;">
  29. <view style="display: flex;align-items: center;color: #007AFF;font-size: 24rpx;justify-content:center;">
  30. <navigator url="/pages2/register/register" style="margin: 0 40rpx;">立即注册</navigator>
  31. <view style="width: 3rpx;height: 20rpx;background-color: #007AFF;"></view>
  32. <navigator url="/pages2/register/forget" style="margin: 0 40rpx;">忘记密码</navigator>
  33. </view>
  34. </view>
  35. <view class="wxBtn">
  36. <button type="default" class="wxloginBtn"></button>
  37. <view style="text-align: center;color: #999999;font-size: 24rpx;margin-top: 10rpx;">微信快捷登录</view>
  38. </view>
  39. </view>
  40. <u-verification-code seconds="60" ref="uCode" @change="codeChange"></u-verification-code>
  41. </view>
  42. </template>
  43. <script>
  44. export default {
  45. data() {
  46. return {
  47. code: '',
  48. form:{
  49. tel:'',
  50. code:'',
  51. account:'',
  52. pwd:''
  53. },
  54. list: [
  55. {
  56. name: '密码登录'
  57. },
  58. {
  59. name: '短信登录'
  60. }
  61. ],
  62. current: 0,
  63. codeTips: '',
  64. isUse:false,
  65. isBack:false
  66. };
  67. },
  68. onLoad(option) {
  69. if(option.isBack){
  70. this.isBack = option.isBack;
  71. }
  72. },
  73. mounted() {},
  74. methods: {
  75. sms_login(){
  76. let that = this
  77. if(!this.form.tel){
  78. this.$u.toast('请输入手机号码');
  79. return
  80. }
  81. if(!this.form.code){
  82. this.$u.toast('请输入验证码');
  83. return
  84. }
  85. that.isUse = true
  86. let datas = {
  87. tel:this.form.tel,
  88. code:this.form.code
  89. }
  90. that.$api.smsLogin(datas).then(
  91. res => {
  92. that.isUse = false
  93. if (res.data.code == 200) {
  94. if(res.data.data.full_info){
  95. //信息完善,直接进入页面
  96. uni.setStorageSync('user_account', res.data.data.user_account);
  97. uni.setStorageSync('token', res.data.data.token);
  98. that.$api.getInfo().then(resdata => {
  99. if(resdata.data.code == 200){
  100. that.$store.state.userInfo = resdata.data.data;
  101. if(that.isBack){
  102. uni.switchTab({
  103. url:'/pages/index/index'
  104. })
  105. }else{
  106. uni.navigateBack();
  107. }
  108. }
  109. });
  110. }else{
  111. //未完善信息,存为临时信息
  112. uni.setStorageSync('user_account_temp', res.data.data.user_account);
  113. uni.setStorageSync('token_temp', res.data.data.token);
  114. that.$navTo.togo('/pages2/register/bind');
  115. }
  116. } else {
  117. that.$u.toast(res.data.msg);
  118. }
  119. },
  120. err => {
  121. that.isUse = false
  122. }
  123. );
  124. },
  125. fakeLogin(){
  126. uni.setStorageSync('user_account', '123');
  127. uni.setStorageSync('token', '123');
  128. uni.switchTab({
  129. url:'/pages/index/index'
  130. })
  131. },
  132. login(){
  133. let that = this
  134. if(!this.form.account){
  135. this.$u.toast('请输入手机号码/身份证号');
  136. return
  137. }
  138. if(!this.form.pwd){
  139. this.$u.toast('请输入密码');
  140. return
  141. }
  142. //虚拟登录
  143. /* that.fakeLogin()
  144. return */
  145. that.isUse = true
  146. that.$api.accountLogin(this.form).then(
  147. res => {
  148. that.isUse = false
  149. if (res.data.code == 200) {
  150. if(res.data.data.full_info){
  151. //信息完善,直接进入页面
  152. uni.setStorageSync('user_account', res.data.data.user_account);
  153. uni.setStorageSync('token', res.data.data.token);
  154. that.$api.getInfo().then(resdata => {
  155. if(resdata.data.code == 200){
  156. that.$store.state.userInfo = resdata.data.data;
  157. if(that.isBack){
  158. uni.switchTab({
  159. url:'/pages/index/index'
  160. })
  161. }else{
  162. uni.navigateBack();
  163. }
  164. }
  165. });
  166. }else{
  167. //未完善信息,存为临时信息
  168. uni.setStorageSync('user_account_temp', res.data.data.user_account);
  169. uni.setStorageSync('token_temp', res.data.data.token);
  170. that.$navTo.togo('/pages2/register/bind');
  171. }
  172. } else {
  173. that.$u.toast(res.data.msg);
  174. }
  175. },
  176. err => {
  177. that.isUse = false
  178. }
  179. );
  180. },
  181. codeChange(text) {
  182. this.codeTips = text;
  183. },
  184. // 获取验证码
  185. getCode() {
  186. let that = this
  187. if(that.$refs.uCode.canGetCode) {
  188. if(!this.form.tel){
  189. this.$u.toast('请输入手机号码');
  190. return
  191. }
  192. let datas = {tel:this.form.tel}
  193. that.$api.loginSms(datas).then(
  194. res => {
  195. if (res.data.code == 200) {
  196. that.$u.toast('验证码已发送');
  197. // 通知验证码组件内部开始倒计时
  198. that.$refs.uCode.start();
  199. } else {
  200. that.$u.toast(res.data.msg);
  201. }
  202. },
  203. err => {
  204. console.log(err);
  205. }
  206. );}
  207. },
  208. sectionChange(index) {
  209. this.current = index;
  210. },
  211. getPhoneNumber(e) {
  212. let that = this;
  213. uni.checkSession({
  214. success () {
  215. //session_key 未过期,并且在本生命周期一直有效
  216. that.putInfo(e)
  217. },
  218. fail () {
  219. // session_key 已经失效,需要重新执行登录流程
  220. uni.login({
  221. provider: 'weixin',
  222. success: function(loginRes) {
  223. that.code = loginRes.code;
  224. that.putInfo(e)
  225. }
  226. });
  227. }
  228. })
  229. },
  230. putInfo(e){
  231. let that = this;
  232. if (e.detail.encryptedData) {
  233. let inviteCode = uni.getStorageSync("inviteCode")
  234. //用户同意授权
  235. var datas = {
  236. iv: e.detail.iv,
  237. encryptedData: e.detail.encryptedData,
  238. code: that.code
  239. };
  240. if(inviteCode){
  241. datas.inviteCode = inviteCode
  242. }
  243. that.$api.login(datas).then(
  244. res => {
  245. if (res.data.code == 200) {
  246. uni.setStorageSync('union_id', res.data.data.union_id);
  247. uni.setStorageSync('token', res.data.data.token);
  248. that.$api.getInfo().then(resdata => {
  249. if(resdata.data.code == 200){
  250. uni.navigateBack();
  251. that.$store.state.userInfo = resdata.data.data;
  252. }
  253. });
  254. } else {
  255. uni.showModal({
  256. title: '提示',
  257. content: res.data.msg,
  258. showCancel: false
  259. });
  260. }
  261. },
  262. err => {
  263. console.log(err);
  264. }
  265. );
  266. }
  267. }
  268. },
  269. onLoad(option) {
  270. let that = this;
  271. this.from = option.from;
  272. uni.login({
  273. provider: 'weixin',
  274. success: function(loginRes) {
  275. that.code = loginRes.code;
  276. }
  277. });
  278. }
  279. };
  280. </script>
  281. <style scoped>
  282. .wxBtn{
  283. position: fixed;
  284. bottom: 10%;
  285. width: 100%;
  286. left: 0;
  287. }
  288. /deep/ .wxBtn button::after{
  289. border: none;
  290. }
  291. .loginBtn{
  292. width: 526rpx;
  293. height: 80rpx;
  294. background: linear-gradient(90deg, #015EEA, #00C0FA);
  295. box-shadow: 0rpx 10rpx 16rpx 4rpx rgba(1, 99, 235, 0.04);
  296. opacity: 0.6;
  297. border-radius: 40rpx;
  298. color: #FFFFFF;
  299. text-align: center;
  300. line-height: 80rpx;
  301. margin: 40rpx auto;
  302. }
  303. .wxloginBtn{
  304. background: url("/static/loginBtn.png") no-repeat;
  305. background-size:100% 100%;
  306. border:none;
  307. width: 100rpx;
  308. height: 100rpx;
  309. }
  310. /deep/page {
  311. background-color: #FFFFFF;
  312. height: 100%;
  313. width: 100%;
  314. }
  315. .login_box{
  316. width: 100%;
  317. height: 360rpx;
  318. background: #FFFFFF;
  319. box-shadow: 0rpx 0rpx 16rpx 4rpx rgba(1, 99, 235, 0.1);
  320. border-radius: 24rpx;
  321. margin-top: 30rpx;
  322. padding:40rpx 35rpx;
  323. }
  324. /deep/ .u-item-bg{
  325. border-radius: 32px !important;
  326. }
  327. /deep/ .u-subsection{
  328. border-radius: 32px !important;
  329. }
  330. .full_img {
  331. position: absolute;
  332. left: 0;
  333. display: block;
  334. width: 100%;
  335. z-index: -999;
  336. top: 0;
  337. }
  338. .head {
  339. height: 96rpx;
  340. width: 100%;
  341. line-height: 96rpx;
  342. margin-top: 40rpx;
  343. text-align: center;
  344. display: flex;
  345. position: relative;
  346. justify-content: center;
  347. }
  348. .icon {
  349. position: absolute;
  350. left: 30rpx;
  351. }
  352. </style>