index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. var util = require('../../../utils/util.js');
  2. var api = require('../../../config/api.js');
  3. var user = require('../../../services/user.js');
  4. var app = getApp();
  5. // 定义插件变量 引入方法
  6. const tempUserInfo = {
  7. nickName: 'Hi,游客',
  8. userName: '点击去登录',
  9. avatarUrl: 'https://platform-wxmall.oss-cn-beijing.aliyuncs.com/upload/20180727/150547696d798c.png'
  10. }
  11. Page({
  12. data: {
  13. userInfo: {},
  14. hasMobile: '',
  15. imgServer: util.imgServer,
  16. all: {
  17. icon: 'ct1',
  18. name: '全部订单',
  19. id: -1
  20. },
  21. orderList:[
  22. {
  23. icon: 'ct2',
  24. name: '待付款',
  25. id: 0
  26. },
  27. {
  28. icon: 'ct3',
  29. name: '待发货',
  30. id: 201
  31. },
  32. {
  33. icon: 'ct4',
  34. name: '待收货',
  35. id: 300
  36. },
  37. {
  38. icon: 'ct5',
  39. name: '已完成',
  40. id: 301
  41. }
  42. ],
  43. barList:[
  44. {
  45. icon: 'cb1',
  46. url: '/pages/ucenter/collect/collect',
  47. name: '我的收藏',
  48. id: 0
  49. },
  50. {
  51. icon: 'cb2',
  52. url: '/pages/ucenter/footprint/footprint',
  53. name: '我的足迹',
  54. id: 1
  55. },
  56. {
  57. icon: 'cb3',
  58. url: '/pages/ucenter/feedback/feedback',
  59. name: '意见反馈',
  60. id: 2
  61. }
  62. ],
  63. relatedGoods: [],
  64. isLogin: app.globalData.isLogin
  65. },
  66. getGoodsRelated: function () {
  67. let that = this;
  68. util.request(api.GoodsRelated, {}).then(function (res) {
  69. if (res.errno === 0) {
  70. that.setData({
  71. relatedGoods: res.data.goodsList,
  72. });
  73. }
  74. });
  75. },
  76. tapByIdx:function(e){
  77. let {
  78. url
  79. } = e.currentTarget.dataset;
  80. console.log(app.globalData)
  81. if (this.data.isLogin) {
  82. wx.navigateTo({
  83. url: url,
  84. success: function (res) { },
  85. fail: function (res) { },
  86. complete: function (res) { },
  87. })
  88. } else {
  89. wx.navigateTo({
  90. url: '/pages/auth/btnAuth/btnAuth',
  91. })
  92. // wx.showModal({
  93. // title: '提示',
  94. // content: '请登录后查看',
  95. // showCancel: false,
  96. // confirmColor: '#1fe4dc',
  97. // success: res=>{
  98. // }
  99. // });
  100. }
  101. },
  102. onLoad: function (options) {
  103. // 页面初始化 options为页面跳转所带来的参数
  104. this.getGoodsRelated()
  105. },
  106. onReady: function () {
  107. },
  108. onShow: function () {
  109. let userInfo = wx.getStorageSync('userInfo');
  110. let token = wx.getStorageSync('token');
  111. // 页面显示
  112. if (userInfo && token) {
  113. app.globalData.userInfo = userInfo;
  114. app.globalData.token = token;
  115. }
  116. this.setData({
  117. userInfo: app.globalData.userInfo,
  118. token: app.globalData.token
  119. });
  120. util.request(api.CartList).then((res) => {
  121. let isLogin = false
  122. if (res.errno === 401) {
  123. isLogin = false
  124. app.globalData.userInfo = tempUserInfo;
  125. app.globalData.token = '';
  126. } else {
  127. isLogin = true
  128. app.globalData.userInfo = userInfo;
  129. app.globalData.token = token;
  130. }
  131. if (!isLogin){
  132. this.setData({
  133. token:'',
  134. userInfo: tempUserInfo
  135. });
  136. }
  137. this.setData({
  138. isLogin
  139. });
  140. });
  141. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  142. this.getTabBar().setData({
  143. selected: 3
  144. })
  145. }
  146. },
  147. onPullDownRefresh() {
  148. },
  149. onHide: function () {
  150. // 页面隐藏
  151. },
  152. onUnload: function () {
  153. // 页面关闭
  154. },
  155. bindGetUserInfo(e) {
  156. let userInfo = wx.getStorageSync('userInfo');
  157. let token = wx.getStorageSync('token');
  158. if (userInfo && token) {
  159. return;
  160. }
  161. if (e.detail.userInfo){
  162. //用户按了允许授权按钮
  163. user.loginByWeixin(e.detail).then(res => {
  164. this.setData({
  165. userInfo: res.data.userInfo,
  166. isLogin: true
  167. });
  168. app.globalData.userInfo = res.data.userInfo;
  169. app.globalData.token = res.data.token;
  170. }).catch((err) => {
  171. this.setData({
  172. token: '',
  173. userInfo: tempUserInfo
  174. });
  175. app.globalData.userInfo = tempUserInfo;
  176. app.globalData.token = '';
  177. console.log(err)
  178. });
  179. } else {
  180. //用户按了拒绝按钮
  181. // wx.showModal({
  182. // title: '温馨提示',
  183. // content: '您点击了"取消"授权,将无法获取四维线上博览会完整服务。请“授权”以保证各项功能正常使用。',
  184. // confirmText:'授权',
  185. // success: function (res) {
  186. // if (res.confirm) {
  187. // wx.openSetting({
  188. // success: (res) => {
  189. // if (res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录
  190. // user.loginByWeixin(e.detail).then(res => {
  191. // this.setData({
  192. // userInfo: res.data.userInfo
  193. // });
  194. // app.globalData.userInfo = res.data.userInfo;
  195. // app.globalData.token = res.data.token;
  196. // }).catch((err) => {
  197. // console.log(err)
  198. // });
  199. // }
  200. // }
  201. // })
  202. // }
  203. // }
  204. // });
  205. }
  206. },
  207. exitLogin: function () {
  208. wx.showModal({
  209. title: '',
  210. confirmColor: '#b4282d',
  211. content: '退出登录?',
  212. success: function (res) {
  213. if (res.confirm) {
  214. wx.removeStorageSync('token');
  215. wx.removeStorageSync('userInfo');
  216. wx.switchTab({
  217. url: '/pages/index/index'
  218. });
  219. }
  220. }
  221. })
  222. }
  223. })