login.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <template>
  2. <view class="container">
  3. <view class="container_text">
  4. <image class="banner-img" src="/static/img/img01.png" mode="scaleToFill"></image>
  5. </view>
  6. <view class="loginTitle"><text>登录</text></view>
  7. <view class="login_text">
  8. <view class="login_input flex">
  9. <view class="login_img"><image src="/static/icon/img03.png"></image></view>
  10. <view class="login_name"><input class="uni-input" v-model="username" focus placeholder="请输入手机号" /></view>
  11. </view>
  12. <view class="login_input flex">
  13. <view class="login_img"><image src="/static/icon/img04.png"></image></view>
  14. <view class="login_name"><input class="uni-input" type="password" v-model="passward" focus placeholder="请输入密码" /></view>
  15. </view>
  16. <view><button type="green" class="uni-button uni-button-green" @click="toLogin">登录</button></view>
  17. <view><button type="green" class="uni-button uni-button-green uni-button-green-plain" plain="true" hover-class="none" @click="register">注册</button></view>
  18. <navigator url="../set/password"><view class="forget">忘记密码</view></navigator>
  19. <!-- <view class="flex other">
  20. <view class="fenge"></view>
  21. <view class="qita">其他方式登录</view>
  22. <view class="fenge"></view>
  23. </view> -->
  24. <!-- <!-- #ifndef APP-PLUS -->-->
  25. <!-- <view class="weixin" @click="wecahtLogin"><image src="/static/img/img05.png"></image></view>
  26. <view class="weixin_text" @click="wecahtLogin">微信登录</view> -->
  27. <!--<!-- #endif -->-->
  28. <!--<!-- #ifdef APP-PLUS --> -->
  29. <!-- <block v-if="!is_ios">
  30. <view class="weixin" @click="wecahtLogin"><image src="/static/img/img05.png" mode="scaleToFill"></image></view>
  31. <view class="weixin_text" @click="wecahtLogin">微信登录</view>
  32. </block> -->
  33. <!-- <block v-else>
  34. <view class="ios_login flex" @click="wecahtLogin('weixin')">
  35. <text class="iconfont iconweixin"></text>
  36. <text class="weixin_text">微信登录</text>
  37. </view>
  38. <view v-if='is_apple_login' class="ios_login flex" @click="wecahtLogin('apple')">
  39. <image class="loginIcon" src="/static/icon/appleIcon.png" mode=" scaleToFill"></image>
  40. <text class="weixin_text">通过Apple登录</text>
  41. </view>
  42. </block> -->
  43. <!-- <!-- #endif -->-->
  44. </view>
  45. </view>
  46. </template>
  47. <script>
  48. import { mapMutations } from 'vuex';
  49. import { login} from '@/api/login.js';
  50. import { getUserInfo} from '@/api/user.js';
  51. // #ifdef APP-PLUS
  52. // applelogin接口需要开发编写,基础项目中可能没有
  53. import { applelogin } from '@/api/set.js';
  54. // loginWx接口需要开发编写,基础项目中可能没有
  55. import { loginWx } from '@/api/login.js';
  56. // #endif
  57. // #ifdef H5
  58. import { loginWinxin } from '@/utils/wxAuthorized';
  59. // #endif
  60. export default {
  61. data() {
  62. return {
  63. username: '',
  64. passward: '',
  65. // #ifdef APP-PLUS
  66. is_ios: false ,//判断是否为ios手机
  67. is_apple_login:false,//是否有ios授权登录功能
  68. // #endif
  69. };
  70. },
  71. onLoad() {
  72. let obj = this;
  73. // #ifdef APP-PLUS
  74. let system = uni.getStorageSync('platform');
  75. // 判断是否为ios
  76. if (system == 'ios') {
  77. obj.is_ios = true;
  78. }
  79. uni.getSystemInfo({
  80. success(e) {
  81. if(+e.system.split('.')[0]>=13){
  82. obj.is_apple_login=true;
  83. }
  84. }
  85. })
  86. // #endif
  87. },
  88. methods: {
  89. ...mapMutations('user', ['setUserInfo', 'login']),
  90. // 微信登录
  91. wecahtLogin(type) {
  92. let obj = this;
  93. // #ifdef H5
  94. let weichatBrowser = uni.getStorageSync('weichatBrowser');
  95. if (weichatBrowser) {
  96. loginWinxin();
  97. }
  98. // #endif
  99. // #ifdef APP-PLUS
  100. uni.login({
  101. provider: type,
  102. success(e) {
  103. uni.getUserInfo({
  104. provider: type,
  105. success(es) {
  106. if(type==='weixin'){
  107. loginWx(es.userInfo)
  108. .then(e => {
  109. uni.setStorageSync('token', e.data.token);
  110. getUserInfo({}).then(e => {
  111. obj.login();
  112. // 保存返回用户数据
  113. obj.setUserInfo(e.data);
  114. if(!e.data.phone){
  115. uni.showModal({
  116. title: '提示',
  117. showCancel:false,
  118. content:'请绑定手机号',
  119. success() {
  120. uni.navigateTo({
  121. url:'/pages/set/phone'
  122. })
  123. }
  124. });
  125. }
  126. //成功跳转首页
  127. uni.switchTab({
  128. url: '/pages/index/index'
  129. });
  130. });
  131. })
  132. .catch(e => {
  133. console.log(e);
  134. uni.showModal({
  135. content: JSON.stringify(e),
  136. success() {},
  137. fail() {}
  138. });
  139. });
  140. }
  141. if(type==='apple'){
  142. console.log(es.userInfo);
  143. applelogin({
  144. account: es.userInfo.openId,
  145. })
  146. .then(function(e) {
  147. console.log(e,'token')
  148. uni.setStorageSync('token', e.data.token);
  149. getUserInfo({}).then(e => {
  150. obj.login();
  151. // 保存返回用户数据
  152. obj.setUserInfo(e.data);
  153. //成功跳转首页
  154. uni.switchTab({
  155. url: '/pages/index/index'
  156. });
  157. });
  158. })
  159. .catch(function(e) {
  160. console.log(e);
  161. });
  162. }
  163. },
  164. fail(es) {
  165. uni.showModal({
  166. content: JSON.stringify(es),
  167. success() {
  168. // obj.login();
  169. // // 保存返回用户数据
  170. // obj.setUserInfo(e.data);
  171. // //成功跳转首页
  172. // uni.switchTab({
  173. // url: '/pages/index/index'
  174. // });
  175. }
  176. });
  177. }
  178. });
  179. },
  180. fail(e) {
  181. uni.showModal({
  182. title: '提示',
  183. content: JSON.stringify(e),
  184. showCancel: false
  185. });
  186. }
  187. });
  188. // #endif
  189. },
  190. //登录
  191. async toLogin() {
  192. let obj = this;
  193. obj.logining = true;
  194. if (obj.username == '') {
  195. obj.$api.msg('请输入手机号');
  196. return;
  197. }
  198. if (obj.passward == '') {
  199. obj.$api.msg('请输入密码');
  200. return;
  201. }
  202. login({
  203. account: obj.username,
  204. password: obj.passward
  205. })
  206. .then(function(e) {
  207. uni.setStorageSync('token', e.data.token);
  208. obj.$store.commit('hasLogin', true);
  209. getUserInfo({}).then(e => {
  210. obj.login();
  211. // 保存返回用户数据
  212. obj.setUserInfo(e.data);
  213. let ur = uni.getStorageSync('present')|| '/pages/index/index';
  214. //成功跳转首页
  215. uni.switchTab({
  216. url: ur,
  217. fail(e) {
  218. uni.navigateTo({
  219. url: ur,
  220. fail(e) {
  221. uni.navigateTo({
  222. url: '/pages/index/index',
  223. });
  224. }
  225. });
  226. }
  227. });
  228. });
  229. })
  230. .catch(function(e) {
  231. console.log(e);
  232. });
  233. },
  234. //跳转注册页
  235. register() {
  236. uni.navigateTo({
  237. url: `/pages/public/register`
  238. });
  239. },
  240. // 后退
  241. navBack() {
  242. uni.navigateBack();
  243. }
  244. }
  245. };
  246. </script>
  247. <style lang="scss">
  248. /* #ifdef APP-PLUS */
  249. .ios_login {
  250. width: 260rpx;
  251. border-radius: 12rpx;
  252. justify-content: center;
  253. border: 1px solid #212121;
  254. margin: 24rpx auto;
  255. padding: 10rpx;
  256. background-color: #212121;
  257. color: #ffffff;
  258. .loginIcon {
  259. width: 50rpx;
  260. height: 50rpx;
  261. }
  262. .weixin_text {
  263. line-height: 1;
  264. margin-left: 20rpx;
  265. color: #ffffff !important;
  266. }
  267. }
  268. /* #endif */
  269. .ios_login {
  270. width: 350rpx;
  271. border-radius: 12rpx;
  272. justify-content: center;
  273. border: 1px solid #212121;
  274. margin: 24rpx auto;
  275. padding: 15rpx;
  276. background-color: #212121;
  277. color: #ffffff;
  278. font-size: 32rpx;
  279. .loginIcon {
  280. font-size: 35rpx;
  281. width: 35rpx;
  282. height: 35rpx;
  283. }
  284. .weixin_text {
  285. line-height: 1;
  286. margin-left: 20rpx;
  287. color: #ffffff !important;
  288. }
  289. }
  290. page {
  291. height: 100%;
  292. }
  293. .container {
  294. width: 100%;
  295. height: 100%;
  296. background-size: 100%;
  297. }
  298. .container_text {
  299. width: 100%;
  300. height: 500rpx;
  301. top: 0rpx;
  302. .banner-img {
  303. width: 100%;
  304. height: 100%;
  305. }
  306. }
  307. .login_text {
  308. margin: auto 30rpx;
  309. position: relative;
  310. padding: 100rpx 102rpx;
  311. background-color: #ffffff;
  312. margin-top: -180rpx;
  313. border-radius: 20rpx;
  314. .login_input {
  315. border-bottom: 1px solid #f0f0f0;
  316. margin-bottom: 65rpx;
  317. .login_img image {
  318. height: 35rpx;
  319. width: 29rpx;
  320. margin-right: 20rpx;
  321. }
  322. .uni-input {
  323. text-align: left;
  324. width: 470rpx;
  325. font-size: 28rpx !important;
  326. }
  327. .login_name {
  328. color: #333333;
  329. }
  330. }
  331. .other {
  332. margin-top: 60rpx;
  333. .fenge {
  334. width: 30%;
  335. height: 2rpx;
  336. background-color: #eeeeee;
  337. }
  338. .qita {
  339. font-size: 28rpx;
  340. color: #999999;
  341. }
  342. }
  343. .weixin {
  344. width: 75rpx;
  345. height: 75rpx;
  346. margin: 25rpx auto;
  347. }
  348. .weixin image {
  349. width: 100%;
  350. height: 100%;
  351. }
  352. .weixin_text {
  353. text-align: center;
  354. font-size: 28rpx;
  355. color: #999999;
  356. }
  357. .forget {
  358. font-size: 28rpx;
  359. width: 100%;
  360. text-align: right;
  361. color: #999999;
  362. }
  363. .uni-button-green {
  364. color: #ffffff;
  365. background-color: #5dbc7c;
  366. margin: 40rpx 10rpx;
  367. border-radius: 50rpx;
  368. }
  369. .uni-button-green-plain {
  370. border: 1px solid #5dbc7c;
  371. margin: 40rpx 10rpx;
  372. border-radius: 50rpx;
  373. color: #5dbc7c;
  374. background-color: #ffffff;
  375. }
  376. .uni-button {
  377. height: 85rpx;
  378. line-height: 85rpx;
  379. }
  380. }
  381. .loginTitle {
  382. position: absolute;
  383. top: 250rpx;
  384. width: 100%;
  385. text-align: center;
  386. color: white;
  387. font-size: 40rpx;
  388. }
  389. </style>