login.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. import { login,loginByWeChat,setPhone,setIPhoneNumber,bindWechat,updatePushId,loginByApple } from '@/api/user'
  2. import { qconfig } from '@/utils/config.js';
  3. import { navBack, registerIdCode,updateMsgDot,setSource } from '@/utils/common.js';
  4. import {isLogin,checkWechatInstalled} from '@/utils/common.js'
  5. let univerifyManager=null;
  6. let registrationID=uni.getStorageSync("registrationID");
  7. let userPhone="";
  8. let unionid="";
  9. let appleKey="";
  10. let loginType=3;//1账号密码 2:微信登录 3:手机号一键登录
  11. let isMobileLoginFst=false; //标识是否先手机号登录且没有绑定微信;(该手机号用户没有微信unionId)
  12. let pages = '';
  13. let url = ''; //当前页页面实例
  14. let source = 'app';
  15. // #ifdef APP-PLUS
  16. let isIOS = plus.os.name == 'iOS';
  17. if(isIOS){
  18. source='iOS';
  19. }
  20. // #endif
  21. // #ifndef APP-PLUS
  22. source = 'h5';
  23. // #endif
  24. export function showLoginPage(){
  25. pages = getCurrentPages();
  26. url = pages[ pages.length - 1];
  27. // #ifdef APP-PLUS
  28. if(qconfig!=undefined && qconfig.applyLogin){
  29. return;
  30. }
  31. if(!isIOS){
  32. if(!plus.runtime.isAgreePrivacy()) {
  33. if(url&&url.route=="pages/auth/login"){
  34. return;
  35. }
  36. uni.navigateTo({
  37. url: "/pages/auth/login"
  38. });
  39. return;
  40. }
  41. }
  42. uni.showLoading({title:""});
  43. getRegistrationID();
  44. let that=this;
  45. univerifyManager = uni.getUniverifyManager();
  46. const loginCallBack = (resinfo) => {
  47. // 获取一键登录弹框协议勾选状态
  48. univerifyManager.getCheckBoxState({success(res) {
  49. if(qconfig!=undefined){
  50. qconfig.applyLogin=false;
  51. }
  52. uni.hideLoading();
  53. if(!res.state){
  54. uni.showToast({title: "请同意服务条款和隐私政策",icon: 'none',position:'bottom'});
  55. return;
  56. }
  57. if(resinfo.provider=='weixin') {
  58. quickWechatLogin(false);
  59. }else if(resinfo.provider=='apple') {
  60. quickAppleLogin()
  61. }
  62. // 关闭一键登录弹框
  63. univerifyManager.close();
  64. }
  65. });
  66. }
  67. if(isIOS){
  68. uni.preLogin({
  69. provider: 'univerify',
  70. success(res){ //预登录成功
  71. // 显示一键登录选项
  72. uni.hideLoading();
  73. //console.log("qxj preLogin success res",res);
  74. univerifyLogin(this);
  75. // 订阅自定义按钮点击事件
  76. univerifyManager.onButtonsClick(loginCallBack);
  77. },
  78. fail(res){ // 预登录失败
  79. // 不显示一键登录选项(或置灰)
  80. // 根据错误信息判断失败原因,如有需要可将错误提交给统计服务器
  81. console.log("qxj preLogin fail res",res);
  82. uni.hideLoading();
  83. if(res.errCode==30005 || res.errMsg.includes("无SIM卡")){
  84. if(url&&url.route=="pages/auth/login"){
  85. return;
  86. }
  87. setTimeout(function() {
  88. uni.navigateTo({url: "/pages/auth/login"});
  89. }, 400);
  90. }
  91. }
  92. });
  93. return;
  94. }else{
  95. //手机号一键登录时
  96. univerifyLogin(this);
  97. // 订阅自定义按钮点击事件
  98. univerifyManager.onButtonsClick(loginCallBack);
  99. // 取消订阅自定义按钮点击事件
  100. //univerifyManager.offButtonsClick(loginCallBack);
  101. if(qconfig!=undefined){
  102. qconfig.applyLogin=true;
  103. }
  104. uni.hideLoading();
  105. }
  106. // #endif
  107. // #ifdef H5
  108. if(String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger"){
  109. let url=window.location.href;
  110. //let urlPaths=url.split("/h5");
  111. let urlPaths=url.split("/pages");
  112. let pagePath="/pages"+urlPaths[1];
  113. uni.setStorageSync('beforLoginPage',pagePath);
  114. uni.navigateTo({
  115. url: "/pages/auth/h5WxLogin"
  116. });
  117. }
  118. else{
  119. if(url&&url.route=="pages/auth/login"){
  120. return;
  121. }
  122. uni.navigateTo({
  123. url: "/pages/auth/login"
  124. });
  125. }
  126. // #endif
  127. }
  128. const univerifyLogin=function(that){
  129. let buttonslist=[
  130. {
  131. "provider": "weixin",
  132. "iconPath": "/static/image/login/weixin.png" // 图标路径仅支持本地图片
  133. }
  134. ];
  135. if(isIOS) {
  136. if(checkWechatInstalled()){
  137. buttonslist=[
  138. {
  139. "provider": "weixin",
  140. "iconPath": "/static/image/login/weixin.png" // 图标路径仅支持本地图片
  141. },
  142. {
  143. "provider": "apple",
  144. "iconPath": "/static/image/login/apple.png" // 图标路径仅支持本地图片
  145. }
  146. ]
  147. }
  148. else{
  149. buttonslist=[
  150. {
  151. "provider": "apple",
  152. "iconPath": "/static/image/login/apple.png" // 图标路径仅支持本地图片
  153. }
  154. ]
  155. }
  156. }
  157. univerifyManager.login({
  158. provider: 'univerify',
  159. univerifyStyle: { // 自定义登录框样式
  160. fullScreen: !isIOS,
  161. authButton: {
  162. "title": "本机号码一键登录",
  163. "normalColor": "#FF5C03",
  164. "highlightColor": "#FF5C03",
  165. "disabledColor": "#FFA366",
  166. },
  167. otherLoginButton: {
  168. "title": "快捷登录",
  169. "visible": "false",
  170. },
  171. navBar: {
  172. backgroundColor: "#ffffff",
  173. title: ""
  174. },
  175. logo: {
  176. "path": "static/image/login/top_bg.png",
  177. "width": "0px",
  178. "height": "0px"
  179. },
  180. closeIcon: {
  181. "path": "static/images/icon_close.png",
  182. "width": "24px",
  183. "height": "24px"
  184. },
  185. // buttons: {
  186. // "iconWidth": "44px", // 图标宽度(高度等比例缩放) 默认值:45px
  187. // "list": buttonslist
  188. // },
  189. privacyTerms: {
  190. "defaultCheckBoxState":false, // 条款勾选框初始状态 默认值: true
  191. "isCenterHint":false, //未勾选服务条款时点击登录按钮的提示是否居中显示 默认值: false (3.7.13+ 版本支持)
  192. "uncheckedImage":"", // 可选 条款勾选框未选中状态图片(仅支持本地图片 建议尺寸 24x24px)(3.2.0+ 版本支持)
  193. "checkedImage":"", // 可选 条款勾选框选中状态图片(仅支持本地图片 建议尺寸24x24px)(3.2.0+ 版本支持)
  194. "checkBoxSize":22, // 可选 条款勾选框大小
  195. "textColor": "#BBBBBB", // 文字颜色 默认值:#BBBBBB
  196. "termsColor": "#FF5C03", // 协议文字颜色 默认值: #5496E3
  197. "prefix": "我已阅读并同意", // 条款前的文案 默认值:“我已阅读并同意”
  198. "suffix": "并使用本机号码登录", // 条款后的文案 默认值:“并使用本机号码登录”
  199. "privacyItems": [ // 自定义协议条款,最大支持2个,需要同时设置url和title. 否则不生效
  200. {
  201. "url": "https://userapp.his.cdwjyyh.com/web/userAgreement", // 点击跳转的协议详情页面
  202. "title": "用户服务协议" // 协议名称
  203. },
  204. {
  205. "url": "https://userapp.his.cdwjyyh.com/web/privacyPolicy", // 点击跳转的协议详情页面
  206. "title": "隐私保护" // 协议名称
  207. }
  208. ]
  209. },
  210. },
  211. success(res){ // 登录成功
  212. console.log("qxj univerify login:"+JSON.stringify(res.authResult)); // {openid:'登录授权唯一标识',access_token:'接口返
  213. uni.hideLoading();
  214. if(qconfig!=undefined){
  215. qconfig.applyLogin=false;
  216. }
  217. loginType=3;
  218. loginByUniverify(res.authResult,0);
  219. },
  220. fail(res){
  221. console.log("qxj fail res",res);
  222. uni.hideLoading();
  223. if(qconfig!=undefined){
  224. qconfig.applyLogin=false;
  225. }
  226. // if (res.code != 30002 || res.code != 30003 || res.code != 30008) {
  227. // handleUniverifyError(res.code);
  228. // }
  229. if(res.code==30008){ //用户点击了自定义按钮
  230. uni.getCheckBoxState({
  231. success(response){
  232. if(!response.state){
  233. uni.showToast({title: "请同意服务条款和隐私政策",icon: 'none',position:'bottom'});
  234. return;
  235. }
  236. if(res.provider=='weixin') {
  237. quickWechatLogin(false);
  238. }else if(res.provider=='apple') {
  239. quickAppleLogin()
  240. }
  241. },
  242. fail(err){
  243. // console.log(err.errCode)
  244. // console.log(err.errMsg)
  245. }
  246. });
  247. console.log("----qxj 一键登录调用2-----");
  248. }
  249. else if(res.code==30003){ //关闭验证界面
  250. univerifyManager.close();
  251. return false;
  252. }
  253. else if(res.code==30005){ //预登录失败
  254. // uni.showToast({title: "一键登录失败请检查网络和SIM卡是否可用",icon: 'none',position:'bottom'});
  255. if(url&&url.route=="pages/auth/login"){
  256. return;
  257. }
  258. setTimeout(function() {
  259. uni.navigateTo({url: "/pages/auth/login"});
  260. }, 400);
  261. }
  262. }
  263. });
  264. };
  265. export function quickWechatLogin(isMobileLogin){
  266. isMobileLoginFst=isMobileLogin;
  267. loginType=2;
  268. uni.login({
  269. provider: "weixin",
  270. onlyAuthorize:true,
  271. success: async (res) => {
  272. let that=this;
  273. uni.showLoading({
  274. title:"处理中..."
  275. });
  276. if(isMobileLogin){
  277. bindWechatAct(userPhone,res.code);
  278. }
  279. else{
  280. const params={"code":res.code,"loginDevice":getLoginDevice(),"jpushId":registrationID,"source":plus.runtime.channel || source};
  281. loginByWeChatAct(params);
  282. }
  283. },
  284. fail: (err) => {
  285. console.log('login fail:', JSON.stringify(err));
  286. // 未开通
  287. if (err.code == 1000) {
  288. uni.showModal({
  289. title: '登录失败',
  290. content: `${err.errMsg}\n,错误码:${err.code}`,
  291. confirmText: '开通指南',
  292. cancelText: '确定',
  293. success: (res) => {
  294. if (res.confirm) {
  295. setTimeout(() => {
  296. plus.runtime.openWeb('https://ask.dcloud.net.cn/article/37965')
  297. }, 500)
  298. }
  299. }
  300. });
  301. return;
  302. }
  303. // if (res.code != 30002 || res.code != 30003 || res.code != 30008) {
  304. // handleUniverifyError(res.code);
  305. // }
  306. // 一键登录预登陆失败
  307. if (err.code == 30005) {
  308. uni.showModal({
  309. showCancel: false,
  310. title: '预登录失败',
  311. content: err.errMsg
  312. });
  313. return;
  314. }
  315. // 一键登录用户关闭验证界面
  316. if (err.code != 30003) {
  317. uni.showModal({
  318. showCancel: false,
  319. title: '登录失败',
  320. content: JSON.stringify(err)
  321. });
  322. }
  323. },
  324. complete: () => {
  325. }
  326. });
  327. };
  328. export function loginByWeChatAct(params){
  329. loginByWeChat(params).then(res => {
  330. console.log("qxj loginByWeChat res",res);
  331. if(res.code==200){
  332. if(res.user!=null){
  333. uni.hideLoading();
  334. uni.setStorageSync('AppToken',res.token);
  335. if(res.liveToken){
  336. uni.setStorageSync('liveToken', res.liveToken);
  337. }
  338. uni.setStorageSync('userInfo',JSON.stringify(res.user));
  339. uni.setStorageSync('userData',JSON.stringify(res.user));
  340. uni.$emit('refreshUserInfo',{ "isFirst":res.isFirst });
  341. uni.$emit('showHealthButler');
  342. uni.closeAuthView();
  343. if(res.isFirst){ //未完成新手任务
  344. uni.navigateTo({ url: '/pages/user/integral/points' });
  345. }
  346. else {
  347. goPage()
  348. }
  349. }
  350. else{
  351. unionid=res.unionid;
  352. console.log("qxj isMobileLoginFst",isMobileLoginFst);
  353. if(isMobileLoginFst){ //若先点击手机号登录且没有绑定微信
  354. bindWechatAct(userPhone,unionid);
  355. }else{
  356. if(res.isNew){ //是否绑定手机号
  357. bindMobile();
  358. }
  359. }
  360. }
  361. }
  362. else{
  363. uni.hideLoading();
  364. uni.showToast({title: res.msg,icon: 'none'});
  365. }
  366. },
  367. rej => {}
  368. );
  369. }
  370. export function bindMobile(type){
  371. // #ifdef APP-PLUS
  372. //uni.showLoading({title:""});
  373. const uniLogin = () => {
  374. uni.login({
  375. provider: 'univerify',
  376. univerifyStyle: { // 自定义登录框样式
  377. fullScreen: false,
  378. authButton: {
  379. "title": "绑定手机号",
  380. "normalColor": "#FF5C03",
  381. "highlightColor": "#FF5C03",
  382. },
  383. privacyTerms: {
  384. "defaultCheckBoxState":true, // 条款勾选框初始状态 默认值: true
  385. "isCenterHint":false, //未勾选服务条款时点击登录按钮的提示是否居中显示 默认值: false (3.7.13+ 版本支持)
  386. "uncheckedImage":"", // 可选 条款勾选框未选中状态图片(仅支持本地图片 建议尺寸 24x24px)(3.2.0+ 版本支持)
  387. "checkedImage":"", // 可选 条款勾选框选中状态图片(仅支持本地图片 建议尺寸24x24px)(3.2.0+ 版本支持)
  388. "checkBoxSize":18, // 可选 条款勾选框大小
  389. "textColor": "#BBBBBB", // 文字颜色 默认值:#BBBBBB
  390. "termsColor": "#FF5C03", // 协议文字颜色 默认值: #5496E3
  391. "prefix": "我已阅读并同意", // 条款前的文案 默认值:“我已阅读并同意”
  392. "suffix": "并使用本机号码登录", // 条款后的文案 默认值:“并使用本机号码登录”
  393. "privacyItems": [ // 自定义协议条款,最大支持2个,需要同时设置url和title. 否则不生效
  394. ]
  395. },
  396. },
  397. success(res){
  398. if(type==1) {
  399. loginByUniverify(res.authResult,3);
  400. } else {
  401. loginByUniverify(res.authResult,1);
  402. }
  403. },
  404. fail(res){
  405. uni.hideLoading();
  406. uni.closeAuthView();
  407. //if (res.code != 30002 || res.code != 30003 || res.code != 30008) {
  408. //handleUniverifyError(res.code);
  409. //}
  410. if(res.errCode==30005){ //预登录失败
  411. //uni.showToast({title: "一键登录失败请检查网络和SIM卡是否可用",icon: 'none',position:'bottom'});
  412. if(!url){
  413. pages = getCurrentPages();
  414. url = pages[ pages.length - 1];
  415. }
  416. if(url&&url.route!="pages/auth/login"){
  417. uni.navigateTo({url: "/pages/auth/login?isBindMoble=1&unionid="+unionid});
  418. }
  419. }
  420. }
  421. });
  422. };
  423. if (isIOS) {
  424. uni.preLogin({
  425. provider: 'univerify',
  426. success(res) { //预登录成功
  427. // 显示一键登录选项
  428. //uni.hideLoading();
  429. console.log("qxj preLogin success res", res);
  430. uniLogin();
  431. },
  432. fail(res) { // 预登录失败
  433. // 不显示一键登录选项(或置灰)
  434. // 根据错误信息判断失败原因,如有需要可将错误提交给统计服务器
  435. console.log("qxj preLogin fail res", res);
  436. //uni.hideLoading();
  437. if (res.errCode == 30005 || res.errMsg.includes("无SIM卡")) {
  438. if (url && url.route == "pages/auth/login") {
  439. return;
  440. }
  441. setTimeout(function() {
  442. uni.navigateTo({
  443. url: "/pages/auth/login"
  444. });
  445. }, 400);
  446. }
  447. }
  448. });
  449. }else{
  450. uniLogin();
  451. }
  452. console.log("----qxj 一键登录调用3-----");
  453. // #endif
  454. // #ifdef H5
  455. if(url&&url.route=="pages/auth/login"){
  456. return;
  457. }
  458. uni.navigateTo({
  459. url: "/pages/auth/login"
  460. });
  461. // #endif
  462. };
  463. export function loginByUniverify(authResult,type){
  464. uniCloud.callFunction({
  465. name: 'loginByMobile', // 你的云函数名称
  466. data: {
  467. access_token: authResult.access_token, // 客户端一键登录接口返回的access_token
  468. openid:authResult.openid // 客户端一键登录接口返回的openid
  469. }
  470. }).then(res => {
  471. if(res.result.errCode==0){
  472. userPhone=res.result.phoneNumber;
  473. if(type==0){ //手机号一键登录
  474. loginActionAct();
  475. }else{ //微信登录绑定手机号
  476. setPhoneAct(userPhone,unionid);
  477. }
  478. }else{
  479. uni.showToast({title:res.result.message,icon: 'none' });
  480. }
  481. }).catch(err=>{
  482. // 处理错误
  483. console.error('调用云函数失败:',err);
  484. // 详细记录错误信息
  485. console.error('错误信息:', err.errMsg);
  486. })
  487. }
  488. export function loginActionAct(){
  489. if (loginType==3 && userPhone==null) {
  490. uni.showToast({title: "请授权手机号登录",icon: 'none'});
  491. return
  492. }
  493. var data = {
  494. phone:userPhone,
  495. jpushId:registrationID,
  496. loginType:loginType,
  497. loginDevice:getLoginDevice(),
  498. source:plus.runtime.channel || source
  499. };
  500. var that=this;
  501. uni.showLoading({
  502. title:"处理中..."
  503. });
  504. login(data).then(res => {
  505. console.log("qxj login",res);
  506. uni.hideLoading();
  507. if (res.code == 200 && res.users && Object.prototype.toString.call(res.users) == '[object Array]') {
  508. uni.setStorageSync('requestParam',data)
  509. uni.setStorageSync('wechatList',res.users)
  510. uni.closeAuthView();
  511. goPage();
  512. return
  513. }
  514. if(res.code==200){
  515. if(res.isNew){
  516. userPhone=res.phone;
  517. quickWechatLogin(true);
  518. return;
  519. }
  520. uni.setStorageSync('AppToken',res.token);
  521. if(res.liveToken){
  522. uni.setStorageSync('liveToken', res.liveToken);
  523. }
  524. uni.setStorageSync('userInfo',JSON.stringify(res.user));
  525. uni.setStorageSync('userData',JSON.stringify(res.user));
  526. uni.$emit('showHealthButler');
  527. uni.closeAuthView();
  528. if(res.isFirst){
  529. // #ifdef APP-PLUS
  530. if(plus.runtime.channel=="baidu"){ //获取百度渠道标识
  531. let bdCmdType=uni.getStorageSync("bdCmdType");
  532. if(bdCmdType!=null && parseInt(bdCmdType)<1){
  533. registerIdCode("register",1,0,res.user.userId); //注册
  534. }
  535. }
  536. // #endif
  537. uni.$emit('refreshIM');
  538. uni.navigateTo({ url: '/pages/user/integral/points' });
  539. } else {
  540. goPage();
  541. }
  542. }
  543. else{
  544. uni.showToast({title: res.msg,icon: 'none'});
  545. }
  546. }
  547. ).catch(err=>{
  548. console.log('登录失败啦',err);
  549. });
  550. }
  551. export function setPhoneAct(phone,unionId){
  552. let params={
  553. "phone":phone,
  554. "unionId":unionId,
  555. "jpushId":registrationID,
  556. "source":plus.runtime.channel || source
  557. };
  558. if(!isIOS){
  559. setPhone(params).then(res => {
  560. uni.hideLoading();
  561. if(res.code==200){
  562. uni.setStorageSync('AppToken',res.token);
  563. if(res.liveToken){
  564. uni.setStorageSync('liveToken', res.liveToken);
  565. }
  566. uni.setStorageSync('userInfo',JSON.stringify(res.user));
  567. uni.setStorageSync('userData',JSON.stringify(res.user));
  568. uni.$emit('showHealthButler');
  569. uni.closeAuthView();
  570. goPage();
  571. }
  572. else{
  573. uni.showToast({title: res.msg,icon: 'none',duration:2000});
  574. }
  575. },
  576. rej => {}
  577. );
  578. }
  579. else{
  580. params.simExist=1;
  581. params.appleKey=appleKey;
  582. setIPhoneNumber(params).then(res => {
  583. uni.hideLoading();
  584. if(res.code==200){
  585. uni.setStorageSync('AppToken',res.token);
  586. if(res.liveToken){
  587. uni.setStorageSync('liveToken', res.liveToken);
  588. }
  589. uni.setStorageSync('userInfo',JSON.stringify(res.user));
  590. uni.setStorageSync('userData',JSON.stringify(res.user));
  591. uni.$emit('showHealthButler');
  592. uni.closeAuthView();
  593. goPage();
  594. }
  595. else{
  596. uni.showToast({title: res.msg,icon: 'none',duration:2000});
  597. }
  598. },
  599. rej => {}
  600. );
  601. }
  602. }
  603. export function bindWechatAct(phone,code){
  604. let params={"phone":phone,"code":code,"jpushId":registrationID,"source":plus.runtime.channel || source};
  605. bindWechat(params).then(res => {
  606. uni.hideLoading();
  607. console.log("qxj bindWeChat res:"+JSON.stringify(res));
  608. if(res.code==200){
  609. uni.setStorageSync('AppToken',res.token);
  610. if(res.liveToken){
  611. uni.setStorageSync('liveToken', res.liveToken);
  612. }
  613. uni.setStorageSync('userInfo',JSON.stringify(res.user));
  614. uni.setStorageSync('userData',JSON.stringify(res.user));
  615. uni.$emit('showHealthButler');
  616. uni.closeAuthView();
  617. goPage();
  618. // #ifdef APP-PLUS
  619. const jyJPush = uni.requireNativePlugin('JY-JPush');
  620. // jyJPush.setJYJPushAlias({userAlias: registrationID}, result=> {
  621. // console.log("qxj userAlias "+JSON.stringify(result));
  622. // });
  623. // #endif
  624. }
  625. else{
  626. uni.showToast({title: res.msg,icon: 'none'});
  627. setTimeout(function() {
  628. uni.closeAuthView();
  629. }, 1800);
  630. }
  631. },
  632. rej => {}
  633. );
  634. }
  635. export function getRegistrationID(type) {
  636. // #ifdef APP-PLUS
  637. registrationID = uni.getStorageSync("registrationID");
  638. const needUpdate = type === 'update' || !registrationID;
  639. if (needUpdate&& (plus.runtime.isAgreePrivacy() || isIOS)) {
  640. uni.getPushClientId({success: res => {
  641. if(!registrationID || registrationID != res.cid) {
  642. registrationID = res.cid
  643. uni.setStorageSync("registrationID",res.cid);
  644. if(type == 'update'&&isLogin()) {
  645. console.log("qxj getPushClientId:",res);
  646. upPushClientId(res.cid)
  647. }
  648. }
  649. },
  650. fail:(err)=> {
  651. console.log("getRegistrationID error:",err)
  652. }
  653. });
  654. }
  655. // #endif
  656. }
  657. export function handleUniverifyError(code) {
  658. const errorMap = {
  659. '-7': '检查是否配置/已通过审核',
  660. '1000': '检查是否配置/已通过审核',
  661. '1001': '检查账号一键登录服务是否正常',
  662. '1002': '检查账号余额是否充足',
  663. '4001': '校验异常,联系官方人员',
  664. '4003': '校验异常,联系官方人员',
  665. '5000': '联系官方人员',
  666. '20202': '引导用户手动开启设备流量',
  667. '30001': '联系官方人员',
  668. '30004': '联系官方人员',
  669. '30005': '不具备一键登录的使用前提,设备不支持/未开启数据流量/其他原因',
  670. '30007': '校验异常,联系官方人员',
  671. '40004': '多出现在自定义基座的场景,请确保应用已通过审核后,且已重新打包',
  672. '40047': '校验异常,联系官方人员',
  673. '40053': '校验异常,联系官方人员',
  674. '40101': '检查一下手机卡类型是否是正常运营商手机卡,关闭飞行模式后重新尝试。',
  675. '40201': '检查一下手机卡类型是否是正常运营商手机卡,关闭飞行模式后重新尝试。',
  676. '40301': '检查一下手机卡类型是否是正常运营商手机卡,关闭飞行模式后重新尝试。'
  677. };
  678. const msg = errorMap[String(code)];
  679. if (msg) {
  680. uni.showToast({
  681. title: msg,
  682. icon: 'none',
  683. position: 'bottom',
  684. duration: 6000
  685. });
  686. }
  687. }
  688. export function quickAppleLogin() {
  689. uni.login({
  690. provider: 'apple',
  691. success: function (loginRes) {
  692. appleKey = loginRes.authResult.openid
  693. const params={
  694. loginDevice:getLoginDevice(),
  695. jpushId:registrationID,
  696. source:plus.runtime.channel || source,
  697. appleKey: appleKey
  698. };
  699. uni.showLoading({
  700. title: '登录中'
  701. })
  702. loginByApple(params).then(res=>{
  703. uni.hideLoading();
  704. if(res.code==200){
  705. if(res.user!=null){
  706. uni.hideLoading();
  707. uni.setStorageSync('AppToken',res.token);
  708. if(res.liveToken){
  709. uni.setStorageSync('liveToken', res.liveToken);
  710. }
  711. uni.setStorageSync('userInfo',JSON.stringify(res.user));
  712. uni.setStorageSync('userData',JSON.stringify(res.user));
  713. uni.$emit('refreshUserInfo',{ "isFirst":res.isFirst });
  714. uni.$emit('showHealthButler');
  715. uni.closeAuthView();
  716. if(res.isFirst){ //未完成新手任务
  717. uni.navigateTo({ url: '/pages/user/integral/points' });
  718. }
  719. else {
  720. goPage()
  721. }
  722. } else {
  723. if(res.isNew){ //是否绑定手机号
  724. bindMobile(1);
  725. }
  726. }
  727. }
  728. else{
  729. uni.hideLoading();
  730. uni.showToast({title: res.msg,icon: 'none'});
  731. }
  732. }).catch(()=>{
  733. uni.hideLoading()
  734. })
  735. },
  736. fail: function (res) {
  737. if (res.code != 30002 || res.code != 30003 || res.code != 30008) {
  738. //handleUniverifyError(res.code);
  739. }
  740. // 登录授权失败
  741. // err.code错误码参考`授权失败错误码(code)说明`
  742. }
  743. });
  744. }
  745. export function getLoginDevice(){
  746. let loginDevice = "";
  747. // #ifdef APP-PLUS
  748. if(plus.runtime.isAgreePrivacy()) {
  749. let devinfo=uni.getDeviceInfo();
  750. if(devinfo!=null){
  751. loginDevice += devinfo.deviceBrand ? devinfo.deviceBrand : "";
  752. loginDevice += devinfo.deviceModel ? " " + devinfo.deviceModel : "";
  753. loginDevice += devinfo.system ? " " + devinfo.system : "";
  754. }
  755. }
  756. // #endif
  757. return loginDevice;
  758. }
  759. export function goPage() {
  760. uni.$emit('refreshIM');
  761. pages = getCurrentPages();
  762. url = pages[ pages.length - 1];
  763. if(univerifyManager) {
  764. univerifyManager.close();
  765. uni.closeAuthView();
  766. univerifyManager = null
  767. }
  768. const loginReturnUrl = uni.getStorageSync('loginReturnUrl');
  769. if(loginReturnUrl){
  770. uni.removeStorageSync('loginReturnUrl');
  771. uni.redirectTo({
  772. url: loginReturnUrl,
  773. fail: ()=>{
  774. uni.navigateTo({
  775. url: loginReturnUrl
  776. })
  777. }
  778. });
  779. return;
  780. }
  781. const wechatList = uni.getStorageSync('wechatList')
  782. if(wechatList){
  783. return uni.navigateTo({
  784. url:'/pages/auth/wechatList'
  785. })
  786. }
  787. const openUrl = uni.getStorageSync("openUrl")
  788. if(openUrl) {
  789. uni.navigateTo({
  790. url: openUrl,
  791. success: function(res) {
  792. uni.removeStorageSync("openUrl")
  793. }
  794. });
  795. } else {
  796. uni.reLaunch({
  797. url: '/pages_im/pages/conversation/conversationList/index',
  798. //url: '../course/video/living-app',
  799. animationType: 'none',
  800. animationDuration: 2000
  801. });
  802. return;
  803. if(pages.length == 1) {
  804. uni.reLaunch({
  805. url: '/pages_im/pages/conversation/conversationList/index',
  806. //url: '../course/video/living-app',
  807. animationType: 'none',
  808. animationDuration: 2000
  809. })
  810. } else {
  811. navBack()
  812. }
  813. }
  814. updateMsgDot()
  815. setSource()
  816. }
  817. export function upPushClientId(pushId) {
  818. updatePushId({pushId:pushId})
  819. }