index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. const _api = require('../../utils/api.js')
  2. const request = require('../../utils/js.js');
  3. const api = _api.api;
  4. const app = getApp()
  5. const globalData = app.globalData
  6. Page({
  7. data: {
  8. tel:''
  9. },
  10. /** 生命周期函数--监听页面加载 **/
  11. onLoad: function (options) {
  12. var that = this;
  13. that.setKey(that)
  14. },
  15. /******************** 微信授权 *****************************/
  16. AuthMap() { app.AuthMap(this) },
  17. //授权登录
  18. Authorize() { app.Authorize(this) },
  19. bindGetUserInfo(e){
  20. var that = this
  21. app.login(that,e,function(res){
  22. })
  23. },
  24. /******************** 微信授权 *****************************/
  25. onPullDownRefresh(e){
  26. },
  27. /** 页面上拉触底事件 **/
  28. onReachBottom: function () {
  29. var that = this;
  30. },
  31. /** 用户点击右上角分享 **/
  32. onShareAppMessage: function () {
  33. return {
  34. title: globalData.share.title,
  35. path: globalData.share.path,
  36. imageUrl: globalData.share.imageUrl // 图片 URL
  37. }
  38. },
  39. getUserProfile(_that, sessionKey,sucFun) {
  40. var that=this
  41. wx.getUserProfile({
  42. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  43. success:function(res2){
  44. // console.log('获取成功: ',res2)
  45. //调用登录接口
  46. var code = ''
  47. request.login().then(function (d) {
  48. code = d.code
  49. console.log(code)
  50. }).then(function (e) {
  51. that.ajaxget(api.code + '?code=' + code, {}, {}, function (res) {
  52. if (res.code == 1) {
  53. // that.saveUserInfo(_that, res.data.session_key, sucFun)
  54. that.ajaxpost(api.login, {}, {
  55. 'encryptedData': res2.encryptedData,
  56. 'iv': res2.iv,
  57. 'sessionKey': res.data.session_key,
  58. 'openid': res.data.openid
  59. }, function (e) {
  60. console.log(e)
  61. if (e.code == 1) {
  62. console.log('成功啦')
  63. wx.setStorageSync('user', e.data)
  64. wx.setStorageSync('sessionKey', res.data.session_key)
  65. wx.setStorageSync('token', e.data.token)
  66. that.setData({ Authorize: true, user: e.data, token: e.data.token })
  67. // sucFun(e.data)
  68. }
  69. }, function (e) {
  70. }, function () { })
  71. } else {
  72. that.showToast(res.msg)
  73. console.log('失败')
  74. }
  75. }, function (res) { }, function (res) { })
  76. });
  77. },
  78. fail:function(err){
  79. console.log("获取失败: ",err)
  80. }
  81. })
  82. },
  83. setKey(_that){
  84. var that=this
  85. _that.setData({
  86. token: wx.getStorageSync('token')
  87. , user: wx.getStorageSync('user')
  88. , Authorize: false
  89. })
  90. },
  91. //post请求-token
  92. ajaxget: function (url, header, data, successFunc, errFunc) {
  93. let that = this;
  94. var _errFunc = function (msg) {
  95. if (!!errMsg) {
  96. wx.showToast({
  97. title: msg || errMsg,
  98. icon: 'none'
  99. })
  100. }
  101. };
  102. let _data = data || {};//数据类型
  103. let token = wx.getStorageSync('token');//授权后的用户唯一码(openid)
  104. let _header = {//请求头
  105. 'content-type': 'application/x-www-form-urlencoded',
  106. 'token': token,
  107. };
  108. wx.request({
  109. url: url,
  110. data: _data,
  111. method: 'get',
  112. header: _header,
  113. success(res) {
  114. if (res) {
  115. res = res.data;
  116. successFunc(res);
  117. }
  118. if (!res) {
  119. _errFunc(res)
  120. return;
  121. }
  122. },
  123. fail() {
  124. _errFunc(res);
  125. }
  126. })
  127. },
  128. //post请求-token
  129. ajaxpost: function (url, header, data, successFunc, errFunc) {
  130. let that = this;
  131. var _errFunc = function (msg) {
  132. if (!!errMsg) {
  133. wx.showToast({
  134. title: msg || errMsg,
  135. icon: 'none'
  136. })
  137. }
  138. };
  139. let _data = data || {};//数据类型
  140. let token = wx.getStorageSync('token');//授权后的用户唯一码(openid)
  141. let _header = {//请求头
  142. 'content-type': 'application/x-www-form-urlencoded',
  143. 'token': token,
  144. lat: wx.getStorageSync('location').lat,
  145. lng: wx.getStorageSync('location').lng
  146. };
  147. wx.request({
  148. url: url,
  149. data: _data,
  150. method: 'post',
  151. header: _header,
  152. success(res) {
  153. if (res) {
  154. res = res.data;
  155. successFunc(res);
  156. }
  157. if (!res) {
  158. _errFunc(res)
  159. return;
  160. }
  161. },
  162. fail() {
  163. _errFunc(res);
  164. }
  165. })
  166. },
  167. getPhoneNumber(res){
  168. console.log(res)
  169. let that = this
  170. let _data = {
  171. encryptedData:res.detail.encryptedData,
  172. iv:res.detail.iv,
  173. sessionKey:wx.getStorageSync('sessionKey')
  174. }
  175. that.ajaxpost(api.getPhone, {}, _data, function (e) {
  176. console.log(e)
  177. if (e.code == 1) {
  178. console.log('成功啦')
  179. that.setData({
  180. tel:e.data.phoneNumber
  181. })
  182. }
  183. })
  184. },
  185. /** 生命周期函数--监听页面初次渲染完成 **/
  186. onReady: function () {
  187. },
  188. /** 生命周期函数--监听页面隐藏 **/
  189. onHide: function () {
  190. },
  191. /** 生命周期函数--监听页面卸载 **/
  192. onUnload: function () {
  193. }
  194. })