问星数据小程序

util copy.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. var rootDocment = 'https://data.kuxuan.com';//域名
  2. // var rootDocment = 'http://47.93.151.217:8006';//测试域名
  3. // var rootDocment = 'http://apitbk.726p.com';//预发域名
  4. var md5 = require('./md5.js');
  5. var count = 0;
  6. wx.setStorageSync('doneLogin',false)
  7. function req(url, method, data, cb, fail_cb, selfHeader) {
  8. var dataes = {'admin_id':wx.getStorageSync('admin_id'),
  9. 'ttl':wx.getStorageSync('ttl'),
  10. 'sign':wx.getStorageSync('sign')}
  11. var header = {
  12. 'sessionKey':wx.getStorageSync('session_key')?wx.getStorageSync('session_key'):false
  13. }
  14. wx.request({
  15. url: rootDocment + url,
  16. data: data ? Object.assign(data, dataes) : dataes,
  17. method: method,
  18. header: selfHeader ? Object.assign(selfHeader,header) : header,
  19. success: function (res) {
  20. if(res.data.errno == '4002' || res.data.errno == '4001'){
  21. console.log(count+"~~"+wx.getStorageSync('isLogin')+"~~~~~"+wx.getStorageSync('doneLogin'))
  22. if(!wx.getStorageSync('isLogin')&&!wx.getStorageSync('doneLogin')){
  23. // 新加
  24. wx.setStorageSync('doneLogin',true);
  25. checkSession()
  26. return;
  27. }else{
  28. console.log(count + '~~~~~')
  29. if(count>=1||wx.getStorageSync('doneLogin')){
  30. return
  31. }
  32. wx.clearStorage()
  33. wx.setStorageSync('isRequest',true)
  34. wx.setStorageSync('doneLogin',true)
  35. count++;
  36. console.log("过期~~~"+count)
  37. login()
  38. }
  39. }else{
  40. count = 0;
  41. return typeof cb == "function" && cb(res.data)
  42. }
  43. },
  44. fail: function (err) {
  45. // if(!wx.getStorageSync('doneLogin')) {
  46. // wx.setStorageSync('doneLogin',false)
  47. // count = 0;
  48. // console.log("跳转授权2")
  49. // wx.redirectTo({
  50. // url: '/pages/welcome/welcome?from=index',
  51. // })
  52. // }
  53. console.log("失败")
  54. return typeof fail_cb == "function" && fail_cb(err)
  55. }
  56. })
  57. }
  58. module.exports = {
  59. req: req
  60. }
  61. //判断微信登录是否过期
  62. function checkSession (cb) {
  63. var _this = this;
  64. wx.checkSession({
  65. success (cb) {
  66. //session_key 未过期,并且在本生命周期一直有效
  67. console.log('未过期')
  68. console.log(cb)
  69. console.log(!wx.getStorageSync('isLogin') +"~~~"+ !wx.getStorageSync('isRequest'))
  70. wx.setStorageSync('hasWxLogin',true);//微信登录是否过期
  71. if(!wx.getStorageSync('isLogin') && !wx.getStorageSync('isRequest')){
  72. login()
  73. }else{
  74. wx.setStorageSync('doneLogin', false)
  75. count=0;
  76. wx.switchTab({
  77. url: '/pages/index/index',
  78. })
  79. }
  80. },
  81. fail () {
  82. console.log('过期,重新登录')
  83. // session_key 已经失效,需要重新执行登录流程
  84. login()
  85. }
  86. })
  87. }
  88. // 旧login
  89. function login(){
  90. var _this = this;
  91. console.log("新login")
  92. wx.getSetting({
  93. success: function(res){
  94. if (res.authSetting['scope.userInfo']) {
  95. wx.getUserInfo({
  96. success:function (e){
  97. console.log(e)
  98. wx.setStorageSync('encryptedData',e.encryptedData)
  99. wx.setStorageSync('iv',e.iv)
  100. wx.login({
  101. success (res) {
  102. if (res.code) {
  103. // 发起网络请求 发送 res.code 到后台换取 openId, sessionKey, unionId
  104. req('/v2/api/wx/login', 'get', {
  105. code: res.code,
  106. encrypted_data:wx.getStorageSync('encryptedData'),
  107. iv: wx.getStorageSync('iv')
  108. }, function (res) {
  109. wx.setStorageSync('isRequest', false)
  110. if(res && res.errno == 0){
  111. //成功
  112. var session_key = res.rst.session_key;
  113. wx.setStorageSync('session_key',session_key)
  114. if(!res.rst.info || res.rst.info.length == 0){//用户未绑定手机号
  115. console.log('用户未绑定手机号')
  116. }else{
  117. wx.setStorageSync('userInfo',res.rst.info)
  118. wx.setStorageSync('admin_id',res.rst.info.id)
  119. wx.setStorageSync('ttl',res.rst.ttl)
  120. var sign=md5.hex_md5("random="+res.rst.random+"&ttl="+res.rst.ttl+"&user_id="+res.rst.info.id);
  121. wx.setStorageSync('sign',sign)
  122. wx.setStorageSync('isLogin',true)
  123. wx.setStorageSync('doneLogin', false)
  124. count = 0;
  125. wx.switchTab({
  126. url: '/pages/index/index?from=login',
  127. success: function(res){
  128. //小程序跳转页面后,动态刷新跳转页面
  129. var page = getCurrentPages().pop();
  130. console.log("页面ing")
  131. console.log(page)
  132. if (page == undefined || page == null) return;
  133. page.onLoad();
  134. console.log("刷新")
  135. },
  136. })
  137. }
  138. }else{
  139. wx.redirectTo({
  140. url: '/pages/welcome/welcome',
  141. })
  142. }
  143. },()=>{
  144. count = 0;
  145. wx.setStorageSync('doneLogin', false)
  146. console.log("跳转授权3")
  147. count = 0;
  148. wx.setStorageSync('doneLogin', false)
  149. wx.redirectTo({
  150. url: '/pages/welcome/welcome',
  151. })
  152. })
  153. } else {
  154. console.log('登录失败!' + res.errMsg)
  155. }
  156. }
  157. })
  158. }
  159. })
  160. }else{
  161. count = 0;
  162. wx.setStorageSync('doneLogin', false)
  163. //未授权
  164. console.log("跳转授权4")
  165. wx.redirectTo({
  166. url: '/pages/welcome/welcome',
  167. })
  168. return
  169. }
  170. }
  171. })
  172. }