login.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <template>
  2. <view class="body">
  3. <u-navbar title="登录" bgColor="rgba(255,255,255,0.3)" :leftIconSize="0" titleStyle="font-weight:bold;color:#7a5831"></u-navbar>
  4. <block>
  5. <!-- #ifndef MP-WEIXIN -->
  6. <view class="login-bg">
  7. <view class="login-card">
  8. <view class="login-head">
  9. <image class="logoimg" :src="baseLogo" mode="widthFix"></image>
  10. </view>
  11. <view class="login-input login-margin-b">
  12. <input type="number" v-model="username" placeholder="请输入手机号" />
  13. </view>
  14. <view class="login-input">
  15. <input :password="true" v-model="password" placeholder="请输入密码(6-16位)" />
  16. </view>
  17. <view class="cu-bar btn-group margin-top">
  18. <button class="cu-btn bg-orange shadow-blur round" :loading="loading" @tap="login">{{ loading ? '登录中...' : '登 录' }}</button>
  19. </view>
  20. <!-- <view class="flex justify-center">
  21. <view class="text-gray text-sm margin-top-xl" @tap="register">注册新账户</view>
  22. </view> -->
  23. </view>
  24. </view>
  25. <!-- #endif -->
  26. <!-- #ifdef MP-WEIXIN -->
  27. <view class="logView">
  28. <button @click="onGetUserProfile" class="logbt">
  29. <view class="login-head">
  30. <image class="logoimg" :src="baseLogo" mode="widthFix"></image>
  31. </view>
  32. <view class="loginTitile"><text decode="true">请点击微信登录,并授权获取公开信息, 登录后您将获得更多权益</text></view>
  33. <view class="cu-btn bg-orange shadow-blur round">
  34. <text class="cuIcon-lightauto"></text>
  35. 微信登录
  36. </view>
  37. </button>
  38. <!-- <view class="text-gray text-sm margin-top-xl" @click="changMobileLogin()">手机登录</view> -->
  39. </view>
  40. <!-- #endif -->
  41. </block>
  42. </view>
  43. </template>
  44. <script>
  45. var _this;
  46. import { baseLogo } from '../../config/config.js';
  47. export default {
  48. data() {
  49. return {
  50. loading: false,
  51. user: [],
  52. username: '',
  53. password: '',
  54. class_id: '',
  55. ismobile: false,
  56. group_id: 1,
  57. code: '',
  58. baseLogo: baseLogo,
  59. redirect: ''
  60. };
  61. },
  62. mounted() {
  63. _this = this;
  64. },
  65. onLoad(e) {
  66. if (e.redirect) {
  67. this.redirect = e.redirect;
  68. }
  69. },
  70. onShow() {
  71. this.user = this.$common.userInfo();
  72. console.log('this.user: ', this.user);
  73. if (typeof this.user == 'undefined' || this.user == '' || this.user == null) {
  74. } else {
  75. this.$common.navigateTo('index');
  76. }
  77. // #ifdef MP-WEIXIN
  78. this.wxLogin();
  79. // #endif
  80. },
  81. methods: {
  82. // onChooseAvatar(e) {
  83. // console.log(e, '头像');
  84. // },
  85. wxLogin() {
  86. wx.login({
  87. success: (res) => {
  88. this.code = res.code;
  89. },
  90. fail: function (error) {
  91. console.log('login failed ' + error);
  92. }
  93. });
  94. },
  95. //切换微信登录
  96. // wechatLogin(){
  97. // _this.ismobile=false;
  98. // },
  99. //切换手机登录
  100. // changMobileLogin(){
  101. // _this.ismobile=true;
  102. // },
  103. // register() {
  104. // this.$common.navigateTo('register');
  105. // },
  106. login() {
  107. _this.loading = true;
  108. if (_this.username == '' || _this.username.length < 11) {
  109. uni.showToast({
  110. icon: 'none',
  111. title: '请输入正确的手机号'
  112. });
  113. _this.loading = false;
  114. return;
  115. }
  116. if (_this.password == '') {
  117. uni.showToast({
  118. icon: 'none',
  119. title: '请输入密码'
  120. });
  121. _this.loading = false;
  122. return;
  123. }
  124. _this.$api.login(
  125. {
  126. account: _this.username,
  127. password: _this.password
  128. },
  129. (data) => {
  130. //console.log(data);
  131. if (data.code == 1) {
  132. _this.loading = false;
  133. //console.log(data);
  134. try {
  135. /* 存入本地 */
  136. _this.$db.set('upload', 1);
  137. _this.$db.set('login', 1);
  138. _this.$db.set('token', data.data.userinfo.token);
  139. _this.$db.set('user', data.data.userinfo);
  140. _this.$db.set('auth', data.data.auth);
  141. _this.$store.commit('setAccessToken', data.data.userinfo.token);
  142. _this.$store.commit('setActivityId', 0);
  143. } catch (e) {}
  144. _this.$common.successToShow(data.msg, function () {
  145. // _this.$common.navigateTo('index');
  146. if (_this.redirect != '') {
  147. console.log(_this.redirect);
  148. if (_this.redirect.indexOf('%2F') > 0) {
  149. _this.redirect = '/' + unescape(_this.redirect);
  150. }
  151. _this.$common.navigateTo(_this.redirect);
  152. return;
  153. }
  154. uni.navigateBack();
  155. });
  156. } else {
  157. _this.loading = false;
  158. uni.showToast({
  159. duration: 1500,
  160. icon: 'none',
  161. title: data.msg
  162. });
  163. }
  164. }
  165. );
  166. },
  167. onGetUserProfile() {
  168. // uni.showLoading({
  169. // title:"正在登录中..."
  170. // })
  171. var platform = 'wechat';
  172. var that = this;
  173. var fid = uni.getStorageSync('parentid') ? uni.getStorageSync('parentid') : '';
  174. uni.getUserProfile({
  175. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  176. success: (res) => {
  177. // console.log(res, 888888);
  178. var invite_id = _this.$db.get('invite_id');
  179. if (!invite_id) invite_id = 0;
  180. _this.$api.third(
  181. {
  182. code: _this.code,
  183. platform: platform,
  184. encrypted_data: res.encryptedData,
  185. iv: res.iv,
  186. raw_data: res.rawData,
  187. signature: res.signature,
  188. invite_id: invite_id,
  189. appid: 'wx9a27d13d29890754',
  190. main_body_id: '1'
  191. },
  192. (data) => {
  193. console.log(data, 33333);
  194. // console.log(data.data.userinfo)
  195. var res = data.data;
  196. if (data.code == 1) {
  197. this.$common.successToShow('登录成功!');
  198. try {
  199. this.$db.set('upload', 1);
  200. this.$db.set('login', 1);
  201. this.$db.set('auth', res.auth);
  202. this.$db.set('user', res.userinfo);
  203. this.$db.set('token', res.auth.token);
  204. this.$db.set('mainBodyUserInfo', res.mainBodyUserInfo);
  205. // // 主体id
  206. // this.$db.set('main_body_id', res.mainBodyUserInfo.main_body_id);
  207. // // // 主体用户id
  208. // this.$db.set('main_body_user_id', res.mainBodyUserInfo.id);
  209. this.$store.commit('setAccessToken', res.auth.token);
  210. this.$store.commit('setActivityId', 2);
  211. } catch (e) {
  212. // console.log('e: ', e);
  213. }
  214. uni.switchTab({
  215. url: '/pages/index/index'
  216. });
  217. if (this.redirect != '') {
  218. console.log(this.redirect);
  219. if (this.redirect.indexOf('%2F') > 0) {
  220. this.redirect = '/' + unescape(this.redirect);
  221. }
  222. this.$common.navigateTo(this.redirect);
  223. return;
  224. }
  225. uni.navigateBack();
  226. } else {
  227. _this.wxLogin();
  228. }
  229. }
  230. );
  231. },
  232. fail: (res) => {
  233. console.log('res: ', res);
  234. _this.wxLogin(); //重新获取登录code
  235. uni.hideLoading();
  236. if (res.errMsg == 'getUserInfo:cancel' || res.errMsg == 'getUserInfo:fail auth deny') {
  237. uni.showModal({
  238. title: '用户授权失败',
  239. showCancel: false,
  240. content: '请点击重新授权,如果未弹出授权,请尝试长按删除小程序,重新进入!',
  241. success: function (res) {
  242. if (res.confirm) {
  243. console.log('用户点击确定');
  244. uni.navigateBack();
  245. }
  246. }
  247. });
  248. }
  249. }
  250. });
  251. // uni.login({
  252. // success: loginRes => {
  253. // uni.hideLoading();
  254. // console.log('第一次登录'+loginRes.code)
  255. // if (loginRes.code && loginRes.code!='') {
  256. // console.log('2222222222222222222')
  257. // }
  258. // }
  259. // })
  260. }
  261. }
  262. };
  263. </script>
  264. <style>
  265. page {
  266. background: #fff;
  267. }
  268. .content {
  269. height: 100%;
  270. }
  271. .logView {
  272. display: flex;
  273. align-items: center;
  274. justify-content: center;
  275. flex-direction: column;
  276. align-items: center;
  277. /* 垂直居中 */
  278. width: 100%;
  279. position: fixed;
  280. left: 50%;
  281. top: 50%;
  282. transform: translate(-50%, -50%);
  283. }
  284. .logbt {
  285. display: flex;
  286. align-items: center;
  287. justify-content: center;
  288. flex-direction: column;
  289. align-items: center;
  290. /* 垂直居中 */
  291. width: 100%;
  292. background: none;
  293. border: none !important;
  294. }
  295. .logbt:after {
  296. border: none !important;
  297. }
  298. .logbt .logoimg {
  299. width: 200rpx;
  300. height: 200rpx;
  301. display: block;
  302. }
  303. .logbt .wechatimg {
  304. width: 150rpx;
  305. height: 150rpx;
  306. display: block;
  307. }
  308. .loginTitile {
  309. padding: 50rpx;
  310. font-size: 28rpx;
  311. color: #787878;
  312. line-height: 1.3;
  313. text-align: center;
  314. }
  315. .loginBtn {
  316. width: 300rpx;
  317. height: 70rpx;
  318. line-height: 70rpx;
  319. color: #fff;
  320. background: #2562a1;
  321. border-radius: 10rpx;
  322. border: none;
  323. }
  324. image {
  325. width: 100rpx;
  326. height: 100rpx;
  327. }
  328. .mobileLogin {
  329. background: none;
  330. color: #999;
  331. text-align: center;
  332. margin: 40rpx auto;
  333. border: none;
  334. font-size: 26rpx;
  335. }
  336. .landing[type='primary'] {
  337. height: 84rpx;
  338. line-height: 84rpx;
  339. border-radius: 44rpx;
  340. font-size: 32rpx;
  341. /* background: linear-gradient(left, #86B5F4, #4790EF); */
  342. background-color: #ffbc32;
  343. }
  344. .login-btn {
  345. padding: 10rpx 20rpx;
  346. margin-top: 60rpx;
  347. }
  348. .login-function {
  349. overflow: auto;
  350. padding: 20rpx 20rpx 30rpx 20rpx;
  351. }
  352. .login-forget {
  353. float: left;
  354. font-size: 26rpx;
  355. color: #999;
  356. }
  357. .login-register {
  358. color: #666;
  359. float: right;
  360. font-size: 26rpx;
  361. }
  362. .login-input input {
  363. background: #f2f5f6;
  364. font-size: 28rpx;
  365. padding: 10rpx 25rpx;
  366. height: 80rpx;
  367. line-height: 80rpx;
  368. border-radius: 40rpx;
  369. }
  370. .login-margin-b {
  371. margin-bottom: 25rpx;
  372. }
  373. .login-input {
  374. padding: 10rpx 20rpx;
  375. }
  376. .login-head {
  377. font-size: 34rpx;
  378. text-align: center;
  379. padding: 25rpx 10rpx 55rpx 10rpx;
  380. }
  381. .login-head image {
  382. width: 200rpx;
  383. }
  384. .login-card {
  385. background: #fff;
  386. border-radius: 12rpx;
  387. padding: 10rpx 25rpx;
  388. position: relative;
  389. margin-top: 120rpx;
  390. }
  391. .login-bg {
  392. height: 100%;
  393. padding: 25rpx;
  394. }
  395. </style>