index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. } from '@dcloudio/uni-app';
  60. import agreeContentDialog from './agreeContentDialog.vue';
  61. import agreeDialog from './agreeDialog.vue'
  62. import {
  63. wxLogin,
  64. getVersion
  65. } from "@/api/login.js"
  66. import {
  67. toast,
  68. getUserIdentity
  69. } from "@/utils/common";
  70. import telDialog from './telDialog.vue'
  71. import cacheManager from "@/utils/cacheManager.js";
  72. import {
  73. onShow,
  74. onUnload,
  75. } from '@dcloudio/uni-app';
  76. import {
  77. useIsCanBack
  78. } from "@/store/isCanBack.js"
  79. import tipDialog from '@/components/dialog/tipDialog.vue';
  80. import config from '../../config.js'
  81. import {
  82. error
  83. } from "uview-plus";
  84. let indexData = reactive({
  85. phoneNumber: null,
  86. clearTelIcon: false,
  87. telStatus: 'login-btn-disabled',
  88. isAgreed: false,
  89. sliderObj: {},
  90. })
  91. const agreeContentDialogRef = ref(null);
  92. const agreeType = ref(null);
  93. const agreeDialogRef = ref(null);
  94. const agreeYkDialog = ref(null);
  95. const agreeWxDialog = ref(null);
  96. const telDialogRef = ref(null);
  97. let telDialogFlag = ref(false);
  98. let showAppleLogin = ref(false);
  99. let wxFlag = ref(true);
  100. const version = config.appInfo.version;
  101. const tipDialogRef = ref(null);
  102. const tipTitle = '升级提醒';
  103. const tipContent = '您的APP不是最新版本,部分功能不能使用,请升级至最新版本!';
  104. const store = useIsCanBack();
  105. onUnload(() => {
  106. console.log('unload')
  107. store.setIsCanBack(false)
  108. })
  109. onShow(() => {
  110. setTimeout(() => {
  111. console.log('onShow')
  112. store.setIsCanBack(true)
  113. }, 500)
  114. })
  115. onLoad((options) => {
  116. showAppleLogin.value = isIOS13OrAbove();
  117. getLoginInit()
  118. if (plus.runtime.isApplicationExist({
  119. pname: 'com.tencent.mm',
  120. action: 'weixin://'
  121. })) {
  122. console.log('111', "已安装微信");
  123. wxFlag.value = true
  124. } else {
  125. wxFlag.value = false
  126. }
  127. console.log('showAppleLogin.value', showAppleLogin.value);
  128. })
  129. function getLoginInit() {
  130. getVersion({}).then(res => {
  131. if (version != res.data.version) {
  132. tipDialogRef.value.handleShow();
  133. }
  134. })
  135. }
  136. const getYzmBtn = () => {
  137. if (indexData.telStatus === 'login-btn-normal') {
  138. if (indexData.isAgreed === true) {
  139. uni.redirectTo({
  140. url: `/pages/login/login?telNum=${indexData.phoneNumber}`
  141. })
  142. } else {
  143. agreeDialogRef.value.handleShow();
  144. }
  145. }
  146. }
  147. const isIOS13OrAbove = () => {
  148. const systemInfo = uni.getSystemInfoSync();
  149. if (systemInfo.platform == 'ios') {
  150. const version = systemInfo.system.split(' ')[1].split('.');
  151. const majorVersion = parseInt(version[0], 10);
  152. return majorVersion >= 13;
  153. }
  154. return false;
  155. }
  156. // 用户协议同意
  157. const confirmBtn = () => {
  158. indexData.isAgreed = true;
  159. getYzmBtn();
  160. }
  161. const telClose = () => {
  162. telDialogFlag.value = false;
  163. }
  164. const bindBtn = (res) => {
  165. telDialogFlag.value = false;
  166. if (res.cardId == 0) {
  167. uni.redirectTo({
  168. url: `/pages/selectGradesTerms/index`
  169. })
  170. } else {
  171. uni.redirectTo({
  172. url: `/pages/study/index`
  173. })
  174. }
  175. }
  176. const ykConfirmBtn = () => {
  177. uni.redirectTo({
  178. url: `/pages/selectGradesTerms/index`
  179. })
  180. }
  181. const wxConfirmBtn = () => {
  182. wxLoginFun();
  183. }
  184. // 手机号校验规则
  185. const validatePhoneNumber = (value) => {
  186. const phoneRegex = /^1[3-9]\d{9}$/;
  187. if (phoneRegex.test(value)) {
  188. indexData.telStatus = 'login-btn-normal';
  189. } else {
  190. indexData.telStatus = 'login-btn-disabled';
  191. }
  192. }
  193. const clearTelInput = (event) => {
  194. if (event.detail.value.length > 0) {
  195. indexData.clearTelIcon = true;
  196. validatePhoneNumber(event.detail.value);
  197. } else {
  198. indexData.clearTelIcon = false;
  199. }
  200. }
  201. const clearTel = () => {
  202. indexData.phoneNumber = '';
  203. indexData.telStatus = 'login-btn-disabled';
  204. indexData.clearTelIcon = false;
  205. }
  206. const agreeBtn = (data) => {
  207. if (data === 'yhxy') {
  208. agreeType.value = 'yhxy'
  209. } else {
  210. agreeType.value = 'ystk'
  211. }
  212. agreeContentDialogRef.value.handleShow();
  213. };
  214. const handleAgreementChange = (event) => {
  215. if (event.detail.value[0] === 'agree') {
  216. indexData.isAgreed = true;
  217. } else {
  218. indexData.isAgreed = false;
  219. }
  220. }
  221. // 游客登录
  222. const ykBtn = () => {
  223. if (indexData.isAgreed === true) {
  224. uni.redirectTo({
  225. url: `/pages/selectGradesTerms/index`
  226. })
  227. } else {
  228. agreeYkDialog.value.handleShow();
  229. }
  230. }
  231. const appleLoginClick = () => {
  232. uni.login({
  233. provider: 'apple',
  234. success: function(loginRes) {
  235. console.log('loginRes', loginRes);
  236. let req = {
  237. "apple": true,
  238. "code": loginRes.appleInfo.user
  239. }
  240. console.log(req);
  241. console.log(req);
  242. wxLogin(req).then(res => {
  243. console.log(res);
  244. if (!res.data.bind) {
  245. console.log('未绑定');
  246. cacheManager.set('wxLogin', {
  247. bind: res.data.bing
  248. })
  249. telDialogFlag.value = true;
  250. nextTick(() => {
  251. telDialogRef.value.getOpenId(res.data.openId);
  252. })
  253. } else {
  254. uni.showLoading({
  255. title: '登录中'
  256. });
  257. setTimeout(() => {
  258. cacheManager.set('auth', res.data.loginVo)
  259. if (res.data.loginVo.subjectId == 0 && res.data.loginVo
  260. .levelId == 0) {
  261. uni.redirectTo({
  262. url: `/pages/selectGradesTerms/index`
  263. })
  264. uni.hideLoading();
  265. } else {
  266. uni.redirectTo({
  267. url: `/pages/study/index`
  268. })
  269. uni.hideLoading();
  270. }
  271. }, 1000)
  272. }
  273. }).catch((error) => {
  274. console.log(error);
  275. })
  276. },
  277. fail: function(err) {
  278. console.log('err.code', err.code);
  279. // 登录授权失败
  280. // err.code错误码参考`授权失败错误码(code)说明`
  281. }
  282. });
  283. }
  284. // 游客登录
  285. const wxLoginClick = () => {
  286. if (indexData.isAgreed === true) {
  287. wxLoginFun();
  288. } else {
  289. agreeWxDialog.value.handleShow();
  290. }
  291. }
  292. const wxLoginFun = () => {
  293. //获取服务商信息判断手机端是否安装了app
  294. // uni.getProvider({
  295. // service: 'oauth',// oauth 代表授权登录
  296. // success: function (res) {
  297. // // 登录
  298. // uni.login({
  299. // // 表示授权方式 如果不设置则弹出登录列表选择界面
  300. // provider: 'weixin',
  301. // "onlyAuthorize": true, // 微信登录仅请求授权认证
  302. // success: function (loginRes) {
  303. // console.log('loginRes',loginRes);
  304. // }
  305. // });
  306. // }
  307. // });
  308. if (plus.runtime.isApplicationExist({
  309. pname: 'com.tencent.mm',
  310. action: 'weixin://'
  311. })) {
  312. console.log('111', "已安装微信");
  313. uni.login({
  314. "provider": "weixin",
  315. "onlyAuthorize": true, // 微信登录仅请求授权认证
  316. success: function(event) {
  317. console.log(event);
  318. const {
  319. code
  320. } = event
  321. //客户端成功获取授权临时票据(code),向业务服务器发起登录请求。
  322. let req = {
  323. "apple": false,
  324. "code": code
  325. }
  326. console.log(req);
  327. wxLogin(req).then(res => {
  328. console.log(res);
  329. if (!res.data.bind) {
  330. console.log('未绑定');
  331. cacheManager.set('wxLogin', {
  332. bind: res.data.bing
  333. })
  334. telDialogFlag.value = true;
  335. nextTick(() => {
  336. telDialogRef.value.getOpenId(res.data.openId);
  337. })
  338. } else {
  339. uni.showLoading({
  340. title: '登录中'
  341. });
  342. setTimeout(() => {
  343. cacheManager.set('auth', res.data.loginVo)
  344. if (res.data.loginVo.subjectId == 0 && res.data.loginVo
  345. .levelId == 0) {
  346. uni.redirectTo({
  347. url: `/pages/selectGradesTerms/index`
  348. })
  349. uni.hideLoading();
  350. } else {
  351. uni.redirectTo({
  352. url: `/pages/study/index`
  353. })
  354. uni.hideLoading();
  355. }
  356. }, 1000)
  357. }
  358. }).catch((error) => {
  359. console.log(error);
  360. })
  361. },
  362. fail: function(err) {
  363. console.log(err);
  364. // 登录授权失败
  365. // err.code是错误码
  366. }
  367. })
  368. } else {
  369. toast("请重启应用重试")
  370. return false
  371. }
  372. }
  373. </script>