index.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <template>
  2. <!-- 背景图片 -->
  3. <!-- <view class="logo_bg">
  4. <image src="../../static/img/logo_bg.png"></image>
  5. </view> -->
  6. <view class="login-view">
  7. <image src="../../static/img/logo2.png" mode="widthFix"></image>
  8. <view class="login-text">测绘地理信息学院仪享云</view>
  9. <view class="login-text1">
  10. </view>
  11. </view>
  12. <view class="logo_sru">
  13. <view>
  14. <uni-forms ref="form" :modelValue="formData">
  15. <view class="denru">
  16. <uni-easyinput prefixIcon="person" v-model="formData.account" type="text" placeholder="请输入账号" />
  17. </view>
  18. <view class="denru">
  19. <uni-easyinput prefixIcon="locked" v-model="formData.password" type="password" placeholder="请输入密码" />
  20. </view>
  21. <button class="button" @click="submit" style="background-color:#2d8cf0 ; color: #fff; " >密码登录</button>
  22. </uni-forms>
  23. </view>
  24. <view class="" style="margin-top: 30rpx;">
  25. <button @click="login" type="primary">微信一键登录</button>
  26. </view>
  27. </view>
  28. <view class="student_login">
  29. <button v-if="studentData.errcode!=0" open-type="getPhoneNumber" @getphonenumber="studentLogin" size="mini" style="background-color:#2d8cf0 ; color: #fff;height: 70rpx; line-height: 70rpx;">学生登录</button>
  30. <button v-if="studentData.errcode==0" @click="studentLogin2" size="mini" style="background-color:#2d8cf0 ; color: #fff;height: 70rpx; line-height: 70rpx;">学生登录</button>
  31. </view>
  32. </template>
  33. <script setup lang="ts">
  34. import {requestApi } from '@/api/request.js'
  35. import { reactive, ref } from 'vue'
  36. // onShow是uni-app中的一个全局生命周期函数,当页面从后台进入前台显示时会被触发。
  37. import {onShow,onUnload} from '@dcloudio/uni-app'
  38. let studentData=ref(null)
  39. studentData.value = wx.getStorageSync('studentData')
  40. // console.log(studentData.value)
  41. // if (studentData.value != null){
  42. // console.log(studentData)
  43. // }else if(studentData.value == null){
  44. // 进学生端,获取手机号
  45. async function studentLogin(e) {
  46. console.log(e)
  47. var detail = e.detail
  48. if (detail.errMsg == "getPhoneNumber:ok") {
  49. console.log("用户同意授权")
  50. var code = detail.code
  51. // console.log(code)
  52. const studentData= await requestApi(
  53. 'api/xmwechat.Miniprogram/getPhoneNumber?server=1',
  54. {
  55. code:code,
  56. },
  57. 'POST'
  58. )
  59. console.log(studentData)
  60. if(studentData.code==1){
  61. wx.showLoading({
  62. title: '加载中',
  63. })
  64. wx.setStorageSync('studentData',studentData.data)
  65. setTimeout(function () {
  66. wx.hideLoading()
  67. }, 1000)
  68. wx.reLaunch({
  69. url: '/pages/student/index'
  70. })
  71. }
  72. }
  73. }
  74. // }
  75. // 缓存里已经有了手机号学生登录
  76. function studentLogin2() {
  77. wx.reLaunch({
  78. url: '/pages/student/index'
  79. })
  80. }
  81. onShow(async () => {
  82. //
  83. const logo =wx.getStorageSync('userInfos')
  84. if(logo.userInfo.group==1 || logo.userInfo.group==2 || logo.userInfo.group==3){
  85. wx.switchTab({
  86. url:'/pages/index/index'
  87. })
  88. }else if(logo.userInfo.group==4){
  89. wx.reLaunch({
  90. url: '/pages/teacher/navigation'
  91. })
  92. }
  93. })
  94. // 微信一键登入
  95. // 定义了一个异步函数,表示该函数内部可以使用await关键字来等待异步操作完成。
  96. async function login(){
  97. wx.showLoading({title:'登录中'})
  98. // 在函数内调用微信小程序提供的wx.login方法发起登录请求。当登录成功时(即success回调触发),执行匿名箭头函数:
  99. wx.login({
  100. success:async(res)=>{
  101. console.log(res)
  102. // 使用await requestApi(...)调用封装好的API请求函数,向后端服务器发送POST请求到'/wxLogin'接口,
  103. // 并携带appid、secret(这两个参数是与微信小程序相关的应用ID和密钥)以及从微信登录接口获取到的code。
  104. // await requestApi(...)会等待后端接口返回数据,然后将结果赋值给userData。
  105. const userData= await requestApi(
  106. 'admin/Miniprogram/miniprogramLogin?server=1',
  107. {
  108. code:res.code,
  109. },
  110. 'POST'
  111. )
  112. const logo = userData.data
  113. // console.log(userData)
  114. // 最后,使用微信小程序提供的wx.setStorageSync('自定义名称', userData.data)将后端返回的用户详细信息(通常包含在userData.data中)保存在本地存储中,
  115. // 以便后续页面或功能可以读取和使用这些信息。
  116. // try{wx.setStorageSync('userInfo',userData)}
  117. // catch{
  118. // console.log(userData)
  119. // }
  120. console.log('userData',userData)
  121. wx.setStorageSync('userInfos',userData.data)
  122. // 关掉 wx.showLoading({title:'登录中'})
  123. wx.hideLoading()
  124. if(userData.data == null){
  125. wx.showModal({
  126. title: '提示',
  127. content: '您尚未绑定微信,请先账号密码登录',
  128. showCancel:false,
  129. success () {
  130. }
  131. })
  132. }else{
  133. wx.showLoading({title:'登录中'})
  134. if(logo.userInfo.group==1 || logo.userInfo.group==2 || logo.userInfo.group==3){
  135. wx.switchTab({
  136. url:'/pages/index/index'
  137. })
  138. }else if(logo.userInfo.group==4){
  139. wx.reLaunch({
  140. url: '/pages/teacher/navigation'
  141. })
  142. }
  143. }
  144. setTimeout(function () {
  145. wx.hideLoading()
  146. }, 2000)
  147. }
  148. })
  149. }
  150. // 账号密码登入
  151. const formData= ref({
  152. account:'',
  153. password:''
  154. })
  155. async function submit(){
  156. // console.log('success',formData)
  157. // console.log(formData.value.account)
  158. wx.showLoading({title:'登录中'})
  159. const denru= await requestApi(
  160. 'admin/Index/mplogin?server=1',
  161. {
  162. // 数据
  163. username:formData.value.account,
  164. password:formData.value.password
  165. },
  166. 'POST'
  167. )
  168. console.log(denru)
  169. wx.showLoading({title:denru.msg})
  170. // 关掉 wx.showLoading({title:})
  171. setTimeout(function () {
  172. wx.hideLoading()
  173. }, 2000)
  174. const logo = denru.data
  175. // 判断微信是否绑定了
  176. if(logo.userInfo.wxbind != null){
  177. if(logo.userInfo.group==1 || logo.userInfo.group==2 || logo.userInfo.group==3){
  178. wx.switchTab({
  179. url:'/pages/index/index'
  180. })
  181. }else if(logo.userInfo.group==4){
  182. wx.reLaunch({
  183. url: '/pages/teacher/navigation'
  184. })
  185. }
  186. // 存储到缓存里
  187. wx.setStorageSync('userInfos',denru.data)
  188. }
  189. else{
  190. wx.hideLoading()
  191. wx.showModal({
  192. title: '微信绑定',
  193. content: '请绑定您的微信账号,以便微信登录',
  194. showCancel:false,
  195. confirmText:'绑定',
  196. success () {
  197. wx.login({
  198. success:async(res)=>{
  199. console.log('用户点击确定')
  200. console.log(logo.userInfo.id)
  201. wx.showLoading({title:'绑定中'})
  202. const banden= await requestApi(
  203. 'admin/Miniprogram/miniprogramBind?server=1',
  204. {
  205. // 数据
  206. uid:logo.userInfo.id,
  207. code:res.code,
  208. },
  209. 'POST'
  210. )
  211. wx.hideLoading()
  212. // console.log(banden)
  213. wx.setStorageSync('userInfos',denru.data)
  214. if(logo.userInfo.group==1 || logo.userInfo.group==2 || logo.userInfo.group==3){
  215. wx.switchTab({
  216. url:'/pages/index/index'
  217. })
  218. }else if(logo.userInfo.group==4){
  219. wx.reLaunch({
  220. url: '/pages/teacher/navigation'
  221. })
  222. }
  223. }
  224. })}
  225. })
  226. }
  227. // console.log(logo.userInfo.group)
  228. }
  229. </script >
  230. <style scoped>
  231. .student_login{
  232. position: absolute;
  233. bottom: 30rpx;
  234. left: 30rpx;
  235. }
  236. .logo_bg{
  237. position: absolute;
  238. top: 0;
  239. width: 100%;
  240. height: 100%;
  241. z-index: -1;
  242. }
  243. .logo_bg image{
  244. width: 100%;
  245. height: 100%;
  246. }
  247. .login-text1{
  248. color: #808695;
  249. }
  250. .login-view{
  251. display: flex;
  252. flex-direction: column;
  253. align-items: center;
  254. padding: 0 80rpx;
  255. margin-top: 200rpx;
  256. font-size: 35rpx;
  257. }
  258. .login-view image{
  259. width: 200rpx;
  260. }
  261. .denru{
  262. margin-bottom: 30rpx;
  263. }
  264. .login-view button{
  265. width: 100%;
  266. margin-top: 600rpx;
  267. background-color:#2d8cf0;
  268. color: #fff;
  269. border:#2d8cf0;
  270. }
  271. .login-text{
  272. margin-top: 20rpx;
  273. font-size: 50rpx;
  274. }
  275. .logo_sru{
  276. width: 90%;
  277. margin: 0 auto;
  278. margin-top: 50rpx;
  279. }
  280. </style>