index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. var util = require('../../utils/util.js');
  2. var location = require('../../utils/Location.js');
  3. var app = getApp();
  4. Component({
  5. properties: {
  6. needAuth: {
  7. type: Boolean,
  8. value: false
  9. },
  10. needPosition: {
  11. type: Boolean,
  12. value: true
  13. },
  14. navBackUrl: {
  15. type: String,
  16. value: '',
  17. observer: function (t) {
  18. if (t) app.globalData.navBackUrl = t;
  19. }
  20. }
  21. },
  22. attached: function () {
  23. this.setData({
  24. skin: getApp().globalData.skin,
  25. })
  26. this.getBg();
  27. },
  28. data: {
  29. nickname:'',
  30. avatarUrl:'../../images/sctx.png',
  31. btnLoading: false,
  32. canIUse: wx.canIUse('button.open-type.getUserInfo')
  33. },
  34. methods: {
  35. agree:function(){
  36. console.log("用户同意隐私授权, 接下来可以调用隐私协议中声明的隐私接口")
  37. },
  38. disagree:function(){
  39. console.log("用户拒绝隐私授权, 未同意过的隐私协议中的接口将不能调用")
  40. // this.triggerEvent("cancel");
  41. wx.showToast({
  42. title: '需同意后才可以继续使用'+app.globalData.title+'小程序',
  43. duration: 2000,
  44. icon:"none"
  45. })
  46. },
  47. onChooseAvatar(e) {
  48. console.log(e.detail,'0000000000')
  49. this.setData({
  50. avatarUrl:e.detail.avatarUrl
  51. })
  52. },
  53. getBg: function () {
  54. let that = this;
  55. app.util.request({
  56. 'url': 'entry/wxapp/index',
  57. 'data': {
  58. controller: 'index.get_newauth_bg'
  59. },
  60. dataType: 'json',
  61. success: function (res) {
  62. that.setData({ loaded: true })
  63. if (res.data.code == 0) {
  64. let { newauth_bg_image, newauth_confirm_image, newauth_cancel_image } = res.data.data;
  65. that.setData({ newauth_bg_image, newauth_confirm_image, newauth_cancel_image })
  66. }
  67. }
  68. })
  69. },
  70. close: function () {
  71. this.triggerEvent("cancel");
  72. },
  73. bindGetUserInfo: function (t) {
  74. var that = this;
  75. if (!this.data.btnLoading) {
  76. var n = t.detail;
  77. if (this.setData({ btnLoading: true }), "getUserInfo:ok" === n.errMsg) {
  78. util.login_prosime(that.data.needPosition).then(function () {
  79. console.log("授权成功")
  80. that.setData({ btnLoading: false });
  81. wx.showToast({
  82. title: '登录成功',
  83. icon: 'success',
  84. duration: 2000
  85. })
  86. that.triggerEvent("authSuccess");
  87. app.globalData.changedCommunity = true;
  88. //检查获取位置权限
  89. that.data.needPosition && location.getGps();
  90. }).catch(function () {
  91. that.triggerEvent("cancel");
  92. console.log('授权失败');
  93. })
  94. } else {
  95. wx.showToast({
  96. title: "授权失败,为了完整体验,获取更多优惠活动,需要您的授权。",
  97. icon: "none"
  98. });
  99. this.triggerEvent("cancel");
  100. this.setData({ btnLoading: false });
  101. }
  102. }
  103. },
  104. getProfile: function() {
  105. var that = this;
  106. console.log(this.data.nickname,'9999')
  107. //非空验证
  108. if(this.data.avatarUrl=='../../images/sctx.png'){
  109. wx.showToast({
  110. title: "请上传头像",
  111. icon: "none"
  112. });
  113. return false;
  114. }
  115. if(this.data.nickname==''){
  116. wx.showToast({
  117. title: "",
  118. icon: "none"
  119. });
  120. return false;
  121. }
  122. wx.uploadFile({
  123. url: app.util.url('entry/wxapp/index', {'m':'lionfish_comshop','controller':'goods.doPageUpload'}),
  124. filePath: that.data.avatarUrl,
  125. name: 'upfile',
  126. formData: {
  127. 'name':that.data.avatarUrl
  128. },
  129. header: {
  130. 'content-type': 'multipart/form-data'
  131. },
  132. success: function (res) {
  133. var data = JSON.parse(res.data);
  134. console.log(data.image_o,'000000000000000000000')
  135. that.setData({
  136. avatarUrl:data.image_o
  137. })
  138. }
  139. })
  140. console.log(wx.canIUse("getUserProfile"));
  141. if (!that.data.btnLoading) {
  142. // if(wx.canIUse("getUserProfile")) {
  143. wx.getUserProfile({
  144. desc: "获取你的昵称、头像、地区及性别",
  145. success: function (msg) {
  146. var userInfo = msg.userInfo
  147. userInfo['nickName']=that.data.nickname
  148. userInfo['avatarUrl']=that.data.avatarUrl
  149. console.log( userInfo,'898989')
  150. wx.setStorage({
  151. key: "userInfo",
  152. data: userInfo
  153. })
  154. that.setData({ btnLoading: true });
  155. util.login_prosime(that.data.needPosition, userInfo).then( res => {
  156. console.log("授权成功")
  157. that.setData({ btnLoading: false });
  158. wx.showToast({
  159. title: '登录成功',
  160. icon: 'success',
  161. duration: 2000
  162. })
  163. that.triggerEvent("authSuccess", res);
  164. app.globalData.changedCommunity = true;
  165. //检查获取位置权限
  166. that.data.needPosition && location.getGps();
  167. }).catch(function () {
  168. that.triggerEvent("cancel");
  169. console.log('授权失败');
  170. })
  171. },
  172. fail: ()=>{
  173. wx.showToast({
  174. title: "授权失败,为了完整体验,获取更多优惠活动,需要您的授权。",
  175. icon: "none"
  176. });
  177. that.triggerEvent("cancel");
  178. that.setData({ btnLoading: false });
  179. }
  180. })
  181. }
  182. // } else {
  183. // wx.showModal({
  184. // title: '提示',
  185. // content: '请升级微信',
  186. // showCancel: false
  187. // })
  188. // }
  189. },
  190. checkWxLogin: function() {
  191. return new Promise((resolve, reject) => {
  192. wx.getSetting({
  193. success(res) {
  194. if (!res.authSetting['scope.userInfo']) {
  195. return reject({
  196. authSetting: false
  197. });
  198. } else {
  199. wx.getStorage({
  200. key: 'token',
  201. success(token) {
  202. util.check_login_new().then(isLogin=>{
  203. if (isLogin) {
  204. // 已登录未过期
  205. return resolve(false);
  206. console.log('已登录未过期')
  207. } else {
  208. console.log('过期')
  209. if (token) {
  210. return resolve(token);
  211. } else {
  212. return reject(res);
  213. }
  214. }
  215. })
  216. },
  217. fail(res) {
  218. return reject(res);
  219. }
  220. })
  221. }
  222. },
  223. fail(res) {
  224. return reject(res);
  225. }
  226. })
  227. })
  228. }
  229. }
  230. });