index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <template>
  2. <view class="ezy-login-page">
  3. <icon class="login-img"></icon>
  4. <!-- 手机号 -->
  5. <view class="login-body-box">
  6. <view class="phone-input-box">
  7. <view class="phone-prefix">+86</view>
  8. <input class="phone-input" type="text" v-model="indexData.phoneNumber" placeholder="请输入手机号"
  9. maxlength="11" @input="clearTelInput" />
  10. <view class="close-btn" v-if="indexData.clearTelIcon" @click="clearTel"></view>
  11. </view>
  12. <!-- 协议勾选框和按钮 -->
  13. <view class="agreement-checkbox-box">
  14. <checkbox-group @change="handleAgreementChange">
  15. <checkbox class="agreement-checkbox-input" color="#FFFFFF" value="agree"
  16. :checked="indexData.isAgreed" />
  17. </checkbox-group>
  18. <view class="agreement-text-box">
  19. 我已阅读并同意<view class="agreement-text" @click="agreeBtn('yhxy')">《用户协议》</view>和<view
  20. @click="agreeBtn('ystk')" class="agreement-text">《隐私政策》</view>
  21. </view>
  22. </view>
  23. <view class="login-btn" @click="getYzmBtn" :class="indexData.telStatus">发送验证码</view>
  24. <view class="bottom-btn-box">
  25. <view class="bottom-tip">选择第三方登录</view>
  26. <view class="btn-box">
  27. <view v-if="wxFlag" class="wx-btn" @click="wxLoginClick">
  28. <icon></icon>
  29. <view>微信登录</view>
  30. </view>
  31. <!-- <view class="yk-btn" @click="ykBtn">
  32. <icon></icon>
  33. <view>游客登录</view>
  34. </view> -->
  35. <view class="apple-btn" v-if="showAppleLogin" @click="appleLoginClick">
  36. <icon></icon>
  37. <view>苹果登录</view>
  38. </view>
  39. </view>
  40. </view>
  41. </view>
  42. <agree-content-dialog ref="agreeContentDialogRef" :agreeType="agreeType"></agree-content-dialog>
  43. <agree-dialog ref="agreeDialogRef" @confirm-btn="confirmBtn"></agree-dialog>
  44. <agree-dialog ref="agreeYkDialog" @confirm-btn="ykConfirmBtn"></agree-dialog>
  45. <agree-dialog ref="agreeWxDialog" @confirm-btn="wxConfirmBtn"></agree-dialog>
  46. <tel-dialog ref="telDialogRef" @telClose="telClose" @bindBtn="bindBtn" v-if="telDialogFlag"></tel-dialog>
  47. <tip-dialog ref="tipDialogRef" :title="tipTitle" :content="tipContent"></tip-dialog>
  48. </view>
  49. </template>
  50. <script setup>
  51. import {
  52. ref,
  53. nextTick,
  54. reactive
  55. } from "vue"
  56. import {
  57. onLoad,
  58. onReady,
  59. onBackPress
  60. } from '@dcloudio/uni-app';
  61. import agreeContentDialog from './agreeContentDialog.vue';
  62. import agreeDialog from './agreeDialog.vue'
  63. import {
  64. wxLogin,
  65. getVersion
  66. } from "@/api/login.js"
  67. import {
  68. toast,
  69. getUserIdentity
  70. } from "@/utils/common";
  71. import telDialog from './telDialog.vue'
  72. import cacheManager from "@/utils/cacheManager.js";
  73. import tipDialog from '@/components/dialog/tipDialog.vue';
  74. import config from '../../config.js'
  75. import {
  76. error
  77. } from "uview-plus";
  78. import {useExitApp} from "@/utils/useExitApp.js"
  79. let indexData = reactive({
  80. phoneNumber: null,
  81. clearTelIcon: false,
  82. telStatus: 'login-btn-disabled',
  83. isAgreed: false,
  84. sliderObj: {},
  85. })
  86. const agreeContentDialogRef = ref(null);
  87. const agreeType = ref(null);
  88. const agreeDialogRef = ref(null);
  89. const agreeYkDialog = ref(null);
  90. const agreeWxDialog = ref(null);
  91. const telDialogRef = ref(null);
  92. let telDialogFlag = ref(false);
  93. let showAppleLogin = ref(false);
  94. let wxFlag = ref(true);
  95. const version = config.appInfo.version;
  96. const tipDialogRef = ref(null);
  97. const tipTitle = '升级提醒';
  98. const tipContent = '您的APP不是最新版本,部分功能不能使用,请升级至最新版本!';
  99. // 初始化退出逻辑
  100. useExitApp();
  101. onLoad((options) => {
  102. showAppleLogin.value = isIOS13OrAbove();
  103. getLoginInit()
  104. if (plus.runtime.isApplicationExist({
  105. pname: 'com.tencent.mm',
  106. action: 'weixin://'
  107. })) {
  108. console.log('111', "已安装微信");
  109. wxFlag.value = true
  110. } else {
  111. wxFlag.value = false
  112. }
  113. console.log('showAppleLogin.value', showAppleLogin.value);
  114. })
  115. function getLoginInit() {
  116. getVersion({}).then(res => {
  117. if (version != res.data.version) {
  118. tipDialogRef.value.handleShow();
  119. }
  120. })
  121. }
  122. const getYzmBtn = () => {
  123. if (indexData.telStatus === 'login-btn-normal') {
  124. if (indexData.isAgreed === true) {
  125. uni.redirectTo({
  126. url: `/pages/login/login?telNum=${indexData.phoneNumber}`
  127. })
  128. } else {
  129. agreeDialogRef.value.handleShow();
  130. }
  131. }
  132. }
  133. const isIOS13OrAbove = () => {
  134. const systemInfo = uni.getSystemInfoSync();
  135. if (systemInfo.platform == 'ios') {
  136. const version = systemInfo.system.split(' ')[1].split('.');
  137. const majorVersion = parseInt(version[0], 10);
  138. return majorVersion >= 13;
  139. }
  140. return false;
  141. }
  142. // 用户协议同意
  143. const confirmBtn = () => {
  144. indexData.isAgreed = true;
  145. getYzmBtn();
  146. }
  147. const telClose = () => {
  148. telDialogFlag.value = false;
  149. }
  150. const bindBtn = (res) => {
  151. telDialogFlag.value = false;
  152. if (res.cardId == 0) {
  153. uni.redirectTo({
  154. url: `/pages/selectGradesTerms/index`
  155. })
  156. } else {
  157. uni.redirectTo({
  158. url: `/pages/study/index`
  159. })
  160. }
  161. }
  162. const ykConfirmBtn = () => {
  163. uni.redirectTo({
  164. url: `/pages/selectGradesTerms/index`
  165. })
  166. }
  167. const wxConfirmBtn = () => {
  168. wxLoginFun();
  169. }
  170. // 手机号校验规则
  171. const validatePhoneNumber = (value) => {
  172. const phoneRegex = /^1[3-9]\d{9}$/;
  173. if (phoneRegex.test(value)) {
  174. indexData.telStatus = 'login-btn-normal';
  175. } else {
  176. indexData.telStatus = 'login-btn-disabled';
  177. }
  178. }
  179. const clearTelInput = (event) => {
  180. if (event.detail.value.length > 0) {
  181. indexData.clearTelIcon = true;
  182. validatePhoneNumber(event.detail.value);
  183. } else {
  184. indexData.clearTelIcon = false;
  185. }
  186. }
  187. const clearTel = () => {
  188. indexData.phoneNumber = '';
  189. indexData.telStatus = 'login-btn-disabled';
  190. indexData.clearTelIcon = false;
  191. }
  192. const agreeBtn = (data) => {
  193. if (data === 'yhxy') {
  194. agreeType.value = 'yhxy'
  195. } else {
  196. agreeType.value = 'ystk'
  197. }
  198. agreeContentDialogRef.value.handleShow();
  199. };
  200. const handleAgreementChange = (event) => {
  201. if (event.detail.value[0] === 'agree') {
  202. indexData.isAgreed = true;
  203. } else {
  204. indexData.isAgreed = false;
  205. }
  206. }
  207. // 游客登录
  208. const ykBtn = () => {
  209. if (indexData.isAgreed === true) {
  210. uni.redirectTo({
  211. url: `/pages/selectGradesTerms/index`
  212. })
  213. } else {
  214. agreeYkDialog.value.handleShow();
  215. }
  216. }
  217. const appleLoginClick = () => {
  218. uni.login({
  219. provider: 'apple',
  220. success: function(loginRes) {
  221. console.log('loginRes', loginRes);
  222. let req = {
  223. "apple": true,
  224. "code": loginRes.appleInfo.user
  225. }
  226. console.log(req);
  227. console.log(req);
  228. wxLogin(req).then(res => {
  229. console.log(res);
  230. if (!res.data.bind) {
  231. console.log('未绑定');
  232. cacheManager.set('wxLogin', {
  233. bind: res.data.bing
  234. })
  235. telDialogFlag.value = true;
  236. nextTick(() => {
  237. telDialogRef.value.getOpenId(res.data.openId);
  238. })
  239. } else {
  240. uni.showLoading({
  241. title: '登录中'
  242. });
  243. setTimeout(() => {
  244. cacheManager.set('auth', res.data.loginVo)
  245. if (res.data.loginVo.subjectId == 0 && res.data.loginVo
  246. .levelId == 0) {
  247. uni.redirectTo({
  248. url: `/pages/selectGradesTerms/index`
  249. })
  250. uni.hideLoading();
  251. } else {
  252. uni.redirectTo({
  253. url: `/pages/study/index`
  254. })
  255. uni.hideLoading();
  256. }
  257. }, 1000)
  258. }
  259. }).catch((error) => {
  260. console.log(error);
  261. })
  262. },
  263. fail: function(err) {
  264. console.log('err.code', err.code);
  265. // 登录授权失败
  266. // err.code错误码参考`授权失败错误码(code)说明`
  267. }
  268. });
  269. }
  270. // 游客登录
  271. const wxLoginClick = () => {
  272. if (indexData.isAgreed === true) {
  273. wxLoginFun();
  274. } else {
  275. agreeWxDialog.value.handleShow();
  276. }
  277. }
  278. const wxLoginFun = () => {
  279. //获取服务商信息判断手机端是否安装了app
  280. // uni.getProvider({
  281. // service: 'oauth',// oauth 代表授权登录
  282. // success: function (res) {
  283. // // 登录
  284. // uni.login({
  285. // // 表示授权方式 如果不设置则弹出登录列表选择界面
  286. // provider: 'weixin',
  287. // "onlyAuthorize": true, // 微信登录仅请求授权认证
  288. // success: function (loginRes) {
  289. // console.log('loginRes',loginRes);
  290. // }
  291. // });
  292. // }
  293. // });
  294. if (plus.runtime.isApplicationExist({
  295. pname: 'com.tencent.mm',
  296. action: 'weixin://'
  297. })) {
  298. console.log('111', "已安装微信");
  299. uni.login({
  300. "provider": "weixin",
  301. "onlyAuthorize": true, // 微信登录仅请求授权认证
  302. success: function(event) {
  303. console.log(event);
  304. const {
  305. code
  306. } = event
  307. //客户端成功获取授权临时票据(code),向业务服务器发起登录请求。
  308. let req = {
  309. "apple": false,
  310. "code": code
  311. }
  312. console.log(req);
  313. wxLogin(req).then(res => {
  314. console.log(res);
  315. if (!res.data.bind) {
  316. console.log('未绑定');
  317. cacheManager.set('wxLogin', {
  318. bind: res.data.bing
  319. })
  320. telDialogFlag.value = true;
  321. nextTick(() => {
  322. telDialogRef.value.getOpenId(res.data.openId);
  323. })
  324. } else {
  325. uni.showLoading({
  326. title: '登录中'
  327. });
  328. setTimeout(() => {
  329. cacheManager.set('auth', res.data.loginVo)
  330. if (res.data.loginVo.subjectId == 0 && res.data.loginVo
  331. .levelId == 0) {
  332. uni.redirectTo({
  333. url: `/pages/selectGradesTerms/index`
  334. })
  335. uni.hideLoading();
  336. } else {
  337. uni.redirectTo({
  338. url: `/pages/study/index`
  339. })
  340. uni.hideLoading();
  341. }
  342. }, 1000)
  343. }
  344. }).catch((error) => {
  345. console.log(error);
  346. })
  347. },
  348. fail: function(err) {
  349. console.log(err);
  350. // 登录授权失败
  351. // err.code是错误码
  352. }
  353. })
  354. } else {
  355. toast("请重启应用重试")
  356. return false
  357. }
  358. }
  359. </script>