index.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <template>
  2. <view class="login-content">
  3. <view class="login-content-info">
  4. <view class="login-bgm">
  5. <image class="login-logo" :src="logoUrl" mode="scaleToFill"></image>
  6. <text>{{ title }}</text>
  7. </view>
  8. <view class="login-info">
  9. <view class="login-info-box">
  10. <text class="login-info-title">账号密码登陆</text>
  11. <view class="login-info-form">
  12. <u--form
  13. labelWidth="0"
  14. :borderBottom="false"
  15. :model="form"
  16. :rules="rules"
  17. ref="uForm">
  18. <u-form-item prop="name">
  19. <u--input
  20. v-model="form.name"
  21. placeholder="请输入账号"
  22. border="surround"
  23. shape="circle"
  24. clearable
  25. prefixIcon="account-fill"
  26. prefixIconStyle="font-size: 22px;color: #909399"
  27. ></u--input>
  28. </u-form-item>
  29. <u-form-item prop="password">
  30. <u--input
  31. v-model="form.password"
  32. placeholder="请输入密码"
  33. border="surround"
  34. shape="circle"
  35. type="password"
  36. clearable
  37. prefixIcon="lock-fill"
  38. prefixIconStyle="font-size: 22px;color: #909399"
  39. ></u--input>
  40. </u-form-item>
  41. </u--form>
  42. </view>
  43. <view class="login-info-submit">
  44. <u-button
  45. class="login-info-submit-but"
  46. @click="submit()"
  47. :loading="loading"
  48. loadingText="登录中..."
  49. >登录</u-button>
  50. </view>
  51. <view class="login-info-tip">暂不支持自行注册</view>
  52. </view>
  53. </view>
  54. </view>
  55. </view>
  56. </template>
  57. <script>
  58. export default {
  59. data() {
  60. return {
  61. title: '《伟大转折》剧目全民分享系统',
  62. logoUrl: this.$commonConfig.staticUrl + "login/logo.png",
  63. loading: false,
  64. code: null,
  65. form: {
  66. name: '',
  67. password: '',
  68. },
  69. rules: {
  70. 'name': {
  71. type: 'string',
  72. required: true,
  73. message: '请填写账号',
  74. trigger: ['blur', 'change']
  75. },
  76. 'password': {
  77. type: 'string',
  78. required: true,
  79. message: '请填写密码',
  80. trigger: ['blur', 'change']
  81. },
  82. },
  83. }
  84. },
  85. onReady() {
  86. //如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则。
  87. this.$refs.uForm.setRules(this.rules)
  88. },
  89. onLoad(e) {
  90. // #ifdef H5
  91. if(this.vuex_user_info && this.vuex_user_info.accessToken){
  92. this.personsLoginCheck()
  93. }else {
  94. if(!e.code) { // 微信第三方登录失败
  95. this.redirectToAuth()
  96. }else {
  97. this.code = e.code
  98. }
  99. }
  100. // #endif
  101. },
  102. onShow() {
  103. },
  104. methods: {
  105. /**
  106. *
  107. * 提交登录
  108. *
  109. */
  110. submit() {
  111. this.loading = true
  112. this.$refs.uForm.validate().then(res => {
  113. this.login()
  114. }).catch(errors => {
  115. this.$u.toast('校验失败')
  116. this.loading = false
  117. })
  118. },
  119. /**
  120. * 登录
  121. */
  122. async login(){
  123. try{
  124. let openid = ''
  125. // #ifdef H5
  126. const node_dev = process.env.H_NODE_ENV;
  127. if(node_dev){
  128. let wxinfo = await this.$u.api.wxinfoH5({code:this.code});
  129. openid = wxinfo.data.openid;
  130. }
  131. // #endif
  132. let res = await this.$u.api.login({
  133. "mobile": this.form.name,
  134. "password": this.form.password,
  135. "h5OpenId": openid
  136. })
  137. if(res && res.code ===200) {
  138. this.loading = false
  139. if(res.data && res.data.accessToken){
  140. this.$u.vuex('vuex_user_info', res.data);
  141. let backUrl = uni.getStorageSync('backUrl')
  142. console.log("backUrl===",backUrl)
  143. if(backUrl && backUrl != '/pages/login/index') {
  144. uni.reLaunch({
  145. url: '/'+backUrl
  146. })
  147. }else {
  148. uni.reLaunch({
  149. url: '/pages/main/index'
  150. })
  151. }
  152. } else {
  153. // 清空用户数据缓存
  154. uni.setStorageSync('lifeData',{}); // 清除缓存
  155. this.$u.vuex('vuex_user_info', {});
  156. this.redirectToAuth()
  157. }
  158. } else {
  159. // 清空用户数据缓存
  160. uni.setStorageSync('lifeData',{}); // 清除缓存
  161. this.$u.vuex('vuex_user_info', {});
  162. this.redirectToAuth()
  163. }
  164. }catch(e){
  165. //TODO handle the exception
  166. console.error("e===",e)
  167. this.loading = false
  168. uni.setStorageSync('lifeData',{}); // 清除缓存
  169. this.$u.vuex('vuex_user_info', {});
  170. this.redirectToAuth()
  171. }
  172. },
  173. /** 公众号 微信授权登录 */
  174. redirectToAuth() {
  175. const node_dev = process.env.H_NODE_ENV;
  176. if(node_dev){
  177. const authUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.$commonConfig.appid}&redirect_uri=${this.$commonConfig.redirectUri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`;
  178. window.location.href = authUrl;
  179. }
  180. },
  181. /** 检测是否已登录 */
  182. async personsLoginCheck() {
  183. try{
  184. let res = await this.$u.api.login({
  185. "noSign": 1,
  186. })
  187. if(res && res.code ===200) {
  188. if(res.data && res.data.accessToken){
  189. this.$u.vuex('vuex_user_info', res.data);
  190. let backUrl = uni.getStorageSync('backUrl')
  191. if(backUrl) {
  192. uni.reLaunch({
  193. url: backUrl
  194. })
  195. }else {
  196. uni.reLaunch({
  197. url: '/pages/main/index'
  198. })
  199. }
  200. } else {
  201. // 清空用户数据缓存
  202. uni.setStorageSync('lifeData',{}); // 清除缓存
  203. this.$u.vuex('vuex_user_info', {});
  204. this.redirectToAuth()
  205. }
  206. } else {
  207. // 清空用户数据缓存
  208. uni.setStorageSync('lifeData',{}); // 清除缓存
  209. this.$u.vuex('vuex_user_info', {});
  210. this.redirectToAuth()
  211. }
  212. }catch(e){
  213. //TODO handle the exception
  214. uni.setStorageSync('lifeData',{}); // 清除缓存
  215. this.$u.vuex('vuex_user_info', {});
  216. this.redirectToAuth()
  217. }
  218. }
  219. }
  220. }
  221. </script>
  222. <style lang="scss" scoped>
  223. .login-content {
  224. width: 100%;
  225. height: 100%;
  226. position: relative;
  227. box-sizing: border-box;
  228. --bgm-height: 630rpx;
  229. }
  230. /** 背景 */
  231. .login-content-box {
  232. width: 100%;
  233. height: 100%;
  234. box-sizing: border-box;
  235. }
  236. .login-bgm {
  237. width: 100%;
  238. height: 630rpx;
  239. background-image: url("#{$image-beas-url}login/bgm.png");
  240. background-size: 100% auto;
  241. background-repeat: no-repeat;
  242. box-sizing: border-box;
  243. display: flex;
  244. flex-direction: column;
  245. justify-content: center;
  246. align-items: center;
  247. .login-logo {
  248. width: 240rpx;
  249. height: 172rpx;
  250. padding: 50rpx 0;
  251. }
  252. >text {
  253. font-size: 32rpx;
  254. font-family: SourceHanSansCN, SourceHanSansCN;
  255. font-weight: bold;
  256. color: #FFFFFF;
  257. }
  258. }
  259. .login-info {
  260. width: 100%;
  261. height: calc(100% - var(--bgm-height) + 40rpx);
  262. background-color: #fff;
  263. border-radius: 28rpx 28rpx 0rpx 0rpx;
  264. position: absolute;
  265. bottom: 0;
  266. left: 0;
  267. z-index: 22;
  268. box-sizing: border-box;
  269. padding: 80rpx;
  270. .login-info-box {
  271. width: 100%;
  272. height: 100%;
  273. display: flex;
  274. flex-direction: column;
  275. box-sizing: border-box;
  276. .login-info-title {
  277. font-size: 32rpx;
  278. font-family: SourceHanSansCN, SourceHanSansCN;
  279. font-weight: bold;
  280. color: #2D2D2D;
  281. padding-bottom: 40rpx;
  282. }
  283. .login-info-form {
  284. }
  285. .login-info-submit {
  286. width: 100%;
  287. height: 80rpx;
  288. padding: 40rpx 0 20rpx;
  289. flex-shrink: 0;
  290. .login-info-submit-but {
  291. display: flex;
  292. justify-content: center;
  293. align-items: center;
  294. border-radius: 40rpx;
  295. width: 100% !important;
  296. height: 100% !important;
  297. background-color: var(--gd-bgm-color);
  298. color: #fff;
  299. }
  300. }
  301. .login-info-tip {
  302. width: 100%;
  303. text-align: center;
  304. font-size: 24rpx;
  305. font-family: SourceHanSansCN, SourceHanSansCN;
  306. font-weight: 400;
  307. color: #C2C2C2;
  308. }
  309. }
  310. }
  311. /** tabar */
  312. .main-tabar {
  313. width: 80vw;
  314. height: 100rpx;
  315. position: fixed;
  316. bottom: 20rpx;
  317. left: 50%;
  318. transform: translateX(-50%);
  319. box-sizing: border-box;
  320. .main-tabar-info {
  321. width: 100%;
  322. height: 100%;
  323. display: flex;
  324. justify-content: space-between;
  325. box-shadow: rgba(0, 0, 0, 0.4) 0px 2px 6px 0px;
  326. border-radius: 40rpx;
  327. .main-tabar-item {
  328. flex: 1;
  329. display: flex;
  330. align-items: center;
  331. flex-direction: column;
  332. justify-content: center;
  333. image {
  334. width: 50rpx;
  335. height: 50rpx;
  336. }
  337. text {
  338. font-size: 24rpx;
  339. }
  340. }
  341. }
  342. }
  343. </style>