index.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //index.js
  2. import Toast from '@vant/weapp/toast/toast';
  3. import {
  4. saveAppUserInfo,
  5. getMpOpenId,
  6. decryptEncryptedData
  7. } from "../../api/home"
  8. Page({
  9. data: {
  10. motto: '微信授权登录后,才可以使用大部分功能。',
  11. userInfo: {},
  12. hasUserInfo: false,
  13. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  14. defaultSize: 'default',
  15. primarySize: 'default',
  16. warnSize: 'default',
  17. disabled: false,
  18. plain: false,
  19. loading: false,
  20. showModal: false, //定义登录弹窗
  21. isShowUserInfoLayer: false, //是否显示自定义授权位置弹框
  22. },
  23. onLoad: function () {
  24. //获取应用实例
  25. let app = getApp()
  26. // console.log("****************onLoad******************");
  27. if (app.globalData.userInfo) {
  28. this.setData({
  29. userInfo: app.globalData.userInfo,
  30. hasUserInfo: true
  31. })
  32. } else if (this.data.canIUse) {
  33. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  34. // 所以此处加入 callback 以防止这种情况
  35. app.userInfoReadyCallback = res => {
  36. this.setData({
  37. userInfo: res.userInfo,
  38. hasUserInfo: true
  39. })
  40. }
  41. } else {
  42. // 在没有 open-type=getUserInfo 版本的兼容处理
  43. wx.getUserInfo({
  44. success: res => {
  45. app.globalData.userInfo = res.userInfo
  46. this.setData({
  47. userInfo: res.userInfo,
  48. hasUserInfo: true
  49. })
  50. }
  51. })
  52. }
  53. // console.log("user info:" + app.globalData.userInfo);
  54. // wx.login({
  55. // success: function (res) {
  56. // if (res.code) {
  57. // //发起网络请求
  58. // console.log(res.code)
  59. // } else {
  60. // console.log('获取用户登录态失败!' + res.errMsg)
  61. // }
  62. // }
  63. // });
  64. },
  65. //授权用户信息按钮
  66. bindAuthUserInfo: function (e) {
  67. var that = this;
  68. //获取授权结果查看是否已授权用户信息
  69. wx.getSetting({
  70. success: (res) => {
  71. if (res.authSetting['scope.userInfo'] == undefined && !res.authSetting['scope.userInfo']) //未授权获取用户信息(首次进来页面)
  72. that.getLocationInfo(); //获取当前用户信息
  73. else if (res.authSetting['scope.userInfo'] === false) //未授权获取用户信息(点击官方授权弹框取消按钮后)
  74. that.setData({
  75. isShowUserInfoLayer: true
  76. }) //显示自定义授权框
  77. else //已授权
  78. that.getUserInfo(); //获取当前用户信息
  79. }
  80. })
  81. },
  82. //用户信息授权框确认按钮
  83. bindConfirmUserInfo: function (e) {
  84. var that = this;
  85. //打开设置页面进行授权设置
  86. wx.openSetting({
  87. success: function (res) {
  88. if (res.authSetting['scope.userInfo']) {
  89. //获取当前用户信息
  90. that.setData({
  91. isShowUserInfoLayer: false,
  92. })
  93. }
  94. }
  95. });
  96. },
  97. //授权框取消按钮
  98. bindCancelUserInfo: function (e) {
  99. let that = this;
  100. that.setData({
  101. isShowUserInfoLayer: false
  102. })
  103. wx.showModal({
  104. title: '拒绝授权提示',
  105. content: '您未授权获取当前用户信息,小程序大部分功能将无法使用!是否拒绝授权?',
  106. showCancel: true, //是否显示取消按钮
  107. cancelText: "否", //默认是“取消”
  108. cancelColor: 'skyblue', //取消文字的颜色
  109. confirmText: "是", //默认是“确定”
  110. confirmColor: 'skyblue', //确定文字的颜色
  111. success: function (res) {
  112. if (res.cancel) { //点击否
  113. that.setData({
  114. isShowUserInfoLayer: true
  115. })
  116. } else { //点击是
  117. that.setData({
  118. isShowUserInfoLayer: false
  119. })
  120. }
  121. },
  122. fail: function (res) {}, //接口调用失败的回调函数
  123. complete: function (res) {}, //接口调用结束的回调函数(调用成功、失败都会执行)
  124. })
  125. },
  126. // 显示一键获取手机号弹窗
  127. showDialogBtn: function () {
  128. this.setData({
  129. showModal: true //修改弹窗状态为true,即显示
  130. })
  131. },
  132. // 隐藏一键获取手机号弹窗
  133. hideModal: function () {
  134. this.setData({
  135. showModal: false //修改弹窗状态为false,即隐藏
  136. });
  137. },
  138. /**
  139. * 生命周期函数--监听页面初次渲染完成
  140. */
  141. onReady: function () {
  142. // console.log("onReady@@@@@@@@@@@@@@@@@");
  143. },
  144. /**
  145. * 生命周期函数--监听页面显示
  146. */
  147. onShow: function () {
  148. // console.log("*************onShow****************");
  149. },
  150. getUserInfo: function (e) {
  151. var app = getApp()
  152. let that = this;
  153. // console.log(e)
  154. // 获取用户信息
  155. wx.getSetting({
  156. success(res) {
  157. // console.log("res", res)
  158. if (res.authSetting['scope.userInfo']) {
  159. // console.log("已授权=====")
  160. // 已经授权,可以直接调用 getUserInfo 获取头像昵称
  161. wx.getUserInfo({
  162. success(res) {
  163. // console.log("获取用户信息成功", res.userInfo);
  164. app.globalData.userInfo = res.userInfo;
  165. // console.log("minaOpenid:"+app.mpOpenId);
  166. // console.log("blogNickName:"+res.userInfo.nickName);
  167. // console.log("sexFlag:"+res.userInfo.gender === 1?'M':'F');
  168. // console.log("blogProfilePhoto:"+res.userInfo.avatarUrl);
  169. that.showDialogBtn(); //调用一键获取手机号弹窗
  170. that.setData({
  171. userInfo: res.userInfo,
  172. hasUserInfo: true
  173. })
  174. },
  175. fail(res) {
  176. console.log("获取用户信息失败", res)
  177. }
  178. })
  179. } else {
  180. // console.log("未授权=====")
  181. that.bindAuthUserInfo();
  182. }
  183. }
  184. })
  185. },
  186. getPhoneNumber: function (e) {
  187. let that = this;
  188. let app = getApp();
  189. // console.log(e.detail.errMsg)
  190. // console.log("iv:" + e.detail.iv)
  191. // console.log("encryptedData:" + e.detail.encryptedData)
  192. let iv = e.detail.iv;
  193. let encryptedData = e.detail.encryptedData
  194. let sessionKey = null;
  195. let mobilePhone = null;
  196. wx.login({
  197. success: function (res) {
  198. if (res.code) {
  199. let code = res.code;
  200. //发起网络请求
  201. getMpOpenId({
  202. code: code
  203. }).then(res => { // handle success
  204. // console.log("sessionKey:" + res.session_key);
  205. that.sessionKey = res.session_key;
  206. decryptEncryptedData({
  207. sessionKey: that.sessionKey,
  208. encryptedData: encryptedData,
  209. iv: iv
  210. }).then(res => { // handle success
  211. mobilePhone = res.phoneNumber;
  212. // console.log("userType:"+2);
  213. // console.log("minaOpenid:"+app.mpOpenId);
  214. // console.log("blogNickName:"+app.globalData.userInfo.nickName);
  215. // console.log("sexFlag:"+app.globalData.userInfo.gender === 1 ? 'M' : 'F');
  216. // console.log("blogProfilePhoto:"+app.globalData.userInfo.avatarUrl);
  217. // console.log("mobilePhone:"+mobilePhone);
  218. //保存appUserInfo信息
  219. saveAppUserInfo({
  220. "userType": "2",
  221. "minaOpenid": app.mpOpenId,
  222. "blogNickName": app.globalData.userInfo.nickName,
  223. "sexFlag": app.globalData.userInfo.gender === 1 ? 'M' : 'F',
  224. "blogProfilePhoto": app.globalData.userInfo.avatarUrl,
  225. "mobilePhone": mobilePhone
  226. }).then(res => { // handle success
  227. // console.log(res.data);
  228. if (res.retCode == 0 || res.retCode == -1) {
  229. // console.log("保存app用户信息成功:"+app.globalData.userInfo);
  230. // console.log("保存app用户信息成功。。。");
  231. }
  232. }).catch(error => { // handle error
  233. console.log(error);
  234. })
  235. }).catch(error => { // handle error
  236. console.log(error);
  237. })
  238. }).catch(error => { // handle error
  239. console.log(error);
  240. })
  241. } else {
  242. console.log('获取用户登录态失败!' + res.errMsg)
  243. }
  244. }
  245. });
  246. if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
  247. wx.showModal({
  248. title: '提示',
  249. showCancel: false,
  250. content: '未授权',
  251. success: function (res) {
  252. // console.log("未授权返回的res:"+res);
  253. }
  254. })
  255. } else {
  256. wx.showModal({
  257. title: '提示',
  258. showCancel: false,
  259. content: '同意授权',
  260. confirmText: "确定", //默认是“确定”
  261. confirmColor: 'skyblue', //确定文字的颜色
  262. success: function (res) {
  263. if (res.confirm) {
  264. that.setData({
  265. showModal: false
  266. });
  267. wx.switchTab({
  268. url: '../gasup/index',
  269. });
  270. } else {}
  271. }
  272. })
  273. }
  274. }
  275. })