index.vue 9.9 KB

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