login.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. }else{
  148. buttonslist=[
  149. {
  150. "provider": "apple",
  151. "iconPath": "/static/image/login/apple.png" // 图标路径仅支持本地图片
  152. }
  153. ]
  154. }
  155. }
  156. univerifyManager.login({
  157. provider: 'univerify',
  158. univerifyStyle: { // 自定义登录框样式
  159. fullScreen: !isIOS,
  160. authButton: {
  161. "title": "本机号码一键登录",
  162. "normalColor": "#FF5C03",
  163. "highlightColor": "#FF5C03",
  164. "disabledColor": "#FFA366",
  165. },
  166. otherLoginButton: {
  167. "title": "快捷登录",
  168. "visible": "false",
  169. },
  170. navBar: {
  171. backgroundColor: "#ffffff",
  172. title: ""
  173. },
  174. logo: {
  175. "path": "static/image/login/top_bg.png",
  176. "width": "0px",
  177. "height": "0px"
  178. },
  179. closeIcon: {
  180. "path": "static/images/icon_close.png",
  181. "width": "24px",
  182. "height": "24px"
  183. },
  184. buttons: {
  185. "iconWidth": "44px", // 图标宽度(高度等比例缩放) 默认值:45px
  186. "list": buttonslist
  187. },
  188. privacyTerms: {
  189. "defaultCheckBoxState":false, // 条款勾选框初始状态 默认值: true
  190. "isCenterHint":false, //未勾选服务条款时点击登录按钮的提示是否居中显示 默认值: false (3.7.13+ 版本支持)
  191. "uncheckedImage":"", // 可选 条款勾选框未选中状态图片(仅支持本地图片 建议尺寸 24x24px)(3.2.0+ 版本支持)
  192. "checkedImage":"", // 可选 条款勾选框选中状态图片(仅支持本地图片 建议尺寸24x24px)(3.2.0+ 版本支持)
  193. "checkBoxSize":22, // 可选 条款勾选框大小
  194. "textColor": "#BBBBBB", // 文字颜色 默认值:#BBBBBB
  195. "termsColor": "#FF5C03", // 协议文字颜色 默认值: #5496E3
  196. "prefix": "我已阅读并同意", // 条款前的文案 默认值:“我已阅读并同意”
  197. "suffix": "并使用本机号码登录", // 条款后的文案 默认值:“并使用本机号码登录”
  198. "privacyItems": [ // 自定义协议条款,最大支持2个,需要同时设置url和title. 否则不生效
  199. {
  200. "url": "https://userapp.zkhj6.com/web/userAgreement", // 点击跳转的协议详情页面
  201. "title": "用户服务协议" // 协议名称
  202. },
  203. {
  204. "url": "https://userapp.zkhj6.com/web/privacyPolicy", // 点击跳转的协议详情页面
  205. "title": "隐私保护" // 协议名称
  206. }
  207. ]
  208. },
  209. },
  210. success(res){ // 登录成功
  211. console.log("qxj univerify login:"+JSON.stringify(res.authResult)); // {openid:'登录授权唯一标识',access_token:'接口返
  212. uni.hideLoading();
  213. if(qconfig!=undefined){
  214. qconfig.applyLogin=false;
  215. }
  216. loginType=3;
  217. loginByUniverify(res.authResult,0);
  218. },
  219. fail(res){
  220. console.log("qxj fail res",res);
  221. uni.hideLoading();
  222. if(qconfig!=undefined){
  223. qconfig.applyLogin=false;
  224. }
  225. if (res.code != 30002 || res.code != 30003 || res.code != 30008) {
  226. handleUniverifyError(res.code);
  227. }
  228. if(res.code==30008){ //用户点击了自定义按钮
  229. uni.getCheckBoxState({
  230. success(response){
  231. if(!response.state){
  232. uni.showToast({title: "请同意服务条款和隐私政策",icon: 'none',position:'bottom'});
  233. return;
  234. }
  235. if(res.provider=='weixin') {
  236. quickWechatLogin(false);
  237. }else if(res.provider=='apple') {
  238. quickAppleLogin()
  239. }
  240. },
  241. fail(err){
  242. // console.log(err.errCode)
  243. // console.log(err.errMsg)
  244. }
  245. });
  246. console.log("----qxj 一键登录调用2-----");
  247. }
  248. else if(res.code==30003){ //关闭验证界面
  249. univerifyManager.close();
  250. return false;
  251. }
  252. else if(res.code==30005){ //预登录失败
  253. // uni.showToast({title: "一键登录失败请检查网络和SIM卡是否可用",icon: 'none',position:'bottom'});
  254. if(url&&url.route=="pages/auth/login"){
  255. return;
  256. }
  257. setTimeout(function() {
  258. uni.navigateTo({url: "/pages/auth/login"});
  259. }, 400);
  260. }
  261. }
  262. });
  263. };
  264. export function quickWechatLogin(isMobileLogin,phone=''){
  265. console.log("qxj quickWechatLogin");
  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 || phone,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. if(res.code==200){
  331. if(res.user!=null){
  332. uni.hideLoading();
  333. uni.setStorageSync('AppToken',res.token);
  334. uni.setStorageSync('userInfo',JSON.stringify(res.user));
  335. uni.$emit('refreshUserInfo',{ "isFirst":res.isFirst });
  336. uni.$emit('showHealthButler');
  337. uni.closeAuthView();
  338. if(res.isFirst){ //未完成新手任务
  339. uni.navigateTo({ url: '/pages/user/integral/points' });
  340. }
  341. else {
  342. goPage()
  343. }
  344. }
  345. else{
  346. unionid=res.unionid;
  347. if(isMobileLoginFst){ //若先点击手机号登录且没有绑定微信
  348. bindWechatAct(userPhone,unionid);
  349. }else{
  350. if(res.isNew){ //是否绑定手机号
  351. bindMobile();
  352. }
  353. }
  354. }
  355. }
  356. else{
  357. uni.hideLoading();
  358. uni.showToast({title: res.msg,icon: 'none'});
  359. }
  360. },
  361. rej => {}
  362. );
  363. }
  364. export function bindMobile(type){
  365. // #ifdef APP-PLUS
  366. //uni.showLoading({title:""});
  367. const uniLogin = () => {
  368. uni.login({
  369. provider: 'univerify',
  370. univerifyStyle: { // 自定义登录框样式
  371. fullScreen: false,
  372. authButton: {
  373. "title": "绑定手机号",
  374. "normalColor": "#FF5C03",
  375. "highlightColor": "#FF5C03",
  376. },
  377. privacyTerms: {
  378. "defaultCheckBoxState":true, // 条款勾选框初始状态 默认值: true
  379. "isCenterHint":false, //未勾选服务条款时点击登录按钮的提示是否居中显示 默认值: false (3.7.13+ 版本支持)
  380. "uncheckedImage":"", // 可选 条款勾选框未选中状态图片(仅支持本地图片 建议尺寸 24x24px)(3.2.0+ 版本支持)
  381. "checkedImage":"", // 可选 条款勾选框选中状态图片(仅支持本地图片 建议尺寸24x24px)(3.2.0+ 版本支持)
  382. "checkBoxSize":18, // 可选 条款勾选框大小
  383. "textColor": "#BBBBBB", // 文字颜色 默认值:#BBBBBB
  384. "termsColor": "#FF5C03", // 协议文字颜色 默认值: #5496E3
  385. "prefix": "我已阅读并同意", // 条款前的文案 默认值:“我已阅读并同意”
  386. "suffix": "并使用本机号码登录", // 条款后的文案 默认值:“并使用本机号码登录”
  387. "privacyItems": [ // 自定义协议条款,最大支持2个,需要同时设置url和title. 否则不生效
  388. ]
  389. },
  390. },
  391. success(res){ //
  392. // console.log("qxj univerify login:"+JSON.stringify(res.authResult)); // {openid:'登录授权唯一标识',access_token:'接口返回的 token'}
  393. if(type==1) {
  394. loginByUniverify(res.authResult,3);
  395. } else {
  396. loginByUniverify(res.authResult,1);
  397. }
  398. },
  399. fail(res){
  400. uni.hideLoading();
  401. uni.closeAuthView();
  402. if (res.code != 30002 || res.code != 30003 || res.code != 30008) {
  403. handleUniverifyError(res.code);
  404. }
  405. if(res.errCode==30005){ //预登录失败
  406. //uni.showToast({title: "一键登录失败请检查网络和SIM卡是否可用",icon: 'none',position:'bottom'});
  407. if(url&&url.route!="pages/auth/login"){
  408. uni.navigateTo({url: "/pages/auth/login"});
  409. }
  410. }
  411. console.log("qxj failRes:"+JSON.stringify(res));
  412. }
  413. });
  414. };
  415. uniLogin();
  416. console.log("----qxj 一键登录调用3-----");
  417. // #endif
  418. // #ifdef H5
  419. if(url&&url.route=="pages/auth/login"){
  420. return;
  421. }
  422. uni.navigateTo({
  423. url: "/pages/auth/login"
  424. });
  425. // #endif
  426. };
  427. export function loginByUniverify(authResult,type){
  428. uniCloud.callFunction({
  429. name: 'loginByMobile', // 你的云函数名称
  430. data: {
  431. access_token: authResult.access_token, // 客户端一键登录接口返回的access_token
  432. openid:authResult.openid // 客户端一键登录接口返回的openid
  433. }
  434. }).then(res => {
  435. if(res.result.errCode==0){
  436. userPhone=res.result.phoneNumber;
  437. if(type==0){ //手机号一键登录
  438. loginActionAct();
  439. }else{ //微信登录绑定手机号
  440. setPhoneAct(userPhone,unionid);
  441. }
  442. }else{
  443. uni.showToast({title:res.result.message,icon: 'none' });
  444. }
  445. }).catch(err=>{
  446. // 处理错误
  447. console.error('调用云函数失败:',err);
  448. // 详细记录错误信息
  449. console.error('错误信息:', err.errMsg);
  450. })
  451. }
  452. export function getLoginCode() {
  453. return new Promise((resolve, reject) => {
  454. uni.login({
  455. provider: "weixin",
  456. onlyAuthorize: true,
  457. success: (res) => {
  458. resolve(res.code)
  459. },
  460. fail: (err) => {
  461. console.log('login fail:', JSON.stringify(err));
  462. // 未开通
  463. if (err.code == 1000) {
  464. uni.showModal({
  465. title: '登录失败',
  466. content: `${err.errMsg}\n,错误码:${err.code}`,
  467. confirmText: '开通指南',
  468. cancelText: '确定',
  469. success: (res) => {
  470. if (res.confirm) {
  471. setTimeout(() => {
  472. plus.runtime.openWeb(
  473. 'https://ask.dcloud.net.cn/article/37965'
  474. )
  475. }, 500)
  476. }
  477. }
  478. });
  479. reject();
  480. }
  481. // 一键登录预登陆失败
  482. if (err.code == '30005') {
  483. uni.showModal({
  484. showCancel: false,
  485. title: '预登录失败',
  486. content: err.errMsg
  487. });
  488. reject();
  489. }
  490. // 一键登录用户关闭验证界面
  491. if (err.code != '30003') {
  492. uni.showModal({
  493. showCancel: false,
  494. title: '登录失败',
  495. content: JSON.stringify(err)
  496. });
  497. reject();
  498. }
  499. },
  500. complete: () => {
  501. }
  502. });
  503. })
  504. };
  505. export function loginActionAct(){
  506. if (loginType==3 && userPhone==null) {
  507. uni.showToast({title: "请授权手机号登录",icon: 'none'});
  508. return
  509. }
  510. var data = {
  511. phone:userPhone,
  512. jpushId:registrationID,
  513. loginType:loginType,
  514. loginDevice:getLoginDevice(),
  515. source:plus.runtime.channel || source
  516. };
  517. var that=this;
  518. uni.showLoading({
  519. title:"处理中..."
  520. });
  521. login(data).then(res => {
  522. uni.hideLoading();
  523. if (res.code == 200 && res.users && Object.prototype.toString.call(res.users) == '[object Array]') {
  524. uni.setStorageSync('requestParam',data)
  525. uni.setStorageSync('wechatList',res.users)
  526. uni.closeAuthView();
  527. goPage();
  528. return
  529. }
  530. if(res.code==200){
  531. if(res.isNew){
  532. userPhone=res.phone;
  533. quickWechatLogin(true);
  534. return;
  535. }
  536. uni.setStorageSync('AppToken',res.token);
  537. uni.setStorageSync('userInfo',JSON.stringify(res.user));
  538. uni.$emit('showHealthButler');
  539. uni.closeAuthView();
  540. if(res.isFirst){
  541. // #ifdef APP-PLUS
  542. if(plus.runtime.channel=="baidu"){ //获取百度渠道标识
  543. let bdCmdType=uni.getStorageSync("bdCmdType");
  544. if(bdCmdType!=null && parseInt(bdCmdType)<1){
  545. registerIdCode("register",1,0,res.user.userId); //注册
  546. }
  547. }
  548. // #endif
  549. uni.$emit('refreshIM');
  550. uni.navigateTo({ url: '/pages/user/integral/points' });
  551. } else {
  552. goPage();
  553. }
  554. }
  555. else{
  556. uni.showToast({title: res.msg,icon: 'none'});
  557. }
  558. }
  559. ).catch(err=>{
  560. console.log('登录失败啦',err);
  561. });
  562. }
  563. export function setPhoneAct(phone,unionId){
  564. let params={
  565. "phone":phone,
  566. "unionId":unionId,
  567. "jpushId":registrationID,
  568. "source":plus.runtime.channel || source
  569. };
  570. if(!isIOS){
  571. setPhone(params).then(res => {
  572. uni.hideLoading();
  573. if(res.code==200){
  574. uni.setStorageSync('AppToken',res.token);
  575. uni.setStorageSync('userInfo',JSON.stringify(res.user));
  576. uni.$emit('showHealthButler');
  577. uni.closeAuthView();
  578. goPage();
  579. }
  580. else{
  581. uni.showToast({title: res.msg,icon: 'none',duration:2000});
  582. }
  583. },
  584. rej => {}
  585. );
  586. }
  587. else{
  588. params.simExist=1;
  589. params.appleKey=appleKey;
  590. setIPhoneNumber(params).then(res => {
  591. uni.hideLoading();
  592. if(res.code==200){
  593. uni.setStorageSync('AppToken',res.token);
  594. uni.setStorageSync('userInfo',JSON.stringify(res.user));
  595. uni.$emit('showHealthButler');
  596. uni.closeAuthView();
  597. goPage();
  598. }
  599. else{
  600. uni.showToast({title: res.msg,icon: 'none',duration:2000});
  601. }
  602. },
  603. rej => {}
  604. );
  605. }
  606. }
  607. export function bindWechatAct(phone,code){
  608. let params={"phone":phone,"code":code,"jpushId":registrationID,"source":plus.runtime.channel || source};
  609. bindWechat(params).then(res => {
  610. uni.hideLoading();
  611. console.log("qxj bindWeChat res:"+JSON.stringify(res));
  612. if(res.code==200){
  613. uni.setStorageSync('AppToken',res.token);
  614. uni.setStorageSync('userInfo',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. console.log("qxj loginByApple:",res);
  705. if(res.code==200){
  706. if(res.user!=null){
  707. uni.hideLoading();
  708. uni.setStorageSync('AppToken',res.token);
  709. uni.setStorageSync('userInfo',JSON.stringify(res.user));
  710. uni.$emit('refreshUserInfo',{ "isFirst":res.isFirst });
  711. uni.$emit('showHealthButler');
  712. uni.closeAuthView();
  713. if(res.isFirst){ //未完成新手任务
  714. uni.navigateTo({ url: '/pages/user/integral/points' });
  715. }
  716. else {
  717. goPage()
  718. }
  719. } else {
  720. if(res.isNew){ //是否绑定手机号
  721. bindMobile(1);
  722. }
  723. }
  724. }
  725. else{
  726. uni.hideLoading();
  727. uni.showToast({title: res.msg,icon: 'none'});
  728. }
  729. }).catch(()=>{
  730. uni.hideLoading()
  731. })
  732. },
  733. fail: function (res) {
  734. if (res.code != 30002 || res.code != 30003 || res.code != 30008) {
  735. handleUniverifyError(res.code);
  736. }
  737. console.log(err);
  738. // 登录授权失败
  739. // err.code错误码参考`授权失败错误码(code)说明`
  740. }
  741. });
  742. }
  743. export function getLoginDevice(){
  744. let loginDevice = "";
  745. // #ifdef APP-PLUS
  746. if(plus.runtime.isAgreePrivacy()) {
  747. let devinfo=uni.getDeviceInfo();
  748. if(devinfo!=null){
  749. loginDevice += devinfo.deviceBrand ? devinfo.deviceBrand : "";
  750. loginDevice += devinfo.deviceModel ? " " + devinfo.deviceModel : "";
  751. loginDevice += devinfo.system ? " " + devinfo.system : "";
  752. }
  753. }
  754. // #endif
  755. return loginDevice;
  756. }
  757. export function goPage() {
  758. uni.$emit('refreshIM');
  759. pages = getCurrentPages();
  760. url = pages[ pages.length - 1];
  761. if(univerifyManager) {
  762. univerifyManager.close();
  763. uni.closeAuthView();
  764. univerifyManager = null
  765. }
  766. const wechatList = uni.getStorageSync('wechatList')
  767. if(wechatList){
  768. return uni.navigateTo({
  769. url:'/pages/auth/wechatList'
  770. })
  771. }
  772. const openUrl = uni.getStorageSync("openUrl")
  773. if(openUrl) {
  774. uni.navigateTo({
  775. url: openUrl,
  776. success: function(res) {
  777. uni.removeStorageSync("openUrl")
  778. }
  779. });
  780. } else {
  781. uni.reLaunch({
  782. url: '/pages_im/pages/conversation/conversationList/index',
  783. //url: '../course/video/living-app',
  784. animationType: 'none',
  785. animationDuration: 2000
  786. });
  787. return;
  788. if(pages.length == 1) {
  789. uni.reLaunch({
  790. url: '/pages_im/pages/conversation/conversationList/index',
  791. //url: '../course/video/living-app',
  792. animationType: 'none',
  793. animationDuration: 2000
  794. })
  795. } else {
  796. navBack()
  797. }
  798. }
  799. updateMsgDot()
  800. setSource()
  801. }
  802. export function upPushClientId(pushId) {
  803. updatePushId({pushId:pushId})
  804. }