index.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. onChooseAvatar(e) {
  36. console.log(e.detail,'0000000000')
  37. this.setData({
  38. avatarUrl:e.detail.avatarUrl
  39. })
  40. },
  41. getBg: function () {
  42. let that = this;
  43. app.util.request({
  44. 'url': 'entry/wxapp/index',
  45. 'data': {
  46. controller: 'index.get_newauth_bg'
  47. },
  48. dataType: 'json',
  49. success: function (res) {
  50. that.setData({ loaded: true })
  51. if (res.data.code == 0) {
  52. let { newauth_bg_image, newauth_confirm_image, newauth_cancel_image } = res.data.data;
  53. that.setData({ newauth_bg_image, newauth_confirm_image, newauth_cancel_image })
  54. }
  55. }
  56. })
  57. },
  58. close: function () {
  59. this.triggerEvent("cancel");
  60. },
  61. bindGetUserInfo: function (t) {
  62. var that = this;
  63. if (!this.data.btnLoading) {
  64. var n = t.detail;
  65. if (this.setData({ btnLoading: true }), "getUserInfo:ok" === n.errMsg) {
  66. util.login_prosime(that.data.needPosition).then(function () {
  67. console.log("授权成功")
  68. that.setData({ btnLoading: false });
  69. wx.showToast({
  70. title: '登录成功',
  71. icon: 'success',
  72. duration: 2000
  73. })
  74. that.triggerEvent("authSuccess");
  75. app.globalData.changedCommunity = true;
  76. //检查获取位置权限
  77. that.data.needPosition && location.getGps();
  78. }).catch(function () {
  79. that.triggerEvent("cancel");
  80. console.log('授权失败');
  81. })
  82. } else {
  83. wx.showToast({
  84. title: "授权失败,为了完整体验,获取更多优惠活动,需要您的授权。",
  85. icon: "none"
  86. });
  87. this.triggerEvent("cancel");
  88. this.setData({ btnLoading: false });
  89. }
  90. }
  91. },
  92. getProfile: function() {
  93. var that = this;
  94. console.log(this.data.nickname,'9999')
  95. //非空验证
  96. if(this.data.avatarUrl=='../../images/sctx.png'){
  97. wx.showToast({
  98. title: "请上传头像",
  99. icon: "none"
  100. });
  101. return false;
  102. }
  103. if(this.data.nickname==''){
  104. wx.showToast({
  105. title: "",
  106. icon: "none"
  107. });
  108. return false;
  109. }
  110. wx.uploadFile({
  111. url: app.util.url('entry/wxapp/index', {'m':'lionfish_comshop','controller':'goods.doPageUpload'}),
  112. filePath: that.data.avatarUrl,
  113. name: 'upfile',
  114. formData: {
  115. 'name':that.data.avatarUrl
  116. },
  117. header: {
  118. 'content-type': 'multipart/form-data'
  119. },
  120. success: function (res) {
  121. var data = JSON.parse(res.data);
  122. console.log(data.image_o,'000000000000000000000')
  123. that.setData({
  124. avatarUrl:data.image_o
  125. })
  126. }
  127. })
  128. console.log(wx.canIUse("getUserProfile"));
  129. if (!that.data.btnLoading) {
  130. // if(wx.canIUse("getUserProfile")) {
  131. wx.getUserProfile({
  132. desc: "获取你的昵称、头像、地区及性别",
  133. success: function (msg) {
  134. var userInfo = msg.userInfo
  135. userInfo['nickName']=that.data.nickname
  136. userInfo['avatarUrl']=that.data.avatarUrl
  137. console.log( userInfo,'898989')
  138. wx.setStorage({
  139. key: "userInfo",
  140. data: userInfo
  141. })
  142. that.setData({ btnLoading: true });
  143. util.login_prosime(that.data.needPosition, userInfo).then( res => {
  144. console.log("授权成功")
  145. that.setData({ btnLoading: false });
  146. wx.showToast({
  147. title: '登录成功',
  148. icon: 'success',
  149. duration: 2000
  150. })
  151. that.triggerEvent("authSuccess", res);
  152. app.globalData.changedCommunity = true;
  153. //检查获取位置权限
  154. that.data.needPosition && location.getGps();
  155. }).catch(function () {
  156. that.triggerEvent("cancel");
  157. console.log('授权失败');
  158. })
  159. },
  160. fail: ()=>{
  161. wx.showToast({
  162. title: "授权失败,为了完整体验,获取更多优惠活动,需要您的授权。",
  163. icon: "none"
  164. });
  165. that.triggerEvent("cancel");
  166. that.setData({ btnLoading: false });
  167. }
  168. })
  169. }
  170. // } else {
  171. // wx.showModal({
  172. // title: '提示',
  173. // content: '请升级微信',
  174. // showCancel: false
  175. // })
  176. // }
  177. },
  178. checkWxLogin: function() {
  179. return new Promise((resolve, reject) => {
  180. wx.getSetting({
  181. success(res) {
  182. if (!res.authSetting['scope.userInfo']) {
  183. return reject({
  184. authSetting: false
  185. });
  186. } else {
  187. wx.getStorage({
  188. key: 'token',
  189. success(token) {
  190. util.check_login_new().then(isLogin=>{
  191. if (isLogin) {
  192. // 已登录未过期
  193. return resolve(false);
  194. console.log('已登录未过期')
  195. } else {
  196. console.log('过期')
  197. if (token) {
  198. return resolve(token);
  199. } else {
  200. return reject(res);
  201. }
  202. }
  203. })
  204. },
  205. fail(res) {
  206. return reject(res);
  207. }
  208. })
  209. }
  210. },
  211. fail(res) {
  212. return reject(res);
  213. }
  214. })
  215. })
  216. }
  217. }
  218. });