wxUtil.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import * as api from '@/service/api'
  2. // uni.login()封装
  3. const wxLogin = function(openid) {
  4. return new Promise((resolve, reject) => {
  5. uni.login({
  6. success(res) {
  7. if (res.code) {
  8. resolve(res.code)
  9. } else {
  10. reject(res.errMsg);
  11. }
  12. }
  13. })
  14. })
  15. }
  16. /*微信小程序登录*/
  17. const wechatAppLogin = function() {
  18. /*登录提示*/
  19. // loading("正在授权")
  20. uni.showLoading({
  21. title:"授权中"
  22. })
  23. uni.getUserProfile({
  24. desc: '获取用户授权',
  25. success: res => {
  26. let userInfo = res.userInfo;
  27. console.log(userInfo);
  28. // 修改头像
  29. api.updateThumb({
  30. thumb:userInfo.avatarUrl,
  31. token:uni.getStorageSync("token")
  32. }).then(res=>{
  33. if(res.code == 200){
  34. uni.setStorage({
  35. key:"userInfo",
  36. data:res.result
  37. })
  38. uni.showToast({
  39. title:"授权成功",
  40. icon:'success'
  41. })
  42. setTimeout(function(){
  43. uni.navigateTo({
  44. url: "/pages/index/index",
  45. })
  46. },1000)
  47. }else{
  48. uni.showToast({
  49. title:res.msg,
  50. icon:'error'
  51. })
  52. }
  53. });
  54. uni.setStorage({
  55. key:'wxUserOriginInfo',
  56. data:userInfo
  57. })
  58. uni.hideLoading();
  59. },
  60. fail() {
  61. uni.hideLoading();
  62. }
  63. })
  64. }
  65. // 获取手机号授权
  66. const getPhoneNumber = function(event) {
  67. uni.showLoading({
  68. title:"授权中"
  69. })
  70. let that = this;
  71. let code = event.detail.code; //获取手机code
  72. let detail = event.detail;
  73. let codeResultInfo = uni.getStorageSync('wxUserInfo');
  74. var promise = new Promise(function(resolve, reject) {
  75. uni.checkSession({
  76. success: (res) => {
  77. api.wxphoneNum({
  78. iv:detail.iv,
  79. encryptedData:detail.encryptedData,
  80. session_key:codeResultInfo.session_key,
  81. openid:codeResultInfo.openid,
  82. code:detail.code
  83. }).then(res => {
  84. console.log('diyige');
  85. if(res.code == 200){
  86. uni.setStorage({
  87. key:"token",
  88. data:res.result.token
  89. })
  90. uni.setStorage({
  91. key:"userInfo",
  92. data:res.result.user
  93. })
  94. uni.showToast({
  95. title:"登录成功",
  96. icon:'success'
  97. })
  98. setTimeout(function(){
  99. uni.navigateTo({
  100. url: "/pages/index/index",
  101. })
  102. },1000)
  103. }else{
  104. console.log(res.msg);
  105. uni.showToast({
  106. title:res.msg,
  107. icon:'error'
  108. })
  109. }
  110. uni.hideLoading();
  111. },resTwo=>{
  112. console.log("异常");
  113. })
  114. },
  115. })
  116. })
  117. uni.hideLoading();
  118. }
  119. module.exports = {
  120. wechatAppLogin,
  121. getPhoneNumber
  122. }