authorize.js 5.0 KB

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