authorize.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. var utilMd5 = require('./md5.js');
  2. const tx_key = 'H3TBZ-Y5VW5-RQEIS-QZPN2-7DLA5-4OBMG'; // 腾讯地图key
  3. const tx_secret_key = ''; // key对应的签名
  4. // 登录
  5. function login() {
  6. let promise = new Promise((resolve, reject) => {
  7. wx.login({
  8. success: res => {
  9. console.log(res)
  10. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  11. wx.request({
  12. url: 'https://t5.9026.com/api/v1/common/wxLogin',
  13. data: {
  14. wechat_code: res.code
  15. },
  16. method: "POST",
  17. header: {
  18. 'content-type': 'application/json'
  19. },
  20. success: function(res) {
  21. resolve(res)
  22. },
  23. fail: function(e) {
  24. reject(e)
  25. }
  26. });
  27. },
  28. fail: res => {
  29. reject('登录失败')
  30. }
  31. })
  32. })
  33. return promise;
  34. }
  35. // 获取用户信息
  36. function getUserInfo() {
  37. let promise = new Promise((resolve, reject) => {
  38. getAuthSetting('scope.userInfo', false, '用户信息').then(res => {
  39. if (res) {
  40. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  41. wx.getUserInfo({
  42. success: res => {
  43. // 可以将 res 发送给后台解码出 unionId
  44. resolve('获取用户信息成功', res.userInfo)
  45. }
  46. })
  47. } else {
  48. resolve(false)
  49. }
  50. }).catch(res => {
  51. reject(res)
  52. })
  53. })
  54. return promise;
  55. }
  56. // 打开授权设置界面
  57. function openSetting(scope, isMust, name) { // scope是权限字段,isMust表示是否必须授权, name是授权信息名,用于模态框信息展示
  58. let promise = new Promise((resolve, reject) => {
  59. wx.showModal({
  60. title: '授权提示',
  61. content: '请先授权获取' + name,
  62. success(res) {
  63. if (res.confirm) {
  64. wx.openSetting({
  65. success(res) {
  66. if (res.authSetting[scope]) { // 用户打开了授权开关
  67. resolve(true)
  68. } else { // 用户没有打开授权开关, 继续打开设置页面
  69. if (isMust) {
  70. openSetting(scope, isMust, name)
  71. } else {
  72. reject('获取用户授权信息失败')
  73. }
  74. }
  75. },
  76. fail(res) {}
  77. })
  78. } else if (res.cancel) {
  79. if (isMust) {
  80. openSetting(scope, isMust, name)
  81. } else {
  82. reject('获取用户授权信息失败')
  83. }
  84. }
  85. }
  86. })
  87. })
  88. return promise;
  89. }
  90. function getAuthSetting(scope, isMust, name) { // 获取用户授权信息,scope是授权的权限字段名, isMust表示是否必须授权才能进行下一步
  91. let promise = new Promise((resolve, reject) => {
  92. wx.getSetting({
  93. success: res => {
  94. if (res.authSetting[scope]) {
  95. resolve(true) // 用户已经授权
  96. } else {
  97. // 用户还没有授权,向用户发起授权请求
  98. wx.authorize({
  99. scope: scope,
  100. success() { // 用户同意授权
  101. resolve(true)
  102. },
  103. fail() { // 用户不同意授权
  104. if (isMust) { // 如果是必须授权才能进行下一步的接口,则打开设置页面
  105. openSetting(scope, isMust, name).then(res => {
  106. resolve(true)
  107. }).catch(res => {
  108. resolve(false)
  109. })
  110. } else { // 如果不是必须授权的接口,则直接返回未授权状态
  111. resolve(false)
  112. }
  113. }
  114. })
  115. }
  116. },
  117. fail: res => {
  118. reject('获取用户授权信息失败')
  119. }
  120. })
  121. })
  122. return promise;
  123. }
  124. function getLocation() { // 获取地理位置信息
  125. let promise = new Promise((resolve, reject) => {
  126. getAuthSetting('scope.userLocation', true, '地理位置信息').then(res => { // 获取用户信息,must必须授权
  127. if (res) {
  128. // 已经授权,可以直接获取地理位置
  129. let _this = this
  130. wx.getLocation({
  131. type: 'wgs84',
  132. success(res) {
  133. console.log(res)
  134. let locationData = { // 新建一个对象用于保存经纬度等信息
  135. latitude: res.latitude, // 纬度
  136. longitude: res.longitude // 经度
  137. }
  138. let sig = utilMd5.hexMD5("/ws/geocoder/v1?key=" + tx_key + "&location=" + res.latitude + "," + res.longitude +
  139. tx_secret_key)
  140. let getAddressUrl = "https://apis.map.qq.com/ws/geocoder/v1?key=" + tx_key + "&location=" + res.latitude +
  141. "," + res.longitude + "&sig=" + sig;
  142. wx.request({ // 请求腾讯地图接口获取地理位置
  143. url: getAddressUrl,
  144. success: function(res) {
  145. if (res.data.status == 0) { // 获取地理位置成功
  146. locationData.address = res.data.result
  147. resolve(locationData)
  148. } else { // 获取地理位置信息失败
  149. reject('获取地理位置信息失败')
  150. }
  151. }
  152. })
  153. },
  154. fail(res) {
  155. reject('获取地理位置信息失败')
  156. }
  157. })
  158. } else {
  159. }
  160. }).catch(res => {
  161. reject(res)
  162. })
  163. })
  164. return promise;
  165. }
  166. module.exports = {
  167. login: login, // 登录 + 获取openID
  168. getUserInfo: getUserInfo, // 获取用户信息
  169. getAuthSetting: getAuthSetting, // 获取授权信息, 可选择是否必须授权
  170. getLocation: getLocation // 获取经纬度 + 经纬度通过腾讯地图签名校验的方式换取城市位置
  171. }